3131
3232
3333class DictPersistence (BasePersistence ):
34- """Using python's dicts and json for making you bot persistent.
34+ """Using python's dicts and json for making your bot persistent.
3535
3636 Attributes:
3737 store_user_data (:obj:`bool`): Whether user_data should be saved by this
3838 persistence class.
3939 store_chat_data (:obj:`bool`): Whether chat_data should be saved by this
4040 persistence class.
41+ store_bot_data (:obj:`bool`): Whether bot_data should be saved by this
42+ persistence class.
4143
4244 Args:
4345 store_user_data (:obj:`bool`, optional): Whether user_data should be saved by this
4446 persistence class. Default is ``True``.
4547 store_chat_data (:obj:`bool`, optional): Whether user_data should be saved by this
4648 persistence class. Default is ``True``.
49+ store_bot_data (:obj:`bool`, optional): Whether bot_data should be saved by this
50+ persistence class. Default is ``True`` .
4751 user_data_json (:obj:`str`, optional): Json string that will be used to reconstruct
4852 user_data on creating this persistence. Default is ``""``.
4953 chat_data_json (:obj:`str`, optional): Json string that will be used to reconstruct
5054 chat_data on creating this persistence. Default is ``""``.
55+ bot_data_json (:obj:`str`, optional): Json string that will be used to reconstruct
56+ bot_data on creating this persistence. Default is ``""``.
5157 conversations_json (:obj:`str`, optional): Json string that will be used to reconstruct
5258 conversation on creating this persistence. Default is ``""``.
5359 """
5460
55- def __init__ (self , store_user_data = True , store_chat_data = True , user_data_json = '' ,
56- chat_data_json = '' , conversations_json = '' ):
57- self .store_user_data = store_user_data
58- self .store_chat_data = store_chat_data
61+ def __init__ (self ,
62+ store_user_data = True ,
63+ store_chat_data = True ,
64+ store_bot_data = True ,
65+ user_data_json = '' ,
66+ chat_data_json = '' ,
67+ bot_data_json = '' ,
68+ conversations_json = '' ):
69+ super (DictPersistence , self ).__init__ (store_user_data = store_user_data ,
70+ store_chat_data = store_chat_data ,
71+ store_bot_data = store_bot_data )
5972 self ._user_data = None
6073 self ._chat_data = None
74+ self ._bot_data = None
6175 self ._conversations = None
6276 self ._user_data_json = None
6377 self ._chat_data_json = None
78+ self ._bot_data_json = None
6479 self ._conversations_json = None
6580 if user_data_json :
6681 try :
@@ -74,6 +89,14 @@ def __init__(self, store_user_data=True, store_chat_data=True, user_data_json=''
7489 self ._chat_data_json = chat_data_json
7590 except (ValueError , AttributeError ):
7691 raise TypeError ("Unable to deserialize chat_data_json. Not valid JSON" )
92+ if bot_data_json :
93+ try :
94+ self ._bot_data = json .loads (bot_data_json )
95+ self ._bot_data_json = bot_data_json
96+ except (ValueError , AttributeError ):
97+ raise TypeError ("Unable to deserialize bot_data_json. Not valid JSON" )
98+ if not isinstance (self ._bot_data , dict ):
99+ raise TypeError ("bot_data_json must be serialized dict" )
77100
78101 if conversations_json :
79102 try :
@@ -108,6 +131,19 @@ def chat_data_json(self):
108131 else :
109132 return json .dumps (self .chat_data )
110133
134+ @property
135+ def bot_data (self ):
136+ """:obj:`dict`: The bot_data as a dict"""
137+ return self ._bot_data
138+
139+ @property
140+ def bot_data_json (self ):
141+ """:obj:`str`: The bot_data serialized as a JSON-string."""
142+ if self ._bot_data_json :
143+ return self ._bot_data_json
144+ else :
145+ return json .dumps (self .bot_data )
146+
111147 @property
112148 def conversations (self ):
113149 """:obj:`dict`: The conversations as a dict"""
@@ -145,6 +181,18 @@ def get_chat_data(self):
145181 self ._chat_data = defaultdict (dict )
146182 return deepcopy (self .chat_data )
147183
184+ def get_bot_data (self ):
185+ """Returns the bot_data created from the ``bot_data_json`` or an empty dict.
186+
187+ Returns:
188+ :obj:`defaultdict`: The restored user data.
189+ """
190+ if self .bot_data :
191+ pass
192+ else :
193+ self ._bot_data = {}
194+ return deepcopy (self .bot_data )
195+
148196 def get_conversations (self , name ):
149197 """Returns the conversations created from the ``conversations_json`` or an empty
150198 defaultdict.
@@ -194,3 +242,14 @@ def update_chat_data(self, chat_id, data):
194242 return
195243 self ._chat_data [chat_id ] = data
196244 self ._chat_data_json = None
245+
246+ def update_bot_data (self , data ):
247+ """Will update the bot_data (if changed).
248+
249+ Args:
250+ data (:obj:`dict`): The :attr:`telegram.ext.dispatcher.bot_data`.
251+ """
252+ if self ._bot_data == data :
253+ return
254+ self ._bot_data = data .copy ()
255+ self ._bot_data_json = None
0 commit comments