pythonschedulerpython-telegram-botjob-queue

How to change job parameters in telegram app using telegram.ext functionality?


I'm trying to understand how jobs working in telegram.ext module and writing small bot for this: user should be able to set time for reminder and change this time. I created a job using job_queue.run_daily: context.job_queue.run_daily(alarm, chat_id=update.effective_user.id, time=time2check, data="smth").enabled = True
How can I change time for a job using build-in functions of telegram.ext? I found this answer: https://stackoverflow.com/a/64874247/20238505, but it is consider using plain APScheduler. Since JobQueue is 'convenience wrapper for the APScheduler library', maybe it has some convenient functions to change job parameters, such as time?

I didn't find an answer in docs (https://docs.python-telegram-bot.org/en/stable/telegram.ext.jobqueue.html), but maybe I don't understand something right.
First thought in solving this task was to recreate job with new 'time' argument, but it seems a little redundant for me.

UPD: Summarizing answer provided by CallMeStag and my research (I’m using cron trigger in my program):
One should use APScheduler functionality to change job parameters. One can reschedule the job which was found using context.job_queue.get_jobs_by_name(jobname) with something like:
job.job.reschedule(trigger='cron', hour=hour, minute=minute, day_of_week=day_of_week, timezone=tz)
Need to mention that if we want to change only one parameter we still have to provide all others parameters otherwise APScheduler will set them to default values. For example, if I had a job which executes at 12:00 every sunday, and I change only hour parameter to 9, then job will execute on every day of the week.
So we need to get values of parameters which have to be preserved.
job.trigger class has all we need to achieve that. Tuple of parameters' names (.FIELD_NAMES = ('year', 'month', 'day', 'week', 'day_of_week', 'hour', 'minute', 'second')) and list of classes with those names which store current settings for each parameter (.fields). So to get day of week settings we can do:
day_of_week = job.trigger.fields[job.trigger.FIELD_NAMES.index('day_of_week')]
Timezone can be read directly from job.trigger.timezone.

I hope this would save some time for other newbies in the future.


Solution

  • The return value of run_daily is an instance of the telegram.ext.Job class, which can be understood as wrepper for the apscheduler.job.Job. More precisely, the underlying APScheduler job is available as Job.job. Combined with How to modify a job on the fly in APScheduler? that you already cited, this should get you started :)


    Disclaimer: I'm currently the maintaner of python-telegram-bot.