V
V
vakonyakhin192021-04-29 16:31:15
Python
vakonyakhin19, 2021-04-29 16:31:15

How to make a reverse timer in telegram?

Hello. There are 2 methods to update received message and countdown:

def update_message(self, chat_id, message_id, new_message):
        self.logger.debug(f'Update message {message_id}: {new_message}')
        self.bot.edit_message_text(chat_id=chat_id, message_id=message_id, text=new_message)

    def create_timer(self, timeout_secs, callback, *args, **kwargs):
        if not callable(callback):
            raise TypeError('Ожидаем функцию на вход')
        if not timeout_secs:
            raise TypeError("Не могу запустить таймер на None секунд")
        if args:
            raise TypeError(f"create_timer() takes 2 positional arguments but {len(args) + 2} were given")

        def wrapper(context):
            callback(**kwargs)

        self.job_queue.run_once(wrapper, timeout_secs)

    def create_countdown(self, timeout_secs, callback, *args, **kwargs):
        if not callable(callback):
            raise TypeError('Ожидаем функцию на вход')
        if not timeout_secs:
            raise TypeError("Не могу запустить таймер на None секунд")
        if args:
            raise TypeError(f"create_countdown() takes 2 positional arguments but {len(args) + 2} were given")

        def wrapper(context):
            job = context.job
            job.context -= 1
            try:
                callback(job.context, **kwargs)
            except Exception as error:
                traceback.print_exception(type(error), error, error.__traceback__, file=sys.stderr, limit=-3)
                job.schedule_removal()
            if job.context <= 0:
                job.schedule_removal()

        first_callback = lambda context: callback(timeout_secs, **kwargs)
        self.job_queue.run_once(first_callback, 0)
        self.job_queue.run_repeating(wrapper, 1, context=timeout_secs)

The original message is required, replaced with a timer, and the timer seconds must also be updated.
Their brains were not enough to solve the problem. I can't figure out how to change the message in the countdown

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Y
Yupiter7575, 2021-04-29
@yupiter7575

Wrap your code in a code tag.

I can't figure out how to change the message in the countdown

I can't understand what you just wrote. This can be done with a simple while loop.

A
Alexander, 2021-04-30
@shabelski89

Here I answered, and an example of a direct and countdown

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question