Answer the question
In order to leave comments, you need to log in
How to make a bot in VK on a python work in a conversation?
I tried to write my first bot, it works for a personal, but does not want to answer in a conversation, why?
from vk_api.bot_longpoll import VkBotLongPoll, VkBotEventType
import vk_api
import random
import time
token = "Token"
vk = vk_api.VkApi(token=token)
longpoll = VkBotLongPoll(vk, ID)
vk_api = vk.get_api()
while True:
try:
for event in longpoll.listen():
messages = vk.method("messages.getConversations", {"offset": 0, "count": 20, "filter": "unanswered"})
if messages["count"] >= 1:
id = messages["items"][0]["last_message"]["from_id"]
body = messages["items"][0]["last_message"]["text"]
if body.lower() == "привет":
vk.method("messages.send", {"peer_id": id, "message": "Привет!", "random_id": random.randint(1, 2147483647)})
elif body.lower() == "кто я?":
vk.method("messages.send", {"peer_id": id, "message": "ты человек", "random_id": random.randint(1, 2147483647)})
else:
vk.method("messages.send", {"peer_id": id, "message": "я не знаю что значит " + str(body.lower()), "random_id": random.randint(1, 2147483647)})
except Exception as E:
print(Exception)
time.sleep(1)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question