@@ -68,9 +68,12 @@ def put(self, job, next_t=None):
6868
6969 Args:
7070 job (telegram.ext.Job): The ``Job`` instance representing the new job
71- next_t (Optional[int, float, datetime.timedelta]): Time in which the job
72- should be executed first. Defaults to ``job.interval``. ``int`` and ``float``
73- will be interpreted as seconds.
71+ next_t (Optional[int, float, datetime.timedelta, datetime.datetime, datetime.time]):
72+ Time in or at which the job should be executed first. Defaults to ``job.interval``.
73+ If it is an ``int`` or a ``float``, it will be interpreted as seconds. If it is
74+ a ``datetime.datetime``, it will be executed at the specified date and time. If it
75+ is a ``datetime.time``, it will execute at the specified time today or, if the time
76+ has already passed, tomorrow.
7477
7578 """
7679 job .job_queue = self
@@ -85,8 +88,20 @@ def put(self, job, next_t=None):
8588 else :
8689 raise ValueError ("The interval argument should be of type datetime.timedelta,"
8790 " int or float" )
88- elif isinstance (next_t , datetime .timedelta ):
89- next_t = next_t .total_second ()
91+
92+ elif isinstance (next_t , datetime .datetime ):
93+ next_t = next_t - datetime .datetime .now ()
94+
95+ elif isinstance (next_t , datetime .time ):
96+ next_datetime = datetime .datetime .combine (datetime .date .today (), next_t )
97+
98+ if datetime .datetime .now ().time () > next_t :
99+ next_datetime += datetime .timedelta (days = 1 )
100+
101+ next_t = next_datetime - datetime .datetime .now ()
102+
103+ if isinstance (next_t , datetime .timedelta ):
104+ next_t = next_t .total_seconds ()
90105
91106 now = time .time ()
92107 next_t += now
@@ -227,8 +242,10 @@ class Job(object):
227242 callback (function): The callback function that should be executed by the Job. It should
228243 take two parameters ``bot`` and ``job``, where ``job`` is the ``Job`` instance. It
229244 can be used to terminate the job or modify its interval.
230- interval ([int, float, datetime.timedelta]): The interval in which the job will execute its
231- callback function. ``int`` and ``float`` will be interpreted as seconds.
245+ interval (Optional[int, float, datetime.timedelta]): The interval in which the job will
246+ execute its callback function. ``int`` and ``float`` will be interpreted as seconds.
247+ If you don't set this value, you must set ``repeat=False`` and specify ``next_t`` when
248+ you put the job into the job queue.
232249 repeat (Optional[bool]): If this job should be periodically execute its callback function
233250 (``True``) or only once (``False``). Defaults to ``True``
234251 context (Optional[object]): Additional data needed for the callback function. Can be
@@ -238,7 +255,7 @@ class Job(object):
238255 """
239256 job_queue = None
240257
241- def __init__ (self , callback , interval , repeat = True , context = None , days = Days .EVERY_DAY ):
258+ def __init__ (self , callback , interval = None , repeat = True , context = None , days = Days .EVERY_DAY ):
242259 self .callback = callback
243260 self .interval = interval
244261 self .repeat = repeat
@@ -250,10 +267,13 @@ def __init__(self, callback, interval, repeat=True, context=None, days=Days.EVER
250267 if not all (isinstance (day , int ) for day in days ):
251268 raise ValueError ("The elements of the 'days' argument should be of type 'int'" )
252269
253- if not all (day >= 0 and day <= 6 for day in days ):
270+ if not all (0 <= day <= 6 for day in days ):
254271 raise ValueError ("The elements of the 'days' argument should be from 0 up to and "
255272 "including 6" )
256273
274+ if interval is None and repeat :
275+ raise ValueError ("You must either set an interval or set 'repeat' to 'False'" )
276+
257277 self .days = days
258278 self .name = callback .__name__
259279 self ._remove = Event ()
0 commit comments