2015-12-31 14:55:15 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# A library that provides a Python interface to the Telegram Bot API
|
2022-01-03 08:15:18 +01:00
|
|
|
# Copyright (C) 2015-2022
|
2016-01-05 14:12:03 +01:00
|
|
|
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
|
2015-12-31 14:55:15 +01:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Lesser Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Lesser Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser Public License
|
|
|
|
# along with this program. If not, see [http://www.gnu.org/licenses/].
|
2016-05-26 14:01:59 +02:00
|
|
|
"""This module contains the classes JobQueue and Job."""
|
2015-12-31 14:55:15 +01:00
|
|
|
|
2018-09-21 08:57:01 +02:00
|
|
|
import datetime
|
2015-12-31 14:55:15 +01:00
|
|
|
import logging
|
2021-01-30 11:38:54 +01:00
|
|
|
from typing import TYPE_CHECKING, Callable, List, Optional, Tuple, Union, cast, overload
|
2020-07-10 13:11:28 +02:00
|
|
|
|
2020-10-31 16:33:34 +01:00
|
|
|
import pytz
|
|
|
|
from apscheduler.events import EVENT_JOB_ERROR, EVENT_JOB_EXECUTED, JobEvent
|
2020-07-10 13:11:28 +02:00
|
|
|
from apscheduler.schedulers.background import BackgroundScheduler
|
2021-01-30 11:38:54 +01:00
|
|
|
from apscheduler.job import Job as APSJob
|
2018-09-21 08:57:01 +02:00
|
|
|
|
|
|
|
from telegram.ext.callbackcontext import CallbackContext
|
2020-10-06 19:28:40 +02:00
|
|
|
from telegram.utils.types import JSONDict
|
2020-10-09 17:22:07 +02:00
|
|
|
|
2020-10-06 19:28:40 +02:00
|
|
|
if TYPE_CHECKING:
|
2020-10-31 16:33:34 +01:00
|
|
|
from telegram.ext import Dispatcher
|
2021-01-30 11:38:54 +01:00
|
|
|
import apscheduler.job # noqa: F401
|
2020-10-06 19:28:40 +02:00
|
|
|
|
2015-12-31 14:55:15 +01:00
|
|
|
|
2020-06-15 18:20:51 +02:00
|
|
|
class JobQueue:
|
2020-07-10 13:11:28 +02:00
|
|
|
"""This class allows you to periodically perform tasks with the bot. It is a convenience
|
|
|
|
wrapper for the APScheduler library.
|
2015-12-31 14:55:15 +01:00
|
|
|
|
|
|
|
Attributes:
|
2020-07-10 13:11:28 +02:00
|
|
|
scheduler (:class:`apscheduler.schedulers.background.BackgroundScheduler`): The APScheduler
|
2017-07-23 22:33:08 +02:00
|
|
|
bot (:class:`telegram.Bot`): The bot instance that should be passed to the jobs.
|
2019-10-27 00:04:48 +02:00
|
|
|
DEPRECATED: Use :attr:`set_dispatcher` instead.
|
|
|
|
|
2015-12-31 14:55:15 +01:00
|
|
|
"""
|
|
|
|
|
2021-08-19 22:01:10 +02:00
|
|
|
__slots__ = ('_dispatcher', 'logger', 'scheduler')
|
2021-05-29 16:18:16 +02:00
|
|
|
|
2020-10-06 19:28:40 +02:00
|
|
|
def __init__(self) -> None:
|
|
|
|
self._dispatcher: 'Dispatcher' = None # type: ignore[assignment]
|
2016-06-21 21:25:15 +02:00
|
|
|
self.logger = logging.getLogger(self.__class__.__name__)
|
2020-07-10 13:11:28 +02:00
|
|
|
self.scheduler = BackgroundScheduler(timezone=pytz.utc)
|
2020-10-09 17:22:07 +02:00
|
|
|
self.scheduler.add_listener(
|
|
|
|
self._update_persistence, mask=EVENT_JOB_EXECUTED | EVENT_JOB_ERROR
|
|
|
|
)
|
2020-07-10 13:11:28 +02:00
|
|
|
|
|
|
|
# Dispatch errors and don't log them in the APS logger
|
2020-10-06 19:28:40 +02:00
|
|
|
def aps_log_filter(record): # type: ignore
|
2020-07-10 13:11:28 +02:00
|
|
|
return 'raised an exception' not in record.msg
|
|
|
|
|
|
|
|
logging.getLogger('apscheduler.executors.default').addFilter(aps_log_filter)
|
|
|
|
self.scheduler.add_listener(self._dispatch_error, EVENT_JOB_ERROR)
|
|
|
|
|
2021-08-29 18:15:04 +02:00
|
|
|
def _build_args(self, job: 'Job') -> List[CallbackContext]:
|
|
|
|
return [self._dispatcher.context_types.context.from_job(job, self._dispatcher)]
|
2020-07-10 13:11:28 +02:00
|
|
|
|
2020-10-06 19:28:40 +02:00
|
|
|
def _tz_now(self) -> datetime.datetime:
|
2020-07-10 13:11:28 +02:00
|
|
|
return datetime.datetime.now(self.scheduler.timezone)
|
|
|
|
|
2021-06-06 11:48:48 +02:00
|
|
|
def _update_persistence(self, _: JobEvent) -> None:
|
2020-07-10 13:11:28 +02:00
|
|
|
self._dispatcher.update_persistence()
|
|
|
|
|
2020-10-06 19:28:40 +02:00
|
|
|
def _dispatch_error(self, event: JobEvent) -> None:
|
2021-09-17 17:48:01 +02:00
|
|
|
self._dispatcher.dispatch_error(None, event.exception)
|
2020-07-10 13:11:28 +02:00
|
|
|
|
2020-10-06 19:28:40 +02:00
|
|
|
@overload
|
|
|
|
def _parse_time_input(self, time: None, shift_day: bool = False) -> None:
|
|
|
|
...
|
|
|
|
|
|
|
|
@overload
|
2020-10-09 17:22:07 +02:00
|
|
|
def _parse_time_input(
|
|
|
|
self,
|
|
|
|
time: Union[float, int, datetime.timedelta, datetime.datetime, datetime.time],
|
|
|
|
shift_day: bool = False,
|
|
|
|
) -> datetime.datetime:
|
2020-10-06 19:28:40 +02:00
|
|
|
...
|
|
|
|
|
2020-10-09 17:22:07 +02:00
|
|
|
def _parse_time_input(
|
|
|
|
self,
|
|
|
|
time: Union[float, int, datetime.timedelta, datetime.datetime, datetime.time, None],
|
|
|
|
shift_day: bool = False,
|
|
|
|
) -> Optional[datetime.datetime]:
|
2020-07-10 13:11:28 +02:00
|
|
|
if time is None:
|
|
|
|
return None
|
|
|
|
if isinstance(time, (int, float)):
|
|
|
|
return self._tz_now() + datetime.timedelta(seconds=time)
|
|
|
|
if isinstance(time, datetime.timedelta):
|
|
|
|
return self._tz_now() + time
|
|
|
|
if isinstance(time, datetime.time):
|
2020-10-31 16:33:34 +01:00
|
|
|
date_time = datetime.datetime.combine(
|
2020-10-09 17:22:07 +02:00
|
|
|
datetime.datetime.now(tz=time.tzinfo or self.scheduler.timezone).date(), time
|
|
|
|
)
|
2020-10-31 16:33:34 +01:00
|
|
|
if date_time.tzinfo is None:
|
|
|
|
date_time = self.scheduler.timezone.localize(date_time)
|
|
|
|
if shift_day and date_time <= datetime.datetime.now(pytz.utc):
|
|
|
|
date_time += datetime.timedelta(days=1)
|
|
|
|
return date_time
|
2020-07-10 13:11:28 +02:00
|
|
|
# isinstance(time, datetime.datetime):
|
|
|
|
return time
|
2015-12-31 14:55:15 +01:00
|
|
|
|
2020-10-06 19:28:40 +02:00
|
|
|
def set_dispatcher(self, dispatcher: 'Dispatcher') -> None:
|
2019-10-27 00:04:48 +02:00
|
|
|
"""Set the dispatcher to be used by this JobQueue. Use this instead of passing a
|
|
|
|
:class:`telegram.Bot` to the JobQueue, which is deprecated.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
dispatcher (:class:`telegram.ext.Dispatcher`): The dispatcher.
|
|
|
|
|
|
|
|
"""
|
2018-09-21 08:57:01 +02:00
|
|
|
self._dispatcher = dispatcher
|
2020-09-27 12:59:48 +02:00
|
|
|
if dispatcher.bot.defaults:
|
2021-05-27 09:38:17 +02:00
|
|
|
self.scheduler.configure(timezone=dispatcher.bot.defaults.tzinfo or pytz.utc)
|
2018-09-21 08:57:01 +02:00
|
|
|
|
2020-10-09 17:22:07 +02:00
|
|
|
def run_once(
|
|
|
|
self,
|
|
|
|
callback: Callable[['CallbackContext'], None],
|
|
|
|
when: Union[float, datetime.timedelta, datetime.datetime, datetime.time],
|
|
|
|
context: object = None,
|
|
|
|
name: str = None,
|
|
|
|
job_kwargs: JSONDict = None,
|
|
|
|
) -> 'Job':
|
2017-09-01 08:43:08 +02:00
|
|
|
"""Creates a new ``Job`` that runs once and adds it to the queue.
|
2016-12-14 16:27:45 +01:00
|
|
|
|
|
|
|
Args:
|
2017-07-23 22:33:08 +02:00
|
|
|
callback (:obj:`callable`): The callback function that should be executed by the new
|
2021-08-29 18:15:04 +02:00
|
|
|
job. Callback signature: ``def callback(update: Update, context: CallbackContext)``
|
2018-02-19 09:36:40 +01:00
|
|
|
when (:obj:`int` | :obj:`float` | :obj:`datetime.timedelta` | \
|
|
|
|
:obj:`datetime.datetime` | :obj:`datetime.time`):
|
2016-12-14 16:27:45 +01:00
|
|
|
Time in or at which the job should run. This parameter will be interpreted
|
|
|
|
depending on its type.
|
2016-12-14 23:08:03 +01:00
|
|
|
|
2017-07-23 22:33:08 +02:00
|
|
|
* :obj:`int` or :obj:`float` will be interpreted as "seconds from now" in which the
|
|
|
|
job should run.
|
|
|
|
* :obj:`datetime.timedelta` will be interpreted as "time from now" in which the
|
|
|
|
job should run.
|
|
|
|
* :obj:`datetime.datetime` will be interpreted as a specific date and time at
|
Documentation Improvements (#2008)
* Minor doc updates, following official API docs
* Fix spelling in Defaults docstrings
* Clarify Changelog of v12.7 about aware dates
* Fix typo in CHANGES.rst (#2024)
* Fix PicklePersistence.flush() with only bot_data (#2017)
* Update pylint in pre-commit to fix CI (#2018)
* Add Filters.via_bot (#2009)
* feat: via_bot filter
also fixing a small mistake in the empty parameter of the user filter and improve docs slightly
* fix: forgot to set via_bot to None
* fix: redoing subclassing to copy paste solution
* Cosmetic changes
Co-authored-by: Hinrich Mahler <hinrich.mahler@freenet.de>
* Update CHANGES.rst
Fixed Typo
Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
* Update downloads badge, add info on IRC Channel to Getting Help section
* Remove RegexHandler from ConversationHandlers Docs (#1973)
Replaced RegexHandler with MessageHandler, since the former is deprecated
* Fix Filters.via_bot docstrings
* Add notes on Markdown v1 being legacy mode
* Fixed typo in the Regex doc.. (#2036)
* Typo: Spelling
* Minor cleanup from #2043
* Document CommandHandler ignoring channel posts
* Doc fixes for a few telegram.ext classes
* Doc fixes for most `telegram` classes.
* pep-8
forgot the hard wrap is at 99 chars, not 100!
fixed a few spelling mistakes too.
* Address review and made rendering of booleans consistent
True, False, None are now rendered with ``bool`` wherever they weren't in telegram and telegram.ext classes.
* Few doc fixes for inline* classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram/files classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram.Game
Mostly just added hyperlinks. And fixed message length doc.
As usual, docs were cross-checked with official tg api docs.
* Very minor doc fix for passportfile.py and passportelementerrors.py
Didn't bother changing too much since this seems to be a custom implementation.
* Doc fixes for telegram.payments
As usual, cross-checked with official bot api docs.
* Address review 2
Few tiny other fixes too.
* Changed from ``True/False/None`` to :obj:`True/False/None` project-wide.
Few tiny other doc fixes too.
Co-authored-by: Robert Geislinger <mitachundkrach@gmail.com>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
Co-authored-by: GauthamramRavichandran <30320759+GauthamramRavichandran@users.noreply.github.com>
Co-authored-by: Mahesh19 <maheshvagicherla99438@gmail.com>
Co-authored-by: hoppingturtles <ilovebhagwan@gmail.com>
2020-08-24 19:35:57 +02:00
|
|
|
which the job should run. If the timezone (``datetime.tzinfo``) is :obj:`None`,
|
2020-09-27 12:59:48 +02:00
|
|
|
the default timezone of the bot will be used.
|
2017-07-23 22:33:08 +02:00
|
|
|
* :obj:`datetime.time` will 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,
|
2020-09-27 12:59:48 +02:00
|
|
|
tomorrow. If the timezone (``time.tzinfo``) is :obj:`None`, the
|
|
|
|
default timezone of the bot will be used.
|
2020-04-18 15:08:16 +02:00
|
|
|
|
2017-07-23 22:33:08 +02:00
|
|
|
context (:obj:`object`, optional): Additional data needed for the callback function.
|
Documentation Improvements (#2008)
* Minor doc updates, following official API docs
* Fix spelling in Defaults docstrings
* Clarify Changelog of v12.7 about aware dates
* Fix typo in CHANGES.rst (#2024)
* Fix PicklePersistence.flush() with only bot_data (#2017)
* Update pylint in pre-commit to fix CI (#2018)
* Add Filters.via_bot (#2009)
* feat: via_bot filter
also fixing a small mistake in the empty parameter of the user filter and improve docs slightly
* fix: forgot to set via_bot to None
* fix: redoing subclassing to copy paste solution
* Cosmetic changes
Co-authored-by: Hinrich Mahler <hinrich.mahler@freenet.de>
* Update CHANGES.rst
Fixed Typo
Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
* Update downloads badge, add info on IRC Channel to Getting Help section
* Remove RegexHandler from ConversationHandlers Docs (#1973)
Replaced RegexHandler with MessageHandler, since the former is deprecated
* Fix Filters.via_bot docstrings
* Add notes on Markdown v1 being legacy mode
* Fixed typo in the Regex doc.. (#2036)
* Typo: Spelling
* Minor cleanup from #2043
* Document CommandHandler ignoring channel posts
* Doc fixes for a few telegram.ext classes
* Doc fixes for most `telegram` classes.
* pep-8
forgot the hard wrap is at 99 chars, not 100!
fixed a few spelling mistakes too.
* Address review and made rendering of booleans consistent
True, False, None are now rendered with ``bool`` wherever they weren't in telegram and telegram.ext classes.
* Few doc fixes for inline* classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram/files classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram.Game
Mostly just added hyperlinks. And fixed message length doc.
As usual, docs were cross-checked with official tg api docs.
* Very minor doc fix for passportfile.py and passportelementerrors.py
Didn't bother changing too much since this seems to be a custom implementation.
* Doc fixes for telegram.payments
As usual, cross-checked with official bot api docs.
* Address review 2
Few tiny other fixes too.
* Changed from ``True/False/None`` to :obj:`True/False/None` project-wide.
Few tiny other doc fixes too.
Co-authored-by: Robert Geislinger <mitachundkrach@gmail.com>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
Co-authored-by: GauthamramRavichandran <30320759+GauthamramRavichandran@users.noreply.github.com>
Co-authored-by: Mahesh19 <maheshvagicherla99438@gmail.com>
Co-authored-by: hoppingturtles <ilovebhagwan@gmail.com>
2020-08-24 19:35:57 +02:00
|
|
|
Can be accessed through ``job.context`` in the callback. Defaults to :obj:`None`.
|
2017-07-23 22:33:08 +02:00
|
|
|
name (:obj:`str`, optional): The name of the new job. Defaults to
|
|
|
|
``callback.__name__``.
|
2020-07-10 13:11:28 +02:00
|
|
|
job_kwargs (:obj:`dict`, optional): Arbitrary keyword arguments to pass to the
|
|
|
|
``scheduler.add_job()``.
|
2016-12-14 16:27:45 +01:00
|
|
|
|
|
|
|
Returns:
|
2017-07-23 22:33:08 +02:00
|
|
|
:class:`telegram.ext.Job`: The new ``Job`` instance that has been added to the job
|
|
|
|
queue.
|
|
|
|
|
2017-09-01 08:43:08 +02:00
|
|
|
"""
|
2020-07-10 13:11:28 +02:00
|
|
|
if not job_kwargs:
|
|
|
|
job_kwargs = {}
|
|
|
|
|
|
|
|
name = name or callback.__name__
|
2021-08-30 16:31:19 +02:00
|
|
|
job = Job(callback, context, name)
|
2020-10-31 16:33:34 +01:00
|
|
|
date_time = self._parse_time_input(when, shift_day=True)
|
2020-07-10 13:11:28 +02:00
|
|
|
|
2020-10-09 17:22:07 +02:00
|
|
|
j = self.scheduler.add_job(
|
|
|
|
callback,
|
|
|
|
name=name,
|
|
|
|
trigger='date',
|
2020-10-31 16:33:34 +01:00
|
|
|
run_date=date_time,
|
2020-10-09 17:22:07 +02:00
|
|
|
args=self._build_args(job),
|
2020-10-31 16:33:34 +01:00
|
|
|
timezone=date_time.tzinfo or self.scheduler.timezone,
|
2020-10-09 17:22:07 +02:00
|
|
|
**job_kwargs,
|
|
|
|
)
|
2020-07-10 13:11:28 +02:00
|
|
|
|
|
|
|
job.job = j
|
2016-12-14 16:27:45 +01:00
|
|
|
return job
|
|
|
|
|
2020-10-09 17:22:07 +02:00
|
|
|
def run_repeating(
|
|
|
|
self,
|
|
|
|
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: JSONDict = None,
|
|
|
|
) -> 'Job':
|
2018-11-22 13:03:58 +01:00
|
|
|
"""Creates a new ``Job`` that runs at specified intervals and adds it to the queue.
|
2016-12-14 16:27:45 +01:00
|
|
|
|
2021-06-06 12:16:23 +02:00
|
|
|
Note:
|
|
|
|
For a note about DST, please see the documentation of `APScheduler`_.
|
|
|
|
|
|
|
|
.. _`APScheduler`: https://apscheduler.readthedocs.io/en/stable/modules/triggers/cron.html
|
|
|
|
#daylight-saving-time-behavior
|
|
|
|
|
2016-12-14 16:27:45 +01:00
|
|
|
Args:
|
2017-07-23 22:33:08 +02:00
|
|
|
callback (:obj:`callable`): The callback function that should be executed by the new
|
2021-08-29 18:15:04 +02:00
|
|
|
job. Callback signature: ``def callback(update: Update, context: CallbackContext)``
|
2017-07-23 22:33:08 +02:00
|
|
|
interval (:obj:`int` | :obj:`float` | :obj:`datetime.timedelta`): The interval in which
|
|
|
|
the job will run. If it is an :obj:`int` or a :obj:`float`, it will be interpreted
|
|
|
|
as seconds.
|
2018-02-19 09:36:40 +01:00
|
|
|
first (:obj:`int` | :obj:`float` | :obj:`datetime.timedelta` | \
|
|
|
|
:obj:`datetime.datetime` | :obj:`datetime.time`, optional):
|
2017-07-23 22:33:08 +02:00
|
|
|
Time in or at which the job should run. This parameter will be interpreted
|
|
|
|
depending on its type.
|
|
|
|
|
|
|
|
* :obj:`int` or :obj:`float` will be interpreted as "seconds from now" in which the
|
|
|
|
job should run.
|
|
|
|
* :obj:`datetime.timedelta` will be interpreted as "time from now" in which the
|
|
|
|
job should run.
|
|
|
|
* :obj:`datetime.datetime` will be interpreted as a specific date and time at
|
Documentation Improvements (#2008)
* Minor doc updates, following official API docs
* Fix spelling in Defaults docstrings
* Clarify Changelog of v12.7 about aware dates
* Fix typo in CHANGES.rst (#2024)
* Fix PicklePersistence.flush() with only bot_data (#2017)
* Update pylint in pre-commit to fix CI (#2018)
* Add Filters.via_bot (#2009)
* feat: via_bot filter
also fixing a small mistake in the empty parameter of the user filter and improve docs slightly
* fix: forgot to set via_bot to None
* fix: redoing subclassing to copy paste solution
* Cosmetic changes
Co-authored-by: Hinrich Mahler <hinrich.mahler@freenet.de>
* Update CHANGES.rst
Fixed Typo
Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
* Update downloads badge, add info on IRC Channel to Getting Help section
* Remove RegexHandler from ConversationHandlers Docs (#1973)
Replaced RegexHandler with MessageHandler, since the former is deprecated
* Fix Filters.via_bot docstrings
* Add notes on Markdown v1 being legacy mode
* Fixed typo in the Regex doc.. (#2036)
* Typo: Spelling
* Minor cleanup from #2043
* Document CommandHandler ignoring channel posts
* Doc fixes for a few telegram.ext classes
* Doc fixes for most `telegram` classes.
* pep-8
forgot the hard wrap is at 99 chars, not 100!
fixed a few spelling mistakes too.
* Address review and made rendering of booleans consistent
True, False, None are now rendered with ``bool`` wherever they weren't in telegram and telegram.ext classes.
* Few doc fixes for inline* classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram/files classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram.Game
Mostly just added hyperlinks. And fixed message length doc.
As usual, docs were cross-checked with official tg api docs.
* Very minor doc fix for passportfile.py and passportelementerrors.py
Didn't bother changing too much since this seems to be a custom implementation.
* Doc fixes for telegram.payments
As usual, cross-checked with official bot api docs.
* Address review 2
Few tiny other fixes too.
* Changed from ``True/False/None`` to :obj:`True/False/None` project-wide.
Few tiny other doc fixes too.
Co-authored-by: Robert Geislinger <mitachundkrach@gmail.com>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
Co-authored-by: GauthamramRavichandran <30320759+GauthamramRavichandran@users.noreply.github.com>
Co-authored-by: Mahesh19 <maheshvagicherla99438@gmail.com>
Co-authored-by: hoppingturtles <ilovebhagwan@gmail.com>
2020-08-24 19:35:57 +02:00
|
|
|
which the job should run. If the timezone (``datetime.tzinfo``) is :obj:`None`,
|
2020-09-27 12:59:48 +02:00
|
|
|
the default timezone of the bot will be used.
|
2017-07-23 22:33:08 +02:00
|
|
|
* :obj:`datetime.time` will 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,
|
2020-09-27 12:59:48 +02:00
|
|
|
tomorrow. If the timezone (``time.tzinfo``) is :obj:`None`, the
|
|
|
|
default timezone of the bot will be used.
|
2020-04-18 15:08:16 +02:00
|
|
|
|
2016-12-14 16:27:45 +01:00
|
|
|
Defaults to ``interval``
|
2020-07-10 13:11:28 +02:00
|
|
|
last (:obj:`int` | :obj:`float` | :obj:`datetime.timedelta` | \
|
|
|
|
:obj:`datetime.datetime` | :obj:`datetime.time`, optional):
|
|
|
|
Latest possible time for the job to run. This parameter will be interpreted
|
|
|
|
depending on its type. See ``first`` for details.
|
|
|
|
|
|
|
|
If ``last`` is :obj:`datetime.datetime` or :obj:`datetime.time` type
|
2020-09-27 12:59:48 +02:00
|
|
|
and ``last.tzinfo`` is :obj:`None`, the default timezone of the bot will be
|
|
|
|
assumed.
|
2020-07-10 13:11:28 +02:00
|
|
|
|
|
|
|
Defaults to :obj:`None`.
|
2017-07-23 22:33:08 +02:00
|
|
|
context (:obj:`object`, optional): Additional data needed for the callback function.
|
Documentation Improvements (#2008)
* Minor doc updates, following official API docs
* Fix spelling in Defaults docstrings
* Clarify Changelog of v12.7 about aware dates
* Fix typo in CHANGES.rst (#2024)
* Fix PicklePersistence.flush() with only bot_data (#2017)
* Update pylint in pre-commit to fix CI (#2018)
* Add Filters.via_bot (#2009)
* feat: via_bot filter
also fixing a small mistake in the empty parameter of the user filter and improve docs slightly
* fix: forgot to set via_bot to None
* fix: redoing subclassing to copy paste solution
* Cosmetic changes
Co-authored-by: Hinrich Mahler <hinrich.mahler@freenet.de>
* Update CHANGES.rst
Fixed Typo
Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
* Update downloads badge, add info on IRC Channel to Getting Help section
* Remove RegexHandler from ConversationHandlers Docs (#1973)
Replaced RegexHandler with MessageHandler, since the former is deprecated
* Fix Filters.via_bot docstrings
* Add notes on Markdown v1 being legacy mode
* Fixed typo in the Regex doc.. (#2036)
* Typo: Spelling
* Minor cleanup from #2043
* Document CommandHandler ignoring channel posts
* Doc fixes for a few telegram.ext classes
* Doc fixes for most `telegram` classes.
* pep-8
forgot the hard wrap is at 99 chars, not 100!
fixed a few spelling mistakes too.
* Address review and made rendering of booleans consistent
True, False, None are now rendered with ``bool`` wherever they weren't in telegram and telegram.ext classes.
* Few doc fixes for inline* classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram/files classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram.Game
Mostly just added hyperlinks. And fixed message length doc.
As usual, docs were cross-checked with official tg api docs.
* Very minor doc fix for passportfile.py and passportelementerrors.py
Didn't bother changing too much since this seems to be a custom implementation.
* Doc fixes for telegram.payments
As usual, cross-checked with official bot api docs.
* Address review 2
Few tiny other fixes too.
* Changed from ``True/False/None`` to :obj:`True/False/None` project-wide.
Few tiny other doc fixes too.
Co-authored-by: Robert Geislinger <mitachundkrach@gmail.com>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
Co-authored-by: GauthamramRavichandran <30320759+GauthamramRavichandran@users.noreply.github.com>
Co-authored-by: Mahesh19 <maheshvagicherla99438@gmail.com>
Co-authored-by: hoppingturtles <ilovebhagwan@gmail.com>
2020-08-24 19:35:57 +02:00
|
|
|
Can be accessed through ``job.context`` in the callback. Defaults to :obj:`None`.
|
2017-07-23 22:33:08 +02:00
|
|
|
name (:obj:`str`, optional): The name of the new job. Defaults to
|
|
|
|
``callback.__name__``.
|
2020-07-10 13:11:28 +02:00
|
|
|
job_kwargs (:obj:`dict`, optional): Arbitrary keyword arguments to pass to the
|
|
|
|
``scheduler.add_job()``.
|
2016-12-14 16:27:45 +01:00
|
|
|
|
|
|
|
Returns:
|
2017-07-23 22:33:08 +02:00
|
|
|
:class:`telegram.ext.Job`: The new ``Job`` instance that has been added to the job
|
|
|
|
queue.
|
2017-09-01 08:43:08 +02:00
|
|
|
|
2016-12-14 16:27:45 +01:00
|
|
|
"""
|
2020-07-10 13:11:28 +02:00
|
|
|
if not job_kwargs:
|
|
|
|
job_kwargs = {}
|
|
|
|
|
|
|
|
name = name or callback.__name__
|
2021-08-30 16:31:19 +02:00
|
|
|
job = Job(callback, context, name)
|
2020-07-10 13:11:28 +02:00
|
|
|
|
|
|
|
dt_first = self._parse_time_input(first)
|
|
|
|
dt_last = self._parse_time_input(last)
|
|
|
|
|
|
|
|
if dt_last and dt_first and dt_last < dt_first:
|
|
|
|
raise ValueError("'last' must not be before 'first'!")
|
|
|
|
|
|
|
|
if isinstance(interval, datetime.timedelta):
|
|
|
|
interval = interval.total_seconds()
|
|
|
|
|
2020-10-09 17:22:07 +02:00
|
|
|
j = self.scheduler.add_job(
|
|
|
|
callback,
|
|
|
|
trigger='interval',
|
|
|
|
args=self._build_args(job),
|
|
|
|
start_date=dt_first,
|
|
|
|
end_date=dt_last,
|
|
|
|
seconds=interval,
|
|
|
|
name=name,
|
|
|
|
**job_kwargs,
|
|
|
|
)
|
2020-07-10 13:11:28 +02:00
|
|
|
|
|
|
|
job.job = j
|
2016-12-14 16:27:45 +01:00
|
|
|
return job
|
|
|
|
|
2020-10-09 17:22:07 +02:00
|
|
|
def run_monthly(
|
|
|
|
self,
|
|
|
|
callback: Callable[['CallbackContext'], None],
|
|
|
|
when: datetime.time,
|
|
|
|
day: int,
|
|
|
|
context: object = None,
|
|
|
|
name: str = None,
|
|
|
|
job_kwargs: JSONDict = None,
|
|
|
|
) -> 'Job':
|
2020-05-02 08:59:50 +02:00
|
|
|
"""Creates a new ``Job`` that runs on a monthly basis and adds it to the queue.
|
|
|
|
|
2021-08-26 20:59:23 +02:00
|
|
|
.. versionchanged:: 14.0
|
|
|
|
The ``day_is_strict`` argument was removed. Instead one can now pass -1 to the ``day``
|
|
|
|
parameter to have the job run on the last day of the month.
|
|
|
|
|
2020-05-02 08:59:50 +02:00
|
|
|
Args:
|
2020-05-02 12:14:01 +02:00
|
|
|
callback (:obj:`callable`): The callback function that should be executed by the new
|
2021-08-29 18:15:04 +02:00
|
|
|
job. Callback signature: ``def callback(update: Update, context: CallbackContext)``
|
2020-05-02 08:59:50 +02:00
|
|
|
when (:obj:`datetime.time`): Time of day at which the job should run. If the timezone
|
2020-09-27 12:59:48 +02:00
|
|
|
(``when.tzinfo``) is :obj:`None`, the default timezone of the bot will be used.
|
2020-05-02 08:59:50 +02:00
|
|
|
day (:obj:`int`): Defines the day of the month whereby the job would run. It should
|
2021-08-26 20:59:23 +02:00
|
|
|
be within the range of 1 and 31, inclusive. If a month has fewer days than this
|
|
|
|
number, the job will not run in this month. Passing -1 leads to the job running on
|
|
|
|
the last day of the month.
|
2020-05-02 08:59:50 +02:00
|
|
|
context (:obj:`object`, optional): Additional data needed for the callback function.
|
Documentation Improvements (#2008)
* Minor doc updates, following official API docs
* Fix spelling in Defaults docstrings
* Clarify Changelog of v12.7 about aware dates
* Fix typo in CHANGES.rst (#2024)
* Fix PicklePersistence.flush() with only bot_data (#2017)
* Update pylint in pre-commit to fix CI (#2018)
* Add Filters.via_bot (#2009)
* feat: via_bot filter
also fixing a small mistake in the empty parameter of the user filter and improve docs slightly
* fix: forgot to set via_bot to None
* fix: redoing subclassing to copy paste solution
* Cosmetic changes
Co-authored-by: Hinrich Mahler <hinrich.mahler@freenet.de>
* Update CHANGES.rst
Fixed Typo
Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
* Update downloads badge, add info on IRC Channel to Getting Help section
* Remove RegexHandler from ConversationHandlers Docs (#1973)
Replaced RegexHandler with MessageHandler, since the former is deprecated
* Fix Filters.via_bot docstrings
* Add notes on Markdown v1 being legacy mode
* Fixed typo in the Regex doc.. (#2036)
* Typo: Spelling
* Minor cleanup from #2043
* Document CommandHandler ignoring channel posts
* Doc fixes for a few telegram.ext classes
* Doc fixes for most `telegram` classes.
* pep-8
forgot the hard wrap is at 99 chars, not 100!
fixed a few spelling mistakes too.
* Address review and made rendering of booleans consistent
True, False, None are now rendered with ``bool`` wherever they weren't in telegram and telegram.ext classes.
* Few doc fixes for inline* classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram/files classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram.Game
Mostly just added hyperlinks. And fixed message length doc.
As usual, docs were cross-checked with official tg api docs.
* Very minor doc fix for passportfile.py and passportelementerrors.py
Didn't bother changing too much since this seems to be a custom implementation.
* Doc fixes for telegram.payments
As usual, cross-checked with official bot api docs.
* Address review 2
Few tiny other fixes too.
* Changed from ``True/False/None`` to :obj:`True/False/None` project-wide.
Few tiny other doc fixes too.
Co-authored-by: Robert Geislinger <mitachundkrach@gmail.com>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
Co-authored-by: GauthamramRavichandran <30320759+GauthamramRavichandran@users.noreply.github.com>
Co-authored-by: Mahesh19 <maheshvagicherla99438@gmail.com>
Co-authored-by: hoppingturtles <ilovebhagwan@gmail.com>
2020-08-24 19:35:57 +02:00
|
|
|
Can be accessed through ``job.context`` in the callback. Defaults to :obj:`None`.
|
2020-05-02 08:59:50 +02:00
|
|
|
name (:obj:`str`, optional): The name of the new job. Defaults to
|
|
|
|
``callback.__name__``.
|
2020-07-10 13:11:28 +02:00
|
|
|
job_kwargs (:obj:`dict`, optional): Arbitrary keyword arguments to pass to the
|
|
|
|
``scheduler.add_job()``.
|
2020-05-02 08:59:50 +02:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
:class:`telegram.ext.Job`: The new ``Job`` instance that has been added to the job
|
|
|
|
queue.
|
|
|
|
|
|
|
|
"""
|
2020-07-10 13:11:28 +02:00
|
|
|
if not job_kwargs:
|
|
|
|
job_kwargs = {}
|
|
|
|
|
|
|
|
name = name or callback.__name__
|
2021-08-30 16:31:19 +02:00
|
|
|
job = Job(callback, context, name)
|
2020-07-10 13:11:28 +02:00
|
|
|
|
2021-08-26 20:59:23 +02:00
|
|
|
j = self.scheduler.add_job(
|
|
|
|
callback,
|
|
|
|
trigger='cron',
|
|
|
|
args=self._build_args(job),
|
|
|
|
name=name,
|
|
|
|
day='last' if day == -1 else day,
|
|
|
|
hour=when.hour,
|
|
|
|
minute=when.minute,
|
|
|
|
second=when.second,
|
|
|
|
timezone=when.tzinfo or self.scheduler.timezone,
|
|
|
|
**job_kwargs,
|
|
|
|
)
|
2020-07-10 13:11:28 +02:00
|
|
|
job.job = j
|
|
|
|
return job
|
2020-05-02 08:59:50 +02:00
|
|
|
|
2020-10-09 17:22:07 +02:00
|
|
|
def run_daily(
|
|
|
|
self,
|
|
|
|
callback: Callable[['CallbackContext'], None],
|
|
|
|
time: datetime.time,
|
2021-05-27 09:38:17 +02:00
|
|
|
days: Tuple[int, ...] = tuple(range(7)),
|
2020-10-09 17:22:07 +02:00
|
|
|
context: object = None,
|
|
|
|
name: str = None,
|
|
|
|
job_kwargs: JSONDict = None,
|
|
|
|
) -> 'Job':
|
2018-11-22 13:03:58 +01:00
|
|
|
"""Creates a new ``Job`` that runs on a daily basis and adds it to the queue.
|
2016-12-14 16:27:45 +01:00
|
|
|
|
2021-06-06 12:16:23 +02:00
|
|
|
Note:
|
|
|
|
For a note about DST, please see the documentation of `APScheduler`_.
|
|
|
|
|
|
|
|
.. _`APScheduler`: https://apscheduler.readthedocs.io/en/stable/modules/triggers/cron.html
|
|
|
|
#daylight-saving-time-behavior
|
|
|
|
|
2016-12-14 16:27:45 +01:00
|
|
|
Args:
|
2017-07-23 22:33:08 +02:00
|
|
|
callback (:obj:`callable`): The callback function that should be executed by the new
|
2021-08-29 18:15:04 +02:00
|
|
|
job. Callback signature: ``def callback(update: Update, context: CallbackContext)``
|
2019-11-15 21:51:22 +01:00
|
|
|
time (:obj:`datetime.time`): Time of day at which the job should run. If the timezone
|
2020-09-27 12:59:48 +02:00
|
|
|
(``time.tzinfo``) is :obj:`None`, the default timezone of the bot will be used.
|
2017-07-23 22:33:08 +02:00
|
|
|
days (Tuple[:obj:`int`], optional): Defines on which days of the week the job should
|
2020-12-17 09:29:17 +01:00
|
|
|
run (where ``0-6`` correspond to monday - sunday). Defaults to ``EVERY_DAY``
|
2017-07-23 22:33:08 +02:00
|
|
|
context (:obj:`object`, optional): Additional data needed for the callback function.
|
Documentation Improvements (#2008)
* Minor doc updates, following official API docs
* Fix spelling in Defaults docstrings
* Clarify Changelog of v12.7 about aware dates
* Fix typo in CHANGES.rst (#2024)
* Fix PicklePersistence.flush() with only bot_data (#2017)
* Update pylint in pre-commit to fix CI (#2018)
* Add Filters.via_bot (#2009)
* feat: via_bot filter
also fixing a small mistake in the empty parameter of the user filter and improve docs slightly
* fix: forgot to set via_bot to None
* fix: redoing subclassing to copy paste solution
* Cosmetic changes
Co-authored-by: Hinrich Mahler <hinrich.mahler@freenet.de>
* Update CHANGES.rst
Fixed Typo
Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
* Update downloads badge, add info on IRC Channel to Getting Help section
* Remove RegexHandler from ConversationHandlers Docs (#1973)
Replaced RegexHandler with MessageHandler, since the former is deprecated
* Fix Filters.via_bot docstrings
* Add notes on Markdown v1 being legacy mode
* Fixed typo in the Regex doc.. (#2036)
* Typo: Spelling
* Minor cleanup from #2043
* Document CommandHandler ignoring channel posts
* Doc fixes for a few telegram.ext classes
* Doc fixes for most `telegram` classes.
* pep-8
forgot the hard wrap is at 99 chars, not 100!
fixed a few spelling mistakes too.
* Address review and made rendering of booleans consistent
True, False, None are now rendered with ``bool`` wherever they weren't in telegram and telegram.ext classes.
* Few doc fixes for inline* classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram/files classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram.Game
Mostly just added hyperlinks. And fixed message length doc.
As usual, docs were cross-checked with official tg api docs.
* Very minor doc fix for passportfile.py and passportelementerrors.py
Didn't bother changing too much since this seems to be a custom implementation.
* Doc fixes for telegram.payments
As usual, cross-checked with official bot api docs.
* Address review 2
Few tiny other fixes too.
* Changed from ``True/False/None`` to :obj:`True/False/None` project-wide.
Few tiny other doc fixes too.
Co-authored-by: Robert Geislinger <mitachundkrach@gmail.com>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
Co-authored-by: GauthamramRavichandran <30320759+GauthamramRavichandran@users.noreply.github.com>
Co-authored-by: Mahesh19 <maheshvagicherla99438@gmail.com>
Co-authored-by: hoppingturtles <ilovebhagwan@gmail.com>
2020-08-24 19:35:57 +02:00
|
|
|
Can be accessed through ``job.context`` in the callback. Defaults to :obj:`None`.
|
2017-07-23 22:33:08 +02:00
|
|
|
name (:obj:`str`, optional): The name of the new job. Defaults to
|
|
|
|
``callback.__name__``.
|
2020-07-10 13:11:28 +02:00
|
|
|
job_kwargs (:obj:`dict`, optional): Arbitrary keyword arguments to pass to the
|
|
|
|
``scheduler.add_job()``.
|
2016-12-14 16:27:45 +01:00
|
|
|
|
|
|
|
Returns:
|
2017-07-23 22:33:08 +02:00
|
|
|
:class:`telegram.ext.Job`: The new ``Job`` instance that has been added to the job
|
|
|
|
queue.
|
2017-09-01 08:43:08 +02:00
|
|
|
|
2016-12-14 16:27:45 +01:00
|
|
|
"""
|
2020-07-10 13:11:28 +02:00
|
|
|
if not job_kwargs:
|
|
|
|
job_kwargs = {}
|
|
|
|
|
|
|
|
name = name or callback.__name__
|
2021-08-30 16:31:19 +02:00
|
|
|
job = Job(callback, context, name)
|
2020-07-10 13:11:28 +02:00
|
|
|
|
2020-10-09 17:22:07 +02:00
|
|
|
j = self.scheduler.add_job(
|
|
|
|
callback,
|
|
|
|
name=name,
|
|
|
|
args=self._build_args(job),
|
|
|
|
trigger='cron',
|
|
|
|
day_of_week=','.join([str(d) for d in days]),
|
|
|
|
hour=time.hour,
|
|
|
|
minute=time.minute,
|
|
|
|
second=time.second,
|
|
|
|
timezone=time.tzinfo or self.scheduler.timezone,
|
|
|
|
**job_kwargs,
|
|
|
|
)
|
2020-07-10 13:11:28 +02:00
|
|
|
|
|
|
|
job.job = j
|
2016-12-14 16:27:45 +01:00
|
|
|
return job
|
|
|
|
|
2020-10-09 17:22:07 +02:00
|
|
|
def run_custom(
|
|
|
|
self,
|
|
|
|
callback: Callable[['CallbackContext'], None],
|
|
|
|
job_kwargs: JSONDict,
|
|
|
|
context: object = None,
|
|
|
|
name: str = None,
|
|
|
|
) -> 'Job':
|
2020-07-10 13:11:28 +02:00
|
|
|
"""Creates a new customly defined ``Job``.
|
2016-01-05 13:32:19 +01:00
|
|
|
|
2020-07-10 13:11:28 +02:00
|
|
|
Args:
|
|
|
|
callback (:obj:`callable`): The callback function that should be executed by the new
|
2021-08-29 18:15:04 +02:00
|
|
|
job. Callback signature: ``def callback(update: Update, context: CallbackContext)``
|
2020-07-10 13:11:28 +02:00
|
|
|
job_kwargs (:obj:`dict`): Arbitrary keyword arguments. Used as arguments for
|
|
|
|
``scheduler.add_job``.
|
|
|
|
context (:obj:`object`, optional): Additional data needed for the callback function.
|
2020-10-06 19:28:40 +02:00
|
|
|
Can be accessed through ``job.context`` in the callback. Defaults to ``None``.
|
2020-07-10 13:11:28 +02:00
|
|
|
name (:obj:`str`, optional): The name of the new job. Defaults to
|
|
|
|
``callback.__name__``.
|
2015-12-31 14:55:15 +01:00
|
|
|
|
2020-07-10 13:11:28 +02:00
|
|
|
Returns:
|
|
|
|
:class:`telegram.ext.Job`: The new ``Job`` instance that has been added to the job
|
|
|
|
queue.
|
2017-07-23 22:33:08 +02:00
|
|
|
|
2017-09-01 08:43:08 +02:00
|
|
|
"""
|
2020-07-10 13:11:28 +02:00
|
|
|
name = name or callback.__name__
|
2021-08-30 16:31:19 +02:00
|
|
|
job = Job(callback, context, name)
|
2016-06-22 00:24:59 +02:00
|
|
|
|
2020-10-09 17:22:07 +02:00
|
|
|
j = self.scheduler.add_job(callback, args=self._build_args(job), name=name, **job_kwargs)
|
2016-05-25 22:51:13 +02:00
|
|
|
|
2020-07-10 13:11:28 +02:00
|
|
|
job.job = j
|
|
|
|
return job
|
2016-01-04 00:01:00 +01:00
|
|
|
|
2020-10-06 19:28:40 +02:00
|
|
|
def start(self) -> None:
|
2020-07-10 13:11:28 +02:00
|
|
|
"""Starts the job_queue thread."""
|
|
|
|
if not self.scheduler.running:
|
|
|
|
self.scheduler.start()
|
2016-01-04 01:56:22 +01:00
|
|
|
|
2020-10-06 19:28:40 +02:00
|
|
|
def stop(self) -> None:
|
2017-09-01 08:43:08 +02:00
|
|
|
"""Stops the thread."""
|
2020-07-10 13:11:28 +02:00
|
|
|
if self.scheduler.running:
|
|
|
|
self.scheduler.shutdown()
|
2016-05-25 22:51:13 +02:00
|
|
|
|
2020-10-06 19:28:40 +02:00
|
|
|
def jobs(self) -> Tuple['Job', ...]:
|
2021-05-27 09:38:17 +02:00
|
|
|
"""Returns a tuple of all *scheduled* jobs that are currently in the ``JobQueue``."""
|
|
|
|
return tuple(
|
2021-08-30 16:31:19 +02:00
|
|
|
Job._from_aps_job(job) # pylint: disable=protected-access
|
2021-05-27 09:38:17 +02:00
|
|
|
for job in self.scheduler.get_jobs()
|
|
|
|
)
|
2016-05-25 22:51:13 +02:00
|
|
|
|
2020-10-06 19:28:40 +02:00
|
|
|
def get_jobs_by_name(self, name: str) -> Tuple['Job', ...]:
|
2020-11-29 16:25:47 +01:00
|
|
|
"""Returns a tuple of all *pending/scheduled* jobs with the given name that are currently
|
2021-05-27 09:38:17 +02:00
|
|
|
in the ``JobQueue``.
|
|
|
|
"""
|
2020-07-10 13:11:28 +02:00
|
|
|
return tuple(job for job in self.jobs() if job.name == name)
|
2018-02-19 09:36:40 +01:00
|
|
|
|
2016-05-25 22:51:13 +02:00
|
|
|
|
2020-06-15 18:20:51 +02:00
|
|
|
class Job:
|
2020-07-10 13:11:28 +02:00
|
|
|
"""This class is a convenience wrapper for the jobs held in a :class:`telegram.ext.JobQueue`.
|
|
|
|
With the current backend APScheduler, :attr:`job` holds a :class:`apscheduler.job.Job`
|
|
|
|
instance.
|
|
|
|
|
2021-07-01 17:34:23 +02:00
|
|
|
Objects of this class are comparable in terms of equality. Two objects of this class are
|
|
|
|
considered equal, if their :attr:`id` is equal.
|
|
|
|
|
2020-07-10 13:11:28 +02:00
|
|
|
Note:
|
|
|
|
* All attributes and instance methods of :attr:`job` are also directly available as
|
|
|
|
attributes/methods of the corresponding :class:`telegram.ext.Job` object.
|
|
|
|
* Two instances of :class:`telegram.ext.Job` are considered equal, if their corresponding
|
|
|
|
``job`` attributes have the same ``id``.
|
|
|
|
* If :attr:`job` isn't passed on initialization, it must be set manually afterwards for
|
|
|
|
this :class:`telegram.ext.Job` to be useful.
|
2016-05-25 22:51:13 +02:00
|
|
|
|
2021-08-30 16:31:19 +02:00
|
|
|
.. versionchanged:: 14.0
|
|
|
|
Removed argument and attribute :attr:`job_queue`.
|
|
|
|
|
2016-05-25 22:51:13 +02:00
|
|
|
Args:
|
2017-07-23 22:33:08 +02:00
|
|
|
callback (:obj:`callable`): The callback function that should be executed by the new job.
|
2021-08-29 18:15:04 +02:00
|
|
|
Callback signature: ``def callback(update: Update, context: CallbackContext)``
|
2017-07-23 22:33:08 +02:00
|
|
|
context (:obj:`object`, optional): Additional data needed for the callback function. Can be
|
Documentation Improvements (#2008)
* Minor doc updates, following official API docs
* Fix spelling in Defaults docstrings
* Clarify Changelog of v12.7 about aware dates
* Fix typo in CHANGES.rst (#2024)
* Fix PicklePersistence.flush() with only bot_data (#2017)
* Update pylint in pre-commit to fix CI (#2018)
* Add Filters.via_bot (#2009)
* feat: via_bot filter
also fixing a small mistake in the empty parameter of the user filter and improve docs slightly
* fix: forgot to set via_bot to None
* fix: redoing subclassing to copy paste solution
* Cosmetic changes
Co-authored-by: Hinrich Mahler <hinrich.mahler@freenet.de>
* Update CHANGES.rst
Fixed Typo
Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
* Update downloads badge, add info on IRC Channel to Getting Help section
* Remove RegexHandler from ConversationHandlers Docs (#1973)
Replaced RegexHandler with MessageHandler, since the former is deprecated
* Fix Filters.via_bot docstrings
* Add notes on Markdown v1 being legacy mode
* Fixed typo in the Regex doc.. (#2036)
* Typo: Spelling
* Minor cleanup from #2043
* Document CommandHandler ignoring channel posts
* Doc fixes for a few telegram.ext classes
* Doc fixes for most `telegram` classes.
* pep-8
forgot the hard wrap is at 99 chars, not 100!
fixed a few spelling mistakes too.
* Address review and made rendering of booleans consistent
True, False, None are now rendered with ``bool`` wherever they weren't in telegram and telegram.ext classes.
* Few doc fixes for inline* classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram/files classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram.Game
Mostly just added hyperlinks. And fixed message length doc.
As usual, docs were cross-checked with official tg api docs.
* Very minor doc fix for passportfile.py and passportelementerrors.py
Didn't bother changing too much since this seems to be a custom implementation.
* Doc fixes for telegram.payments
As usual, cross-checked with official bot api docs.
* Address review 2
Few tiny other fixes too.
* Changed from ``True/False/None`` to :obj:`True/False/None` project-wide.
Few tiny other doc fixes too.
Co-authored-by: Robert Geislinger <mitachundkrach@gmail.com>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
Co-authored-by: GauthamramRavichandran <30320759+GauthamramRavichandran@users.noreply.github.com>
Co-authored-by: Mahesh19 <maheshvagicherla99438@gmail.com>
Co-authored-by: hoppingturtles <ilovebhagwan@gmail.com>
2020-08-24 19:35:57 +02:00
|
|
|
accessed through ``job.context`` in the callback. Defaults to :obj:`None`.
|
2017-07-23 22:33:08 +02:00
|
|
|
name (:obj:`str`, optional): The name of the new job. Defaults to ``callback.__name__``.
|
2020-07-10 13:11:28 +02:00
|
|
|
job (:class:`apscheduler.job.Job`, optional): The APS Job this job is a wrapper for.
|
2020-12-30 15:59:50 +01:00
|
|
|
|
|
|
|
Attributes:
|
|
|
|
callback (:obj:`callable`): The callback function that should be executed by the new job.
|
|
|
|
context (:obj:`object`): Optional. Additional data needed for the callback function.
|
|
|
|
name (:obj:`str`): Optional. The name of the new job.
|
|
|
|
job (:class:`apscheduler.job.Job`): Optional. The APS Job this job is a wrapper for.
|
2016-05-25 22:51:13 +02:00
|
|
|
"""
|
|
|
|
|
2021-05-29 16:18:16 +02:00
|
|
|
__slots__ = (
|
|
|
|
'callback',
|
|
|
|
'context',
|
|
|
|
'name',
|
|
|
|
'_removed',
|
|
|
|
'_enabled',
|
|
|
|
'job',
|
|
|
|
)
|
|
|
|
|
2020-10-09 17:22:07 +02:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
callback: Callable[['CallbackContext'], None],
|
|
|
|
context: object = None,
|
|
|
|
name: str = None,
|
2021-01-30 11:38:54 +01:00
|
|
|
job: APSJob = None,
|
2020-10-09 17:22:07 +02:00
|
|
|
):
|
2016-12-14 16:27:45 +01:00
|
|
|
|
2016-05-25 22:51:13 +02:00
|
|
|
self.callback = callback
|
2016-05-26 13:55:30 +02:00
|
|
|
self.context = context
|
2016-12-14 16:27:45 +01:00
|
|
|
self.name = name or callback.__name__
|
2016-05-25 22:51:13 +02:00
|
|
|
|
2020-07-10 13:11:28 +02:00
|
|
|
self._removed = False
|
|
|
|
self._enabled = False
|
2016-12-13 23:38:13 +01:00
|
|
|
|
2021-05-27 09:38:17 +02:00
|
|
|
self.job = cast(APSJob, job) # skipcq: PTC-W0052
|
2016-05-25 22:51:13 +02:00
|
|
|
|
2020-10-06 19:28:40 +02:00
|
|
|
def run(self, dispatcher: 'Dispatcher') -> None:
|
2020-07-10 13:11:28 +02:00
|
|
|
"""Executes the callback function independently of the jobs schedule."""
|
|
|
|
try:
|
2021-08-29 18:15:04 +02:00
|
|
|
self.callback(dispatcher.context_types.context.from_job(self, dispatcher))
|
2020-10-31 16:33:34 +01:00
|
|
|
except Exception as exc:
|
2021-09-17 17:48:01 +02:00
|
|
|
dispatcher.dispatch_error(None, exc)
|
2016-05-25 22:51:13 +02:00
|
|
|
|
2020-10-06 19:28:40 +02:00
|
|
|
def schedule_removal(self) -> None:
|
2016-05-25 22:51:13 +02:00
|
|
|
"""
|
|
|
|
Schedules this job for removal from the ``JobQueue``. It will be removed without executing
|
|
|
|
its callback function again.
|
|
|
|
"""
|
2020-07-10 13:11:28 +02:00
|
|
|
self.job.remove()
|
|
|
|
self._removed = True
|
2016-05-25 22:51:13 +02:00
|
|
|
|
2016-12-19 23:14:03 +01:00
|
|
|
@property
|
2020-10-06 19:28:40 +02:00
|
|
|
def removed(self) -> bool:
|
2017-09-01 08:43:08 +02:00
|
|
|
""":obj:`bool`: Whether this job is due to be removed."""
|
2020-07-10 13:11:28 +02:00
|
|
|
return self._removed
|
2016-12-14 23:08:03 +01:00
|
|
|
|
2016-12-14 16:27:45 +01:00
|
|
|
@property
|
2020-10-06 19:28:40 +02:00
|
|
|
def enabled(self) -> bool:
|
2017-09-01 08:43:08 +02:00
|
|
|
""":obj:`bool`: Whether this job is enabled."""
|
2020-07-10 13:11:28 +02:00
|
|
|
return self._enabled
|
2016-05-25 22:51:13 +02:00
|
|
|
|
2016-12-14 16:27:45 +01:00
|
|
|
@enabled.setter
|
2020-10-06 19:28:40 +02:00
|
|
|
def enabled(self, status: bool) -> None:
|
2016-05-25 22:51:13 +02:00
|
|
|
if status:
|
2020-07-10 13:11:28 +02:00
|
|
|
self.job.resume()
|
2016-12-14 16:27:45 +01:00
|
|
|
else:
|
2020-07-10 13:11:28 +02:00
|
|
|
self.job.pause()
|
|
|
|
self._enabled = status
|
2016-12-14 16:27:45 +01:00
|
|
|
|
2020-04-18 15:08:16 +02:00
|
|
|
@property
|
2020-10-06 19:28:40 +02:00
|
|
|
def next_t(self) -> Optional[datetime.datetime]:
|
2020-04-18 15:08:16 +02:00
|
|
|
"""
|
2020-04-25 12:34:13 +02:00
|
|
|
:obj:`datetime.datetime`: Datetime for the next job execution.
|
2020-04-18 15:08:16 +02:00
|
|
|
Datetime is localized according to :attr:`tzinfo`.
|
Documentation Improvements (#2008)
* Minor doc updates, following official API docs
* Fix spelling in Defaults docstrings
* Clarify Changelog of v12.7 about aware dates
* Fix typo in CHANGES.rst (#2024)
* Fix PicklePersistence.flush() with only bot_data (#2017)
* Update pylint in pre-commit to fix CI (#2018)
* Add Filters.via_bot (#2009)
* feat: via_bot filter
also fixing a small mistake in the empty parameter of the user filter and improve docs slightly
* fix: forgot to set via_bot to None
* fix: redoing subclassing to copy paste solution
* Cosmetic changes
Co-authored-by: Hinrich Mahler <hinrich.mahler@freenet.de>
* Update CHANGES.rst
Fixed Typo
Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
* Update downloads badge, add info on IRC Channel to Getting Help section
* Remove RegexHandler from ConversationHandlers Docs (#1973)
Replaced RegexHandler with MessageHandler, since the former is deprecated
* Fix Filters.via_bot docstrings
* Add notes on Markdown v1 being legacy mode
* Fixed typo in the Regex doc.. (#2036)
* Typo: Spelling
* Minor cleanup from #2043
* Document CommandHandler ignoring channel posts
* Doc fixes for a few telegram.ext classes
* Doc fixes for most `telegram` classes.
* pep-8
forgot the hard wrap is at 99 chars, not 100!
fixed a few spelling mistakes too.
* Address review and made rendering of booleans consistent
True, False, None are now rendered with ``bool`` wherever they weren't in telegram and telegram.ext classes.
* Few doc fixes for inline* classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram/files classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram.Game
Mostly just added hyperlinks. And fixed message length doc.
As usual, docs were cross-checked with official tg api docs.
* Very minor doc fix for passportfile.py and passportelementerrors.py
Didn't bother changing too much since this seems to be a custom implementation.
* Doc fixes for telegram.payments
As usual, cross-checked with official bot api docs.
* Address review 2
Few tiny other fixes too.
* Changed from ``True/False/None`` to :obj:`True/False/None` project-wide.
Few tiny other doc fixes too.
Co-authored-by: Robert Geislinger <mitachundkrach@gmail.com>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
Co-authored-by: GauthamramRavichandran <30320759+GauthamramRavichandran@users.noreply.github.com>
Co-authored-by: Mahesh19 <maheshvagicherla99438@gmail.com>
Co-authored-by: hoppingturtles <ilovebhagwan@gmail.com>
2020-08-24 19:35:57 +02:00
|
|
|
If job is removed or already ran it equals to :obj:`None`.
|
2020-04-18 15:08:16 +02:00
|
|
|
"""
|
2020-07-10 13:11:28 +02:00
|
|
|
return self.job.next_run_time
|
2016-12-14 16:27:45 +01:00
|
|
|
|
2020-07-10 13:11:28 +02:00
|
|
|
@classmethod
|
2021-08-30 16:31:19 +02:00
|
|
|
def _from_aps_job(cls, job: APSJob) -> 'Job':
|
2020-07-10 13:11:28 +02:00
|
|
|
# context based callbacks
|
|
|
|
if len(job.args) == 1:
|
|
|
|
context = job.args[0].job.context
|
2016-12-14 16:27:45 +01:00
|
|
|
else:
|
2020-07-10 13:11:28 +02:00
|
|
|
context = job.args[1].context
|
2021-08-30 16:31:19 +02:00
|
|
|
return cls(job.func, context=context, name=job.name, job=job)
|
2020-07-10 13:11:28 +02:00
|
|
|
|
2021-01-30 11:38:54 +01:00
|
|
|
def __getattr__(self, item: str) -> object:
|
2020-07-10 13:11:28 +02:00
|
|
|
return getattr(self.job, item)
|
2015-12-31 14:55:15 +01:00
|
|
|
|
2020-10-06 19:28:40 +02:00
|
|
|
def __lt__(self, other: object) -> bool:
|
2016-05-25 22:51:13 +02:00
|
|
|
return False
|
2020-07-10 13:11:28 +02:00
|
|
|
|
2020-10-06 19:28:40 +02:00
|
|
|
def __eq__(self, other: object) -> bool:
|
2020-07-10 13:11:28 +02:00
|
|
|
if isinstance(other, self.__class__):
|
|
|
|
return self.id == other.id
|
|
|
|
return False
|