@
@
@falong2020-05-07 12:18:16
Python
@falong, 2020-05-07 12:18:16

How to add a key phrase for which an answer will be given in a VK Api Python conversation?

With the code as on the screen, it gives an error as on the 2nd screen

spoiler
import random
import requests
import vk_api
from vk_api.bot_longpoll import VkBotLongPoll, VkBotEventType
 
session = requests.Session()
vk_session = vk_api.VkApi(
    token='token')
try:
    vk_session._auth_token()
except vk_api.AuthError as error_msg:
    print(error_msg)
    exit(0)
 
longpoll = VkBotLongPoll(vk_session, group_id)
vk = vk_session.get_api()
response = event.text.lower()
 
 
while True:
    for event in longpoll.listen():
        if event.type == VkBotEventType.MESSAGE_NEW and event.from_chat:
            		if event.from_chat:
                  	vk.messages.send( #Отправляем собщение
                      	chat_id=event.chat_id,
                      	message='Ваш текст', random_id=random.randint(1, 10000000))


5eb3d1ae52ecc449595948.png
5eb3d1b52bed5276382305.png

When using event.object['text'], after entering something into the conversation, there will be such an error
5eb3d25173e21458548715.png
5eb3d254c00db533213502.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
WolfInChains, 2020-05-07
_

import vk_api
import random
from vk_api.bot_longpoll import VkBotLongPoll, VkBotEventType

vk = vk_api.VkApi(token="токен")
vk._auth_token()
vk.get_api()
longpoll = VkBotLongPoll(vk, айди группы)

while True:
       for event in longpoll.listen():
           if event.type == VkBotEventType.MESSAGE_NEW:
               if event.object.peer_id != event.object.from_id:
                   if event.object.text == "пинг"
                        vk.method("messages.send",
                                          {
                                              "chat_id": event.object.peer_id - 2000000000,
                                              "message": "понг",
                                              "random_id": random.randint(1, 2147483647)
                                          }
                                         )

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question