1
0
mirror of https://github.com/eternnoir/pyTelegramBotAPI.git synced 2023-08-10 21:12:57 +03:00
pyTelegramBotAPI/examples/middleware
2022-03-07 17:14:51 +05:00
..
i18n.py polling should leave our world. :) 2021-09-28 19:17:09 +03:00
README.md Update README.md 2022-03-07 17:14:51 +05:00
session.py polling should leave our world. :) 2021-09-28 19:17:09 +03:00

Middlewares

Type of middlewares in pyTelegramBotAPI

Currently, synchronous version of pyTelegramBotAPI has two types of middlewares:

  • Class-based middlewares
  • Function-based middlewares

Purpose of middlewares

Middlewares are designed to get update before handler's execution.

Class-based middlewares

This type of middleware has more functionality compared to function-based one.

Class based middleware should be instance of telebot.handler_backends.BaseMiddleware.

Each middleware should have 2 main functions:

pre_process -> is a method of class which receives update, and data.

Data - is a dictionary, which could be passed right to handler, and post_process function.

post_process -> is a function of class which receives update, data, and exception, that happened in handler. If handler was executed correctly - then exception will equal to None.

Function-based middlewares

To use function-based middleware, you should set apihelper.ENABLE_MIDDLEWARE = True. This type of middleware is created by using a decorator for middleware. With this type middleware, you can retrieve update immediately after update came. You should set update_types as well.

Why class-based middlewares are better?

  • You can pass data between post, pre_process functions, and handler.
  • If there is an exception in handler, you will get exception parameter with exception class in post_process.
  • Has post_process -> method which works after the handler's execution.

Take a look at examples for more.