mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2025-01-05 18:27:22 +01:00
a0720b9ac6
* 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>
151 lines
6 KiB
Python
151 lines
6 KiB
Python
#!/usr/bin/env python
|
|
#
|
|
# A library that provides a Python interface to the Telegram Bot API
|
|
# Copyright (C) 2015-2020
|
|
# 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."""
|
|
|
|
from abc import ABC, abstractmethod
|
|
|
|
|
|
class BasePersistence(ABC):
|
|
"""Interface class for adding persistence to your bot.
|
|
Subclass this object for different implementations of a persistent bot.
|
|
|
|
All relevant methods must be overwritten. This means:
|
|
|
|
* If :attr:`store_bot_data` is :obj:`True` you must overwrite :meth:`get_bot_data` and
|
|
:meth:`update_bot_data`.
|
|
* If :attr:`store_chat_data` is :obj:`True` you must overwrite :meth:`get_chat_data` and
|
|
:meth:`update_chat_data`.
|
|
* If :attr:`store_user_data` is :obj:`True` you must overwrite :meth:`get_user_data` and
|
|
:meth:`update_user_data`.
|
|
* If you want to store conversation data with :class:`telegram.ext.ConversationHandler`, you
|
|
must overwrite :meth:`get_conversations` and :meth:`update_conversation`.
|
|
* :meth:`flush` will be called when the bot is shutdown.
|
|
|
|
Attributes:
|
|
store_user_data (:obj:`bool`): Optional, Whether user_data should be saved by this
|
|
persistence class.
|
|
store_chat_data (:obj:`bool`): Optional. Whether chat_data should be saved by this
|
|
persistence class.
|
|
store_bot_data (:obj:`bool`): Optional. Whether bot_data should be saved by this
|
|
persistence class.
|
|
|
|
Args:
|
|
store_user_data (:obj:`bool`, optional): Whether user_data should be saved by this
|
|
persistence class. Default is :obj:`True`.
|
|
store_chat_data (:obj:`bool`, optional): Whether chat_data should be saved by this
|
|
persistence class. Default is :obj:`True` .
|
|
store_bot_data (:obj:`bool`, optional): Whether bot_data should be saved by this
|
|
persistence class. Default is :obj:`True` .
|
|
"""
|
|
|
|
def __init__(self, store_user_data=True, store_chat_data=True, store_bot_data=True):
|
|
self.store_user_data = store_user_data
|
|
self.store_chat_data = store_chat_data
|
|
self.store_bot_data = store_bot_data
|
|
|
|
@abstractmethod
|
|
def get_user_data(self):
|
|
""""Will be called by :class:`telegram.ext.Dispatcher` upon creation with a
|
|
persistence object. It should return the user_data if stored, or an empty
|
|
``defaultdict(dict)``.
|
|
|
|
Returns:
|
|
:obj:`defaultdict`: The restored user data.
|
|
"""
|
|
|
|
@abstractmethod
|
|
def get_chat_data(self):
|
|
""""Will be called by :class:`telegram.ext.Dispatcher` upon creation with a
|
|
persistence object. It should return the chat_data if stored, or an empty
|
|
``defaultdict(dict)``.
|
|
|
|
Returns:
|
|
:obj:`defaultdict`: The restored chat data.
|
|
"""
|
|
|
|
@abstractmethod
|
|
def get_bot_data(self):
|
|
""""Will be called by :class:`telegram.ext.Dispatcher` upon creation with a
|
|
persistence object. It should return the bot_data if stored, or an empty
|
|
:obj:`dict`.
|
|
|
|
Returns:
|
|
:obj:`dict`: The restored bot data.
|
|
"""
|
|
|
|
@abstractmethod
|
|
def get_conversations(self, name):
|
|
""""Will be called by :class:`telegram.ext.Dispatcher` when a
|
|
:class:`telegram.ext.ConversationHandler` is added if
|
|
:attr:`telegram.ext.ConversationHandler.persistent` is :obj:`True`.
|
|
It should return the conversations for the handler with `name` or an empty :obj:`dict`
|
|
|
|
Args:
|
|
name (:obj:`str`): The handlers name.
|
|
|
|
Returns:
|
|
:obj:`dict`: The restored conversations for the handler.
|
|
"""
|
|
|
|
@abstractmethod
|
|
def update_conversation(self, name, key, new_state):
|
|
"""Will be called when a :attr:`telegram.ext.ConversationHandler.update_state`
|
|
is called. This allows the storage of the new state in the persistence.
|
|
|
|
Args:
|
|
name (:obj:`str`): The handler's name.
|
|
key (:obj:`tuple`): The key the state is changed for.
|
|
new_state (:obj:`tuple` | :obj:`any`): The new state for the given key.
|
|
"""
|
|
|
|
@abstractmethod
|
|
def update_user_data(self, user_id, data):
|
|
"""Will be called by the :class:`telegram.ext.Dispatcher` after a handler has
|
|
handled an update.
|
|
|
|
Args:
|
|
user_id (:obj:`int`): The user the data might have been changed for.
|
|
data (:obj:`dict`): The :attr:`telegram.ext.dispatcher.user_data` [user_id].
|
|
"""
|
|
|
|
@abstractmethod
|
|
def update_chat_data(self, chat_id, data):
|
|
"""Will be called by the :class:`telegram.ext.Dispatcher` after a handler has
|
|
handled an update.
|
|
|
|
Args:
|
|
chat_id (:obj:`int`): The chat the data might have been changed for.
|
|
data (:obj:`dict`): The :attr:`telegram.ext.dispatcher.chat_data` [chat_id].
|
|
"""
|
|
|
|
@abstractmethod
|
|
def update_bot_data(self, data):
|
|
"""Will be called by the :class:`telegram.ext.Dispatcher` after a handler has
|
|
handled an update.
|
|
|
|
Args:
|
|
data (:obj:`dict`): The :attr:`telegram.ext.dispatcher.bot_data` .
|
|
"""
|
|
|
|
def flush(self):
|
|
"""Will be called by :class:`telegram.ext.Updater` upon receiving a stop signal. Gives the
|
|
persistence a chance to finish up saving or close a database connection gracefully. If this
|
|
is not of any importance just pass will be sufficient.
|
|
"""
|
|
pass
|