M
M
Myhabr022020-06-23 12:42:39
Python
Myhabr02, 2020-06-23 12:42:39

Why python bot on vk longpoll doesn't work in conversation?

Hi, Habr! The bot does not respond to messages in conversations, why? I tried methods from similar articles, it still does not respond, it works in PM

import requests
import vk_api
import random as r
import wikipedia 
from vk_api.longpoll import VkLongPoll, VkEventType


wikipedia.set_lang("RU")


vk_session = vk_api.VkApi(token="Мой токен")
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 == 'Вики':  
            if event.from_user: 
                vk.messages.send(
                    user_id=event.user_id,
                    message='Введите запрос',
                    random_id=r.randint(1, 2147483647) 
                )
            if event.from_chat:
                vk.messages.send(
                    chat_id=event.chat_id,
                    message='Введите запрос',
                    random_id=r.randint(1, 2147483647)
                )    
        
            for event in longpoll.listen():
                if event.type == VkEventType.MESSAGE_NEW and event.text: 
                    if event.from_user:
                        vk.messages.send( 
                            user_id=event.user_id,
                            message='Вот что я нашёл: \n' + str(wikipedia.summary(event.text)),
                            random_id=r.randint(1, 2147483647)
                        )
                        break 
                	elif event.from_chat: 
                    	vk.messages.send(
                        	chat_id=event.chat_id,
                        	message='Вот что я нашёл: \n' + str(wikipedia.summary(event.text)),
                        	random_id=r.randint(1, 2147483647)
                    	)
                    break 
            continue

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
Lukic, 2020-06-23
@Kerbx

The bot might need to be granted admin rights to the conversation.

A
Andrey, 2020-06-23
@anerev

It should be something like this:

import vk_api
from vk_api.bot_longpoll import VkBotLongPoll, VkBotEventType
from vk_api.utils import get_random_id

vk_session = vk_api.VkApi(token=TOKEN)
longpoll = VkBotLongPoll(vk_session, VKGroupID)
vk = vk_session.get_api()

status = {'stat' : 0}

def main():

for event in longpoll.listen():
    if event.type == VkEventType.MESSAGE_NEW and event.text :

        if event.text == 'Вики' and not status['stat']:  
            if event.from_user: 
                vk.messages.send(
                    peer_id=event.obj.from_id,
                    message='Введите запрос',
                    random_id=get_random_id()
                )
            if event.from_chat:
                vk.messages.send(
                   peer_id=event.obj.peer_id,
                    message='Введите запрос',
                    random_id=get_random_id()
                )
            status['stat'] = 1
        elif status['stat']:
          status['stat'] = 0# тут все действия с поиском

it seems so, although I haven’t worked with this library for a long time, but I think the essence is clear

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question