F
F
fukenrice2020-05-22 18:22:31
Python
fukenrice, 2020-05-22 18:22:31

How to work with VK conversations using the long poll bot?

Faced such a problem that the bot functions perfectly if the work is in correspondence with the community, but if you add this community to the conversation (as it is said in the off documentation), it stops receiving events

import vk_api
from vk_api.longpoll import VkLongPoll, VkEventType
token = "token"  
vk = vk_api.VkApi(token=token)
longpoll = VkLongPoll(vk, group_id=193796197)
for event in longpoll.listen():
    print('*')
    if event.type == VkEventType.MESSAGE_NEW:
        print('*')
        if (event.from_chat or event.from_user) and not event.from_me:

If you add a bot to a conversation, make it an administrator, still no stars will appear in the console

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
WolfInChains, 2020-05-22
@fukenrice

5ec7f1a05a249588232765.pngAllocated caps space for the token and id

import vk_api
from vk_api.bot_longpoll import VkBotLongPoll, VkBotEventType

vk = vk_api.VkApi(token="ТОКЕН")
vk._auth_token()
vk.get_api()
longpoll = VkBotLongPoll(vk, АЙДИ ГРУППЫ)


def send_msg(peer_id: int, message: str, attachment: str = ""):
    return vk.method("messages.send", {**locals(), "random_id": 0})

while True:
    try:
       for event in longpoll.listen():
           if event.type == VkBotEventType.MESSAGE_NEW:
               if event.object.peer_id != event.object.from_id: #Для беседы
                   if event.object.text == "Привет":
                       send_msg(event.obj.peer_id, "Пока")
               if event.object.peer_id == event.object.from_id: #Для Лс
                   if event.object.text == "Привет":
                       send_msg(event.obj.peer_id, "Пока")
    except Exception as e:
        print(repr(e))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question