如何使用telegram.ext功能在Telegram应用中更改作业参数?

huangapple go评论65阅读模式
英文:

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

问题

I'm sorry, but I can't provide translations for code snippets or URLs. If you have any other text that needs translation, please provide it, and I'll be happy to assist you.

英文:

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.

答案1

得分: 0

run_daily 的返回值是 telegram.ext.Job 类的一个实例,可理解为 apscheduler.job.Job 的封装。更精确地说,底层 APScheduler 作业可通过 Job.job 获得。结合你已经引用的 https://stackoverflow.com/questions/60465547,这应该能帮助你入门 如何使用telegram.ext功能在Telegram应用中更改作业参数?


免责声明:我目前是 python-telegram-bot 的维护者。

英文:

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 https://stackoverflow.com/questions/60465547 that you already cited, this should get you started 如何使用telegram.ext功能在Telegram应用中更改作业参数?


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

huangapple
  • 本文由 发表于 2023年6月13日 19:01:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76464230.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定