D
D
Dmitry Shnur2021-05-03 02:11:05
Python
Dmitry Shnur, 2021-05-03 02:11:05

VK API, Python. How to eliminate interference from other users in the message thread with the bot?

I have a VK bot that, upon receiving a "Report a bug" message, prompts the user to enter text to describe what happened, which is then sent to the admin. Here is that part of the code:

def message_fail():
    try:
        for event in longpoll.listen():
            if event.type == VkEventType.MESSAGE_NEW and event.to_me and event.text:
                mess = str(event.user_id)
                mess_fail = event.text
                vk.messages.send(keyboard = keyboards.keyboard1.get_keyboard(), user_id=event.user_id, message='Сообщение отправлено администратору!.', random_id = random_id())
                vk.messages.send(user_id=admin_id, message='Поступило сообщение об ошибке от vk.com/id' + mess + '\n\nТекст обращения: ' + mess_fail, random_id = random_id())
                break
    except Exception as e:
         vk.messages.send(keyboard = keyboards.keyboard.get_keyboard(), user_id=event.user_id, message="Произошла неизвестная ошибка", random_id = random_id())


for event in longpoll.listen():
    if event.type == VkEventType.MESSAGE_NEW and event.to_me:
        if event.text == 'Сообщить об ошибке':
            vk.messages.send(user_id=event.user_id, message='Опишите ошибку, как можно подробнее', random_id = random_id())
            message_fail()

All this business works, but there is a problem. If one user writes "Report an error", receives a message that it is necessary to enter a description of the error, and at the same moment another user writes to the bot, then the bot will send the text that the second user wrote. This is all due to for event in longpoll.listen():, but I could not otherwise implement the message chain. Please tell me how to fix this. I will be grateful.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Yumikov, 2021-05-03
@Shnur_D

If you want it to be possible through a conversation, then try to connect a database to your bot. For example sqlite3: create a database, set the user ID column (column) 1, create another column for what it currently writes about an error (column 2) (then you can add more if necessary). After the user writes "Report an error", add some value to column 2 (For example, the standard 0, and you added 1), check each message event and check the ID through the database (and also add if it is not there, and just also check whether it exists or not). If this user wrote an error - change the value of column 2 to 0 and display his message to yourself. It is possible in another way, just the bot would write to him in private messages, and then you would have brought it to yourself. If you need a code, then I will write it when I wake up.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question