2018-09-20 22:50:40 +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-20 22:50:40 +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 BasePersistence class."""
|
2020-05-01 20:27:34 +02:00
|
|
|
from abc import ABC, abstractmethod
|
2024-10-24 20:48:49 +02:00
|
|
|
from typing import Generic, NamedTuple, NoReturn, Optional
|
2020-07-13 21:52:26 +02:00
|
|
|
|
2022-05-05 09:27:54 +02:00
|
|
|
from telegram._bot import Bot
|
|
|
|
from telegram.ext._extbot import ExtBot
|
|
|
|
from telegram.ext._utils.types import BD, CD, UD, CDCData, ConversationDict, ConversationKey
|
2020-10-06 19:28:40 +02:00
|
|
|
|
2020-05-01 20:27:34 +02:00
|
|
|
|
2024-02-07 20:45:57 +01:00
|
|
|
class PersistenceInput(NamedTuple):
|
2022-06-09 17:08:54 +02:00
|
|
|
"""Convenience wrapper to group boolean input for the :paramref:`~BasePersistence.store_data`
|
|
|
|
parameter for :class:`BasePersistence`.
|
2021-08-13 16:18:42 +02:00
|
|
|
|
|
|
|
Args:
|
|
|
|
bot_data (:obj:`bool`, optional): Whether the setting should be applied for ``bot_data``.
|
|
|
|
Defaults to :obj:`True`.
|
|
|
|
chat_data (:obj:`bool`, optional): Whether the setting should be applied for ``chat_data``.
|
|
|
|
Defaults to :obj:`True`.
|
|
|
|
user_data (:obj:`bool`, optional): Whether the setting should be applied for ``user_data``.
|
|
|
|
Defaults to :obj:`True`.
|
|
|
|
callback_data (:obj:`bool`, optional): Whether the setting should be applied for
|
|
|
|
``callback_data``. Defaults to :obj:`True`.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
bot_data (:obj:`bool`): Whether the setting should be applied for ``bot_data``.
|
|
|
|
chat_data (:obj:`bool`): Whether the setting should be applied for ``chat_data``.
|
|
|
|
user_data (:obj:`bool`): Whether the setting should be applied for ``user_data``.
|
|
|
|
callback_data (:obj:`bool`): Whether the setting should be applied for ``callback_data``.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
bot_data: bool = True
|
|
|
|
chat_data: bool = True
|
|
|
|
user_data: bool = True
|
|
|
|
callback_data: bool = True
|
|
|
|
|
|
|
|
|
2021-06-06 10:37:53 +02:00
|
|
|
class BasePersistence(Generic[UD, CD, BD], ABC):
|
2018-09-20 22:50:40 +02:00
|
|
|
"""Interface class for adding persistence to your bot.
|
|
|
|
Subclass this object for different implementations of a persistent bot.
|
|
|
|
|
2022-02-09 17:30:16 +01:00
|
|
|
Attention:
|
|
|
|
The interface provided by this class is intended to be accessed exclusively by
|
2022-04-24 12:38:09 +02:00
|
|
|
:class:`~telegram.ext.Application`. Calling any of the methods below manually might
|
|
|
|
interfere with the integration of persistence into :class:`~telegram.ext.Application`.
|
2022-02-09 17:30:16 +01:00
|
|
|
|
2020-12-17 09:29:17 +01:00
|
|
|
All relevant methods must be overwritten. This includes:
|
|
|
|
|
2020-12-30 15:59:50 +01:00
|
|
|
* :meth:`get_bot_data`
|
2020-12-17 09:29:17 +01:00
|
|
|
* :meth:`update_bot_data`
|
2021-06-06 10:37:53 +02:00
|
|
|
* :meth:`refresh_bot_data`
|
2020-12-17 09:29:17 +01:00
|
|
|
* :meth:`get_chat_data`
|
|
|
|
* :meth:`update_chat_data`
|
2021-06-06 10:37:53 +02:00
|
|
|
* :meth:`refresh_chat_data`
|
2022-03-12 12:27:18 +01:00
|
|
|
* :meth:`drop_chat_data`
|
2020-12-17 09:29:17 +01:00
|
|
|
* :meth:`get_user_data`
|
|
|
|
* :meth:`update_user_data`
|
2021-06-06 10:37:53 +02:00
|
|
|
* :meth:`refresh_user_data`
|
2022-03-12 12:27:18 +01:00
|
|
|
* :meth:`drop_user_data`
|
2021-06-06 11:48:48 +02:00
|
|
|
* :meth:`get_callback_data`
|
|
|
|
* :meth:`update_callback_data`
|
2020-12-17 09:29:17 +01:00
|
|
|
* :meth:`get_conversations`
|
|
|
|
* :meth:`update_conversation`
|
|
|
|
* :meth:`flush`
|
|
|
|
|
2022-03-12 12:27:18 +01:00
|
|
|
If you don't actually need one of those methods, a simple :keyword:`pass` is enough.
|
|
|
|
For example, if you don't store ``bot_data``, you don't need :meth:`get_bot_data`,
|
|
|
|
:meth:`update_bot_data` or :meth:`refresh_bot_data`.
|
2020-07-13 21:52:26 +02:00
|
|
|
|
|
|
|
Note:
|
2022-03-12 12:27:18 +01:00
|
|
|
You should avoid saving :class:`telegram.Bot` instances. This is because if you change e.g.
|
|
|
|
the bots token, this won't propagate to the serialized instances and may lead to exceptions.
|
|
|
|
|
|
|
|
To prevent this, the implementation may use :attr:`bot` to replace bot instances with a
|
|
|
|
placeholder before serialization and insert :attr:`bot` back when loading the data.
|
|
|
|
Since :attr:`bot` will be set when the process starts, this will be the up-to-date bot
|
|
|
|
instance.
|
|
|
|
|
|
|
|
If the persistence implementation does not take care of this, you should make sure not to
|
|
|
|
store any bot instances in the data that will be persisted. E.g. in case of
|
|
|
|
:class:`telegram.TelegramObject`, one may call :meth:`set_bot` to ensure that shortcuts like
|
|
|
|
:meth:`telegram.Message.reply_text` are available.
|
2020-07-13 21:52:26 +02:00
|
|
|
|
2022-05-06 17:15:23 +02:00
|
|
|
This class is a :class:`~typing.Generic` class and accepts three type variables:
|
|
|
|
|
|
|
|
1. The type of the second argument of :meth:`update_user_data`, which must coincide with the
|
|
|
|
type of the second argument of :meth:`refresh_user_data` and the values in the dictionary
|
|
|
|
returned by :meth:`get_user_data`.
|
|
|
|
2. The type of the second argument of :meth:`update_chat_data`, which must coincide with the
|
|
|
|
type of the second argument of :meth:`refresh_chat_data` and the values in the dictionary
|
|
|
|
returned by :meth:`get_chat_data`.
|
|
|
|
3. The type of the argument of :meth:`update_bot_data`, which must coincide with the
|
|
|
|
type of the argument of :meth:`refresh_bot_data` and the return value of
|
|
|
|
:meth:`get_bot_data`.
|
|
|
|
|
2023-01-01 16:24:00 +01:00
|
|
|
.. seealso:: :wiki:`Architecture Overview <Architecture>`,
|
|
|
|
:wiki:`Making Your Bot Persistent <Making-your-bot-persistent>`
|
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-05-06 17:15:23 +02:00
|
|
|
.. versionchanged:: 20.0
|
|
|
|
|
2022-03-12 12:27:18 +01:00
|
|
|
* The parameters and attributes ``store_*_data`` were replaced by :attr:`store_data`.
|
|
|
|
* ``insert/replace_bot`` was dropped. Serialization of bot instances now needs to be
|
|
|
|
handled by the specific implementation - see above note.
|
2021-06-06 11:48:48 +02:00
|
|
|
|
2021-08-13 16:18:42 +02:00
|
|
|
Args:
|
2023-02-05 18:09:55 +01:00
|
|
|
store_data (:class:`~telegram.ext.PersistenceInput`, optional): Specifies which kinds of
|
|
|
|
data will be saved by this persistence instance. By default, all available kinds of
|
|
|
|
data will be saved.
|
2022-04-24 12:38:09 +02:00
|
|
|
update_interval (:obj:`int` | :obj:`float`, optional): The
|
|
|
|
:class:`~telegram.ext.Application` will update
|
|
|
|
the persistence in regular intervals. This parameter specifies the time (in seconds) to
|
2022-06-09 17:08:54 +02:00
|
|
|
wait between two consecutive runs of updating the persistence. Defaults to ``60``
|
|
|
|
seconds.
|
2022-04-24 12:38:09 +02:00
|
|
|
|
2022-05-06 17:15:23 +02:00
|
|
|
.. versionadded:: 20.0
|
2020-12-30 15:59:50 +01:00
|
|
|
Attributes:
|
2023-02-05 18:09:55 +01:00
|
|
|
store_data (:class:`~telegram.ext.PersistenceInput`): Specifies which kinds of data will
|
|
|
|
be saved by this persistence instance.
|
2022-03-12 12:27:18 +01:00
|
|
|
bot (:class:`telegram.Bot`): The bot associated with the persistence.
|
2018-09-20 22:50:40 +02:00
|
|
|
"""
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
__slots__ = (
|
2024-02-05 19:24:00 +01:00
|
|
|
"_update_interval",
|
2022-04-24 12:38:09 +02:00
|
|
|
"bot",
|
|
|
|
"store_data",
|
|
|
|
)
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
2023-05-18 07:57:59 +02:00
|
|
|
store_data: Optional[PersistenceInput] = None,
|
2022-04-24 12:38:09 +02:00
|
|
|
update_interval: float = 60,
|
|
|
|
):
|
2023-02-02 18:55:07 +01:00
|
|
|
self.store_data: PersistenceInput = store_data or PersistenceInput()
|
|
|
|
self._update_interval: float = update_interval
|
2021-08-13 16:18:42 +02:00
|
|
|
|
2020-10-06 19:28:40 +02:00
|
|
|
self.bot: Bot = None # type: ignore[assignment]
|
2021-05-29 16:18:16 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
@property
|
|
|
|
def update_interval(self) -> float:
|
|
|
|
""":obj:`float`: Time (in seconds) that the :class:`~telegram.ext.Application`
|
|
|
|
will wait between two consecutive runs of updating the persistence.
|
|
|
|
|
2022-05-06 17:15:23 +02:00
|
|
|
.. versionadded:: 20.0
|
2022-04-24 12:38:09 +02:00
|
|
|
"""
|
|
|
|
return self._update_interval
|
|
|
|
|
|
|
|
@update_interval.setter
|
2024-05-12 09:13:20 +02:00
|
|
|
def update_interval(self, _: object) -> NoReturn:
|
2022-04-24 12:38:09 +02:00
|
|
|
raise AttributeError(
|
|
|
|
"You can not assign a new value to update_interval after initialization."
|
|
|
|
)
|
|
|
|
|
2020-10-06 19:28:40 +02:00
|
|
|
def set_bot(self, bot: Bot) -> None:
|
2020-07-13 21:52:26 +02:00
|
|
|
"""Set the Bot to be used by this persistence instance.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
bot (:class:`telegram.Bot`): The bot.
|
2022-04-24 12:38:09 +02:00
|
|
|
|
|
|
|
Raises:
|
|
|
|
:exc:`TypeError`: If :attr:`PersistenceInput.callback_data` is :obj:`True` and the
|
2022-11-24 12:13:54 +01:00
|
|
|
:paramref:`bot` is not an instance of :class:`telegram.ext.ExtBot`.
|
2020-07-13 21:52:26 +02:00
|
|
|
"""
|
2022-11-24 12:13:54 +01:00
|
|
|
if self.store_data.callback_data and (not isinstance(bot, ExtBot)):
|
|
|
|
raise TypeError("callback_data can only be stored when using telegram.ext.ExtBot.")
|
2021-06-06 11:48:48 +02:00
|
|
|
|
2020-07-13 21:52:26 +02:00
|
|
|
self.bot = bot
|
|
|
|
|
2020-05-01 20:27:34 +02:00
|
|
|
@abstractmethod
|
2024-10-24 20:48:49 +02:00
|
|
|
async def get_user_data(self) -> dict[int, UD]:
|
2022-04-24 12:38:09 +02:00
|
|
|
"""Will be called by :class:`telegram.ext.Application` upon creation with a
|
2020-12-17 09:29:17 +01:00
|
|
|
persistence object. It should return the ``user_data`` if stored, or an empty
|
2022-02-01 17:50:17 +01:00
|
|
|
:obj:`dict`. In the latter case, the dictionary should produce values
|
2021-10-10 15:10:21 +02:00
|
|
|
corresponding to one of the following:
|
|
|
|
|
2022-06-09 17:08:54 +02:00
|
|
|
- :obj:`dict`
|
|
|
|
- The type from :attr:`telegram.ext.ContextTypes.user_data`
|
|
|
|
if :class:`telegram.ext.ContextTypes` is used.
|
2022-02-01 17:50:17 +01:00
|
|
|
|
2022-05-06 17:15:23 +02:00
|
|
|
.. versionchanged:: 20.0
|
2022-02-01 17:50:17 +01:00
|
|
|
This method may now return a :obj:`dict` instead of a :obj:`collections.defaultdict`
|
2018-09-20 22:50:40 +02:00
|
|
|
|
|
|
|
Returns:
|
2024-10-24 20:48:49 +02:00
|
|
|
dict[:obj:`int`, :obj:`dict` | :attr:`telegram.ext.ContextTypes.user_data`]:
|
2021-10-10 15:10:21 +02:00
|
|
|
The restored user data.
|
2018-09-20 22:50:40 +02:00
|
|
|
"""
|
|
|
|
|
2020-05-01 20:27:34 +02:00
|
|
|
@abstractmethod
|
2024-10-24 20:48:49 +02:00
|
|
|
async def get_chat_data(self) -> dict[int, CD]:
|
2022-04-24 12:38:09 +02:00
|
|
|
"""Will be called by :class:`telegram.ext.Application` upon creation with a
|
2020-12-17 09:29:17 +01:00
|
|
|
persistence object. It should return the ``chat_data`` if stored, or an empty
|
2022-02-01 17:50:17 +01:00
|
|
|
:obj:`dict`. In the latter case, the dictionary should produce values
|
2021-10-10 15:10:21 +02:00
|
|
|
corresponding to one of the following:
|
|
|
|
|
2022-06-09 17:08:54 +02:00
|
|
|
- :obj:`dict`
|
|
|
|
- The type from :attr:`telegram.ext.ContextTypes.chat_data`
|
|
|
|
if :class:`telegram.ext.ContextTypes` is used.
|
2022-02-01 17:50:17 +01:00
|
|
|
|
2022-05-06 17:15:23 +02:00
|
|
|
.. versionchanged:: 20.0
|
2022-02-01 17:50:17 +01:00
|
|
|
This method may now return a :obj:`dict` instead of a :obj:`collections.defaultdict`
|
2018-09-20 22:50:40 +02:00
|
|
|
|
|
|
|
Returns:
|
2024-10-24 20:48:49 +02:00
|
|
|
dict[:obj:`int`, :obj:`dict` | :attr:`telegram.ext.ContextTypes.chat_data`]:
|
2021-10-10 15:10:21 +02:00
|
|
|
The restored chat data.
|
2018-09-20 22:50:40 +02:00
|
|
|
"""
|
|
|
|
|
2020-05-01 20:27:34 +02:00
|
|
|
@abstractmethod
|
2022-04-24 12:38:09 +02:00
|
|
|
async def get_bot_data(self) -> BD:
|
|
|
|
"""Will be called by :class:`telegram.ext.Application` upon creation with a
|
2020-12-17 09:29:17 +01:00
|
|
|
persistence object. It should return the ``bot_data`` if stored, or an empty
|
2022-02-01 17:50:17 +01:00
|
|
|
:obj:`dict`. In the latter case, the :obj:`dict` should produce values
|
2021-10-10 15:10:21 +02:00
|
|
|
corresponding to one of the following:
|
|
|
|
|
2022-06-09 17:08:54 +02:00
|
|
|
- :obj:`dict`
|
|
|
|
- The type from :attr:`telegram.ext.ContextTypes.bot_data`
|
|
|
|
if :class:`telegram.ext.ContextTypes` are used.
|
2020-02-02 22:20:31 +01:00
|
|
|
|
|
|
|
Returns:
|
2024-10-24 20:48:49 +02:00
|
|
|
dict[:obj:`int`, :obj:`dict` | :attr:`telegram.ext.ContextTypes.bot_data`]:
|
2021-10-10 15:10:21 +02:00
|
|
|
The restored bot data.
|
2020-02-02 22:20:31 +01:00
|
|
|
"""
|
|
|
|
|
2021-08-12 08:58:32 +02:00
|
|
|
@abstractmethod
|
2022-04-24 12:38:09 +02:00
|
|
|
async def get_callback_data(self) -> Optional[CDCData]:
|
|
|
|
"""Will be called by :class:`telegram.ext.Application` upon creation with a
|
2021-06-06 11:48:48 +02:00
|
|
|
persistence object. If callback data was stored, it should be returned.
|
|
|
|
|
|
|
|
.. versionadded:: 13.6
|
|
|
|
|
2022-05-06 17:15:23 +02:00
|
|
|
.. versionchanged:: 20.0
|
2022-04-24 12:38:09 +02:00
|
|
|
Changed this method into an :external:func:`~abc.abstractmethod`.
|
2021-08-12 08:58:32 +02:00
|
|
|
|
2021-06-06 11:48:48 +02:00
|
|
|
Returns:
|
2024-10-24 20:48:49 +02:00
|
|
|
tuple[list[tuple[:obj:`str`, :obj:`float`, dict[:obj:`str`, :class:`object`]]],
|
|
|
|
dict[:obj:`str`, :obj:`str`]] | :obj:`None`: The restored metadata or :obj:`None`,
|
2022-06-09 17:08:54 +02:00
|
|
|
if no data was stored.
|
2021-06-06 11:48:48 +02:00
|
|
|
"""
|
|
|
|
|
2020-05-01 20:27:34 +02:00
|
|
|
@abstractmethod
|
2022-04-24 12:38:09 +02:00
|
|
|
async def get_conversations(self, name: str) -> ConversationDict:
|
|
|
|
"""Will be called by :class:`telegram.ext.Application` when a
|
2018-09-20 22:50:40 +02:00
|
|
|
:class:`telegram.ext.ConversationHandler` is added if
|
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
|
|
|
:attr:`telegram.ext.ConversationHandler.persistent` is :obj:`True`.
|
2022-04-24 12:38:09 +02:00
|
|
|
It should return the conversations for the handler with :paramref:`name` or an empty
|
|
|
|
:obj:`dict`.
|
2018-09-20 22:50:40 +02:00
|
|
|
|
|
|
|
Args:
|
|
|
|
name (:obj:`str`): The handlers name.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
:obj:`dict`: The restored conversations for the handler.
|
|
|
|
"""
|
|
|
|
|
2020-05-01 20:27:34 +02:00
|
|
|
@abstractmethod
|
2022-04-24 12:38:09 +02:00
|
|
|
async def update_conversation(
|
|
|
|
self, name: str, key: ConversationKey, new_state: Optional[object]
|
2020-10-06 19:28:40 +02:00
|
|
|
) -> None:
|
2021-07-01 17:34:23 +02:00
|
|
|
"""Will be called when a :class:`telegram.ext.ConversationHandler` changes states.
|
|
|
|
This allows the storage of the new state in the persistence.
|
2018-09-20 22:50:40 +02:00
|
|
|
|
|
|
|
Args:
|
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
|
|
|
name (:obj:`str`): The handler's name.
|
2018-09-20 22:50:40 +02:00
|
|
|
key (:obj:`tuple`): The key the state is changed for.
|
2022-04-24 12:38:09 +02:00
|
|
|
new_state (:class:`object`): The new state for the given key.
|
2018-09-20 22:50:40 +02:00
|
|
|
"""
|
|
|
|
|
2020-05-01 20:27:34 +02:00
|
|
|
@abstractmethod
|
2022-04-24 12:38:09 +02:00
|
|
|
async def update_user_data(self, user_id: int, data: UD) -> None:
|
|
|
|
"""Will be called by the :class:`telegram.ext.Application` after a handler has
|
2018-09-20 22:50:40 +02:00
|
|
|
handled an update.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user_id (:obj:`int`): The user the data might have been changed for.
|
2021-10-10 15:10:21 +02:00
|
|
|
data (:obj:`dict` | :attr:`telegram.ext.ContextTypes.user_data`):
|
2022-04-24 12:38:09 +02:00
|
|
|
The :attr:`telegram.ext.Application.user_data` ``[user_id]``.
|
2018-09-20 22:50:40 +02:00
|
|
|
"""
|
|
|
|
|
2020-05-01 20:27:34 +02:00
|
|
|
@abstractmethod
|
2022-04-24 12:38:09 +02:00
|
|
|
async def update_chat_data(self, chat_id: int, data: CD) -> None:
|
|
|
|
"""Will be called by the :class:`telegram.ext.Application` after a handler has
|
2018-09-20 22:50:40 +02:00
|
|
|
handled an update.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
chat_id (:obj:`int`): The chat the data might have been changed for.
|
2021-10-10 15:10:21 +02:00
|
|
|
data (:obj:`dict` | :attr:`telegram.ext.ContextTypes.chat_data`):
|
2022-04-24 12:38:09 +02:00
|
|
|
The :attr:`telegram.ext.Application.chat_data` ``[chat_id]``.
|
2020-02-02 22:20:31 +01:00
|
|
|
"""
|
|
|
|
|
2020-05-01 20:27:34 +02:00
|
|
|
@abstractmethod
|
2022-04-24 12:38:09 +02:00
|
|
|
async def update_bot_data(self, data: BD) -> None:
|
|
|
|
"""Will be called by the :class:`telegram.ext.Application` after a handler has
|
2020-02-02 22:20:31 +01:00
|
|
|
handled an update.
|
|
|
|
|
|
|
|
Args:
|
2021-10-10 15:10:21 +02:00
|
|
|
data (:obj:`dict` | :attr:`telegram.ext.ContextTypes.bot_data`):
|
2022-04-24 12:38:09 +02:00
|
|
|
The :attr:`telegram.ext.Application.bot_data`.
|
2021-06-06 10:37:53 +02:00
|
|
|
"""
|
|
|
|
|
2022-01-19 17:27:02 +01:00
|
|
|
@abstractmethod
|
2022-04-24 12:38:09 +02:00
|
|
|
async def update_callback_data(self, data: CDCData) -> None:
|
|
|
|
"""Will be called by the :class:`telegram.ext.Application` after a handler has
|
2022-01-19 17:27:02 +01:00
|
|
|
handled an update.
|
|
|
|
|
|
|
|
.. versionadded:: 13.6
|
|
|
|
|
2022-05-06 17:15:23 +02:00
|
|
|
.. versionchanged:: 20.0
|
2022-04-24 12:38:09 +02:00
|
|
|
Changed this method into an :external:func:`~abc.abstractmethod`.
|
2022-01-19 17:27:02 +01:00
|
|
|
|
|
|
|
Args:
|
2024-10-24 20:48:49 +02:00
|
|
|
data (tuple[list[tuple[:obj:`str`, :obj:`float`, \
|
|
|
|
dict[:obj:`str`, :obj:`Any`]]], dict[:obj:`str`, :obj:`str`]] | :obj:`None`):
|
2022-01-19 17:27:02 +01:00
|
|
|
The relevant data to restore :class:`telegram.ext.CallbackDataCache`.
|
|
|
|
"""
|
|
|
|
|
|
|
|
@abstractmethod
|
2022-04-24 12:38:09 +02:00
|
|
|
async def drop_chat_data(self, chat_id: int) -> None:
|
|
|
|
"""Will be called by the :class:`telegram.ext.Application`, when using
|
|
|
|
:meth:`~telegram.ext.Application.drop_chat_data`.
|
2022-01-19 17:27:02 +01:00
|
|
|
|
2022-05-06 17:15:23 +02:00
|
|
|
.. versionadded:: 20.0
|
2022-01-19 17:27:02 +01:00
|
|
|
|
|
|
|
Args:
|
|
|
|
chat_id (:obj:`int`): The chat id to delete from the persistence.
|
|
|
|
"""
|
|
|
|
|
|
|
|
@abstractmethod
|
2022-04-24 12:38:09 +02:00
|
|
|
async def drop_user_data(self, user_id: int) -> None:
|
|
|
|
"""Will be called by the :class:`telegram.ext.Application`, when using
|
|
|
|
:meth:`~telegram.ext.Application.drop_user_data`.
|
2022-01-19 17:27:02 +01:00
|
|
|
|
2022-05-06 17:15:23 +02:00
|
|
|
.. versionadded:: 20.0
|
2022-01-19 17:27:02 +01:00
|
|
|
|
|
|
|
Args:
|
|
|
|
user_id (:obj:`int`): The user id to delete from the persistence.
|
|
|
|
"""
|
|
|
|
|
2021-08-12 08:58:32 +02:00
|
|
|
@abstractmethod
|
2022-04-24 12:38:09 +02:00
|
|
|
async def refresh_user_data(self, user_id: int, user_data: UD) -> None:
|
|
|
|
"""Will be called by the :class:`telegram.ext.Application` before passing the
|
|
|
|
:attr:`~telegram.ext.Application.user_data` to a callback. Can be used to update data
|
|
|
|
stored in :attr:`~telegram.ext.Application.user_data` from an external source.
|
2021-06-06 10:37:53 +02:00
|
|
|
|
2024-07-12 16:33:42 +02:00
|
|
|
Tip:
|
|
|
|
This method is expected to edit the object :paramref:`user_data` in-place instead of
|
|
|
|
returning a new object.
|
|
|
|
|
2023-04-02 22:29:16 +02:00
|
|
|
Warning:
|
|
|
|
When using :meth:`~telegram.ext.ApplicationBuilder.concurrent_updates`, this method
|
|
|
|
may be called while a handler callback is still running. This might lead to race
|
|
|
|
conditions.
|
|
|
|
|
2021-06-06 10:37:53 +02:00
|
|
|
.. versionadded:: 13.6
|
|
|
|
|
2022-05-06 17:15:23 +02:00
|
|
|
.. versionchanged:: 20.0
|
2022-04-24 12:38:09 +02:00
|
|
|
Changed this method into an :external:func:`~abc.abstractmethod`.
|
2021-08-12 08:58:32 +02:00
|
|
|
|
2021-06-06 10:37:53 +02:00
|
|
|
Args:
|
2022-04-24 12:38:09 +02:00
|
|
|
user_id (:obj:`int`): The user ID this :attr:`~telegram.ext.Application.user_data` is
|
2022-02-09 17:30:16 +01:00
|
|
|
associated with.
|
2021-10-10 15:10:21 +02:00
|
|
|
user_data (:obj:`dict` | :attr:`telegram.ext.ContextTypes.user_data`):
|
|
|
|
The ``user_data`` of a single user.
|
2021-06-06 10:37:53 +02:00
|
|
|
"""
|
|
|
|
|
2021-08-12 08:58:32 +02:00
|
|
|
@abstractmethod
|
2022-04-24 12:38:09 +02:00
|
|
|
async def refresh_chat_data(self, chat_id: int, chat_data: CD) -> None:
|
|
|
|
"""Will be called by the :class:`telegram.ext.Application` before passing the
|
|
|
|
:attr:`~telegram.ext.Application.chat_data` to a callback. Can be used to update data
|
|
|
|
stored in :attr:`~telegram.ext.Application.chat_data` from an external source.
|
2021-06-06 10:37:53 +02:00
|
|
|
|
2024-07-12 16:33:42 +02:00
|
|
|
Tip:
|
|
|
|
This method is expected to edit the object :paramref:`chat_data` in-place instead of
|
|
|
|
returning a new object.
|
|
|
|
|
2023-04-02 22:29:16 +02:00
|
|
|
Warning:
|
|
|
|
When using :meth:`~telegram.ext.ApplicationBuilder.concurrent_updates`, this method
|
|
|
|
may be called while a handler callback is still running. This might lead to race
|
|
|
|
conditions.
|
|
|
|
|
2021-06-06 10:37:53 +02:00
|
|
|
.. versionadded:: 13.6
|
|
|
|
|
2022-05-06 17:15:23 +02:00
|
|
|
.. versionchanged:: 20.0
|
2022-04-24 12:38:09 +02:00
|
|
|
Changed this method into an :external:func:`~abc.abstractmethod`.
|
2021-08-12 08:58:32 +02:00
|
|
|
|
2021-06-06 10:37:53 +02:00
|
|
|
Args:
|
2022-04-24 12:38:09 +02:00
|
|
|
chat_id (:obj:`int`): The chat ID this :attr:`~telegram.ext.Application.chat_data` is
|
2022-02-09 17:30:16 +01:00
|
|
|
associated with.
|
2021-10-10 15:10:21 +02:00
|
|
|
chat_data (:obj:`dict` | :attr:`telegram.ext.ContextTypes.chat_data`):
|
|
|
|
The ``chat_data`` of a single chat.
|
2021-06-06 10:37:53 +02:00
|
|
|
"""
|
|
|
|
|
2021-08-12 08:58:32 +02:00
|
|
|
@abstractmethod
|
2022-04-24 12:38:09 +02:00
|
|
|
async def refresh_bot_data(self, bot_data: BD) -> None:
|
|
|
|
"""Will be called by the :class:`telegram.ext.Application` before passing the
|
|
|
|
:attr:`~telegram.ext.Application.bot_data` to a callback. Can be used to update data stored
|
|
|
|
in :attr:`~telegram.ext.Application.bot_data` from an external source.
|
2021-06-06 10:37:53 +02:00
|
|
|
|
2024-07-12 16:33:42 +02:00
|
|
|
Tip:
|
|
|
|
This method is expected to edit the object :paramref:`bot_data` in-place instead of
|
|
|
|
returning a new object.
|
|
|
|
|
2023-04-02 22:29:16 +02:00
|
|
|
Warning:
|
|
|
|
When using :meth:`~telegram.ext.ApplicationBuilder.concurrent_updates`, this method
|
|
|
|
may be called while a handler callback is still running. This might lead to race
|
|
|
|
conditions.
|
|
|
|
|
2021-06-06 10:37:53 +02:00
|
|
|
.. versionadded:: 13.6
|
|
|
|
|
2022-05-06 17:15:23 +02:00
|
|
|
.. versionchanged:: 20.0
|
2022-04-24 12:38:09 +02:00
|
|
|
Changed this method into an :external:func:`~abc.abstractmethod`.
|
2021-08-12 08:58:32 +02:00
|
|
|
|
2021-06-06 10:37:53 +02:00
|
|
|
Args:
|
2021-10-10 15:10:21 +02:00
|
|
|
bot_data (:obj:`dict` | :attr:`telegram.ext.ContextTypes.bot_data`):
|
|
|
|
The ``bot_data``.
|
2018-09-20 22:50:40 +02:00
|
|
|
"""
|
|
|
|
|
2021-08-12 08:58:32 +02:00
|
|
|
@abstractmethod
|
2022-04-24 12:38:09 +02:00
|
|
|
async def flush(self) -> None:
|
|
|
|
"""Will be called by :meth:`telegram.ext.Application.stop`. Gives the
|
2020-12-17 09:29:17 +01:00
|
|
|
persistence a chance to finish up saving or close a database connection gracefully.
|
2021-08-12 08:58:32 +02:00
|
|
|
|
2022-05-06 17:15:23 +02:00
|
|
|
.. versionchanged:: 20.0
|
2022-04-24 12:38:09 +02:00
|
|
|
Changed this method into an :external:func:`~abc.abstractmethod`.
|
2018-09-20 22:50:40 +02:00
|
|
|
"""
|