简体   繁体   中英

How do I use run_once with CallbackQueryHandlers in a telegram bot written in Python?

I have written a telegram bot in python. In particular, I am working on this flow: "recheck2" has an inlinekeyboardbutton that links to "timer", and "timer" will wait for an hour before activating "recheck3". "recheck3" runs a database query and sends a message to the telegram user.

The purpose of the bot is to allow the user to have an option to re-run the same query after an hour (or any predefined duration).

I receive an error on the line "context.job_queue.run_once...." saying that "ValueError: The following arguments have not been supplied: context".

I've based on code on timerbot.py and inlinekeyboard2.py avail at https://github.com/python-telegram-bot/python-telegram-bot/blob/master/examples/README.md... . I've scanned thru many forums and QnAs, but unable to find any solution. I'm trying to avoid using sleep() as it suspends/blocks the code. Also, I tried asyncio.sleep but it doesn't work with bots with CallbackQueryHandlers.

Could a kind soul please point me in the right direction please? Thanks!

def timer(update: Update,context: CallbackContext)-> None:
    chat_id=CallbackContext.chat_id
    due = int(3600) #to run recheck3 after one hour
    ****context.job_queue.run_once(recheck3, due, context=chat_id, name=str(chat_id))****


def recheck3(update: Update, context: CallbackContext) -> int:
    """Stores the info about the user and ends the conversation."""
    #user = update.message.from_user
    logger.info("%s: rechecked  for %s with timed delay", CallbackContext.firstname, CallbackContext.tag)
    update.callback_query.message.edit_text('Sure, will send you a message again in xxmins.')   
    
    connection = cx_Oracle.connect(dsn="XXXXXX") ## ,encoding="UTF-8"
    
    cur = connection.cursor()
    tag=CallbackContext.tag #recall the user input
    cur.execute("SELECT * \ 
........ code truncated.....

Solved! Each job can only have ONE argument / can only take context as argument. Eg: My “timer” calls for “recheck3” and recheck3 must only take one argument (cannot have both update and context as arguments). Thanks to a kind Telegram user on https://t.me/pythontelegrambotgroup for the assistance!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM