S
S
Skauyt2021-05-05 17:28:05
Python
Skauyt, 2021-05-05 17:28:05

How to make a bot that reads a VK message?

I want to make a bot that responds to a private message on VK. Let me give you an example, the user writes any text to my personal VKontakte, the bot processes this message and independently writes him some message from my account. I found messages.get in the documentation, but the method is already outdated. How can this be implemented?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
WolfInChains, 2021-05-05
@Skauyt

via longpoll. But in order to answer only in PM, you need to check where the event comes from, this option will also respond to the conversation. I don’t remember exactly how to do the check there (but it seems like how to add it if event.from_user:after the command / line if event.type == VkEventType.MESSAGE_NEW:), there are a lot of examples on the Internet.

from vk_api.longpoll import VkLongPoll, VkEventType

vk_session = vk_api.VkApi(token="токен от страницы")
longpoll = VkLongPoll(vk_session)
vk = vk_session.get_api()


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


for event in longpoll.listen():
    if event.type == VkEventType.MESSAGE_NEW:		
        # если вообще на любое сообщение, то убираем строку if event.text == 'текст который пишет пользователь': 
        if event.text == 'текст который пишет пользователь': 
            send_msg(event.peer_id, 'ответ бота')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question