0
y=str("12:50AM")+"+0000"
bot_refresh_time = datetime.strptime(y,'%I:%M%p%z').timetz()
bot_refresh_time=bot_refresh_time.replace(tzinfo = tz)
updater.job_queue.run_once(bot_bal,bot_refresh_time,name="daily_check_task")

上面的代码执行没有错误,它被添加到作业队列但没有调用回调。

而如果我将run_once更改为run_daily,它工作得非常好,即updater.job_queue.run_daily(bot_bal,bot_refresh_time,name="daily_check_task")

或者

如果上述代码中的时区为“无”(tzinfo=None),那么run_once工作得非常好,所以我认为问题出在时区上,但如果是这种情况,那么run_daily不应该工作,因为它们都使用日期时间。语法中的时间

python 电报机器人的作业队列run_once的语法。

python 电报机器人的作业队列run_daily的语法。

任何建议或答案都会有所帮助。我只是编码的初学者:)

编辑: run_once有效,但不是将给定时间设置为时区(bot_refresh_time=bot_refresh_time.replace(tzinfo = tz)),而是将时间转换为给定时区(即,如果我的时区是亚洲/加尔各答,给定时间是上午 01:00,而使用run_once作业执行在 IST 上午 06:30 而不是 IST 上午 01:00)。这个问题只在使用run_once时出现, run_daily没有问题

4

1 回答 1

1

从 13.0 版开始,JobQueue只能处理pytz时区,因为 PTB 将库 APScheduler 用于JobQueue. 请注意,不再支持旧版本。一个工作示例run_once是:

import datetime as dtm
import pytz

from telegram.ext import Updater

updater = Updater('TOKEN')


def test(_):
    print('running at', dtm.datetime.utcnow())


time = pytz.timezone('Asia/Kolkata').localize(dtm.datetime.now() + dtm.timedelta(seconds=5))
updater.job_queue.run_once(test, time, name="daily_check_task")
updater.start_polling()
updater.idle()
于 2021-04-06T13:21:09.957 回答