@@ -158,6 +158,30 @@ def application(self) -> "Application[Any, CCT, Any, Any, Any, JobQueue[CCT]]":
158
158
return application
159
159
raise RuntimeError ("The application instance is no longer alive." )
160
160
161
+ @staticmethod
162
+ async def job_callback (job_queue : "JobQueue[CCT]" , job : "Job[CCT]" ) -> None :
163
+ """This method is used as a callback for the APScheduler jobs.
164
+
165
+ More precisely, the ``func`` argument of :class:`apscheduler.job.Job` is set to this method
166
+ and the ``arg`` argument (representing positional arguments to ``func``) is set to a tuple
167
+ containing the :class:`JobQueue` itself and the :class:`~telegram.ext.Job` instance.
168
+
169
+ Tip:
170
+ This method is a static method rather than a bound method. This makes the arguments
171
+ more transparent and allows for easier handling of PTBs integration of APScheduler
172
+ when utilizing advanced features of APScheduler.
173
+
174
+ Hint:
175
+ This method is effectively a wrapper for :meth:`telegram.ext.Job.run`.
176
+
177
+ .. versionadded:: NEXT.VERSION
178
+
179
+ Args:
180
+ job_queue (:class:`JobQueue`): The job queue that created the job.
181
+ job (:class:`~telegram.ext.Job`): The job to run.
182
+ """
183
+ await job .run (job_queue .application )
184
+
161
185
def run_once (
162
186
self ,
163
187
callback : JobCallback [CCT ],
@@ -230,11 +254,11 @@ async def callback(context: CallbackContext)
230
254
date_time = self ._parse_time_input (when , shift_day = True )
231
255
232
256
j = self .scheduler .add_job (
233
- job . run ,
257
+ self . job_callback ,
234
258
name = name ,
235
259
trigger = "date" ,
236
260
run_date = date_time ,
237
- args = (self . application , ),
261
+ args = (self , job ),
238
262
timezone = date_time .tzinfo or self .scheduler .timezone ,
239
263
** job_kwargs ,
240
264
)
@@ -356,9 +380,9 @@ async def callback(context: CallbackContext)
356
380
interval = interval .total_seconds ()
357
381
358
382
j = self .scheduler .add_job (
359
- job . run ,
383
+ self . job_callback ,
360
384
trigger = "interval" ,
361
- args = (self . application , ),
385
+ args = (self , job ),
362
386
start_date = dt_first ,
363
387
end_date = dt_last ,
364
388
seconds = interval ,
@@ -433,9 +457,9 @@ async def callback(context: CallbackContext)
433
457
job = Job (callback = callback , data = data , name = name , chat_id = chat_id , user_id = user_id )
434
458
435
459
j = self .scheduler .add_job (
436
- job . run ,
460
+ self . job_callback ,
437
461
trigger = "cron" ,
438
- args = (self . application , ),
462
+ args = (self , job ),
439
463
name = name ,
440
464
day = "last" if day == - 1 else day ,
441
465
hour = when .hour ,
@@ -523,9 +547,9 @@ async def callback(context: CallbackContext)
523
547
job = Job (callback = callback , data = data , name = name , chat_id = chat_id , user_id = user_id )
524
548
525
549
j = self .scheduler .add_job (
526
- job . run ,
550
+ self . job_callback ,
527
551
name = name ,
528
- args = (self . application , ),
552
+ args = (self , job ),
529
553
trigger = "cron" ,
530
554
day_of_week = "," .join ([self ._CRON_MAPPING [d ] for d in days ]),
531
555
hour = time .hour ,
@@ -585,7 +609,7 @@ async def callback(context: CallbackContext)
585
609
name = name or callback .__name__
586
610
job = Job (callback = callback , data = data , name = name , chat_id = chat_id , user_id = user_id )
587
611
588
- j = self .scheduler .add_job (job . run , args = (self . application , ), name = name , ** job_kwargs )
612
+ j = self .scheduler .add_job (self . job_callback , args = (self , job ), name = name , ** job_kwargs )
589
613
590
614
job ._job = j # pylint: disable=protected-access
591
615
return job
@@ -625,10 +649,7 @@ def jobs(self) -> Tuple["Job[CCT]", ...]:
625
649
Returns:
626
650
Tuple[:class:`Job`]: Tuple of all *scheduled* jobs.
627
651
"""
628
- return tuple (
629
- Job ._from_aps_job (job ) # pylint: disable=protected-access
630
- for job in self .scheduler .get_jobs ()
631
- )
652
+ return tuple (Job .from_aps_job (job ) for job in self .scheduler .get_jobs ())
632
653
633
654
def get_jobs_by_name (self , name : str ) -> Tuple ["Job[CCT]" , ...]:
634
655
"""Returns a tuple of all *pending/scheduled* jobs with the given name that are currently
@@ -821,8 +842,25 @@ def next_t(self) -> Optional[datetime.datetime]:
821
842
return self .job .next_run_time
822
843
823
844
@classmethod
824
- def _from_aps_job (cls , job : "APSJob" ) -> "Job[CCT]" :
825
- return job .func .__self__
845
+ def from_aps_job (cls , aps_job : "APSJob" ) -> "Job[CCT]" :
846
+ """Provides the :class:`telegram.ext.Job` that is associated with the given APScheduler
847
+ job.
848
+
849
+ Tip:
850
+ This method can be useful when using advanced APScheduler features along with
851
+ :class:`telegram.ext.JobQueue`.
852
+
853
+ .. versionadded:: NEXT.VERSION
854
+
855
+ Args:
856
+ aps_job (:class:`apscheduler.job.Job`): The APScheduler job
857
+
858
+ Returns:
859
+ :class:`telegram.ext.Job`
860
+ """
861
+ ext_job = aps_job .args [1 ]
862
+ ext_job ._job = aps_job # pylint: disable=protected-access
863
+ return ext_job
826
864
827
865
def __getattr__ (self , item : str ) -> object :
828
866
try :
0 commit comments