2022-07-24 21:14:09 +03:00
|
|
|
"""
|
|
|
|
File with all middleware classes, states.
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2022-02-19 22:08:14 +03:00
|
|
|
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
|
2022-07-24 21:14:09 +03:00
|
|
|
: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']
|
|
|
|
|
2022-07-24 21:14:09 +03:00
|
|
|
async def pre_process_message(self, message, data):
|
2022-07-15 14:24:15 +03:00
|
|
|
# only message update here
|
|
|
|
pass
|
|
|
|
|
2022-07-24 21:14:09 +03:00
|
|
|
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
|
|
|
|
|
2022-07-24 21:14:09 +03:00
|
|
|
async def pre_process_edited_message(self, message, data):
|
2022-07-15 14:24:15 +03:00
|
|
|
# only edited_message update here
|
|
|
|
pass
|
|
|
|
|
2022-07-24 21:14:09 +03:00
|
|
|
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-02-19 22:08:14 +03:00
|
|
|
"""
|
|
|
|
|
2022-07-15 14:24:15 +03:00
|
|
|
update_sensitive: bool = False
|
|
|
|
|
2022-02-19 22:08:14 +03:00
|
|
|
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:
|
2022-07-24 21:14:09 +03:00
|
|
|
"""
|
|
|
|
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:
|
2022-07-24 21:14:09 +03:00
|
|
|
"""
|
|
|
|
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))
|
2022-06-29 18:21:42 +03:00
|
|
|
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-10-09 01:58:05 +03:00
|
|
|
|
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:
|
2022-10-09 01:58:05 +03:00
|
|
|
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'])
|
2022-10-11 18:15:38 +03:00
|
|
|
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'])
|
2022-10-11 18:15:38 +03:00
|
|
|
async def start2(message):
|
|
|
|
await bot.send_message(message.chat.id, 'Hello World2!')
|
2022-10-11 18:15:01 +03:00
|
|
|
|
2022-10-09 01:58:05 +03:00
|
|
|
"""
|
|
|
|
def __init__(self) -> None:
|
|
|
|
pass
|