我正在尝试将作业添加到数据库,然后在脚本再次运行时加载它们。以下代码将其添加到db,但是当您重新启动脚本时,它会加载作业但是会永久运行它。 似乎作业导入是正确的,但我无法弄清楚为什么它运行王尔德?
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, InlineQueryHandler
import logging,sqlite3, datetime, json
from telegram import Update
# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
logger = logging.getLogger(__name__)
def seturl(bot,job):
bot.send_message(chat_id= job.context.message.chat_id, text=job.context.message.text)
def userinfo(bot,update,job_queue):
interval = datetime.time(5)
context = update
db = sqlite3.connect('thedb.db')
job = job_queue.run_daily(seturl, interval, context=context)
with db as connection:
c = connection.cursor()
c.execute('INSERT INTO jobq(interval, context) VALUES (?, ?)', (str(interval), json.dumps(context.to_dict())))
db.commit()
db.close()
def error(bot, update, error):
"""Log Errors caused by Updates."""
logger.warning('Update "%s" caused error "%s"', update, error)
def main():
updater = Updater("TOKEN")
dp = updater.dispatcher
db = sqlite3.connect('thedb.db')
c = db.cursor()
c.execute("CREATE TABLE IF NOT EXISTS jobq(interval INTEGER, context TEXT)")
c.execute('SELECT * FROM jobq')
results = c.fetchall()
for row in results:
dp.job_queue.run_daily(seturl, datetime.datetime.strptime(row[0],"%H:%M:%S"), context = Update.de_json(json.loads(row[1]),dp.bot))
dp.add_handler(MessageHandler(Filters.text , userinfo,pass_job_queue=True))
db.commit()
db.close()
dp.add_error_handler(error)
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()
答案 0 :(得分:0)
这是我找到的解决方案。我们需要将DB中的加载时间转换为日期时间对象,因为它存储为TEXT。
在我为问题输入的代码中,我需要添加x
以完成转换。
.time()