Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 23 additions & 17 deletions telebot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5564,7 +5564,7 @@ def create_invoice_link(self,

# noinspection PyShadowingBuiltins
def send_poll(
self, chat_id: Union[int, str], question: str, options: List[types.InputPollOption],
self, chat_id: Union[int, str], question: str, options: List[Union[str, types.InputPollOption]],
is_anonymous: Optional[bool]=None, type: Optional[str]=None,
allows_multiple_answers: Optional[bool]=None,
correct_option_id: Optional[int]=None,
Expand Down Expand Up @@ -5600,7 +5600,7 @@ def send_poll(
:type question: :obj:`str`

:param options: A JSON-serialized list of 2-10 answer options
:type options: :obj:`list` of :obj:`InputPollOption`
:type options: :obj:`list` of :obj:`InputPollOption` | :obj:`list` of :obj:`str`

:param is_anonymous: True, if the poll needs to be anonymous, defaults to True
:type is_anonymous: :obj:`bool`
Expand Down Expand Up @@ -7578,9 +7578,8 @@ def set_state(self, user_id: int, state: Union[str, State], chat_id: Optional[in
chat_id = user_id
if bot_id is None:
bot_id = self.bot_id
return self.current_states.set_state(
chat_id=chat_id, user_id=user_id, state=state, bot_id=bot_id,
business_connection_id=business_connection_id, message_thread_id=message_thread_id)
return self.current_states.set_state(chat_id, user_id, state,
bot_id=bot_id, business_connection_id=business_connection_id, message_thread_id=message_thread_id)


def reset_data(self, user_id: int, chat_id: Optional[int]=None,
Expand Down Expand Up @@ -7611,8 +7610,8 @@ def reset_data(self, user_id: int, chat_id: Optional[int]=None,
chat_id = user_id
if bot_id is None:
bot_id = self.bot_id
return self.current_states.reset_data(chat_id=chat_id, user_id=user_id, bot_id=bot_id,
business_connection_id=business_connection_id, message_thread_id=message_thread_id)
return self.current_states.reset_data(chat_id, user_id,
bot_id=bot_id, business_connection_id=business_connection_id, message_thread_id=message_thread_id)


def delete_state(self, user_id: int, chat_id: Optional[int]=None, business_connection_id: Optional[str]=None,
Expand All @@ -7630,15 +7629,24 @@ def delete_state(self, user_id: int, chat_id: Optional[int]=None, business_conne
:param chat_id: Chat's identifier
:type chat_id: :obj:`int`

:param bot_id: Bot's identifier, defaults to current bot id
:type bot_id: :obj:`int`

:param business_connection_id: Business identifier
:type business_connection_id: :obj:`str`

:param message_thread_id: Identifier of the message thread
:type message_thread_id: :obj:`int`

:return: True on success
:rtype: :obj:`bool`
"""
if chat_id is None:
chat_id = user_id
if bot_id is None:
bot_id = self.bot_id
return self.current_states.delete_state(chat_id=chat_id, user_id=user_id, bot_id=bot_id,
business_connection_id=business_connection_id, message_thread_id=message_thread_id)
return self.current_states.delete_state(chat_id, user_id,
bot_id=bot_id, business_connection_id=business_connection_id, message_thread_id=message_thread_id)


def retrieve_data(self, user_id: int, chat_id: Optional[int]=None, business_connection_id: Optional[str]=None,
Expand Down Expand Up @@ -7668,9 +7676,8 @@ def retrieve_data(self, user_id: int, chat_id: Optional[int]=None, business_conn
chat_id = user_id
if bot_id is None:
bot_id = self.bot_id
return self.current_states.get_interactive_data(chat_id=chat_id, user_id=user_id, bot_id=bot_id,
business_connection_id=business_connection_id,
message_thread_id=message_thread_id)
return self.current_states.get_interactive_data(chat_id, user_id,
bot_id=bot_id, business_connection_id=business_connection_id, message_thread_id=message_thread_id)


def get_state(self, user_id: int, chat_id: Optional[int]=None,
Expand Down Expand Up @@ -7707,8 +7714,8 @@ def get_state(self, user_id: int, chat_id: Optional[int]=None,
chat_id = user_id
if bot_id is None:
bot_id = self.bot_id
return self.current_states.get_state(chat_id=chat_id, user_id=user_id, bot_id=bot_id,
business_connection_id=business_connection_id, message_thread_id=message_thread_id)
return self.current_states.get_state(chat_id, user_id,
bot_id=bot_id, business_connection_id=business_connection_id, message_thread_id=message_thread_id)


def add_data(self, user_id: int, chat_id: Optional[int]=None,
Expand Down Expand Up @@ -7742,8 +7749,8 @@ def add_data(self, user_id: int, chat_id: Optional[int]=None,
if bot_id is None:
bot_id = self.bot_id
for key, value in kwargs.items():
self.current_states.set_data(chat_id=chat_id, user_id=user_id, key=key, value=value, bot_id=bot_id,
business_connection_id=business_connection_id, message_thread_id=message_thread_id)
self.current_states.set_data(chat_id, user_id, key, value,
bot_id=bot_id, business_connection_id=business_connection_id, message_thread_id=message_thread_id)


def register_next_step_handler_by_chat_id(
Expand Down Expand Up @@ -9848,4 +9855,3 @@ def _notify_command_handlers(self, handlers, new_messages, update_type):
handlers=handlers,
middlewares=middlewares,
update_type=update_type)

40 changes: 24 additions & 16 deletions telebot/async_telebot.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import sys

# this imports are used to avoid circular import error
# noinspection PyUnresolvedReferences
import telebot.util
import telebot.types

Expand Down Expand Up @@ -7008,7 +7009,7 @@ async def create_invoice_link(self,

# noinspection PyShadowingBuiltins
async def send_poll(
self, chat_id: Union[int, str], question: str, options: List[types.InputPollOption],
self, chat_id: Union[int, str], question: str, options: List[Union[str, types.InputPollOption]],
is_anonymous: Optional[bool]=None, type: Optional[str]=None,
allows_multiple_answers: Optional[bool]=None,
correct_option_id: Optional[int]=None,
Expand Down Expand Up @@ -7044,7 +7045,7 @@ async def send_poll(
:type question: :obj:`str`

:param options: A JSON-serialized list of 2-10 answer options
:type options: :obj:`list` of :obj:`InputPollOption`
:type options: :obj:`list` of :obj:`InputPollOption` | :obj:`list` of :obj:`str`

:param is_anonymous: True, if the poll needs to be anonymous, defaults to True
:type is_anonymous: :obj:`bool`
Expand Down Expand Up @@ -8828,9 +8829,8 @@ async def set_state(self, user_id: int, state: Union[int, str, State], chat_id:
chat_id = user_id
if bot_id is None:
bot_id = self.bot_id
return await self.current_states.set_state(
chat_id=chat_id, user_id=user_id, state=state, bot_id=bot_id,
business_connection_id=business_connection_id, message_thread_id=message_thread_id)
return await self.current_states.set_state(chat_id, user_id, state,
bot_id=bot_id, business_connection_id=business_connection_id, message_thread_id=message_thread_id)


async def reset_data(self, user_id: int, chat_id: Optional[int]=None,
Expand Down Expand Up @@ -8861,8 +8861,8 @@ async def reset_data(self, user_id: int, chat_id: Optional[int]=None,
chat_id = user_id
if bot_id is None:
bot_id = self.bot_id
return await self.current_states.reset_data(chat_id=chat_id, user_id=user_id, bot_id=bot_id,
business_connection_id=business_connection_id, message_thread_id=message_thread_id)
return await self.current_states.reset_data(chat_id, user_id,
bot_id=bot_id, business_connection_id=business_connection_id, message_thread_id=message_thread_id)


async def delete_state(self, user_id: int, chat_id: Optional[int]=None, business_connection_id: Optional[str]=None,
Expand All @@ -8876,14 +8876,23 @@ async def delete_state(self, user_id: int, chat_id: Optional[int]=None, business
:param chat_id: Chat's identifier
:type chat_id: :obj:`int`

:param bot_id: Bot's identifier, defaults to current bot id
:type bot_id: :obj:`int`

:param business_connection_id: Business identifier
:type business_connection_id: :obj:`str`

:param message_thread_id: Identifier of the message thread
:type message_thread_id: :obj:`int`

:return: None
"""
if chat_id is None:
chat_id = user_id
if bot_id is None:
bot_id = self.bot_id
return await self.current_states.delete_state(chat_id=chat_id, user_id=user_id, bot_id=bot_id,
business_connection_id=business_connection_id, message_thread_id=message_thread_id)
return await self.current_states.delete_state(chat_id, user_id,
bot_id=bot_id, business_connection_id=business_connection_id, message_thread_id=message_thread_id)


def retrieve_data(self, user_id: int, chat_id: Optional[int]=None, business_connection_id: Optional[str]=None,
Expand Down Expand Up @@ -8913,9 +8922,8 @@ def retrieve_data(self, user_id: int, chat_id: Optional[int]=None, business_conn
chat_id = user_id
if bot_id is None:
bot_id = self.bot_id
return self.current_states.get_interactive_data(chat_id=chat_id, user_id=user_id, bot_id=bot_id,
business_connection_id=business_connection_id,
message_thread_id=message_thread_id)
return self.current_states.get_interactive_data(chat_id, user_id,
bot_id=bot_id, business_connection_id=business_connection_id, message_thread_id=message_thread_id)


async def get_state(self, user_id: int, chat_id: Optional[int]=None,
Expand Down Expand Up @@ -8952,8 +8960,8 @@ async def get_state(self, user_id: int, chat_id: Optional[int]=None,
chat_id = user_id
if bot_id is None:
bot_id = self.bot_id
return await self.current_states.get_state(chat_id=chat_id, user_id=user_id, bot_id=bot_id,
business_connection_id=business_connection_id, message_thread_id=message_thread_id)
return await self.current_states.get_state(chat_id, user_id,
bot_id=bot_id, business_connection_id=business_connection_id, message_thread_id=message_thread_id)


async def add_data(self, user_id: int, chat_id: Optional[int]=None,
Expand Down Expand Up @@ -8987,5 +8995,5 @@ async def add_data(self, user_id: int, chat_id: Optional[int]=None,
if bot_id is None:
bot_id = self.bot_id
for key, value in kwargs.items():
await self.current_states.set_data(chat_id=chat_id, user_id=user_id, key=key, value=value, bot_id=bot_id,
business_connection_id=business_connection_id, message_thread_id=message_thread_id)
await self.current_states.set_data(chat_id, user_id, key, value,
bot_id=bot_id, business_connection_id=business_connection_id, message_thread_id=message_thread_id)
36 changes: 30 additions & 6 deletions telebot/asyncio_storage/base_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ class StateStorageBase:
def __init__(self) -> None:
pass

async def set_data(self, chat_id, user_id, key, value):
async def set_data(self, chat_id, user_id, key, value,
business_connection_id=None,
message_thread_id=None,
bot_id=None,
):
"""
Set data for a user in a particular chat.
"""
Expand All @@ -17,7 +21,11 @@ async def get_data(self, chat_id, user_id):
"""
raise NotImplementedError

async def set_state(self, chat_id, user_id, state):
async def set_state(self, chat_id, user_id, state,
business_connection_id=None,
message_thread_id=None,
bot_id=None,
):
"""
Set state for a particular user.

Expand All @@ -28,22 +36,38 @@ async def set_state(self, chat_id, user_id, state):
"""
raise NotImplementedError

async def delete_state(self, chat_id, user_id):
async def delete_state(self, chat_id, user_id,
business_connection_id=None,
message_thread_id=None,
bot_id=None,
):
"""
Delete state for a particular user.
"""
raise NotImplementedError

async def reset_data(self, chat_id, user_id):
async def reset_data(self, chat_id, user_id,
business_connection_id=None,
message_thread_id=None,
bot_id=None,
):
"""
Reset data for a particular user in a chat.
"""
raise NotImplementedError

async def get_state(self, chat_id, user_id):
async def get_state(self, chat_id, user_id,
business_connection_id=None,
message_thread_id=None,
bot_id=None,
):
raise NotImplementedError

def get_interactive_data(self, chat_id, user_id):
def get_interactive_data(self, chat_id, user_id,
business_connection_id=None,
message_thread_id=None,
bot_id=None,
):
"""
Should be sync, but should provide a context manager
with __aenter__ and __aexit__ methods.
Expand Down
4 changes: 2 additions & 2 deletions telebot/states/asyncio/context.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from telebot.states import State, StatesGroup
from telebot.states import State
from telebot.types import CallbackQuery, Message
from telebot.async_telebot import AsyncTeleBot
from telebot.states import resolve_context
Expand All @@ -21,7 +21,7 @@ async def start_ex(message: types.Message, state_context: StateContext):
# also, state_context.data(), .add_data(), .reset_data(), .delete() methods available.
"""

def __init__(self, message: Union[Message, CallbackQuery], bot: str) -> None:
def __init__(self, message: Union[Message, CallbackQuery], bot: AsyncTeleBot) -> None:
self.message: Union[Message, CallbackQuery] = message
self.bot: AsyncTeleBot = bot
self.bot_id = self.bot.bot_id
Expand Down
36 changes: 30 additions & 6 deletions telebot/storage/base_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ class StateStorageBase:
def __init__(self) -> None:
pass

def set_data(self, chat_id, user_id, key, value):
def set_data(self, chat_id, user_id, key, value,
business_connection_id=None,
message_thread_id=None,
bot_id=None,
):
"""
Set data for a user in a particular chat.
"""
Expand All @@ -17,7 +21,11 @@ def get_data(self, chat_id, user_id):
"""
raise NotImplementedError

def set_state(self, chat_id, user_id, state):
def set_state(self, chat_id, user_id, state,
business_connection_id=None,
message_thread_id=None,
bot_id=None,
):
"""
Set state for a particular user.

Expand All @@ -28,22 +36,38 @@ def set_state(self, chat_id, user_id, state):
"""
raise NotImplementedError

def delete_state(self, chat_id, user_id):
def delete_state(self, chat_id, user_id,
business_connection_id=None,
message_thread_id=None,
bot_id=None,
):
"""
Delete state for a particular user.
"""
raise NotImplementedError

def reset_data(self, chat_id, user_id):
def reset_data(self, chat_id, user_id,
business_connection_id=None,
message_thread_id=None,
bot_id=None,
):
"""
Reset data for a particular user in a chat.
"""
raise NotImplementedError

def get_state(self, chat_id, user_id):
def get_state(self, chat_id, user_id,
business_connection_id=None,
message_thread_id=None,
bot_id=None,
):
raise NotImplementedError

def get_interactive_data(self, chat_id, user_id):
def get_interactive_data(self, chat_id, user_id,
business_connection_id=None,
message_thread_id=None,
bot_id=None,
):
raise NotImplementedError

def save(self, chat_id, user_id, data):
Expand Down