telegram.ext.JobQueue¶
-
class
telegram.ext.JobQueue¶ Bases:
objectThis class allows you to periodically perform tasks with the bot. It is a convenience wrapper for the APScheduler library.
-
scheduler¶ The APScheduler
Type: apscheduler.schedulers.background.BackgroundScheduler
-
bot¶ The bot instance that should be passed to the jobs. DEPRECATED: Use
set_dispatcherinstead.Type: telegram.Bot
-
get_jobs_by_name(name: str) → Tuple[telegram.ext.jobqueue.Job, ...]¶ Returns a tuple of all pending/scheduled jobs with the given name that are currently in the
JobQueue
-
jobs() → Tuple[telegram.ext.jobqueue.Job, ...]¶ Returns a tuple of all pending/scheduled jobs that are currently in the
JobQueue.
-
run_custom(callback: Callable[[CallbackContext], None], job_kwargs: Dict[str, Any], context: object = None, name: str = None) → telegram.ext.jobqueue.Job¶ Creates a new customly defined
Job.Parameters: - callback (
callable) –The callback function that should be executed by the new job. Callback signature for context based API:
def callback(CallbackContext)context.jobis thetelegram.ext.Jobinstance. It can be used to access itsjob.contextor change it to a repeating job. - job_kwargs (
dict) – Arbitrary keyword arguments. Used as arguments forscheduler.add_job. - context (
object, optional) – Additional data needed for the callback function. Can be accessed throughjob.contextin the callback. Defaults toNone. - name (
str, optional) – The name of the new job. Defaults tocallback.__name__.
Returns: The new
Jobinstance that has been added to the job queue.Return type: - callback (
-
run_daily(callback: Callable[[CallbackContext], None], time: datetime.time, days: Tuple[int, ...] = (0, 1, 2, 3, 4, 5, 6), context: object = None, name: str = None, job_kwargs: Dict[str, Any] = None) → telegram.ext.jobqueue.Job¶ Creates a new
Jobthat runs on a daily basis and adds it to the queue.Parameters: - callback (
callable) –The callback function that should be executed by the new job. Callback signature for context based API:
def callback(CallbackContext)context.jobis thetelegram.ext.Jobinstance. It can be used to access itsjob.contextor change it to a repeating job. - time (
datetime.time) – Time of day at which the job should run. If the timezone (time.tzinfo) isNone, the default timezone of the bot will be used. - days (Tuple[
int], optional) – Defines on which days of the week the job should run (where0-6correspond to monday - sunday). Defaults toEVERY_DAY - context (
object, optional) – Additional data needed for the callback function. Can be accessed throughjob.contextin the callback. Defaults toNone. - name (
str, optional) – The name of the new job. Defaults tocallback.__name__. - job_kwargs (
dict, optional) – Arbitrary keyword arguments to pass to thescheduler.add_job().
Returns: The new
Jobinstance that has been added to the job queue.Return type: Note
For a note about DST, please see the documentation of APScheduler.
- callback (
-
run_monthly(callback: Callable[[CallbackContext], None], when: datetime.time, day: int, context: object = None, name: str = None, day_is_strict: bool = True, job_kwargs: Dict[str, Any] = None) → telegram.ext.jobqueue.Job¶ Creates a new
Jobthat runs on a monthly basis and adds it to the queue.Parameters: - callback (
callable) –The callback function that should be executed by the new job. Callback signature for context based API:
def callback(CallbackContext)context.jobis thetelegram.ext.Jobinstance. It can be used to access itsjob.contextor change it to a repeating job. - when (
datetime.time) – Time of day at which the job should run. If the timezone (when.tzinfo) isNone, the default timezone of the bot will be used. - day (
int) – Defines the day of the month whereby the job would run. It should be within the range of 1 and 31, inclusive. - context (
object, optional) – Additional data needed for the callback function. Can be accessed throughjob.contextin the callback. Defaults toNone. - name (
str, optional) – The name of the new job. Defaults tocallback.__name__. - day_is_strict (
bool, optional) – IfFalseand day > month.days, will pick the last day in the month. Defaults toTrue. - job_kwargs (
dict, optional) – Arbitrary keyword arguments to pass to thescheduler.add_job().
Returns: The new
Jobinstance that has been added to the job queue.Return type: - callback (
-
run_once(callback: Callable[[CallbackContext], None], when: Union[float, datetime.timedelta, datetime.datetime, datetime.time], context: object = None, name: str = None, job_kwargs: Dict[str, Any] = None) → telegram.ext.jobqueue.Job¶ Creates a new
Jobthat runs once and adds it to the queue.Parameters: - callback (
callable) –The callback function that should be executed by the new job. Callback signature for context based API:
def callback(CallbackContext)context.jobis thetelegram.ext.Jobinstance. It can be used to access itsjob.contextor change it to a repeating job. - when (
int|float|datetime.timedelta|datetime.datetime|datetime.time) –Time in or at which the job should run. This parameter will be interpreted depending on its type.
intorfloatwill be interpreted as “seconds from now” in which the job should run.datetime.timedeltawill be interpreted as “time from now” in which the job should run.datetime.datetimewill be interpreted as a specific date and time at which the job should run. If the timezone (datetime.tzinfo) isNone, the default timezone of the bot will be used.datetime.timewill be interpreted as a specific time of day at which the job should run. This could be either today or, if the time has already passed, tomorrow. If the timezone (time.tzinfo) isNone, the default timezone of the bot will be used.
- context (
object, optional) – Additional data needed for the callback function. Can be accessed throughjob.contextin the callback. Defaults toNone. - name (
str, optional) – The name of the new job. Defaults tocallback.__name__. - job_kwargs (
dict, optional) – Arbitrary keyword arguments to pass to thescheduler.add_job().
Returns: The new
Jobinstance that has been added to the job queue.Return type: - callback (
-
run_repeating(callback: Callable[[CallbackContext], None], interval: Union[float, datetime.timedelta], first: Union[float, datetime.timedelta, datetime.datetime, datetime.time] = None, last: Union[float, datetime.timedelta, datetime.datetime, datetime.time] = None, context: object = None, name: str = None, job_kwargs: Dict[str, Any] = None) → telegram.ext.jobqueue.Job¶ Creates a new
Jobthat runs at specified intervals and adds it to the queue.Parameters: - callback (
callable) –The callback function that should be executed by the new job. Callback signature for context based API:
def callback(CallbackContext)context.jobis thetelegram.ext.Jobinstance. It can be used to access itsjob.contextor change it to a repeating job. - interval (
int|float|datetime.timedelta) – The interval in which the job will run. If it is anintor afloat, it will be interpreted as seconds. - first (
int|float|datetime.timedelta|datetime.datetime|datetime.time, optional) –Time in or at which the job should run. This parameter will be interpreted depending on its type.
intorfloatwill be interpreted as “seconds from now” in which the job should run.datetime.timedeltawill be interpreted as “time from now” in which the job should run.datetime.datetimewill be interpreted as a specific date and time at which the job should run. If the timezone (datetime.tzinfo) isNone, the default timezone of the bot will be used.datetime.timewill be interpreted as a specific time of day at which the job should run. This could be either today or, if the time has already passed, tomorrow. If the timezone (time.tzinfo) isNone, the default timezone of the bot will be used.
Defaults to
interval - last (
int|float|datetime.timedelta|datetime.datetime|datetime.time, optional) –Latest possible time for the job to run. This parameter will be interpreted depending on its type. See
firstfor details.If
lastisdatetime.datetimeordatetime.timetype andlast.tzinfoisNone, the default timezone of the bot will be assumed.Defaults to
None. - context (
object, optional) – Additional data needed for the callback function. Can be accessed throughjob.contextin the callback. Defaults toNone. - name (
str, optional) – The name of the new job. Defaults tocallback.__name__. - job_kwargs (
dict, optional) – Arbitrary keyword arguments to pass to thescheduler.add_job().
Returns: The new
Jobinstance that has been added to the job queue.Return type: Note
interval is always respected “as-is”. That means that if DST changes during that interval, the job might not run at the time one would expect. It is always recommended to pin servers to UTC time, then time related behaviour can always be expected.
- callback (
-
set_dispatcher(dispatcher: Dispatcher) → None¶ Set the dispatcher to be used by this JobQueue. Use this instead of passing a
telegram.Botto the JobQueue, which is deprecated.Parameters: dispatcher ( telegram.ext.Dispatcher) – The dispatcher.
-
start() → None¶ Starts the job_queue thread.
-
stop() → None¶ Stops the thread.
-