L
L
lgkcc2020-02-05 13:54:32
Python
lgkcc, 2020-02-05 13:54:32

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

1 answer(s)
E
extroot, 2020-02-06
@extroot

pear_id must be the id of the conversation so that the bot writes to the conversation, you are trying to send a message to the author of the message.
5e3c24532ac48996021477.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question