2018-09-21 08:57:01 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# A library that provides a Python interface to the Telegram Bot API
|
2021-01-03 06:10:24 +01:00
|
|
|
# Copyright (C) 2015-2021
|
2018-09-21 08:57:01 +02:00
|
|
|
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
|
|
|
|
#
|
|
|
|
# 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/].
|
2020-10-31 16:33:34 +01:00
|
|
|
# pylint: disable=R0201
|
2018-09-21 08:57:01 +02:00
|
|
|
"""This module contains the CallbackContext class."""
|
2020-10-06 19:28:40 +02:00
|
|
|
from queue import Queue
|
2021-06-06 10:37:53 +02:00
|
|
|
from typing import (
|
|
|
|
TYPE_CHECKING,
|
|
|
|
Dict,
|
|
|
|
List,
|
|
|
|
Match,
|
|
|
|
NoReturn,
|
|
|
|
Optional,
|
|
|
|
Tuple,
|
|
|
|
Union,
|
|
|
|
Generic,
|
|
|
|
Type,
|
|
|
|
TypeVar,
|
|
|
|
)
|
2018-09-21 08:57:01 +02:00
|
|
|
|
2021-06-06 11:48:48 +02:00
|
|
|
from telegram import Update, CallbackQuery
|
|
|
|
from telegram.ext import ExtBot
|
2021-06-06 10:37:53 +02:00
|
|
|
from telegram.ext.utils.types import UD, CD, BD
|
2020-10-09 17:22:07 +02:00
|
|
|
|
2020-10-06 19:28:40 +02:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from telegram import Bot
|
|
|
|
from telegram.ext import Dispatcher, Job, JobQueue
|
2018-09-21 08:57:01 +02:00
|
|
|
|
2021-06-06 10:37:53 +02:00
|
|
|
CC = TypeVar('CC', bound='CallbackContext')
|
2018-09-21 08:57:01 +02:00
|
|
|
|
2021-06-06 10:37:53 +02:00
|
|
|
|
|
|
|
class CallbackContext(Generic[UD, CD, BD]):
|
2018-09-21 08:57:01 +02:00
|
|
|
"""
|
|
|
|
This is a context object passed to the callback called by :class:`telegram.ext.Handler`
|
|
|
|
or by the :class:`telegram.ext.Dispatcher` in an error handler added by
|
|
|
|
:attr:`telegram.ext.Dispatcher.add_error_handler` or to the callback of a
|
|
|
|
:class:`telegram.ext.Job`.
|
|
|
|
|
2019-02-13 12:18:37 +01:00
|
|
|
Note:
|
|
|
|
:class:`telegram.ext.Dispatcher` will create a single context for an entire update. This
|
|
|
|
means that if you got 2 handlers in different groups and they both get called, they will
|
|
|
|
get passed the same `CallbackContext` object (of course with proper attributes like
|
|
|
|
`.matches` differing). This allows you to add custom attributes in a lower handler group
|
|
|
|
callback, and then subsequently access those attributes in a higher handler group callback.
|
|
|
|
Note that the attributes on `CallbackContext` might change in the future, so make sure to
|
|
|
|
use a fairly unique name for the attributes.
|
|
|
|
|
|
|
|
Warning:
|
2020-10-04 17:20:33 +02:00
|
|
|
Do not combine custom attributes and ``@run_async``/
|
|
|
|
:meth:`telegram.ext.Disptacher.run_async`. Due to how ``run_async`` works, it will
|
2019-02-13 12:18:37 +01:00
|
|
|
almost certainly execute the callbacks for an update out of order, and the attributes
|
|
|
|
that you think you added will not be present.
|
|
|
|
|
2021-06-06 10:37:53 +02:00
|
|
|
Args:
|
|
|
|
dispatcher (:class:`telegram.ext.Dispatcher`): The dispatcher associated with this context.
|
|
|
|
|
2018-09-21 08:57:01 +02:00
|
|
|
Attributes:
|
2020-05-15 15:59:41 +02:00
|
|
|
matches (List[:obj:`re match object`]): Optional. If the associated update originated from
|
2019-02-13 12:07:25 +01:00
|
|
|
a regex-supported handler or had a :class:`Filters.regex`, this will contain a list of
|
|
|
|
match objects for every pattern where ``re.search(pattern, string)`` returned a match.
|
2019-03-14 09:03:21 +01:00
|
|
|
Note that filters short circuit, so combined regex filters will not always
|
|
|
|
be evaluated.
|
2020-05-15 15:59:41 +02:00
|
|
|
args (List[:obj:`str`]): Optional. Arguments passed to a command if the associated update
|
2018-09-21 08:57:43 +02:00
|
|
|
is handled by :class:`telegram.ext.CommandHandler`, :class:`telegram.ext.PrefixHandler`
|
|
|
|
or :class:`telegram.ext.StringCommandHandler`. It contains a list of the words in the
|
|
|
|
text after the command, using any whitespace string as a delimiter.
|
2021-02-01 17:59:39 +01:00
|
|
|
error (:obj:`Exception`): Optional. The error that was raised. Only present when passed
|
|
|
|
to a error handler registered with :attr:`telegram.ext.Dispatcher.add_error_handler`.
|
2020-10-04 17:20:33 +02:00
|
|
|
async_args (List[:obj:`object`]): Optional. Positional arguments of the function that
|
|
|
|
raised the error. Only present when the raising function was run asynchronously using
|
|
|
|
:meth:`telegram.ext.Dispatcher.run_async`.
|
|
|
|
async_kwargs (Dict[:obj:`str`, :obj:`object`]): Optional. Keyword arguments of the function
|
|
|
|
that raised the error. Only present when the raising function was run asynchronously
|
|
|
|
using :meth:`telegram.ext.Dispatcher.run_async`.
|
|
|
|
job (:class:`telegram.ext.Job`): Optional. The job which originated this callback.
|
2018-09-21 08:57:01 +02:00
|
|
|
Only present when passed to the callback of :class:`telegram.ext.Job`.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2021-05-29 16:18:16 +02:00
|
|
|
__slots__ = (
|
|
|
|
'_dispatcher',
|
2021-06-06 10:37:53 +02:00
|
|
|
'_chat_id_and_data',
|
|
|
|
'_user_id_and_data',
|
2021-05-29 16:18:16 +02:00
|
|
|
'args',
|
|
|
|
'matches',
|
|
|
|
'error',
|
|
|
|
'job',
|
|
|
|
'async_args',
|
|
|
|
'async_kwargs',
|
|
|
|
'__dict__',
|
|
|
|
)
|
|
|
|
|
2020-10-06 19:28:40 +02:00
|
|
|
def __init__(self, dispatcher: 'Dispatcher'):
|
2018-09-21 08:57:01 +02:00
|
|
|
"""
|
|
|
|
Args:
|
|
|
|
dispatcher (:class:`telegram.ext.Dispatcher`):
|
|
|
|
"""
|
|
|
|
if not dispatcher.use_context:
|
2020-10-09 17:22:07 +02:00
|
|
|
raise ValueError(
|
|
|
|
'CallbackContext should not be used with a non context aware ' 'dispatcher!'
|
|
|
|
)
|
2018-09-21 08:57:01 +02:00
|
|
|
self._dispatcher = dispatcher
|
2021-06-06 10:37:53 +02:00
|
|
|
self._chat_id_and_data: Optional[Tuple[int, CD]] = None
|
|
|
|
self._user_id_and_data: Optional[Tuple[int, UD]] = None
|
2020-10-06 19:28:40 +02:00
|
|
|
self.args: Optional[List[str]] = None
|
|
|
|
self.matches: Optional[List[Match]] = None
|
|
|
|
self.error: Optional[Exception] = None
|
|
|
|
self.job: Optional['Job'] = None
|
|
|
|
self.async_args: Optional[Union[List, Tuple]] = None
|
2021-01-30 11:38:54 +01:00
|
|
|
self.async_kwargs: Optional[Dict[str, object]] = None
|
2018-09-21 08:57:01 +02:00
|
|
|
|
2020-01-26 21:55:00 +01:00
|
|
|
@property
|
2020-10-06 19:28:40 +02:00
|
|
|
def dispatcher(self) -> 'Dispatcher':
|
2020-01-26 21:55:00 +01:00
|
|
|
""":class:`telegram.ext.Dispatcher`: The dispatcher associated with this context."""
|
|
|
|
return self._dispatcher
|
|
|
|
|
2020-02-02 22:20:31 +01:00
|
|
|
@property
|
2021-06-06 10:37:53 +02:00
|
|
|
def bot_data(self) -> BD:
|
2021-05-27 09:38:17 +02:00
|
|
|
""":obj:`dict`: Optional. A dict that can be used to keep any data in. For each
|
|
|
|
update it will be the same ``dict``.
|
|
|
|
"""
|
2021-06-06 10:37:53 +02:00
|
|
|
return self.dispatcher.bot_data
|
2020-02-02 22:20:31 +01:00
|
|
|
|
|
|
|
@bot_data.setter
|
2021-01-30 11:38:54 +01:00
|
|
|
def bot_data(self, value: object) -> NoReturn:
|
2020-10-09 17:22:07 +02:00
|
|
|
raise AttributeError(
|
2021-02-19 19:07:48 +01:00
|
|
|
"You can not assign a new value to bot_data, see https://git.io/Jt6ic"
|
2020-10-09 17:22:07 +02:00
|
|
|
)
|
2020-02-02 22:20:31 +01:00
|
|
|
|
2019-09-05 22:48:28 +02:00
|
|
|
@property
|
2021-06-06 10:37:53 +02:00
|
|
|
def chat_data(self) -> Optional[CD]:
|
2021-05-27 09:38:17 +02:00
|
|
|
""":obj:`dict`: Optional. A dict that can be used to keep any data in. For each
|
|
|
|
update from the same chat id it will be the same ``dict``.
|
|
|
|
|
|
|
|
Warning:
|
|
|
|
When a group chat migrates to a supergroup, its chat id will change and the
|
|
|
|
``chat_data`` needs to be transferred. For details see our `wiki page
|
|
|
|
<https://github.com/python-telegram-bot/python-telegram-bot/wiki/
|
|
|
|
Storing-bot,-user-and-chat-related-data#chat-migration>`_.
|
|
|
|
"""
|
2021-06-06 10:37:53 +02:00
|
|
|
if self._chat_id_and_data:
|
|
|
|
return self._chat_id_and_data[1]
|
|
|
|
return None
|
2019-09-05 22:48:28 +02:00
|
|
|
|
|
|
|
@chat_data.setter
|
2021-01-30 11:38:54 +01:00
|
|
|
def chat_data(self, value: object) -> NoReturn:
|
2020-10-09 17:22:07 +02:00
|
|
|
raise AttributeError(
|
2021-02-19 19:07:48 +01:00
|
|
|
"You can not assign a new value to chat_data, see https://git.io/Jt6ic"
|
2020-10-09 17:22:07 +02:00
|
|
|
)
|
2019-09-05 22:48:28 +02:00
|
|
|
|
|
|
|
@property
|
2021-06-06 10:37:53 +02:00
|
|
|
def user_data(self) -> Optional[UD]:
|
2021-05-27 09:38:17 +02:00
|
|
|
""":obj:`dict`: Optional. A dict that can be used to keep any data in. For each
|
|
|
|
update from the same user it will be the same ``dict``.
|
|
|
|
"""
|
2021-06-06 10:37:53 +02:00
|
|
|
if self._user_id_and_data:
|
|
|
|
return self._user_id_and_data[1]
|
|
|
|
return None
|
2019-09-05 22:48:28 +02:00
|
|
|
|
|
|
|
@user_data.setter
|
2021-01-30 11:38:54 +01:00
|
|
|
def user_data(self, value: object) -> NoReturn:
|
2020-10-09 17:22:07 +02:00
|
|
|
raise AttributeError(
|
2021-02-19 19:07:48 +01:00
|
|
|
"You can not assign a new value to user_data, see https://git.io/Jt6ic"
|
2020-10-09 17:22:07 +02:00
|
|
|
)
|
2019-09-05 22:48:28 +02:00
|
|
|
|
2021-06-06 10:37:53 +02:00
|
|
|
def refresh_data(self) -> None:
|
|
|
|
"""If :attr:`dispatcher` uses persistence, calls
|
|
|
|
:meth:`telegram.ext.BasePersistence.refresh_bot_data` on :attr:`bot_data`,
|
|
|
|
:meth:`telegram.ext.BasePersistence.refresh_chat_data` on :attr:`chat_data` and
|
|
|
|
:meth:`telegram.ext.BasePersistence.refresh_user_data` on :attr:`user_data`, if
|
|
|
|
appropriate.
|
|
|
|
|
|
|
|
.. versionadded:: 13.6
|
|
|
|
"""
|
|
|
|
if self.dispatcher.persistence:
|
|
|
|
if self.dispatcher.persistence.store_bot_data:
|
|
|
|
self.dispatcher.persistence.refresh_bot_data(self.bot_data)
|
|
|
|
if self.dispatcher.persistence.store_chat_data and self._chat_id_and_data is not None:
|
|
|
|
self.dispatcher.persistence.refresh_chat_data(*self._chat_id_and_data)
|
|
|
|
if self.dispatcher.persistence.store_user_data and self._user_id_and_data is not None:
|
|
|
|
self.dispatcher.persistence.refresh_user_data(*self._user_id_and_data)
|
|
|
|
|
2021-06-06 11:48:48 +02:00
|
|
|
def drop_callback_data(self, callback_query: CallbackQuery) -> None:
|
|
|
|
"""
|
|
|
|
Deletes the cached data for the specified callback query.
|
|
|
|
|
|
|
|
.. versionadded:: 13.6
|
|
|
|
|
|
|
|
Note:
|
|
|
|
Will *not* raise exceptions in case the data is not found in the cache.
|
|
|
|
*Will* raise :class:`KeyError` in case the callback query can not be found in the
|
|
|
|
cache.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
callback_query (:class:`telegram.CallbackQuery`): The callback query.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
KeyError | RuntimeError: :class:`KeyError`, if the callback query can not be found in
|
|
|
|
the cache and :class:`RuntimeError`, if the bot doesn't allow for arbitrary
|
|
|
|
callback data.
|
|
|
|
"""
|
|
|
|
if isinstance(self.bot, ExtBot):
|
|
|
|
if not self.bot.arbitrary_callback_data:
|
|
|
|
raise RuntimeError(
|
|
|
|
'This telegram.ext.ExtBot instance does not use arbitrary callback data.'
|
|
|
|
)
|
|
|
|
self.bot.callback_data_cache.drop_data(callback_query)
|
|
|
|
else:
|
|
|
|
raise RuntimeError('telegram.Bot does not allow for arbitrary callback data.')
|
|
|
|
|
2018-09-21 08:57:01 +02:00
|
|
|
@classmethod
|
2020-10-09 17:22:07 +02:00
|
|
|
def from_error(
|
2021-06-06 10:37:53 +02:00
|
|
|
cls: Type[CC],
|
2020-10-09 17:22:07 +02:00
|
|
|
update: object,
|
|
|
|
error: Exception,
|
|
|
|
dispatcher: 'Dispatcher',
|
|
|
|
async_args: Union[List, Tuple] = None,
|
2021-01-30 11:38:54 +01:00
|
|
|
async_kwargs: Dict[str, object] = None,
|
2021-06-06 10:37:53 +02:00
|
|
|
) -> CC:
|
2021-05-27 09:38:17 +02:00
|
|
|
"""
|
|
|
|
Constructs an instance of :class:`telegram.ext.CallbackContext` to be passed to the error
|
|
|
|
handlers.
|
|
|
|
|
|
|
|
.. seealso:: :meth:`telegram.ext.Dispatcher.add_error_handler`
|
|
|
|
|
|
|
|
Args:
|
|
|
|
update (:obj:`object` | :class:`telegram.Update`): The update associated with the
|
|
|
|
error. May be :obj:`None`, e.g. for errors in job callbacks.
|
|
|
|
error (:obj:`Exception`): The error.
|
|
|
|
dispatcher (:class:`telegram.ext.Dispatcher`): The dispatcher associated with this
|
|
|
|
context.
|
|
|
|
async_args (List[:obj:`object`]): Optional. Positional arguments of the function that
|
|
|
|
raised the error. Pass only when the raising function was run asynchronously using
|
|
|
|
:meth:`telegram.ext.Dispatcher.run_async`.
|
|
|
|
async_kwargs (Dict[:obj:`str`, :obj:`object`]): Optional. Keyword arguments of the
|
|
|
|
function that raised the error. Pass only when the raising function was run
|
|
|
|
asynchronously using :meth:`telegram.ext.Dispatcher.run_async`.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
:class:`telegram.ext.CallbackContext`
|
|
|
|
"""
|
2018-09-21 08:57:01 +02:00
|
|
|
self = cls.from_update(update, dispatcher)
|
|
|
|
self.error = error
|
2020-10-04 17:20:33 +02:00
|
|
|
self.async_args = async_args
|
|
|
|
self.async_kwargs = async_kwargs
|
2018-09-21 08:57:01 +02:00
|
|
|
return self
|
|
|
|
|
|
|
|
@classmethod
|
2021-06-06 10:37:53 +02:00
|
|
|
def from_update(cls: Type[CC], update: object, dispatcher: 'Dispatcher') -> CC:
|
2021-05-27 09:38:17 +02:00
|
|
|
"""
|
|
|
|
Constructs an instance of :class:`telegram.ext.CallbackContext` to be passed to the
|
|
|
|
handlers.
|
|
|
|
|
|
|
|
.. seealso:: :meth:`telegram.ext.Dispatcher.add_handler`
|
|
|
|
|
|
|
|
Args:
|
|
|
|
update (:obj:`object` | :class:`telegram.Update`): The update.
|
|
|
|
dispatcher (:class:`telegram.ext.Dispatcher`): The dispatcher associated with this
|
|
|
|
context.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
:class:`telegram.ext.CallbackContext`
|
|
|
|
"""
|
2018-09-21 08:57:01 +02:00
|
|
|
self = cls(dispatcher)
|
2020-02-02 22:20:31 +01:00
|
|
|
|
2018-09-21 08:57:01 +02:00
|
|
|
if update is not None and isinstance(update, Update):
|
|
|
|
chat = update.effective_chat
|
|
|
|
user = update.effective_user
|
|
|
|
|
|
|
|
if chat:
|
2021-06-06 10:37:53 +02:00
|
|
|
self._chat_id_and_data = (
|
|
|
|
chat.id,
|
|
|
|
dispatcher.chat_data[chat.id], # pylint: disable=W0212
|
|
|
|
)
|
2018-09-21 08:57:01 +02:00
|
|
|
if user:
|
2021-06-06 10:37:53 +02:00
|
|
|
self._user_id_and_data = (
|
|
|
|
user.id,
|
|
|
|
dispatcher.user_data[user.id], # pylint: disable=W0212
|
|
|
|
)
|
2018-09-21 08:57:01 +02:00
|
|
|
return self
|
|
|
|
|
|
|
|
@classmethod
|
2021-06-06 10:37:53 +02:00
|
|
|
def from_job(cls: Type[CC], job: 'Job', dispatcher: 'Dispatcher') -> CC:
|
2021-05-27 09:38:17 +02:00
|
|
|
"""
|
|
|
|
Constructs an instance of :class:`telegram.ext.CallbackContext` to be passed to a
|
|
|
|
job callback.
|
|
|
|
|
|
|
|
.. seealso:: :meth:`telegram.ext.JobQueue`
|
|
|
|
|
|
|
|
Args:
|
|
|
|
job (:class:`telegram.ext.Job`): The job.
|
|
|
|
dispatcher (:class:`telegram.ext.Dispatcher`): The dispatcher associated with this
|
|
|
|
context.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
:class:`telegram.ext.CallbackContext`
|
|
|
|
"""
|
2018-09-21 08:57:01 +02:00
|
|
|
self = cls(dispatcher)
|
|
|
|
self.job = job
|
|
|
|
return self
|
|
|
|
|
2021-01-30 11:38:54 +01:00
|
|
|
def update(self, data: Dict[str, object]) -> None:
|
2021-05-29 16:18:16 +02:00
|
|
|
"""Updates ``self.__slots__`` with the passed data.
|
2021-05-27 09:38:17 +02:00
|
|
|
|
|
|
|
Args:
|
|
|
|
data (Dict[:obj:`str`, :obj:`object`]): The data.
|
|
|
|
"""
|
2021-05-29 16:18:16 +02:00
|
|
|
for key, value in data.items():
|
|
|
|
setattr(self, key, value)
|
2019-02-13 12:07:25 +01:00
|
|
|
|
2018-09-21 08:57:01 +02:00
|
|
|
@property
|
2020-10-06 19:28:40 +02:00
|
|
|
def bot(self) -> 'Bot':
|
2018-09-21 08:57:01 +02:00
|
|
|
""":class:`telegram.Bot`: The bot associated with this context."""
|
|
|
|
return self._dispatcher.bot
|
|
|
|
|
|
|
|
@property
|
2020-10-06 19:28:40 +02:00
|
|
|
def job_queue(self) -> Optional['JobQueue']:
|
2018-09-21 08:57:01 +02:00
|
|
|
"""
|
|
|
|
:class:`telegram.ext.JobQueue`: The ``JobQueue`` used by the
|
|
|
|
:class:`telegram.ext.Dispatcher` and (usually) the :class:`telegram.ext.Updater`
|
|
|
|
associated with this context.
|
|
|
|
|
|
|
|
"""
|
|
|
|
return self._dispatcher.job_queue
|
|
|
|
|
|
|
|
@property
|
2020-10-06 19:28:40 +02:00
|
|
|
def update_queue(self) -> Queue:
|
2018-09-21 08:57:01 +02:00
|
|
|
"""
|
|
|
|
:class:`queue.Queue`: The ``Queue`` instance used by the
|
|
|
|
:class:`telegram.ext.Dispatcher` and (usually) the :class:`telegram.ext.Updater`
|
|
|
|
associated with this context.
|
|
|
|
|
|
|
|
"""
|
|
|
|
return self._dispatcher.update_queue
|
2019-02-13 12:07:25 +01:00
|
|
|
|
|
|
|
@property
|
2020-10-06 19:28:40 +02:00
|
|
|
def match(self) -> Optional[Match[str]]:
|
2019-02-13 12:07:25 +01:00
|
|
|
"""
|
|
|
|
`Regex match type`: The first match from :attr:`matches`.
|
|
|
|
Useful if you are only filtering using a single regex filter.
|
|
|
|
Returns `None` if :attr:`matches` is empty.
|
|
|
|
"""
|
|
|
|
try:
|
2020-10-06 19:28:40 +02:00
|
|
|
return self.matches[0] # type: ignore[index] # pylint: disable=unsubscriptable-object
|
2019-02-13 12:07:25 +01:00
|
|
|
except (IndexError, TypeError):
|
|
|
|
return None
|