我有以下机器人:
class Bot:
def __init__(self, handlers: list[BaseHandler], daily_routines):
self.application = Application.builder().token(BOT_TOKEN).build()
for handler in handlers:
self.application.add_handler(handler)
self.job_queue = JobQueue()
self.job_queue.set_application(self.application)
for routine in daily_routines:
self.job_queue.run_daily(routine, time=datetime.time(hour=15, minute=49,
tzinfo=pytz.timezone('Asia/Jerusalem')))
def __call__(self, *args, **kwargs):
self.application.run_polling()
if __name__ == "__main__":
_handlers = [
...
]
bot = Bot(_handlers, [daily_routine])
bot()
daily_routine
定义为:
async def daily_routine(context: CallbackContext) -> None:
job = context.job
await context.bot.send_message(job.chat_id, text="HELLO")
daily_routine
回调未在指定时间触发。
我怎样才能让它工作?
您没有start
和JobQueue
,因此它不会处理任何作业。
我建议只使用ApplicationBuilder
为您提供的JobQueue
实例,即self.application.job_queue
。Application.run_polling
已经负责启动和停止该实例。
免责声明:我目前是python-telegram-bot
的维护者。