2018-09-21 08:57:01 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# A library that provides a Python interface to the Telegram Bot API
|
2024-02-19 20:06:25 +01:00
|
|
|
# Copyright (C) 2015-2024
|
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/].
|
|
|
|
"""This module contains the CallbackContext class."""
|
2024-10-24 20:48:49 +02:00
|
|
|
|
|
|
|
from collections.abc import Awaitable, Generator
|
|
|
|
from re import Match
|
|
|
|
from typing import TYPE_CHECKING, Any, Generic, NoReturn, Optional, TypeVar, Union
|
2018-09-21 08:57:01 +02:00
|
|
|
|
2022-05-05 09:27:54 +02:00
|
|
|
from telegram._callbackquery import CallbackQuery
|
|
|
|
from telegram._update import Update
|
2022-12-02 12:08:11 +01:00
|
|
|
from telegram._utils.warnings import warn
|
2022-05-05 09:27:54 +02:00
|
|
|
from telegram.ext._extbot import ExtBot
|
2022-05-12 19:36:25 +02:00
|
|
|
from telegram.ext._utils.types import BD, BT, CD, UD
|
2020-10-09 17:22:07 +02:00
|
|
|
|
2020-10-06 19:28:40 +02:00
|
|
|
if TYPE_CHECKING:
|
2023-02-20 19:53:27 +01:00
|
|
|
from asyncio import Future, Queue
|
2022-05-05 09:27:54 +02:00
|
|
|
|
2023-03-25 19:18:04 +01:00
|
|
|
from telegram.ext import Application, Job, JobQueue
|
2022-05-12 19:36:25 +02:00
|
|
|
from telegram.ext._utils.types import CCT
|
2018-09-21 08:57:01 +02:00
|
|
|
|
2022-01-21 16:51:03 +01:00
|
|
|
_STORING_DATA_WIKI = (
|
|
|
|
"https://github.com/python-telegram-bot/python-telegram-bot"
|
|
|
|
"/wiki/Storing-bot%2C-user-and-chat-related-data"
|
|
|
|
)
|
|
|
|
|
2024-09-14 08:16:39 +02:00
|
|
|
# something like poor mans "typing.Self" for py<3.11
|
|
|
|
ST = TypeVar("ST", bound="CallbackContext[Any, Any, Any, Any]")
|
|
|
|
|
2021-06-06 10:37:53 +02:00
|
|
|
|
2021-10-09 13:56:50 +02:00
|
|
|
class CallbackContext(Generic[BT, UD, CD, BD]):
|
2018-09-21 08:57:01 +02:00
|
|
|
"""
|
2022-05-12 18:18:40 +02:00
|
|
|
This is a context object passed to the callback called by :class:`telegram.ext.BaseHandler`
|
2022-04-24 12:38:09 +02:00
|
|
|
or by the :class:`telegram.ext.Application` in an error handler added by
|
|
|
|
:attr:`telegram.ext.Application.add_error_handler` or to the callback of a
|
2018-09-21 08:57:01 +02:00
|
|
|
:class:`telegram.ext.Job`.
|
|
|
|
|
2019-02-13 12:18:37 +01:00
|
|
|
Note:
|
2022-04-24 12:38:09 +02:00
|
|
|
:class:`telegram.ext.Application` will create a single context for an entire update. This
|
2019-02-13 12:18:37 +01:00
|
|
|
means that if you got 2 handlers in different groups and they both get called, they will
|
2022-04-24 12:38:09 +02:00
|
|
|
receive the same :class:`CallbackContext` object (of course with proper attributes like
|
|
|
|
:attr:`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 :class:`CallbackContext` might change in the future,
|
|
|
|
so make sure to use a fairly unique name for the attributes.
|
2019-02-13 12:18:37 +01:00
|
|
|
|
|
|
|
Warning:
|
2022-05-12 18:18:40 +02:00
|
|
|
Do not combine custom attributes with :paramref:`telegram.ext.BaseHandler.block` set to
|
2022-05-06 17:15:23 +02:00
|
|
|
:obj:`False` or :attr:`telegram.ext.Application.concurrent_updates` set to
|
2022-04-24 12:38:09 +02:00
|
|
|
:obj:`True`. Due to how those work, it will almost certainly execute the callbacks for an
|
|
|
|
update out of order, and the attributes that you think you added will not be present.
|
2019-02-13 12:18:37 +01:00
|
|
|
|
2022-05-06 17:15:23 +02:00
|
|
|
This class is a :class:`~typing.Generic` class and accepts four type variables:
|
|
|
|
|
|
|
|
1. The type of :attr:`bot`. Must be :class:`telegram.Bot` or a subclass of that class.
|
|
|
|
2. The type of :attr:`user_data` (if :attr:`user_data` is not :obj:`None`).
|
|
|
|
3. The type of :attr:`chat_data` (if :attr:`chat_data` is not :obj:`None`).
|
|
|
|
4. The type of :attr:`bot_data` (if :attr:`bot_data` is not :obj:`None`).
|
|
|
|
|
Documentation Improvements (#3214, #3217, #3218, #3271, #3289, #3292, #3303, #3312, #3306, #3319, #3326, #3314)
Co-authored-by: Harshil <37377066+harshil21@users.noreply.github.com>
Co-authored-by: Simon Fong <44134941+simonfongnt@users.noreply.github.com>
Co-authored-by: Piotr Rogulski <rivinek@gmail.com>
Co-authored-by: poolitzer <25934244+Poolitzer@users.noreply.github.com>
Co-authored-by: Or Bin <or@raftt.io>
Co-authored-by: Sandro <j32g7f67hb@liamekaens.com>
Co-authored-by: Hatim Zahid <63000127+HatimZ@users.noreply.github.com>
Co-authored-by: Robi <53259730+RobiMez@users.noreply.github.com>
Co-authored-by: Dmitry Kolomatskiy <58207913+lemontree210@users.noreply.github.com>
2022-11-15 09:06:23 +01:00
|
|
|
Examples:
|
|
|
|
* :any:`Context Types Bot <examples.contexttypesbot>`
|
|
|
|
* :any:`Custom Webhook Bot <examples.customwebhookbot>`
|
|
|
|
|
|
|
|
.. seealso:: :attr:`telegram.ext.ContextTypes.DEFAULT_TYPE`,
|
2023-07-09 11:22:08 +02:00
|
|
|
:wiki:`Job Queue <Extensions---JobQueue>`
|
2022-05-12 19:36:25 +02:00
|
|
|
|
2021-06-06 10:37:53 +02:00
|
|
|
Args:
|
2022-04-24 12:38:09 +02:00
|
|
|
application (:class:`telegram.ext.Application`): The application associated with this
|
|
|
|
context.
|
2022-06-03 16:55:36 +02:00
|
|
|
chat_id (:obj:`int`, optional): The ID of the chat associated with this object. Used
|
|
|
|
to provide :attr:`chat_data`.
|
|
|
|
|
|
|
|
.. versionadded:: 20.0
|
|
|
|
user_id (:obj:`int`, optional): The ID of the user associated with this object. Used
|
|
|
|
to provide :attr:`user_data`.
|
|
|
|
|
|
|
|
.. versionadded:: 20.0
|
2018-09-21 08:57:01 +02:00
|
|
|
Attributes:
|
2023-02-20 19:53:27 +01:00
|
|
|
coroutine (:term:`awaitable`): Optional. Only present in error handlers if the
|
|
|
|
error was caused by an awaitable run with :meth:`Application.create_task` or a handler
|
2022-05-12 18:18:40 +02:00
|
|
|
callback with :attr:`block=False <BaseHandler.block>`.
|
2024-10-24 20:48:49 +02:00
|
|
|
matches (list[:meth:`re.Match <re.Match.expand>`]): Optional. If the associated update
|
2022-04-24 12:38:09 +02:00
|
|
|
originated from a :class:`filters.Regex`, this will contain a list of match objects for
|
|
|
|
every pattern where ``re.search(pattern, string)`` returned a match. Note that filters
|
|
|
|
short circuit, so combined regex filters will not always be evaluated.
|
2024-10-24 20:48:49 +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.
|
2022-04-24 12:38:09 +02:00
|
|
|
error (:exc:`Exception`): Optional. The error that was raised. Only present when passed
|
|
|
|
to an error handler registered with :attr:`telegram.ext.Application.add_error_handler`.
|
2020-10-04 17:20:33 +02:00
|
|
|
job (:class:`telegram.ext.Job`): Optional. The job which originated this callback.
|
2021-10-03 20:00:54 +02:00
|
|
|
Only present when passed to the callback of :class:`telegram.ext.Job` or in error
|
|
|
|
handlers if the error is caused by a job.
|
|
|
|
|
2022-05-06 17:15:23 +02:00
|
|
|
.. versionchanged:: 20.0
|
2021-10-03 20:00:54 +02:00
|
|
|
:attr:`job` is now also present in error handlers if the error is caused by a job.
|
2018-09-21 08:57:01 +02:00
|
|
|
|
|
|
|
"""
|
|
|
|
|
2021-05-29 16:18:16 +02:00
|
|
|
__slots__ = (
|
2024-02-05 19:24:00 +01:00
|
|
|
"__dict__",
|
2022-04-24 12:38:09 +02:00
|
|
|
"_application",
|
2022-06-03 16:55:36 +02:00
|
|
|
"_chat_id",
|
|
|
|
"_user_id",
|
2021-05-29 16:18:16 +02:00
|
|
|
"args",
|
2024-02-05 19:24:00 +01:00
|
|
|
"coroutine",
|
2021-05-29 16:18:16 +02:00
|
|
|
"error",
|
|
|
|
"job",
|
2024-02-05 19:24:00 +01:00
|
|
|
"matches",
|
2021-05-29 16:18:16 +02:00
|
|
|
)
|
|
|
|
|
2022-06-03 16:55:36 +02:00
|
|
|
def __init__(
|
2024-09-14 08:16:39 +02:00
|
|
|
self: ST,
|
|
|
|
application: "Application[BT, ST, UD, CD, BD, Any]",
|
2023-05-18 07:57:59 +02:00
|
|
|
chat_id: Optional[int] = None,
|
|
|
|
user_id: Optional[int] = None,
|
2022-06-03 16:55:36 +02:00
|
|
|
):
|
2024-09-14 08:16:39 +02:00
|
|
|
self._application: Application[BT, ST, UD, CD, BD, Any] = application
|
2023-02-02 18:55:07 +01:00
|
|
|
self._chat_id: Optional[int] = chat_id
|
|
|
|
self._user_id: Optional[int] = user_id
|
2024-10-24 20:48:49 +02:00
|
|
|
self.args: Optional[list[str]] = None
|
|
|
|
self.matches: Optional[list[Match[str]]] = None
|
2020-10-06 19:28:40 +02:00
|
|
|
self.error: Optional[Exception] = None
|
2024-09-14 08:16:39 +02:00
|
|
|
self.job: Optional[Job[Any]] = None
|
2023-02-20 19:53:27 +01:00
|
|
|
self.coroutine: Optional[
|
2023-08-02 11:51:17 +02:00
|
|
|
Union[Generator[Optional[Future[object]], None, Any], Awaitable[Any]]
|
2023-02-20 19:53:27 +01:00
|
|
|
] = None
|
2018-09-21 08:57:01 +02:00
|
|
|
|
2020-01-26 21:55:00 +01:00
|
|
|
@property
|
2024-09-14 08:16:39 +02:00
|
|
|
def application(self) -> "Application[BT, ST, UD, CD, BD, Any]":
|
2022-04-24 12:38:09 +02:00
|
|
|
""":class:`telegram.ext.Application`: The application associated with this context."""
|
|
|
|
return self._application
|
2020-01-26 21:55:00 +01:00
|
|
|
|
2020-02-02 22:20:31 +01:00
|
|
|
@property
|
2021-06-06 10:37:53 +02:00
|
|
|
def bot_data(self) -> BD:
|
2022-04-24 12:38:09 +02:00
|
|
|
""":obj:`ContextTypes.bot_data`: Optional. An object that can be used to keep any data in.
|
|
|
|
For each update it will be the same :attr:`ContextTypes.bot_data`. Defaults to :obj:`dict`.
|
Documentation Improvements (#3214, #3217, #3218, #3271, #3289, #3292, #3303, #3312, #3306, #3319, #3326, #3314)
Co-authored-by: Harshil <37377066+harshil21@users.noreply.github.com>
Co-authored-by: Simon Fong <44134941+simonfongnt@users.noreply.github.com>
Co-authored-by: Piotr Rogulski <rivinek@gmail.com>
Co-authored-by: poolitzer <25934244+Poolitzer@users.noreply.github.com>
Co-authored-by: Or Bin <or@raftt.io>
Co-authored-by: Sandro <j32g7f67hb@liamekaens.com>
Co-authored-by: Hatim Zahid <63000127+HatimZ@users.noreply.github.com>
Co-authored-by: Robi <53259730+RobiMez@users.noreply.github.com>
Co-authored-by: Dmitry Kolomatskiy <58207913+lemontree210@users.noreply.github.com>
2022-11-15 09:06:23 +01:00
|
|
|
|
2023-01-01 16:24:00 +01:00
|
|
|
.. seealso:: :wiki:`Storing Bot, User and Chat Related Data\
|
|
|
|
<Storing-bot%2C-user-and-chat-related-data>`
|
2021-05-27 09:38:17 +02:00
|
|
|
"""
|
2022-04-24 12:38:09 +02:00
|
|
|
return self.application.bot_data
|
2020-02-02 22:20:31 +01:00
|
|
|
|
|
|
|
@bot_data.setter
|
2024-05-12 09:13:20 +02:00
|
|
|
def bot_data(self, _: object) -> NoReturn:
|
2020-02-02 22:20:31 +01:00
|
|
|
raise AttributeError(
|
2022-01-21 16:51:03 +01:00
|
|
|
f"You can not assign a new value to bot_data, see {_STORING_DATA_WIKI}"
|
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]:
|
2022-04-24 12:38:09 +02:00
|
|
|
""":obj:`ContextTypes.chat_data`: Optional. An object that can be used to keep any data in.
|
|
|
|
For each update from the same chat id it will be the same :obj:`ContextTypes.chat_data`.
|
|
|
|
Defaults to :obj:`dict`.
|
2021-05-27 09:38:17 +02:00
|
|
|
|
|
|
|
Warning:
|
|
|
|
When a group chat migrates to a supergroup, its chat id will change and the
|
2023-01-01 16:24:00 +01:00
|
|
|
``chat_data`` needs to be transferred. For details see our
|
|
|
|
:wiki:`wiki page <Storing-bot,-user-and-chat-related-data#chat-migration>`.
|
2022-07-11 07:54:03 +02:00
|
|
|
|
2023-01-01 16:24:00 +01:00
|
|
|
.. seealso:: :wiki:`Storing Bot, User and Chat Related Data\
|
|
|
|
<Storing-bot%2C-user-and-chat-related-data>`
|
Documentation Improvements (#3214, #3217, #3218, #3271, #3289, #3292, #3303, #3312, #3306, #3319, #3326, #3314)
Co-authored-by: Harshil <37377066+harshil21@users.noreply.github.com>
Co-authored-by: Simon Fong <44134941+simonfongnt@users.noreply.github.com>
Co-authored-by: Piotr Rogulski <rivinek@gmail.com>
Co-authored-by: poolitzer <25934244+Poolitzer@users.noreply.github.com>
Co-authored-by: Or Bin <or@raftt.io>
Co-authored-by: Sandro <j32g7f67hb@liamekaens.com>
Co-authored-by: Hatim Zahid <63000127+HatimZ@users.noreply.github.com>
Co-authored-by: Robi <53259730+RobiMez@users.noreply.github.com>
Co-authored-by: Dmitry Kolomatskiy <58207913+lemontree210@users.noreply.github.com>
2022-11-15 09:06:23 +01:00
|
|
|
|
2022-07-11 07:54:03 +02:00
|
|
|
.. versionchanged:: 20.0
|
|
|
|
The chat data is now also present in error handlers if the error is caused by a job.
|
2021-05-27 09:38:17 +02:00
|
|
|
"""
|
2022-06-03 16:55:36 +02:00
|
|
|
if self._chat_id is not None:
|
|
|
|
return self._application.chat_data[self._chat_id]
|
2021-06-06 10:37:53 +02:00
|
|
|
return None
|
2019-09-05 22:48:28 +02:00
|
|
|
|
|
|
|
@chat_data.setter
|
2024-05-12 09:13:20 +02:00
|
|
|
def chat_data(self, _: object) -> NoReturn:
|
2019-09-05 22:48:28 +02:00
|
|
|
raise AttributeError(
|
2022-01-21 16:51:03 +01:00
|
|
|
f"You can not assign a new value to chat_data, see {_STORING_DATA_WIKI}"
|
2019-09-05 22:48:28 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
@property
|
2021-06-06 10:37:53 +02:00
|
|
|
def user_data(self) -> Optional[UD]:
|
2022-04-24 12:38:09 +02:00
|
|
|
""":obj:`ContextTypes.user_data`: Optional. An object that can be used to keep any data in.
|
|
|
|
For each update from the same user it will be the same :obj:`ContextTypes.user_data`.
|
|
|
|
Defaults to :obj:`dict`.
|
2022-07-11 07:54:03 +02:00
|
|
|
|
2023-01-01 16:24:00 +01:00
|
|
|
.. seealso:: :wiki:`Storing Bot, User and Chat Related Data\
|
|
|
|
<Storing-bot%2C-user-and-chat-related-data>`
|
Documentation Improvements (#3214, #3217, #3218, #3271, #3289, #3292, #3303, #3312, #3306, #3319, #3326, #3314)
Co-authored-by: Harshil <37377066+harshil21@users.noreply.github.com>
Co-authored-by: Simon Fong <44134941+simonfongnt@users.noreply.github.com>
Co-authored-by: Piotr Rogulski <rivinek@gmail.com>
Co-authored-by: poolitzer <25934244+Poolitzer@users.noreply.github.com>
Co-authored-by: Or Bin <or@raftt.io>
Co-authored-by: Sandro <j32g7f67hb@liamekaens.com>
Co-authored-by: Hatim Zahid <63000127+HatimZ@users.noreply.github.com>
Co-authored-by: Robi <53259730+RobiMez@users.noreply.github.com>
Co-authored-by: Dmitry Kolomatskiy <58207913+lemontree210@users.noreply.github.com>
2022-11-15 09:06:23 +01:00
|
|
|
|
2022-07-11 07:54:03 +02:00
|
|
|
.. versionchanged:: 20.0
|
|
|
|
The user data is now also present in error handlers if the error is caused by a job.
|
2021-05-27 09:38:17 +02:00
|
|
|
"""
|
2022-06-03 16:55:36 +02:00
|
|
|
if self._user_id is not None:
|
|
|
|
return self._application.user_data[self._user_id]
|
2021-06-06 10:37:53 +02:00
|
|
|
return None
|
2019-09-05 22:48:28 +02:00
|
|
|
|
|
|
|
@user_data.setter
|
2024-05-12 09:13:20 +02:00
|
|
|
def user_data(self, _: object) -> NoReturn:
|
2019-09-05 22:48:28 +02:00
|
|
|
raise AttributeError(
|
2022-01-21 16:51:03 +01:00
|
|
|
f"You can not assign a new value to user_data, see {_STORING_DATA_WIKI}"
|
2019-09-05 22:48:28 +02:00
|
|
|
)
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def refresh_data(self) -> None:
|
|
|
|
"""If :attr:`application` uses persistence, calls
|
2021-06-06 10:37:53 +02:00
|
|
|
: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.
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
Will be called by :meth:`telegram.ext.Application.process_update` and
|
|
|
|
:meth:`telegram.ext.Job.run`.
|
|
|
|
|
2021-06-06 10:37:53 +02:00
|
|
|
.. versionadded:: 13.6
|
|
|
|
"""
|
2022-04-24 12:38:09 +02:00
|
|
|
if self.application.persistence:
|
|
|
|
if self.application.persistence.store_data.bot_data:
|
|
|
|
await self.application.persistence.refresh_bot_data(self.bot_data)
|
2022-06-03 16:55:36 +02:00
|
|
|
if self.application.persistence.store_data.chat_data and self._chat_id is not None:
|
|
|
|
await self.application.persistence.refresh_chat_data(
|
2023-11-12 10:11:22 +01:00
|
|
|
chat_id=self._chat_id,
|
|
|
|
chat_data=self.chat_data, # type: ignore[arg-type]
|
2022-06-03 16:55:36 +02:00
|
|
|
)
|
|
|
|
if self.application.persistence.store_data.user_data and self._user_id is not None:
|
|
|
|
await self.application.persistence.refresh_user_data(
|
2023-11-12 10:11:22 +01:00
|
|
|
user_id=self._user_id,
|
|
|
|
user_data=self.user_data, # type: ignore[arg-type]
|
2022-06-03 16:55:36 +02:00
|
|
|
)
|
2021-06-06 10:37:53 +02:00
|
|
|
|
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.
|
2022-04-24 12:38:09 +02:00
|
|
|
*Will* raise :exc:`KeyError` in case the callback query can not be found in the cache.
|
2021-06-06 11:48:48 +02:00
|
|
|
|
2023-01-01 16:24:00 +01:00
|
|
|
.. seealso:: :wiki:`Arbitrary callback_data <Arbitrary-callback_data>`
|
Documentation Improvements (#3214, #3217, #3218, #3271, #3289, #3292, #3303, #3312, #3306, #3319, #3326, #3314)
Co-authored-by: Harshil <37377066+harshil21@users.noreply.github.com>
Co-authored-by: Simon Fong <44134941+simonfongnt@users.noreply.github.com>
Co-authored-by: Piotr Rogulski <rivinek@gmail.com>
Co-authored-by: poolitzer <25934244+Poolitzer@users.noreply.github.com>
Co-authored-by: Or Bin <or@raftt.io>
Co-authored-by: Sandro <j32g7f67hb@liamekaens.com>
Co-authored-by: Hatim Zahid <63000127+HatimZ@users.noreply.github.com>
Co-authored-by: Robi <53259730+RobiMez@users.noreply.github.com>
Co-authored-by: Dmitry Kolomatskiy <58207913+lemontree210@users.noreply.github.com>
2022-11-15 09:06:23 +01:00
|
|
|
|
2021-06-06 11:48:48 +02:00
|
|
|
Args:
|
|
|
|
callback_query (:class:`telegram.CallbackQuery`): The callback query.
|
|
|
|
|
|
|
|
Raises:
|
2022-04-24 12:38:09 +02:00
|
|
|
KeyError | RuntimeError: :exc:`KeyError`, if the callback query can not be found in
|
|
|
|
the cache and :exc:`RuntimeError`, if the bot doesn't allow for arbitrary
|
2021-06-06 11:48:48 +02:00
|
|
|
callback data.
|
|
|
|
"""
|
|
|
|
if isinstance(self.bot, ExtBot):
|
2022-10-07 10:18:08 +02:00
|
|
|
if self.bot.callback_data_cache is None:
|
2021-06-06 11:48:48 +02:00
|
|
|
raise RuntimeError(
|
|
|
|
"This telegram.ext.ExtBot instance does not use arbitrary callback data."
|
|
|
|
)
|
|
|
|
self.bot.callback_data_cache.drop_data(callback_query)
|
|
|
|
else:
|
2024-08-07 21:56:46 +02:00
|
|
|
raise RuntimeError( # noqa: TRY004
|
|
|
|
"telegram.Bot does not allow for arbitrary callback data."
|
|
|
|
)
|
2021-06-06 11:48:48 +02:00
|
|
|
|
2018-09-21 08:57:01 +02:00
|
|
|
@classmethod
|
2020-10-06 19:28:40 +02:00
|
|
|
def from_error(
|
2024-10-24 20:48:49 +02:00
|
|
|
cls: type["CCT"],
|
2020-10-06 19:28:40 +02:00
|
|
|
update: object,
|
|
|
|
error: Exception,
|
2022-05-12 19:36:25 +02:00
|
|
|
application: "Application[BT, CCT, UD, CD, BD, Any]",
|
2023-05-18 07:57:59 +02:00
|
|
|
job: Optional["Job[Any]"] = None,
|
|
|
|
coroutine: Optional[
|
|
|
|
Union[Generator[Optional["Future[object]"], None, Any], Awaitable[Any]]
|
|
|
|
] = None,
|
2021-08-12 08:11:00 +02:00
|
|
|
) -> "CCT":
|
2021-05-27 09:38:17 +02:00
|
|
|
"""
|
|
|
|
Constructs an instance of :class:`telegram.ext.CallbackContext` to be passed to the error
|
|
|
|
handlers.
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
.. seealso:: :meth:`telegram.ext.Application.add_error_handler`
|
|
|
|
|
2022-05-06 17:15:23 +02:00
|
|
|
.. versionchanged:: 20.0
|
2022-04-24 12:38:09 +02:00
|
|
|
Removed arguments ``async_args`` and ``async_kwargs``.
|
2021-05-27 09:38:17 +02:00
|
|
|
|
|
|
|
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.
|
2022-04-24 12:38:09 +02:00
|
|
|
application (:class:`telegram.ext.Application`): The application associated with this
|
2021-05-27 09:38:17 +02:00
|
|
|
context.
|
2021-10-03 20:00:54 +02:00
|
|
|
job (:class:`telegram.ext.Job`, optional): The job associated with the error.
|
|
|
|
|
2022-06-03 16:55:36 +02:00
|
|
|
.. versionadded:: 20.0
|
2023-02-20 19:53:27 +01:00
|
|
|
coroutine (:term:`awaitable`, optional): The awaitable associated
|
2022-06-03 16:55:36 +02:00
|
|
|
with this error if the error was caused by a coroutine run with
|
|
|
|
:meth:`Application.create_task` or a handler callback with
|
|
|
|
:attr:`block=False <BaseHandler.block>`.
|
|
|
|
|
2022-05-06 17:15:23 +02:00
|
|
|
.. versionadded:: 20.0
|
2021-05-27 09:38:17 +02:00
|
|
|
|
2023-03-25 13:25:59 +01:00
|
|
|
.. versionchanged:: 20.2
|
2023-02-20 19:53:27 +01:00
|
|
|
Accepts :class:`asyncio.Future` and generator-based coroutine functions.
|
2021-05-27 09:38:17 +02:00
|
|
|
Returns:
|
|
|
|
:class:`telegram.ext.CallbackContext`
|
|
|
|
"""
|
2022-07-11 07:54:03 +02:00
|
|
|
# update and job will never be present at the same time
|
|
|
|
if update is not None:
|
|
|
|
self = cls.from_update(update, application)
|
|
|
|
elif job is not None:
|
|
|
|
self = cls.from_job(job, application)
|
|
|
|
else:
|
|
|
|
self = cls(application) # type: ignore
|
|
|
|
|
2018-09-21 08:57:01 +02:00
|
|
|
self.error = error
|
2022-04-24 12:38:09 +02:00
|
|
|
self.coroutine = coroutine
|
2018-09-21 08:57:01 +02:00
|
|
|
return self
|
|
|
|
|
|
|
|
@classmethod
|
2021-08-12 08:11:00 +02:00
|
|
|
def from_update(
|
2024-10-24 20:48:49 +02:00
|
|
|
cls: type["CCT"],
|
2022-04-24 12:38:09 +02:00
|
|
|
update: object,
|
2022-05-12 19:36:25 +02:00
|
|
|
application: "Application[Any, CCT, Any, Any, Any, Any]",
|
2021-08-12 08:11:00 +02:00
|
|
|
) -> "CCT":
|
2021-05-27 09:38:17 +02:00
|
|
|
"""
|
|
|
|
Constructs an instance of :class:`telegram.ext.CallbackContext` to be passed to the
|
|
|
|
handlers.
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
.. seealso:: :meth:`telegram.ext.Application.add_handler`
|
2021-05-27 09:38:17 +02:00
|
|
|
|
|
|
|
Args:
|
|
|
|
update (:obj:`object` | :class:`telegram.Update`): The update.
|
2022-04-24 12:38:09 +02:00
|
|
|
application (:class:`telegram.ext.Application`): The application associated with this
|
2021-05-27 09:38:17 +02:00
|
|
|
context.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
:class:`telegram.ext.CallbackContext`
|
|
|
|
"""
|
2022-06-03 16:55:36 +02:00
|
|
|
if isinstance(update, Update):
|
2018-09-21 08:57:01 +02:00
|
|
|
chat = update.effective_chat
|
|
|
|
user = update.effective_user
|
|
|
|
|
2022-06-03 16:55:36 +02:00
|
|
|
chat_id = chat.id if chat else None
|
|
|
|
user_id = user.id if user else None
|
|
|
|
|
|
|
|
return cls(application, chat_id=chat_id, user_id=user_id) # type: ignore
|
|
|
|
return cls(application) # type: ignore
|
2018-09-21 08:57:01 +02:00
|
|
|
|
|
|
|
@classmethod
|
2021-10-09 13:56:50 +02:00
|
|
|
def from_job(
|
2024-10-24 20:48:49 +02:00
|
|
|
cls: type["CCT"],
|
2023-02-02 18:55:07 +01:00
|
|
|
job: "Job[CCT]",
|
2022-05-12 19:36:25 +02:00
|
|
|
application: "Application[Any, CCT, Any, Any, Any, Any]",
|
2021-10-09 13:56:50 +02:00
|
|
|
) -> "CCT":
|
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.
|
2022-04-24 12:38:09 +02:00
|
|
|
application (:class:`telegram.ext.Application`): The application associated with this
|
2021-05-27 09:38:17 +02:00
|
|
|
context.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
:class:`telegram.ext.CallbackContext`
|
|
|
|
"""
|
2022-06-03 16:55:36 +02:00
|
|
|
self = cls(application, chat_id=job.chat_id, user_id=job.user_id) # type: ignore
|
2018-09-21 08:57:01 +02:00
|
|
|
self.job = job
|
|
|
|
return self
|
|
|
|
|
2024-10-24 20:48:49 +02: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:
|
2024-10-24 20:48:49 +02:00
|
|
|
data (dict[:obj:`str`, :obj:`object`]): The data.
|
2021-05-27 09:38:17 +02:00
|
|
|
"""
|
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
|
2021-10-09 13:56:50 +02:00
|
|
|
def bot(self) -> BT:
|
2018-09-21 08:57:01 +02:00
|
|
|
""":class:`telegram.Bot`: The bot associated with this context."""
|
2022-04-24 12:38:09 +02:00
|
|
|
return self._application.bot
|
2018-09-21 08:57:01 +02:00
|
|
|
|
|
|
|
@property
|
2024-09-14 08:16:39 +02:00
|
|
|
def job_queue(self) -> Optional["JobQueue[ST]"]:
|
2018-09-21 08:57:01 +02:00
|
|
|
"""
|
2022-04-24 12:38:09 +02:00
|
|
|
:class:`telegram.ext.JobQueue`: The :class:`JobQueue` used by the
|
2023-02-05 18:09:55 +01:00
|
|
|
:class:`telegram.ext.Application`.
|
2018-09-21 08:57:01 +02:00
|
|
|
|
2023-07-09 11:22:08 +02:00
|
|
|
.. seealso:: :wiki:`Job Queue <Extensions---JobQueue>`
|
2018-09-21 08:57:01 +02:00
|
|
|
"""
|
2022-12-02 12:08:11 +01:00
|
|
|
if self._application._job_queue is None: # pylint: disable=protected-access
|
|
|
|
warn(
|
|
|
|
"No `JobQueue` set up. To use `JobQueue`, you must install PTB via "
|
2023-06-29 07:46:00 +02:00
|
|
|
'`pip install "python-telegram-bot[job-queue]"`.',
|
2022-12-02 12:08:11 +01:00
|
|
|
stacklevel=2,
|
|
|
|
)
|
|
|
|
return self._application._job_queue # pylint: disable=protected-access
|
2018-09-21 08:57:01 +02:00
|
|
|
|
|
|
|
@property
|
2022-04-24 12:38:09 +02:00
|
|
|
def update_queue(self) -> "Queue[object]":
|
2018-09-21 08:57:01 +02:00
|
|
|
"""
|
2022-04-24 12:38:09 +02:00
|
|
|
:class:`asyncio.Queue`: The :class:`asyncio.Queue` instance used by the
|
|
|
|
:class:`telegram.ext.Application` and (usually) the :class:`telegram.ext.Updater`
|
2018-09-21 08:57:01 +02:00
|
|
|
associated with this context.
|
|
|
|
|
|
|
|
"""
|
2022-04-24 12:38:09 +02:00
|
|
|
return self._application.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
|
|
|
"""
|
2022-04-24 12:38:09 +02:00
|
|
|
:meth:`re.Match <re.Match.expand>`: The first match from :attr:`matches`.
|
2019-02-13 12:07:25 +01:00
|
|
|
Useful if you are only filtering using a single regex filter.
|
2022-04-24 12:38:09 +02:00
|
|
|
Returns :obj:`None` if :attr:`matches` is empty.
|
2019-02-13 12:07:25 +01:00
|
|
|
"""
|
|
|
|
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
|