7
7
import threading
8
8
import time
9
9
import traceback
10
- from typing import Any , Callable , List , Optional , Union
10
+ from typing import Any , Callable , List , Optional , Union , Dict
11
11
12
12
# these imports are used to avoid circular import error
13
13
import telebot .util
@@ -168,7 +168,8 @@ def __init__(
168
168
disable_notification : Optional [bool ]= None ,
169
169
protect_content : Optional [bool ]= None ,
170
170
allow_sending_without_reply : Optional [bool ]= None ,
171
- colorful_logs : Optional [bool ]= False
171
+ colorful_logs : Optional [bool ]= False ,
172
+ token_check : Optional [bool ]= True
172
173
):
173
174
174
175
# update-related
@@ -186,6 +187,11 @@ def __init__(
186
187
self .webhook_listener = None
187
188
self ._user = None
188
189
190
+ # token check
191
+ if token_check :
192
+ self ._user = self .get_me ()
193
+ self .bot_id = self ._user .id
194
+
189
195
# logs-related
190
196
if colorful_logs :
191
197
try :
@@ -280,6 +286,8 @@ def __init__(
280
286
self .threaded = threaded
281
287
if self .threaded :
282
288
self .worker_pool = util .ThreadPool (self , num_threads = num_threads )
289
+
290
+
283
291
284
292
@property
285
293
def user (self ) -> types .User :
@@ -6572,7 +6580,9 @@ def setup_middleware(self, middleware: BaseMiddleware):
6572
6580
self .middlewares .append (middleware )
6573
6581
6574
6582
6575
- def set_state (self , user_id : int , state : Union [int , str , State ], chat_id : Optional [int ]= None ) -> None :
6583
+ def set_state (self , user_id : int , state : Union [int , str , State ], chat_id : Optional [int ]= None ,
6584
+ business_connection_id : Optional [str ]= None , message_thread_id : Optional [int ]= None ,
6585
+ bot_id : Optional [int ]= None ) -> None :
6576
6586
"""
6577
6587
Sets a new state of a user.
6578
6588
@@ -6591,14 +6601,29 @@ def set_state(self, user_id: int, state: Union[int, str, State], chat_id: Option
6591
6601
:param chat_id: Chat's identifier
6592
6602
:type chat_id: :obj:`int`
6593
6603
6604
+ :param bot_id: Bot's identifier
6605
+ :type bot_id: :obj:`int`
6606
+
6607
+ :param business_connection_id: Business identifier
6608
+ :type business_connection_id: :obj:`str`
6609
+
6610
+ :param message_thread_id: Identifier of the message thread
6611
+ :type message_thread_id: :obj:`int`
6612
+
6594
6613
:return: None
6595
6614
"""
6596
6615
if chat_id is None :
6597
6616
chat_id = user_id
6598
- self .current_states .set_state (chat_id , user_id , state )
6617
+ if bot_id is None :
6618
+ bot_id = self .bot_id
6619
+ self .current_states .set_state (
6620
+ chat_id = chat_id , user_id = user_id , state = state , bot_id = bot_id ,
6621
+ business_connection_id = business_connection_id , message_thread_id = message_thread_id )
6599
6622
6600
6623
6601
- def reset_data (self , user_id : int , chat_id : Optional [int ]= None ):
6624
+ def reset_data (self , user_id : int , chat_id : Optional [int ]= None ,
6625
+ business_connection_id : Optional [str ]= None ,
6626
+ message_thread_id : Optional [int ]= None , bot_id : Optional [int ]= None ) -> None :
6602
6627
"""
6603
6628
Reset data for a user in chat.
6604
6629
@@ -6608,14 +6633,27 @@ def reset_data(self, user_id: int, chat_id: Optional[int]=None):
6608
6633
:param chat_id: Chat's identifier
6609
6634
:type chat_id: :obj:`int`
6610
6635
6636
+ :param bot_id: Bot's identifier
6637
+ :type bot_id: :obj:`int`
6638
+
6639
+ :param business_connection_id: Business identifier
6640
+ :type business_connection_id: :obj:`str`
6641
+
6642
+ :param message_thread_id: Identifier of the message thread
6643
+ :type message_thread_id: :obj:`int`
6644
+
6611
6645
:return: None
6612
6646
"""
6613
6647
if chat_id is None :
6614
6648
chat_id = user_id
6615
- self .current_states .reset_data (chat_id , user_id )
6649
+ if bot_id is None :
6650
+ bot_id = self .bot_id
6651
+ self .current_states .reset_data (chat_id = chat_id , user_id = user_id , bot_id = bot_id ,
6652
+ business_connection_id = business_connection_id , message_thread_id = message_thread_id )
6616
6653
6617
6654
6618
- def delete_state (self , user_id : int , chat_id : Optional [int ]= None ) -> None :
6655
+ def delete_state (self , user_id : int , chat_id : Optional [int ]= None , business_connection_id : Optional [str ]= None ,
6656
+ message_thread_id : Optional [int ]= None , bot_id : Optional [int ]= None ) -> None :
6619
6657
"""
6620
6658
Delete the current state of a user.
6621
6659
@@ -6629,10 +6667,14 @@ def delete_state(self, user_id: int, chat_id: Optional[int]=None) -> None:
6629
6667
"""
6630
6668
if chat_id is None :
6631
6669
chat_id = user_id
6632
- self .current_states .delete_state (chat_id , user_id )
6670
+ if bot_id is None :
6671
+ bot_id = self .bot_id
6672
+ self .current_states .delete_state (chat_id = chat_id , user_id = user_id , bot_id = bot_id ,
6673
+ business_connection_id = business_connection_id , message_thread_id = message_thread_id )
6633
6674
6634
6675
6635
- def retrieve_data (self , user_id : int , chat_id : Optional [int ]= None ) -> Optional [Any ]:
6676
+ def retrieve_data (self , user_id : int , chat_id : Optional [int ]= None , business_connection_id : Optional [str ]= None ,
6677
+ message_thread_id : Optional [int ]= None , bot_id : Optional [int ]= None ) -> Optional [Dict [str , Any ]]:
6636
6678
"""
6637
6679
Returns context manager with data for a user in chat.
6638
6680
@@ -6642,15 +6684,30 @@ def retrieve_data(self, user_id: int, chat_id: Optional[int]=None) -> Optional[A
6642
6684
:param chat_id: Chat's unique identifier, defaults to user_id
6643
6685
:type chat_id: int, optional
6644
6686
6687
+ :param bot_id: Bot's identifier
6688
+ :type bot_id: int, optional
6689
+
6690
+ :param business_connection_id: Business identifier
6691
+ :type business_connection_id: str, optional
6692
+
6693
+ :param message_thread_id: Identifier of the message thread
6694
+ :type message_thread_id: int, optional
6695
+
6645
6696
:return: Context manager with data for a user in chat
6646
6697
:rtype: Optional[Any]
6647
6698
"""
6648
6699
if chat_id is None :
6649
6700
chat_id = user_id
6650
- return self .current_states .get_interactive_data (chat_id , user_id )
6701
+ if bot_id is None :
6702
+ bot_id = self .bot_id
6703
+ return self .current_states .get_interactive_data (chat_id = chat_id , user_id = user_id , bot_id = bot_id ,
6704
+ business_connection_id = business_connection_id ,
6705
+ message_thread_id = message_thread_id )
6651
6706
6652
6707
6653
- def get_state (self , user_id : int , chat_id : Optional [int ]= None ) -> Optional [Union [int , str , State ]]:
6708
+ def get_state (self , user_id : int , chat_id : Optional [int ]= None ,
6709
+ business_connection_id : Optional [str ]= None ,
6710
+ message_thread_id : Optional [int ]= None , bot_id : Optional [int ]= None ) -> Union [int , str ]:
6654
6711
"""
6655
6712
Gets current state of a user.
6656
6713
Not recommended to use this method. But it is ok for debugging.
@@ -6661,15 +6718,31 @@ def get_state(self, user_id: int, chat_id: Optional[int]=None) -> Optional[Union
6661
6718
:param chat_id: Chat's identifier
6662
6719
:type chat_id: :obj:`int`
6663
6720
6721
+ :param bot_id: Bot's identifier
6722
+ :type bot_id: :obj:`int`
6723
+
6724
+ :param business_connection_id: Business identifier
6725
+ :type business_connection_id: :obj:`str`
6726
+
6727
+ :param message_thread_id: Identifier of the message thread
6728
+ :type message_thread_id: :obj:`int`
6729
+
6664
6730
:return: state of a user
6665
6731
:rtype: :obj:`int` or :obj:`str` or :class:`telebot.types.State`
6666
6732
"""
6667
6733
if chat_id is None :
6668
6734
chat_id = user_id
6669
- return self .current_states .get_state (chat_id , user_id )
6735
+ if bot_id is None :
6736
+ bot_id = self .bot_id
6737
+ return self .current_states .get_state (chat_id = chat_id , user_id = user_id , bot_id = bot_id ,
6738
+ business_connection_id = business_connection_id , message_thread_id = message_thread_id )
6670
6739
6671
6740
6672
- def add_data (self , user_id : int , chat_id : Optional [int ]= None , ** kwargs ):
6741
+ def add_data (self , user_id : int , chat_id : Optional [int ]= None ,
6742
+ business_connection_id : Optional [str ]= None ,
6743
+ message_thread_id : Optional [int ]= None ,
6744
+ bot_id : Optional [int ]= None ,
6745
+ ** kwargs ) -> None :
6673
6746
"""
6674
6747
Add data to states.
6675
6748
@@ -6679,13 +6752,25 @@ def add_data(self, user_id: int, chat_id: Optional[int]=None, **kwargs):
6679
6752
:param chat_id: Chat's identifier
6680
6753
:type chat_id: :obj:`int`
6681
6754
6755
+ :param bot_id: Bot's identifier
6756
+ :type bot_id: :obj:`int`
6757
+
6758
+ :param business_connection_id: Business identifier
6759
+ :type business_connection_id: :obj:`str`
6760
+
6761
+ :param message_thread_id: Identifier of the message thread
6762
+ :type message_thread_id: :obj:`int`
6763
+
6682
6764
:param kwargs: Data to add
6683
6765
:return: None
6684
6766
"""
6685
6767
if chat_id is None :
6686
6768
chat_id = user_id
6769
+ if bot_id is None :
6770
+ bot_id = self .bot_id
6687
6771
for key , value in kwargs .items ():
6688
- self .current_states .set_data (chat_id , user_id , key , value )
6772
+ self .current_states .set_data (chat_id = chat_id , user_id = user_id , key = key , value = value , bot_id = bot_id ,
6773
+ business_connection_id = business_connection_id , message_thread_id = message_thread_id )
6689
6774
6690
6775
6691
6776
def register_next_step_handler_by_chat_id (
0 commit comments