S
S
shampyn_2018-11-24 21:57:58
Python
shampyn_, 2018-11-24 21:57:58

How to make waiting for a message from the user in Python 3?

I made a bot for VK, in python 3, and I needed the bot to wait for a message.
Here is an example code:

import vk_api
import time
import json
while True:
    try:
    #  главный цикл
        messages = vk.method("messages.getConversations", {"offset": 0, "count": 20})
        if messages["count"] >= 1:
            id = messages["items"][0]["last_message"]["from_id"]
            body = messages["items"][0]["last_message"]["text"]
            elif body == "Пароли":
                vk.method("messages.send", {"peer_id": id, "message": "Введите ваш пароль: "})
                if body == "*пароль*":
                    vk.method("messages.send", {"peer_id": id, "message": "*все пароли*"})
                else:
                    vk.method("messages.send", {"peer_id": id, "message": "Не верный пароль: "})
        time.sleep(0.5)
    except Exception as E:
        time.sleep(1)

The problem is that after doing "elif body == 'Passwords': ", "vk.method("messages.send", {"peer_id": id, "message": "Enter your password: "})" is executed simultaneously and "vk.method("messages.send", {"peer_id": id, "message": "Wrong password: "})", but I need something after "vk.method("messages.send", {"peer_id": id, "message": "Enter your password: "})" it was necessary to enter a password and if it was correct (in this case *password*), the command "vk.method("messages.send", {"peer_id": id, "message": "*all passwords*"})", but if not correct, then "vk.method("messages.send", {"peer_id": id, "message": "Incorrect password: "})".

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stockholm Syndrome, 2018-11-24
@StockholmSyndrome

in general, you receive messages incorrectly
, there are special tools for this: Bots LongPoll API and Callback API
, most likely, it will be easier to solve your problem with them

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question