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 DictPersistence class."""
|
2022-05-19 12:47:53 +02:00
|
|
|
import json
|
2022-03-12 12:27:18 +01:00
|
|
|
from copy import deepcopy
|
2024-10-24 20:48:49 +02:00
|
|
|
from typing import TYPE_CHECKING, Any, Optional, cast
|
2022-03-12 12:27:18 +01:00
|
|
|
|
2022-05-05 09:27:54 +02:00
|
|
|
from telegram.ext import BasePersistence, PersistenceInput
|
|
|
|
from telegram.ext._utils.types import CDCData, ConversationDict, ConversationKey
|
2018-09-20 22:50:40 +02:00
|
|
|
|
2023-06-29 11:38:09 +02:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from telegram._utils.types import JSONDict
|
|
|
|
|
2018-09-20 22:50:40 +02:00
|
|
|
|
2024-10-24 20:48:49 +02:00
|
|
|
class DictPersistence(BasePersistence[dict[Any, Any], dict[Any, Any], dict[Any, Any]]):
|
2022-03-12 12:27:18 +01:00
|
|
|
"""Using Python's :obj:`dict` and :mod:`json` for making your bot persistent.
|
2018-09-20 22:50:40 +02:00
|
|
|
|
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-10-07 17:12:05 +02:00
|
|
|
Note:
|
2022-03-12 12:27:18 +01:00
|
|
|
* Data managed by :class:`DictPersistence` is in-memory only and will be lost when the bot
|
|
|
|
shuts down. This is, because :class:`DictPersistence` is mainly intended as starting
|
|
|
|
point for custom persistence classes that need to JSON-serialize the stored data before
|
|
|
|
writing them to file/database.
|
|
|
|
|
|
|
|
* This implementation of :class:`BasePersistence` does not handle data that cannot be
|
|
|
|
serialized by :func:`json.dumps`.
|
2020-07-13 21:52:26 +02:00
|
|
|
|
2023-01-01 16:24:00 +01:00
|
|
|
.. seealso:: :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
|
2021-08-13 16:18:42 +02:00
|
|
|
The parameters and attributes ``store_*_data`` were replaced by :attr:`store_data`.
|
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.
|
2021-06-06 12:16:23 +02:00
|
|
|
user_data_json (:obj:`str`, optional): JSON string that will be used to reconstruct
|
2018-09-20 22:50:40 +02:00
|
|
|
user_data on creating this persistence. Default is ``""``.
|
2021-06-06 12:16:23 +02:00
|
|
|
chat_data_json (:obj:`str`, optional): JSON string that will be used to reconstruct
|
2018-09-20 22:50:40 +02:00
|
|
|
chat_data on creating this persistence. Default is ``""``.
|
2021-06-06 12:16:23 +02:00
|
|
|
bot_data_json (:obj:`str`, optional): JSON string that will be used to reconstruct
|
2020-02-02 22:20:31 +01:00
|
|
|
bot_data on creating this persistence. Default is ``""``.
|
2022-04-24 12:38:09 +02:00
|
|
|
conversations_json (:obj:`str`, optional): JSON string that will be used to reconstruct
|
|
|
|
conversation on creating this persistence. Default is ``""``.
|
2022-06-09 17:08:54 +02:00
|
|
|
callback_data_json (:obj:`str`, optional): JSON string that will be used to reconstruct
|
2021-06-06 11:48:48 +02:00
|
|
|
callback_data on creating this persistence. Default is ``""``.
|
|
|
|
|
|
|
|
.. versionadded:: 13.6
|
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
|
|
|
|
wait between two consecutive runs of updating the persistence. Defaults to 60 seconds.
|
|
|
|
|
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.
|
2018-09-20 22:50:40 +02:00
|
|
|
"""
|
|
|
|
|
2021-05-29 16:18:16 +02:00
|
|
|
__slots__ = (
|
|
|
|
"_bot_data",
|
|
|
|
"_bot_data_json",
|
2024-02-05 19:24:00 +01:00
|
|
|
"_callback_data",
|
2021-06-06 11:48:48 +02:00
|
|
|
"_callback_data_json",
|
2024-02-05 19:24:00 +01:00
|
|
|
"_chat_data",
|
|
|
|
"_chat_data_json",
|
|
|
|
"_conversations",
|
2021-05-29 16:18:16 +02:00
|
|
|
"_conversations_json",
|
2024-02-05 19:24:00 +01:00
|
|
|
"_user_data",
|
|
|
|
"_user_data_json",
|
2021-05-29 16:18:16 +02:00
|
|
|
)
|
|
|
|
|
2020-02-02 22:20:31 +01:00
|
|
|
def __init__(
|
|
|
|
self,
|
2023-05-18 07:57:59 +02:00
|
|
|
store_data: Optional[PersistenceInput] = None,
|
2020-10-06 19:28:40 +02:00
|
|
|
user_data_json: str = "",
|
|
|
|
chat_data_json: str = "",
|
|
|
|
bot_data_json: str = "",
|
|
|
|
conversations_json: str = "",
|
2021-06-06 11:48:48 +02:00
|
|
|
callback_data_json: str = "",
|
2022-04-24 12:38:09 +02:00
|
|
|
update_interval: float = 60,
|
2020-10-06 19:28:40 +02:00
|
|
|
):
|
2022-04-24 12:38:09 +02:00
|
|
|
super().__init__(store_data=store_data, update_interval=update_interval)
|
2018-09-20 22:50:40 +02:00
|
|
|
self._user_data = None
|
|
|
|
self._chat_data = None
|
2020-02-02 22:20:31 +01:00
|
|
|
self._bot_data = None
|
2021-06-06 11:48:48 +02:00
|
|
|
self._callback_data = None
|
2018-09-20 22:50:40 +02:00
|
|
|
self._conversations = None
|
2023-02-02 18:55:07 +01:00
|
|
|
self._user_data_json: Optional[str] = None
|
|
|
|
self._chat_data_json: Optional[str] = None
|
|
|
|
self._bot_data_json: Optional[str] = None
|
|
|
|
self._callback_data_json: Optional[str] = None
|
|
|
|
self._conversations_json: Optional[str] = None
|
2018-09-20 22:50:40 +02:00
|
|
|
if user_data_json:
|
|
|
|
try:
|
2021-09-22 16:49:10 +02:00
|
|
|
self._user_data = self._decode_user_chat_data_from_json(user_data_json)
|
2018-09-20 22:50:40 +02:00
|
|
|
self._user_data_json = user_data_json
|
2020-10-31 16:33:34 +01:00
|
|
|
except (ValueError, AttributeError) as exc:
|
|
|
|
raise TypeError("Unable to deserialize user_data_json. Not valid JSON") from exc
|
2018-09-20 22:50:40 +02:00
|
|
|
if chat_data_json:
|
|
|
|
try:
|
2021-09-22 16:49:10 +02:00
|
|
|
self._chat_data = self._decode_user_chat_data_from_json(chat_data_json)
|
2018-09-20 22:50:40 +02:00
|
|
|
self._chat_data_json = chat_data_json
|
2020-10-31 16:33:34 +01:00
|
|
|
except (ValueError, AttributeError) as exc:
|
|
|
|
raise TypeError("Unable to deserialize chat_data_json. Not valid JSON") from exc
|
2020-02-02 22:20:31 +01:00
|
|
|
if bot_data_json:
|
|
|
|
try:
|
|
|
|
self._bot_data = json.loads(bot_data_json)
|
|
|
|
self._bot_data_json = bot_data_json
|
2020-10-31 16:33:34 +01:00
|
|
|
except (ValueError, AttributeError) as exc:
|
|
|
|
raise TypeError("Unable to deserialize bot_data_json. Not valid JSON") from exc
|
2020-02-02 22:20:31 +01:00
|
|
|
if not isinstance(self._bot_data, dict):
|
|
|
|
raise TypeError("bot_data_json must be serialized dict")
|
2021-06-06 11:48:48 +02:00
|
|
|
if callback_data_json:
|
|
|
|
try:
|
|
|
|
data = json.loads(callback_data_json)
|
|
|
|
except (ValueError, AttributeError) as exc:
|
|
|
|
raise TypeError(
|
|
|
|
"Unable to deserialize callback_data_json. Not valid JSON"
|
|
|
|
) from exc
|
|
|
|
# We are a bit more thorough with the checking of the format here, because it's
|
|
|
|
# more complicated than for the other things
|
|
|
|
try:
|
|
|
|
if data is None:
|
|
|
|
self._callback_data = None
|
|
|
|
else:
|
|
|
|
self._callback_data = cast(
|
|
|
|
CDCData,
|
|
|
|
([(one, float(two), three) for one, two, three in data[0]], data[1]),
|
|
|
|
)
|
|
|
|
self._callback_data_json = callback_data_json
|
|
|
|
except (ValueError, IndexError) as exc:
|
|
|
|
raise TypeError("callback_data_json is not in the required format") from exc
|
|
|
|
if self._callback_data is not None and (
|
|
|
|
not all(
|
|
|
|
isinstance(entry[2], dict) and isinstance(entry[0], str)
|
|
|
|
for entry in self._callback_data[0]
|
|
|
|
)
|
|
|
|
or not isinstance(self._callback_data[1], dict)
|
|
|
|
):
|
|
|
|
raise TypeError("callback_data_json is not in the required format")
|
2018-09-20 22:50:40 +02:00
|
|
|
|
|
|
|
if conversations_json:
|
|
|
|
try:
|
2021-09-22 16:49:10 +02:00
|
|
|
self._conversations = self._decode_conversations_from_json(conversations_json)
|
2018-09-20 22:50:40 +02:00
|
|
|
self._conversations_json = conversations_json
|
2020-10-31 16:33:34 +01:00
|
|
|
except (ValueError, AttributeError) as exc:
|
|
|
|
raise TypeError(
|
|
|
|
"Unable to deserialize conversations_json. Not valid JSON"
|
|
|
|
) from exc
|
2018-09-20 22:50:40 +02:00
|
|
|
|
|
|
|
@property
|
2024-10-24 20:48:49 +02:00
|
|
|
def user_data(self) -> Optional[dict[int, dict[Any, Any]]]:
|
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
|
|
|
""":obj:`dict`: The user_data as a dict."""
|
2018-09-20 22:50:40 +02:00
|
|
|
return self._user_data
|
|
|
|
|
|
|
|
@property
|
2020-10-06 19:28:40 +02:00
|
|
|
def user_data_json(self) -> str:
|
2018-09-20 22:50:40 +02:00
|
|
|
""":obj:`str`: The user_data serialized as a JSON-string."""
|
|
|
|
if self._user_data_json:
|
|
|
|
return self._user_data_json
|
2020-10-31 16:33:34 +01:00
|
|
|
return json.dumps(self.user_data)
|
2018-09-20 22:50:40 +02:00
|
|
|
|
|
|
|
@property
|
2024-10-24 20:48:49 +02:00
|
|
|
def chat_data(self) -> Optional[dict[int, dict[Any, Any]]]:
|
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
|
|
|
""":obj:`dict`: The chat_data as a dict."""
|
2018-09-20 22:50:40 +02:00
|
|
|
return self._chat_data
|
|
|
|
|
|
|
|
@property
|
2020-10-06 19:28:40 +02:00
|
|
|
def chat_data_json(self) -> str:
|
2018-09-20 22:50:40 +02:00
|
|
|
""":obj:`str`: The chat_data serialized as a JSON-string."""
|
|
|
|
if self._chat_data_json:
|
|
|
|
return self._chat_data_json
|
2020-10-31 16:33:34 +01:00
|
|
|
return json.dumps(self.chat_data)
|
2018-09-20 22:50:40 +02:00
|
|
|
|
2020-02-02 22:20:31 +01:00
|
|
|
@property
|
2024-10-24 20:48:49 +02:00
|
|
|
def bot_data(self) -> Optional[dict[Any, Any]]:
|
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
|
|
|
""":obj:`dict`: The bot_data as a dict."""
|
2020-02-02 22:20:31 +01:00
|
|
|
return self._bot_data
|
|
|
|
|
|
|
|
@property
|
2020-10-06 19:28:40 +02:00
|
|
|
def bot_data_json(self) -> str:
|
2020-02-02 22:20:31 +01:00
|
|
|
""":obj:`str`: The bot_data serialized as a JSON-string."""
|
|
|
|
if self._bot_data_json:
|
|
|
|
return self._bot_data_json
|
2020-10-31 16:33:34 +01:00
|
|
|
return json.dumps(self.bot_data)
|
2020-02-02 22:20:31 +01:00
|
|
|
|
2018-09-20 22:50:40 +02:00
|
|
|
@property
|
2021-06-06 11:48:48 +02:00
|
|
|
def callback_data(self) -> Optional[CDCData]:
|
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`]]: The metadata on the stored callback data.
|
2021-06-06 11:48:48 +02:00
|
|
|
|
|
|
|
.. versionadded:: 13.6
|
|
|
|
"""
|
|
|
|
return self._callback_data
|
|
|
|
|
|
|
|
@property
|
|
|
|
def callback_data_json(self) -> str:
|
2022-02-01 17:50:17 +01:00
|
|
|
""":obj:`str`: The metadata on the stored callback data as a JSON-string.
|
2021-06-06 11:48:48 +02:00
|
|
|
|
|
|
|
.. versionadded:: 13.6
|
|
|
|
"""
|
|
|
|
if self._callback_data_json:
|
|
|
|
return self._callback_data_json
|
|
|
|
return json.dumps(self.callback_data)
|
|
|
|
|
|
|
|
@property
|
2024-10-24 20:48:49 +02:00
|
|
|
def conversations(self) -> Optional[dict[str, ConversationDict]]:
|
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
|
|
|
""":obj:`dict`: The conversations as a dict."""
|
2018-09-20 22:50:40 +02:00
|
|
|
return self._conversations
|
|
|
|
|
|
|
|
@property
|
2020-10-06 19:28:40 +02:00
|
|
|
def conversations_json(self) -> str:
|
2018-09-20 22:50:40 +02:00
|
|
|
""":obj:`str`: The conversations serialized as a JSON-string."""
|
|
|
|
if self._conversations_json:
|
|
|
|
return self._conversations_json
|
2022-04-24 12:38:09 +02:00
|
|
|
if self.conversations:
|
|
|
|
return self._encode_conversations_to_json(self.conversations)
|
|
|
|
return json.dumps(self.conversations)
|
2018-09-20 22:50:40 +02:00
|
|
|
|
2024-10-24 20:48:49 +02:00
|
|
|
async def get_user_data(self) -> dict[int, dict[object, object]]:
|
2022-02-01 17:50:17 +01:00
|
|
|
"""Returns the user_data created from the ``user_data_json`` or an empty :obj:`dict`.
|
2018-09-20 22:50:40 +02:00
|
|
|
|
|
|
|
Returns:
|
2022-02-01 17:50:17 +01:00
|
|
|
:obj:`dict`: The restored user data.
|
2018-09-20 22:50:40 +02:00
|
|
|
"""
|
2021-06-06 11:48:48 +02:00
|
|
|
if self.user_data is None:
|
2022-02-01 17:50:17 +01:00
|
|
|
self._user_data = {}
|
2022-03-12 12:27:18 +01:00
|
|
|
return deepcopy(self.user_data) # type: ignore[arg-type]
|
2018-09-20 22:50:40 +02:00
|
|
|
|
2024-10-24 20:48:49 +02:00
|
|
|
async def get_chat_data(self) -> dict[int, dict[object, object]]:
|
2022-02-01 17:50:17 +01:00
|
|
|
"""Returns the chat_data created from the ``chat_data_json`` or an empty :obj:`dict`.
|
2018-09-20 22:50:40 +02:00
|
|
|
|
|
|
|
Returns:
|
2022-02-01 17:50:17 +01:00
|
|
|
:obj:`dict`: The restored chat data.
|
2018-09-20 22:50:40 +02:00
|
|
|
"""
|
2021-06-06 11:48:48 +02:00
|
|
|
if self.chat_data is None:
|
2022-02-01 17:50:17 +01:00
|
|
|
self._chat_data = {}
|
2022-03-12 12:27:18 +01:00
|
|
|
return deepcopy(self.chat_data) # type: ignore[arg-type]
|
2018-09-20 22:50:40 +02:00
|
|
|
|
2024-10-24 20:48:49 +02:00
|
|
|
async def get_bot_data(self) -> dict[object, object]:
|
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
|
|
|
"""Returns the bot_data created from the ``bot_data_json`` or an empty :obj:`dict`.
|
2020-02-02 22:20:31 +01:00
|
|
|
|
|
|
|
Returns:
|
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
|
|
|
:obj:`dict`: The restored bot data.
|
2020-02-02 22:20:31 +01:00
|
|
|
"""
|
2021-06-06 11:48:48 +02:00
|
|
|
if self.bot_data is None:
|
2020-02-02 22:20:31 +01:00
|
|
|
self._bot_data = {}
|
2022-03-12 12:27:18 +01:00
|
|
|
return deepcopy(self.bot_data) # type: ignore[arg-type]
|
2020-02-02 22:20:31 +01:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def get_callback_data(self) -> Optional[CDCData]:
|
2021-06-06 11:48:48 +02:00
|
|
|
"""Returns the callback_data created from the ``callback_data_json`` or :obj:`None`.
|
|
|
|
|
|
|
|
.. versionadded:: 13.6
|
|
|
|
|
|
|
|
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`]]: The restored metadata or :obj:`None`, \
|
2021-10-10 15:10:21 +02:00
|
|
|
if no data was stored.
|
2021-06-06 11:48:48 +02:00
|
|
|
"""
|
|
|
|
if self.callback_data is None:
|
|
|
|
self._callback_data = None
|
|
|
|
return None
|
2022-04-24 12:38:09 +02:00
|
|
|
return deepcopy(self.callback_data)
|
2021-06-06 11:48:48 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def get_conversations(self, name: str) -> ConversationDict:
|
2018-09-20 22:50:40 +02:00
|
|
|
"""Returns the conversations created from the ``conversations_json`` or an empty
|
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
|
|
|
:obj:`dict`.
|
2018-09-20 22:50:40 +02:00
|
|
|
|
|
|
|
Returns:
|
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
|
|
|
:obj:`dict`: The restored conversations data.
|
2018-09-20 22:50:40 +02:00
|
|
|
"""
|
2021-06-06 11:48:48 +02:00
|
|
|
if self.conversations is None:
|
2018-09-20 22:50:40 +02:00
|
|
|
self._conversations = {}
|
2020-10-06 19:28:40 +02:00
|
|
|
return self.conversations.get(name, {}).copy() # type: ignore[union-attr]
|
2018-09-20 22:50:40 +02:00
|
|
|
|
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:
|
2018-09-20 22:50:40 +02:00
|
|
|
"""Will update the conversations for the given handler.
|
|
|
|
|
|
|
|
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-02-09 17:30:16 +01:00
|
|
|
new_state (:obj:`tuple` | :class:`object`): The new state for the given key.
|
2018-09-20 22:50:40 +02:00
|
|
|
"""
|
2020-10-06 19:28:40 +02:00
|
|
|
if not self._conversations:
|
|
|
|
self._conversations = {}
|
2018-09-20 22:50:40 +02:00
|
|
|
if self._conversations.setdefault(name, {}).get(key) == new_state:
|
|
|
|
return
|
|
|
|
self._conversations[name][key] = new_state
|
|
|
|
self._conversations_json = None
|
|
|
|
|
2024-10-24 20:48:49 +02:00
|
|
|
async def update_user_data(self, user_id: int, data: dict[Any, Any]) -> None:
|
2018-09-20 22:50:40 +02:00
|
|
|
"""Will update the user_data (if changed).
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user_id (:obj:`int`): The user the data might have been changed for.
|
2022-04-24 12:38:09 +02:00
|
|
|
data (:obj:`dict`): The :attr:`telegram.ext.Application.user_data` ``[user_id]``.
|
2018-09-20 22:50:40 +02:00
|
|
|
"""
|
2020-04-10 13:23:13 +02:00
|
|
|
if self._user_data is None:
|
2022-02-01 17:50:17 +01:00
|
|
|
self._user_data = {}
|
2018-09-20 22:50:40 +02:00
|
|
|
if self._user_data.get(user_id) == data:
|
|
|
|
return
|
2022-04-24 12:38:09 +02:00
|
|
|
self._user_data[user_id] = data
|
2018-09-20 22:50:40 +02:00
|
|
|
self._user_data_json = None
|
|
|
|
|
2024-10-24 20:48:49 +02:00
|
|
|
async def update_chat_data(self, chat_id: int, data: dict[Any, Any]) -> None:
|
2018-09-20 22:50:40 +02:00
|
|
|
"""Will update the chat_data (if changed).
|
|
|
|
|
|
|
|
Args:
|
|
|
|
chat_id (:obj:`int`): The chat the data might have been changed for.
|
2022-04-24 12:38:09 +02:00
|
|
|
data (:obj:`dict`): The :attr:`telegram.ext.Application.chat_data` ``[chat_id]``.
|
2018-09-20 22:50:40 +02:00
|
|
|
"""
|
2020-04-10 13:23:13 +02:00
|
|
|
if self._chat_data is None:
|
2022-02-01 17:50:17 +01:00
|
|
|
self._chat_data = {}
|
2018-09-20 22:50:40 +02:00
|
|
|
if self._chat_data.get(chat_id) == data:
|
|
|
|
return
|
2022-04-24 12:38:09 +02:00
|
|
|
self._chat_data[chat_id] = data
|
2018-09-20 22:50:40 +02:00
|
|
|
self._chat_data_json = None
|
2020-02-02 22:20:31 +01:00
|
|
|
|
2024-10-24 20:48:49 +02:00
|
|
|
async def update_bot_data(self, data: dict[Any, Any]) -> None:
|
2020-02-02 22:20:31 +01:00
|
|
|
"""Will update the bot_data (if changed).
|
|
|
|
|
|
|
|
Args:
|
2022-04-24 12:38:09 +02:00
|
|
|
data (:obj:`dict`): The :attr:`telegram.ext.Application.bot_data`.
|
2020-02-02 22:20:31 +01:00
|
|
|
"""
|
|
|
|
if self._bot_data == data:
|
|
|
|
return
|
2022-04-24 12:38:09 +02:00
|
|
|
self._bot_data = data
|
2020-02-02 22:20:31 +01:00
|
|
|
self._bot_data_json = None
|
2021-06-06 10:37:53 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def update_callback_data(self, data: CDCData) -> None:
|
2021-06-06 11:48:48 +02:00
|
|
|
"""Will update the callback_data (if changed).
|
|
|
|
|
|
|
|
.. versionadded:: 13.6
|
|
|
|
|
|
|
|
Args:
|
2024-10-24 20:48:49 +02:00
|
|
|
data (tuple[list[tuple[:obj:`str`, :obj:`float`, dict[:obj:`str`, :class:`object`]]], \
|
|
|
|
dict[:obj:`str`, :obj:`str`]]): The relevant data to restore
|
2021-07-01 17:34:23 +02:00
|
|
|
:class:`telegram.ext.CallbackDataCache`.
|
2021-06-06 11:48:48 +02:00
|
|
|
"""
|
|
|
|
if self._callback_data == data:
|
|
|
|
return
|
2022-04-24 12:38:09 +02:00
|
|
|
self._callback_data = data
|
2021-06-06 11:48:48 +02:00
|
|
|
self._callback_data_json = None
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def drop_chat_data(self, chat_id: int) -> None:
|
2022-01-19 17:27:02 +01:00
|
|
|
"""Will delete the specified key from the :attr:`chat_data`.
|
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
if self._chat_data is None:
|
|
|
|
return
|
|
|
|
self._chat_data.pop(chat_id, None)
|
|
|
|
self._chat_data_json = None
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def drop_user_data(self, user_id: int) -> None:
|
2022-01-19 17:27:02 +01:00
|
|
|
"""Will delete the specified key from the :attr:`user_data`.
|
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
if self._user_data is None:
|
|
|
|
return
|
|
|
|
self._user_data.pop(user_id, None)
|
|
|
|
self._user_data_json = None
|
|
|
|
|
2024-10-24 20:48:49 +02:00
|
|
|
async def refresh_user_data(self, user_id: int, user_data: dict[Any, Any]) -> None:
|
2021-06-06 10:37:53 +02:00
|
|
|
"""Does nothing.
|
|
|
|
|
|
|
|
.. versionadded:: 13.6
|
|
|
|
.. seealso:: :meth:`telegram.ext.BasePersistence.refresh_user_data`
|
|
|
|
"""
|
|
|
|
|
2024-10-24 20:48:49 +02:00
|
|
|
async def refresh_chat_data(self, chat_id: int, chat_data: dict[Any, Any]) -> None:
|
2021-06-06 10:37:53 +02:00
|
|
|
"""Does nothing.
|
|
|
|
|
|
|
|
.. versionadded:: 13.6
|
|
|
|
.. seealso:: :meth:`telegram.ext.BasePersistence.refresh_chat_data`
|
|
|
|
"""
|
|
|
|
|
2024-10-24 20:48:49 +02:00
|
|
|
async def refresh_bot_data(self, bot_data: dict[Any, Any]) -> None:
|
2021-06-06 10:37:53 +02:00
|
|
|
"""Does nothing.
|
|
|
|
|
|
|
|
.. versionadded:: 13.6
|
|
|
|
.. seealso:: :meth:`telegram.ext.BasePersistence.refresh_bot_data`
|
|
|
|
"""
|
2021-08-12 08:58:32 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def flush(self) -> None:
|
2021-08-12 08:58:32 +02:00
|
|
|
"""Does nothing.
|
|
|
|
|
2022-05-06 17:15:23 +02:00
|
|
|
.. versionadded:: 20.0
|
2021-08-12 08:58:32 +02:00
|
|
|
.. seealso:: :meth:`telegram.ext.BasePersistence.flush`
|
|
|
|
"""
|
2021-09-22 16:49:10 +02:00
|
|
|
|
|
|
|
@staticmethod
|
2024-10-24 20:48:49 +02:00
|
|
|
def _encode_conversations_to_json(conversations: dict[str, ConversationDict]) -> str:
|
2021-09-22 16:49:10 +02:00
|
|
|
"""Helper method to encode a conversations dict (that uses tuples as keys) to a
|
|
|
|
JSON-serializable way. Use :meth:`self._decode_conversations_from_json` to decode.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
conversations (:obj:`dict`): The conversations dict to transform to JSON.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
:obj:`str`: The JSON-serialized conversations dict
|
|
|
|
"""
|
2024-10-24 20:48:49 +02:00
|
|
|
tmp: dict[str, JSONDict] = {}
|
2021-09-22 16:49:10 +02:00
|
|
|
for handler, states in conversations.items():
|
|
|
|
tmp[handler] = {}
|
|
|
|
for key, state in states.items():
|
|
|
|
tmp[handler][json.dumps(key)] = state
|
|
|
|
return json.dumps(tmp)
|
|
|
|
|
|
|
|
@staticmethod
|
2024-10-24 20:48:49 +02:00
|
|
|
def _decode_conversations_from_json(json_string: str) -> dict[str, ConversationDict]:
|
2021-09-22 16:49:10 +02:00
|
|
|
"""Helper method to decode a conversations dict (that uses tuples as keys) from a
|
|
|
|
JSON-string created with :meth:`self._encode_conversations_to_json`.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
json_string (:obj:`str`): The conversations dict as JSON string.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
:obj:`dict`: The conversations dict after decoding
|
|
|
|
"""
|
|
|
|
tmp = json.loads(json_string)
|
2024-10-24 20:48:49 +02:00
|
|
|
conversations: dict[str, ConversationDict] = {}
|
2021-09-22 16:49:10 +02:00
|
|
|
for handler, states in tmp.items():
|
|
|
|
conversations[handler] = {}
|
|
|
|
for key, state in states.items():
|
|
|
|
conversations[handler][tuple(json.loads(key))] = state
|
|
|
|
return conversations
|
|
|
|
|
|
|
|
@staticmethod
|
2024-10-24 20:48:49 +02:00
|
|
|
def _decode_user_chat_data_from_json(data: str) -> dict[int, dict[object, object]]:
|
2021-09-22 16:49:10 +02:00
|
|
|
"""Helper method to decode chat or user data (that uses ints as keys) from a
|
|
|
|
JSON-string.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
data (:obj:`str`): The user/chat_data dict as JSON string.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
:obj:`dict`: The user/chat_data defaultdict after decoding
|
|
|
|
"""
|
2024-10-24 20:48:49 +02:00
|
|
|
tmp: dict[int, dict[object, object]] = {}
|
2021-09-22 16:49:10 +02:00
|
|
|
decoded_data = json.loads(data)
|
|
|
|
for user, user_data in decoded_data.items():
|
2023-03-25 19:18:04 +01:00
|
|
|
int_user_id = int(user)
|
|
|
|
tmp[int_user_id] = {}
|
2021-09-22 16:49:10 +02:00
|
|
|
for key, value in user_data.items():
|
|
|
|
try:
|
2023-03-25 19:18:04 +01:00
|
|
|
_id = int(key)
|
2021-09-22 16:49:10 +02:00
|
|
|
except ValueError:
|
2023-03-25 19:18:04 +01:00
|
|
|
_id = key
|
|
|
|
tmp[int_user_id][_id] = value
|
2021-09-22 16:49:10 +02:00
|
|
|
return tmp
|