1
0
mirror of https://github.com/eternnoir/pyTelegramBotAPI.git synced 2023-08-10 21:12:57 +03:00
pyTelegramBotAPI/telebot/asyncio_handler_backends.py

109 lines
2.9 KiB
Python
Raw 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:
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))
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