Skip to content

Commit 93ce320

Browse files
committed
Fix states functions with new parameters
... and some minor issues.
1 parent f6906ec commit 93ce320

File tree

5 files changed

+109
-47
lines changed

5 files changed

+109
-47
lines changed

telebot/__init__.py

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5564,7 +5564,7 @@ def create_invoice_link(self,
55645564

55655565
# noinspection PyShadowingBuiltins
55665566
def send_poll(
5567-
self, chat_id: Union[int, str], question: str, options: List[types.InputPollOption],
5567+
self, chat_id: Union[int, str], question: str, options: List[Union[str, types.InputPollOption]],
55685568
is_anonymous: Optional[bool]=None, type: Optional[str]=None,
55695569
allows_multiple_answers: Optional[bool]=None,
55705570
correct_option_id: Optional[int]=None,
@@ -5600,7 +5600,7 @@ def send_poll(
56005600
:type question: :obj:`str`
56015601
56025602
:param options: A JSON-serialized list of 2-10 answer options
5603-
:type options: :obj:`list` of :obj:`InputPollOption`
5603+
:type options: :obj:`list` of :obj:`InputPollOption` | :obj:`list` of :obj:`str`
56045604
56055605
:param is_anonymous: True, if the poll needs to be anonymous, defaults to True
56065606
:type is_anonymous: :obj:`bool`
@@ -7578,9 +7578,8 @@ def set_state(self, user_id: int, state: Union[str, State], chat_id: Optional[in
75787578
chat_id = user_id
75797579
if bot_id is None:
75807580
bot_id = self.bot_id
7581-
return self.current_states.set_state(
7582-
chat_id=chat_id, user_id=user_id, state=state, bot_id=bot_id,
7583-
business_connection_id=business_connection_id, message_thread_id=message_thread_id)
7581+
return self.current_states.set_state(chat_id, user_id, state,
7582+
bot_id=bot_id, business_connection_id=business_connection_id, message_thread_id=message_thread_id)
75847583

75857584

75867585
def reset_data(self, user_id: int, chat_id: Optional[int]=None,
@@ -7611,8 +7610,8 @@ def reset_data(self, user_id: int, chat_id: Optional[int]=None,
76117610
chat_id = user_id
76127611
if bot_id is None:
76137612
bot_id = self.bot_id
7614-
return self.current_states.reset_data(chat_id=chat_id, user_id=user_id, bot_id=bot_id,
7615-
business_connection_id=business_connection_id, message_thread_id=message_thread_id)
7613+
return self.current_states.reset_data(chat_id, user_id,
7614+
bot_id=bot_id, business_connection_id=business_connection_id, message_thread_id=message_thread_id)
76167615

76177616

76187617
def delete_state(self, user_id: int, chat_id: Optional[int]=None, business_connection_id: Optional[str]=None,
@@ -7630,15 +7629,24 @@ def delete_state(self, user_id: int, chat_id: Optional[int]=None, business_conne
76307629
:param chat_id: Chat's identifier
76317630
:type chat_id: :obj:`int`
76327631
7632+
:param bot_id: Bot's identifier, defaults to current bot id
7633+
:type bot_id: :obj:`int`
7634+
7635+
:param business_connection_id: Business identifier
7636+
:type business_connection_id: :obj:`str`
7637+
7638+
:param message_thread_id: Identifier of the message thread
7639+
:type message_thread_id: :obj:`int`
7640+
76337641
:return: True on success
76347642
:rtype: :obj:`bool`
76357643
"""
76367644
if chat_id is None:
76377645
chat_id = user_id
76387646
if bot_id is None:
76397647
bot_id = self.bot_id
7640-
return self.current_states.delete_state(chat_id=chat_id, user_id=user_id, bot_id=bot_id,
7641-
business_connection_id=business_connection_id, message_thread_id=message_thread_id)
7648+
return self.current_states.delete_state(chat_id, user_id,
7649+
bot_id=bot_id, business_connection_id=business_connection_id, message_thread_id=message_thread_id)
76427650

76437651

76447652
def retrieve_data(self, user_id: int, chat_id: Optional[int]=None, business_connection_id: Optional[str]=None,
@@ -7668,9 +7676,8 @@ def retrieve_data(self, user_id: int, chat_id: Optional[int]=None, business_conn
76687676
chat_id = user_id
76697677
if bot_id is None:
76707678
bot_id = self.bot_id
7671-
return self.current_states.get_interactive_data(chat_id=chat_id, user_id=user_id, bot_id=bot_id,
7672-
business_connection_id=business_connection_id,
7673-
message_thread_id=message_thread_id)
7679+
return self.current_states.get_interactive_data(chat_id, user_id,
7680+
bot_id=bot_id, business_connection_id=business_connection_id, message_thread_id=message_thread_id)
76747681

76757682

76767683
def get_state(self, user_id: int, chat_id: Optional[int]=None,
@@ -7707,8 +7714,8 @@ def get_state(self, user_id: int, chat_id: Optional[int]=None,
77077714
chat_id = user_id
77087715
if bot_id is None:
77097716
bot_id = self.bot_id
7710-
return self.current_states.get_state(chat_id=chat_id, user_id=user_id, bot_id=bot_id,
7711-
business_connection_id=business_connection_id, message_thread_id=message_thread_id)
7717+
return self.current_states.get_state(chat_id, user_id,
7718+
bot_id=bot_id, business_connection_id=business_connection_id, message_thread_id=message_thread_id)
77127719

77137720

77147721
def add_data(self, user_id: int, chat_id: Optional[int]=None,
@@ -7742,8 +7749,8 @@ def add_data(self, user_id: int, chat_id: Optional[int]=None,
77427749
if bot_id is None:
77437750
bot_id = self.bot_id
77447751
for key, value in kwargs.items():
7745-
self.current_states.set_data(chat_id=chat_id, user_id=user_id, key=key, value=value, bot_id=bot_id,
7746-
business_connection_id=business_connection_id, message_thread_id=message_thread_id)
7752+
self.current_states.set_data(chat_id, user_id, key, value,
7753+
bot_id=bot_id, business_connection_id=business_connection_id, message_thread_id=message_thread_id)
77477754

77487755

77497756
def register_next_step_handler_by_chat_id(
@@ -9848,4 +9855,3 @@ def _notify_command_handlers(self, handlers, new_messages, update_type):
98489855
handlers=handlers,
98499856
middlewares=middlewares,
98509857
update_type=update_type)
9851-

telebot/async_telebot.py

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import sys
99

1010
# this imports are used to avoid circular import error
11+
# noinspection PyUnresolvedReferences
1112
import telebot.util
1213
import telebot.types
1314

@@ -7008,7 +7009,7 @@ async def create_invoice_link(self,
70087009

70097010
# noinspection PyShadowingBuiltins
70107011
async def send_poll(
7011-
self, chat_id: Union[int, str], question: str, options: List[types.InputPollOption],
7012+
self, chat_id: Union[int, str], question: str, options: List[Union[str, types.InputPollOption]],
70127013
is_anonymous: Optional[bool]=None, type: Optional[str]=None,
70137014
allows_multiple_answers: Optional[bool]=None,
70147015
correct_option_id: Optional[int]=None,
@@ -7044,7 +7045,7 @@ async def send_poll(
70447045
:type question: :obj:`str`
70457046
70467047
:param options: A JSON-serialized list of 2-10 answer options
7047-
:type options: :obj:`list` of :obj:`InputPollOption`
7048+
:type options: :obj:`list` of :obj:`InputPollOption` | :obj:`list` of :obj:`str`
70487049
70497050
:param is_anonymous: True, if the poll needs to be anonymous, defaults to True
70507051
:type is_anonymous: :obj:`bool`
@@ -8828,9 +8829,8 @@ async def set_state(self, user_id: int, state: Union[int, str, State], chat_id:
88288829
chat_id = user_id
88298830
if bot_id is None:
88308831
bot_id = self.bot_id
8831-
return await self.current_states.set_state(
8832-
chat_id=chat_id, user_id=user_id, state=state, bot_id=bot_id,
8833-
business_connection_id=business_connection_id, message_thread_id=message_thread_id)
8832+
return await self.current_states.set_state(chat_id, user_id, state,
8833+
bot_id=bot_id, business_connection_id=business_connection_id, message_thread_id=message_thread_id)
88348834

88358835

88368836
async def reset_data(self, user_id: int, chat_id: Optional[int]=None,
@@ -8861,8 +8861,8 @@ async def reset_data(self, user_id: int, chat_id: Optional[int]=None,
88618861
chat_id = user_id
88628862
if bot_id is None:
88638863
bot_id = self.bot_id
8864-
return await self.current_states.reset_data(chat_id=chat_id, user_id=user_id, bot_id=bot_id,
8865-
business_connection_id=business_connection_id, message_thread_id=message_thread_id)
8864+
return await self.current_states.reset_data(chat_id, user_id,
8865+
bot_id=bot_id, business_connection_id=business_connection_id, message_thread_id=message_thread_id)
88668866

88678867

88688868
async def delete_state(self, user_id: int, chat_id: Optional[int]=None, business_connection_id: Optional[str]=None,
@@ -8876,14 +8876,23 @@ async def delete_state(self, user_id: int, chat_id: Optional[int]=None, business
88768876
:param chat_id: Chat's identifier
88778877
:type chat_id: :obj:`int`
88788878
8879+
:param bot_id: Bot's identifier, defaults to current bot id
8880+
:type bot_id: :obj:`int`
8881+
8882+
:param business_connection_id: Business identifier
8883+
:type business_connection_id: :obj:`str`
8884+
8885+
:param message_thread_id: Identifier of the message thread
8886+
:type message_thread_id: :obj:`int`
8887+
88798888
:return: None
88808889
"""
88818890
if chat_id is None:
88828891
chat_id = user_id
88838892
if bot_id is None:
88848893
bot_id = self.bot_id
8885-
return await self.current_states.delete_state(chat_id=chat_id, user_id=user_id, bot_id=bot_id,
8886-
business_connection_id=business_connection_id, message_thread_id=message_thread_id)
8894+
return await self.current_states.delete_state(chat_id, user_id,
8895+
bot_id=bot_id, business_connection_id=business_connection_id, message_thread_id=message_thread_id)
88878896

88888897

88898898
def retrieve_data(self, user_id: int, chat_id: Optional[int]=None, business_connection_id: Optional[str]=None,
@@ -8913,9 +8922,8 @@ def retrieve_data(self, user_id: int, chat_id: Optional[int]=None, business_conn
89138922
chat_id = user_id
89148923
if bot_id is None:
89158924
bot_id = self.bot_id
8916-
return self.current_states.get_interactive_data(chat_id=chat_id, user_id=user_id, bot_id=bot_id,
8917-
business_connection_id=business_connection_id,
8918-
message_thread_id=message_thread_id)
8925+
return self.current_states.get_interactive_data(chat_id, user_id,
8926+
bot_id=bot_id, business_connection_id=business_connection_id, message_thread_id=message_thread_id)
89198927

89208928

89218929
async def get_state(self, user_id: int, chat_id: Optional[int]=None,
@@ -8952,8 +8960,8 @@ async def get_state(self, user_id: int, chat_id: Optional[int]=None,
89528960
chat_id = user_id
89538961
if bot_id is None:
89548962
bot_id = self.bot_id
8955-
return await self.current_states.get_state(chat_id=chat_id, user_id=user_id, bot_id=bot_id,
8956-
business_connection_id=business_connection_id, message_thread_id=message_thread_id)
8963+
return await self.current_states.get_state(chat_id, user_id,
8964+
bot_id=bot_id, business_connection_id=business_connection_id, message_thread_id=message_thread_id)
89578965

89588966

89598967
async def add_data(self, user_id: int, chat_id: Optional[int]=None,
@@ -8987,5 +8995,5 @@ async def add_data(self, user_id: int, chat_id: Optional[int]=None,
89878995
if bot_id is None:
89888996
bot_id = self.bot_id
89898997
for key, value in kwargs.items():
8990-
await self.current_states.set_data(chat_id=chat_id, user_id=user_id, key=key, value=value, bot_id=bot_id,
8991-
business_connection_id=business_connection_id, message_thread_id=message_thread_id)
8998+
await self.current_states.set_data(chat_id, user_id, key, value,
8999+
bot_id=bot_id, business_connection_id=business_connection_id, message_thread_id=message_thread_id)

telebot/asyncio_storage/base_storage.py

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ class StateStorageBase:
55
def __init__(self) -> None:
66
pass
77

8-
async def set_data(self, chat_id, user_id, key, value):
8+
async def set_data(self, chat_id, user_id, key, value,
9+
business_connection_id=None,
10+
message_thread_id=None,
11+
bot_id=None,
12+
):
913
"""
1014
Set data for a user in a particular chat.
1115
"""
@@ -17,7 +21,11 @@ async def get_data(self, chat_id, user_id):
1721
"""
1822
raise NotImplementedError
1923

20-
async def set_state(self, chat_id, user_id, state):
24+
async def set_state(self, chat_id, user_id, state,
25+
business_connection_id=None,
26+
message_thread_id=None,
27+
bot_id=None,
28+
):
2129
"""
2230
Set state for a particular user.
2331
@@ -28,22 +36,38 @@ async def set_state(self, chat_id, user_id, state):
2836
"""
2937
raise NotImplementedError
3038

31-
async def delete_state(self, chat_id, user_id):
39+
async def delete_state(self, chat_id, user_id,
40+
business_connection_id=None,
41+
message_thread_id=None,
42+
bot_id=None,
43+
):
3244
"""
3345
Delete state for a particular user.
3446
"""
3547
raise NotImplementedError
3648

37-
async def reset_data(self, chat_id, user_id):
49+
async def reset_data(self, chat_id, user_id,
50+
business_connection_id=None,
51+
message_thread_id=None,
52+
bot_id=None,
53+
):
3854
"""
3955
Reset data for a particular user in a chat.
4056
"""
4157
raise NotImplementedError
4258

43-
async def get_state(self, chat_id, user_id):
59+
async def get_state(self, chat_id, user_id,
60+
business_connection_id=None,
61+
message_thread_id=None,
62+
bot_id=None,
63+
):
4464
raise NotImplementedError
4565

46-
def get_interactive_data(self, chat_id, user_id):
66+
def get_interactive_data(self, chat_id, user_id,
67+
business_connection_id=None,
68+
message_thread_id=None,
69+
bot_id=None,
70+
):
4771
"""
4872
Should be sync, but should provide a context manager
4973
with __aenter__ and __aexit__ methods.

telebot/states/asyncio/context.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from telebot.states import State, StatesGroup
1+
from telebot.states import State
22
from telebot.types import CallbackQuery, Message
33
from telebot.async_telebot import AsyncTeleBot
44
from telebot.states import resolve_context
@@ -21,7 +21,7 @@ async def start_ex(message: types.Message, state_context: StateContext):
2121
# also, state_context.data(), .add_data(), .reset_data(), .delete() methods available.
2222
"""
2323

24-
def __init__(self, message: Union[Message, CallbackQuery], bot: str) -> None:
24+
def __init__(self, message: Union[Message, CallbackQuery], bot: AsyncTeleBot) -> None:
2525
self.message: Union[Message, CallbackQuery] = message
2626
self.bot: AsyncTeleBot = bot
2727
self.bot_id = self.bot.bot_id

telebot/storage/base_storage.py

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ class StateStorageBase:
55
def __init__(self) -> None:
66
pass
77

8-
def set_data(self, chat_id, user_id, key, value):
8+
def set_data(self, chat_id, user_id, key, value,
9+
business_connection_id=None,
10+
message_thread_id=None,
11+
bot_id=None,
12+
):
913
"""
1014
Set data for a user in a particular chat.
1115
"""
@@ -17,7 +21,11 @@ def get_data(self, chat_id, user_id):
1721
"""
1822
raise NotImplementedError
1923

20-
def set_state(self, chat_id, user_id, state):
24+
def set_state(self, chat_id, user_id, state,
25+
business_connection_id=None,
26+
message_thread_id=None,
27+
bot_id=None,
28+
):
2129
"""
2230
Set state for a particular user.
2331
@@ -28,22 +36,38 @@ def set_state(self, chat_id, user_id, state):
2836
"""
2937
raise NotImplementedError
3038

31-
def delete_state(self, chat_id, user_id):
39+
def delete_state(self, chat_id, user_id,
40+
business_connection_id=None,
41+
message_thread_id=None,
42+
bot_id=None,
43+
):
3244
"""
3345
Delete state for a particular user.
3446
"""
3547
raise NotImplementedError
3648

37-
def reset_data(self, chat_id, user_id):
49+
def reset_data(self, chat_id, user_id,
50+
business_connection_id=None,
51+
message_thread_id=None,
52+
bot_id=None,
53+
):
3854
"""
3955
Reset data for a particular user in a chat.
4056
"""
4157
raise NotImplementedError
4258

43-
def get_state(self, chat_id, user_id):
59+
def get_state(self, chat_id, user_id,
60+
business_connection_id=None,
61+
message_thread_id=None,
62+
bot_id=None,
63+
):
4464
raise NotImplementedError
4565

46-
def get_interactive_data(self, chat_id, user_id):
66+
def get_interactive_data(self, chat_id, user_id,
67+
business_connection_id=None,
68+
message_thread_id=None,
69+
bot_id=None,
70+
):
4771
raise NotImplementedError
4872

4973
def save(self, chat_id, user_id, data):

0 commit comments

Comments
 (0)