A
A
Aztart2021-06-08 20:28:30
Python
Aztart, 2021-06-08 20:28:30

Why is the bot message not being deleted in the chat?

1. There is a bot on aiogram

2. There is an algorithm that deletes the just sent command by the user and the previous message sent by the bot to the user, which is a response to the previous user command.
2.1 The bot's message id is written to the user's database
2.2 The algorithm retrieves the id from the database and deletes the message using it
2.3 The algorithm itself:

async def del_last_bot_message(*args):
'''
Удаление лишних сообщений для реализации "чистого интерфейса"
'''
    print(f'ENTER MESSAGE: {args[0].message_id + 1}')

# Удаление сообщения пользователя
    try:
        await args[0].delete()
    except:
        Exception

# Удаление последнего сообщения бота по id
    bmi = dict(ast.literal_eval(database.select_user_data(args[0])))
    while len(bmi['bot_mes_id']) > 0:
        bmi = dict(ast.literal_eval(database.select_user_data(args[0])))
        for ids in bmi['bot_mes_id']:
            try:
                print(f'TRY DEL MES: {ids} OF {args[0].from_user.username}')
                await bot.delete_message(args[0].chat.id, ids)
                print(f'DONE DEL MES: {ids} OF {args[0].from_user.username}')
                database.del_bmi_element(args[0], ids)
                print(f'DONE DEL ELEM: {ids} OF {args[0].from_user.username}')
            except:
                print(f'FAILE DEL MESSAGE: {ids} OF {args[0].from_user.username}')
                continue
    if len(bmi['bot_mes_id']) == 0:
        database.create_bmi_in_user_data(args[0])
        print(f'LAST FOO: {args[0].message_id + 1} {args[0].from_user.username}')

2.4 The situation in the terminal ("Clean start" with a zeroed database and chat history):
60bfa4fbab00a669628128.jpeg

3. Based on the impromptu log data presented above and the work of the bot, the algorithm works as it should and for each user, everything is ok.

4. The problem appears when two or more users send a command to the bot at the same time. After it, the algorithm also works as it should, but the next time the command is sent, it stops working.
4.1 "Logs":
60bfa6f4be571013119144.jpeg
4.2 Based on the logs, the algorithm cannot delete the message by id and tries to do it an infinite number of times, which is very strange, since the bot message corresponding to the id used exists in the chat:
60bfa822dce03908561122.jpeg

2nd day I'm scratching my head over the problem, I don't understand , what's the matter.
I will add information if necessary.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question