Новичок в программировании, хочу написать бота для обмена сообщениями через группу ВКонтакте.
Как исправить ошибку?
VKGroupAPI:
from urllib.request import *
from json import loads
URL = 'https://api.vk.com/method/'
class API:
def __init__(self, token, v='5.60'):
self.token = token
self.v = v
def Send(self, method, params=[]):
self.method = method
self.params = '&'.join(params)
response = urlopen(URL + self.method + '?v=' + self.v + '&access_token=' + self.token + '&' + self.params).read().decode('utf-8')
response = loads(response)
return response
def GetLongPoll(self):
self.poll_methodName = 'messages.getLongPollServer'
self.key = ''
self.server = ''
self.ts = ''
response = urlopen(URL + self.poll_methodName + '?v=' + self.v + '&access_token=' + self.token).read().decode('utf-8')
response = loads(response).get('response')
self.key = response.get('key')
self.server = response.get('server')
self.ts = response.get('ts')
return self.key, self.server, self.ts
Вот программа:
# coding: utf8
from VKGroupAPI import *
from urllib.request import *
from json import loads
token = 'HERE IS MY GROUP TOKEN'
class Chat:
def __init__(self, token, v='5.60'):
self.token = token
self.v = v
# Model: self.rooms = [[{'id':'opponent_id', 'opponent_id':'id'}], [{'id':'opponent_id', 'opponent_id':'id'}]]
self.rooms = []
# Model: self.find_opponents = [id, id, id]
self.find_opponents = []
def GetLongConnection(self):
self.connection = API(token=self.token, v=self.v)
self.key, self.server, self.ts = self.connection.GetLongPoll()
self.URL_LongPoll = 'https://' + str(self.server) + '?act=a_check&key=' + str(self.key) + '&ts=' + str(self.ts) + '&wait=25&mode=2&version=1'
self.request = urlopen(self.URL_LongPoll).read().decode('utf-8')
self.request = loads(self.request)
while True:
self.ts = self.request.get('ts')
self.URL_LongPoll = 'https://' + str(self.server) + '?act=a_check&key=' + str(self.key) + '&ts=' + str(self.ts) + '&wait=25&mode=2&version=1'
self.request = urlopen(self.URL_LongPoll).read().decode('utf-8')
self.request = loads(self.request)
self.updates = self.request['updates']
self.new_messages = []
for i in self.updates:
if i[0] == 4:
i[6] = i[6].replace('<br>', '\n')
self.new_messages.append(i)
if self.new_messages != []:
print(self.new_messages)
for i in self.new_messages:
if i[6] == 'го':
self.find_opponents.append(str(i[3]))
self.connection.Send(method='messages.send', params=['user_id=' + str(i[3]), 'message=фыв'])
NewChat = Chat(token=token)
NewChat.GetLongConnection()
input('Exit ')
P.S.: requests не предлагать