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:
|
2022-03-07 15:31:02 +03:00
|
|
|
|
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
|
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
|
|
|
|
|
|
|
|
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
|