如何在用python编写的电报机器人中使用run_once与callbackqueryhandlers?
我在Python写了一个电报机器人。特别是,我正在研究这个流程: “ recheck2”具有一个InlineKeyboardButton,它链接到“计时器”,而“计时器”将等待一个小时,然后激活“ recheck3”。 “ recheck3”运行数据库查询并向电报用户发送消息。
该机器人的目的是允许用户可以选择一个小时后(或任何预定义的持续时间)重新运行相同的查询。
我在“ context.job_queue.run_once ....”行上收到一个错误,说“ valueerror:尚未提供以下参数:上下文”。
我基于timerbot.py和inlineKeyboard2.py aver at https://github.com/python-telegram-bot/python-telegram-bot/blob/blob/master/master/examples/readme.md ... 。我已经通过许多论坛和QNA进行了扫描,但找不到任何解决方案。我正在尝试避免使用Sleep()暂停/阻止代码。另外,我尝试了asyncio.sleep,但它与带有callbackqueryhandlers的机器人无法使用。
善良的灵魂可以请我指向正确的方向吗?谢谢!
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.....
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.....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决! 每个作业只能有一个参数 /只能将上下文作为参数。 。感谢 https://t.me/pythontelegrambotgroupgroup 提供帮助!
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!