S
S
shuzzixd2021-06-05 20:18:54
Python
shuzzixd, 2021-06-05 20:18:54

How to make the bot edit its message after a certain period of time?

Well, for example:
Message1: Loading 1% (wait for a second) Message2
: Loading 26%
and so on up to 100%

I use a telebot

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vindicar, 2021-06-06
@Vindicar

I'm not familiar with telebot, but a quick google shows that the logic is similar to how discord works.
The method of sending a message (both send_message() and reply_to()) returns you an object of the sent message, which contains incl. his ID.
You call edit_message_text() with this message ID and new text.
The only question is delay. If I understand correctly, the telebot either works sequentially (i.e. while one handler is running, the bot cannot do anything else), or uses threads to run different handlers in parallel. So you don't have much choice - good old time.sleep().

O
o5a, 2021-06-07
@o5a

Run edit_message_text() in a loop to update the message, limiting time.sleep().
In order not to block input during multiple launches, you can use threading. Perhaps there is a more correct way in this case than threading, I don’t really know how best for telegram.
For example, filling by 10%

from threading import Thread

@bot.message_handler(commands=['start'])
def start(message):
    msg = bot.send_message(message.chat.id, f'заполнение 0%')
    t = Thread(target=thread_update_text, args=[msg, 'заполнение {}%'])
    t.start()

def thread_update_text(message, text):
    for i in range(10,110,10):
        time.sleep(1)
        bot.edit_message_text(chat_id=message.chat.id, message_id=message.message_id, text=text.format(i))

A
Alexander, 2021-06-07
@shabelski89

Here he showed 2 streams of messages to the telebot . You make your own logic for each function.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question