pyTelegramBotAPI/telebot/asyncio_handler_backends.py

138 lines
3.7 KiB
Python
Raw Permalink Normal View History

"""
File with all middleware classes, states.
"""
class BaseMiddleware:
"""
Base class for middleware.
Your middlewares should be inherited from this class.
2022-07-15 14:24:15 +03:00
Set update_sensitive=True if you want to get different updates on
different functions. For example, if you want to handle pre_process for
message update, then you will have to create pre_process_message function, and
so on. Same applies to post_process.
.. code-block:: python
:caption: Example of class-based middlewares
2022-07-15 14:24:15 +03:00
class MyMiddleware(BaseMiddleware):
def __init__(self):
self.update_sensitive = True
self.update_types = ['message', 'edited_message']
async def pre_process_message(self, message, data):
2022-07-15 14:24:15 +03:00
# only message update here
pass
async def post_process_message(self, message, data, exception):
2022-07-15 14:24:15 +03:00
pass # only message update here for post_process
async def pre_process_edited_message(self, message, data):
2022-07-15 14:24:15 +03:00
# only edited_message update here
pass
async def post_process_edited_message(self, message, data, exception):
2022-07-15 14:24:15 +03:00
pass # only edited_message update here for post_process
"""
2022-07-15 14:24:15 +03:00
update_sensitive: bool = False
def __init__(self):
pass
async def pre_process(self, message, data):
raise NotImplementedError
async def post_process(self, message, data, exception):
raise NotImplementedError
2022-02-19 21:37:03 +03:00
2022-01-24 20:24:56 +03:00
class State:
"""
Class representing a state.
.. code-block:: python3
class MyStates(StatesGroup):
my_state = State() # returns my_state:State string.
"""
2022-01-24 20:24:56 +03:00
def __init__(self) -> None:
self.name = None
2022-02-19 21:37:03 +03:00
2022-01-24 20:24:56 +03:00
def __str__(self) -> str:
return self.name
2022-02-19 21:37:03 +03:00
2022-01-24 20:24:56 +03:00
class StatesGroup:
"""
Class representing common states.
.. code-block:: python3
class MyStates(StatesGroup):
my_state = State() # returns my_state:State string.
"""
2022-01-24 20:24:56 +03:00
def __init_subclass__(cls) -> None:
2023-04-14 22:11:08 +03:00
state_list = []
2022-01-24 20:24:56 +03:00
for name, value in cls.__dict__.items():
if not name.startswith('__') and not callable(value) and isinstance(value, State):
# change value of that variable
2022-03-06 22:18:11 +03:00
value.name = ':'.join((cls.__name__, name))
value.group = cls
2023-04-14 22:11:08 +03:00
state_list.append(value)
cls._state_list = state_list
2022-03-06 22:18:11 +03:00
2023-04-14 22:00:42 +03:00
@property
def state_list(self):
2023-04-14 22:11:08 +03:00
return self._state_list
2023-04-14 22:00:42 +03:00
2022-03-06 22:18:11 +03:00
class SkipHandler:
"""
Class for skipping handlers.
Just return instance of this class
in middleware to skip handler.
Update will go to post_process,
but will skip execution of handler.
"""
def __init__(self) -> None:
pass
2022-03-06 22:18:11 +03:00
class CancelUpdate:
"""
Class for canceling updates.
Just return instance of this class
in middleware to skip update.
Update will skip handler and execution
of post_process in middlewares.
"""
def __init__(self) -> None:
pass
class ContinueHandling:
"""
Class for continue updates in handlers.
Just return instance of this class
in handlers to continue process.
2022-10-11 18:15:01 +03:00
.. code-block:: python3
:caption: Example of using ContinueHandling
@bot.message_handler(commands=['start'])
async def start(message):
await bot.send_message(message.chat.id, 'Hello World!')
2022-10-11 18:15:01 +03:00
return ContinueHandling()
@bot.message_handler(commands=['start'])
async def start2(message):
await bot.send_message(message.chat.id, 'Hello World2!')
2022-10-11 18:15:01 +03:00
"""
def __init__(self) -> None:
pass