V
V
Vladimir Korshunov2020-07-16 20:28:51
Python
Vladimir Korshunov, 2020-07-16 20:28:51

Why do some vk api events not work in python?

I started studying the vk api documentation and wanted to write my own bot in python. I say right away - I gave him all the rights, generated the key, etc. There is this page - https://vk.com/dev/groups_event , it describes all the events that can be used in the vk api. I do everything through Long Poll. The simplest, reaction to a specific user message, works (message_new event) through the following code:

import requests
import vk_api
from vk_api.longpoll import VkLongPoll, VkEventType
from vk_api.utils import get_random_id

vk_session = vk_api.VkApi(token='a94555a01de3af80559b4184b10c33d5b8c88203afb449090b4bc0a06fa7485e3be0114453a3bf7681f1f')

longpoll = VkLongPoll(vk_session)
vk = vk_session.get_api()

for event in longpoll.listen():
    if event.type == VkEventType.MESSAGE_NEW and event.text:		
        if event.text == 'привет' or event.text == 'здарова':
            vk.messages.send(user_id=event.user_id, random_id=get_random_id(), message='Привет!')
            print('Ja')


According to this logic, as I understand it, the bot can respond to any event in this way:

if event.type == VkEventType.{any event from the documentation}: vk.messages.send(user_id=event.user_id, random_id=get_random_id(), message='Hi!')

However, for some reason this doesn't work. This is the code that throws an error:
elif event.type == VkEventType.PHOTO_NEW:
        vk.messages.send(user_id=event.user_id, random_id=get_random_id(), message='Привет!')
        print('Ja')


At the same time, it seems that the PHOTO_NEW event does not exist at all, it is not even offered by visual studio in the tooltips, although it is in the documentation. Here is the verbose report:

Message = PHOTO_NEW
Source = C:\Users\vovak\source\repos\PythonApplication2\PythonApplication2\PythonApplication2.py
Stack trace:
File "C:\Users\vovak\source\repos\PythonApplication2\PythonApplication2\PythonApplication2.py ", line 16, in
elif event.type == VkEventType.PHOTO_NEW:

In a brief description of the error, it simply gives PHOTO_NEW. At the same time, there are events that are not in the documentation AND THEY WORK, for example, USER_TYPING (triggered when the user writes something in the chat), learned about it through visual studio prompts. If you know how the bot should react, let's say, exactly in the photo, please show how you learned this, because I didn't find anything for python.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Korshunov, 2020-07-16
@GashhLab

Added a line:

from vk_api.bot_longpoll import VkBotLongPoll, VkBotEventType

And I replaced VkEventType with VkBotEventType everywhere, now there are necessary events in the tooltips, it doesn’t give an error, but nothing works, generally zero reaction

R
Renat Ataev, 2020-07-17
@fanepka

So read the bot_longpoll documentation well. These methods are not listed there

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question