E
E
Elisey Zhelikhovsky2019-07-17 12:45:32
Python
Elisey Zhelikhovsky, 2019-07-17 12:45:32

How can a chatbot wait for a user's response?

Using the VK API, the bot receives the user's last message and processes it. But what if the bot needs to wait for the user to send a message?
For example - a bot asks a person to enter a password or other response and should not respond until it receives this message. At the same time, what exactly will be in this message is unknown, so the usual if - else is indispensable. What can be done in such a situation?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ivan Yakushenko, 2019-07-17
@Lightly

Wait for a message from a specific ID. The ID can be obtained both from the message itself and in other ways, depending on the library used, for example:

def func(user_id):
    for event in long_poll.listen():
        if event.type == VkBotEventType.MESSAGE_NEW:
            if event.obj.from_id == user_id:
                print('something')

You just need to do this either asynchronously or in a separate thread, otherwise while the bot will wait for messages from a specific user, it will not "see" all the others.
Those. you get the initial message, then take the user ID from it, and with this ID in a separate thread, start waiting for the message and then do what you want.

I
iBird Rose, 2019-07-17
@iiiBird

The simplest option is to bind the field to a user, let's say "path". And conditionally create a structure like in a website. Those. all users initially have path = /
For example, he wrote "Menu" - You send it to path / menu - and you already process the following messages based on the place where the user is located.
In your case, something like:
A person wrote - you sent him to /password
Then, no matter what the person writes, you will process it along this path as entering a password. Well, then you also send the user to the desired path

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question