Python-telegram-bot нужно передать update в функцию, вызываемую context.job_queue.run_once(...)

Код не мой, взят из примера в интернете, где всё работает (но я хочу его изменить):

import logging

from telegram import Update
from telegram.ext import Updater, CommandHandler, CallbackContext

logging.basicConfig(
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO
)

logger = logging.getLogger(__name__)


def start(update: Update, context: CallbackContext) -> None:
    """Sends explanation on how to use the bot."""
    update.message.reply_text('Hi! Use /set <seconds> to set a timer')


def alarm(context: CallbackContext) -> None:
    job = context.job
    context.bot.send_message(job.context, text='Beep!')
    set_timer()


def remove_job_if_exists(name: str, context: CallbackContext) -> bool:
    current_jobs = context.job_queue.get_jobs_by_name(name)
    if not current_jobs:
        return False
    for job in current_jobs:
        job.schedule_removal()
    return True


def set_timer(update: Update, context: CallbackContext) -> None:
    chat_id = update.message.chat_id
    try:
        due = int(context.args[0])
        if due < 0:
            update.message.reply_text('Sorry we can not go back to future!')
            return

        job_removed = remove_job_if_exists(str(chat_id), context)
        context.job_queue.run_once(alarm, due, context=chat_id, name=str(chat_id))

        text = 'Timer successfully set!'
        if job_removed:
            text += ' Old one was removed.'
        update.message.reply_text(text)

    except (IndexError, ValueError):
        update.message.reply_text('Usage: /set <seconds>')


def unset(update: Update, context: CallbackContext) -> None:
    chat_id = update.message.chat_id
    job_removed = remove_job_if_exists(str(chat_id), context)
    text = 'Timer successfully cancelled!' if job_removed else 'You have no active timer.'
    update.message.reply_text(text)


def main() -> None:
    updater = Updater("ТОКЕН БОТА")

    dispatcher = updater.dispatcher

    dispatcher.add_handler(CommandHandler("start", start))
    dispatcher.add_handler(CommandHandler("help", start))
    dispatcher.add_handler(CommandHandler("set", set_timer))
    dispatcher.add_handler(CommandHandler("unset", unset))

    updater.start_polling()

    updater.idle()


if __name__ == '__main__':
    main()

Но мне нужно передать update в функцию alarm, вызываемую в context.job_queue.run_once(alarm, due, context=chat_id, name=str(chat_id)) но если поменять строки кода alarm(context: CallbackContext) -> None: на alarm(context: CallbackContext, update: Update) -> None: и context.job_queue.run_once(alarm, due, context=chat_id, name=str(chat_id)) на context.job_queue.run_once(alarm(context, update), due, context=chat_id, name=str(chat_id)) то ничего уже не работает, и я попробовал много разных вариантов. Как правильно решить задачу, подскажите, пожалуйста.


Ответы (0 шт):