Answer the question
In order to leave comments, you need to log in
VK_API, LONGPOLL. Problem with required positional argument: 'self'. How to decide?
I run the code, and it gives the following error:
line 23, in
for event in longpoll.listen():
TypeError: listen() missing 1 required positional argument: 'self'
import random
import vk_api
from vk_api.longpoll import VkLongPoll, VkEventType
# --
from vk_bot import VkBot
# --
def write_msg(user_id, message):
vk.method('messages.send', {'user_id': user_id, 'message' : message, 'random_id': random.randint(0, 2048)})
# API key created earlier
token = "I won't tell"
# Working with messages
longpoll = VkLongPoll
print("Server started")
for event in longpoll.listen( ):
if event.type == VkEventType.MESSAGE_NEW:
if event.to_me:
print(f'New message from {event.user_id}', end='')
bot = VkBot(event.userid)
if event.text[0] == "/":
write_msg(event.user_id, commander.do(event.text[1::]))
else:
write_msg(event.user_id, bot.new_message(event.text))
print('Text: ', event.text)
print("-------------------")
Answer the question
In order to leave comments, you need to log in
Or I do not understand something, or you have an error in authorization.
It should flow like this:
vks = vk_api.VkApi(token='токен')
longpoll = VkLongPoll(vks)
session = vk.Session(access_token = 'токен')
vk= vk.API(session, v = "5.95")
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question