import requests
class API:
API_URL = 'https://api.vk.com/method/'
def __init___(self, image_path, access_token):
self.img = image_path
self.token = access_token
def send(self):
response = requests.post(self.API_URL + 'photos.getMessagesUploadServer', params={'access_token': self.token}).json()
req = requests.post(response['upload_url'], params={'access_token': self.token}, files={'file': open(self.img, 'rb')}).json()
req.update({'access_token': self.token})
req = requests.post(self.API_URL + 'photos.saveMessagesPhoto', params=req)
return req.json()
# Usage :
# api = API('img.jpeg', 'XXX')
# response = api.send()
import vk, time
session = vk.AuthSession(user_login='<user login>', user_password='<user password>', app_id='<app id>') # может не сработать, сразу пихай в vk.Session token, чтобы не заморачиваться, а лучше юзай либу vkAPI(позже залью в PyPI)
api = vk.API(session, v='5.68')
values = {'out': 0,'count': 100,'time_offset': 60}
def write_msg(chat_id, s):
api.messages.send(**{'chat_id':chat_id,'message':s}))
while True:
response = api.messages.get(**values)
if response['items']:
values['last_message_id'] = response['items'][0]['id']
for item in response['items']:
if (item['body'] == '/hello') and (int(item['chat_id']) == 9):
write_msg(9,'Привет')
time.sleep(1)