0

I'm trying to add the Jobs to DB and then load them when the script runs again. The following code adds it to db but when you restart the script it loads the job but runs it for ever. It seems that the job import is correct but I can not figure out why it is running Wilde?

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()
lameei
  • 184
  • 2
  • 11

1 Answers1

0

Here is the solution I have found. We need to convert the loaded time from the DB into date time object cause it is stored as TEXT.

In the code I entered for the question I needed to add .time() to make the conversion complete.

dp.job_queue.run_daily(seturl, datetime.datetime.strptime(row[0],'%H:%M:%S').time(), context = Update.de_json(json.loads(row[1]),dp.bot))
lameei
  • 184
  • 2
  • 11