T
T
timka2282020-09-04 16:58:40
Python
timka228, 2020-09-04 16:58:40

How to make a chain for a VK bot in Python?

There is a Vkontakte bot in python. Now he can only reply to messages. It has this structure:

while True:
    try:
        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":
                            "Не понял",   
                            "random_id": random.randint(1, 2147483647)
                        })

And I want to make message chains. Those. so that you can have a similar dialogue with the bot:

User: Fill out the form
Bot: What is your name?
User: Alexey
Bot: How old are you?
User: 18
Bot: Thanks for the answers.

And the answers will be written to a specific file, but I can do it myself.
How to implement it? I don't have much of an idea. Please help please.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
FrCl, 2020-09-04
@timka228

For example, create a simple User class in which to store the current state of the conversation, and when receiving a message, look at the user id to see if there has already been a conversation with him, and what state he is in now.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question