F
F
fodiche2021-04-17 14:27:48
Python
fodiche, 2021-04-17 14:27:48

I am writing a bot on vk_api and it gives an error, what should I do?

The error itself is:
Traceback (most recent call last):
File "C:\vkbot\vkbot.py", line 15, in
for event in VkLongPoll(session).listen():
File "C:\Users\user\AppData\ Roaming\Python\Python39\site-packages\vk_api\longpoll.py", line 517, in __init__
self.update_longpoll_server()
File "C:\Users\user\AppData\Roaming\Python\Python39\site-packages\vk_api\ longpoll.py", line 531, in update_longpoll_server
response = self.vk.method('messages.getLongPollServer', values)
File "C:\Users\user\AppData\Roaming\Python\Python39\site-packages\vk_api\vk_api .py", line 656, in method
raise error
vk_api.exceptions.ApiError: [5] User authorization failed: invalid session.

import vk_api
from vk_api.longpoll import VkLongPoll, VkEventType

session = vk_api.VkApi(token="скрыт в целях безопасности")


def send_message(user_id, message):
  session.method("messages.send", {
    "user_id": user_id,
    "message": message,
    "random_id": 0
  })


for event in VkLongPoll(session).listen():
  if event.type == VkEventType.MESSAGE_NEW and event.to_me:
    text = event.text.lower()
    user_id = event.user_id

    if text == "hello":
      send_message(user_id, "Hi")

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Karbivnichy, 2021-04-17
@fodich

I am writing a bot on vk_api
Well done.
what to do?
The best solution would be, of course, not to write bots, but to do something more useful, not addictive.
Now let's look at the error together: User authorization failed: invalid session.Didn't learn English at school? No problem! There is a google translate:
Ошибка авторизации пользователя: недействительный сеанс.

I hope you can read Russian.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question