diff --git a/README.md b/README.md index 9d78f0d..7ecb129 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ * [General use of the API](#general-use-of-the-api) * [Message handlers](#message-handlers) * [Callback Query handlers](#callback-query-handler) + * [Middleware handlers](#middleware-handler) * [TeleBot](#telebot) * [Reply markup](#reply-markup) * [Inline Mode](#inline-mode) @@ -223,6 +224,24 @@ In bot2.0 update. You can get `callback_query` in update object. In telebot use def test_callback(call): logger.info(call) ``` +#### Middleware Handler + +A middleware handler is a function that allows you to modify requests or the bot context as they pass through the +Telegram to the bot. You can imagine middleware as a chain of logic connection handled before any other handlers are +executed. + +```python +@bot.middleware_handler(update_types=['message']) +def modify_message(bot_instance, message): + # modifying the message before it reaches any other handler + message.another_text = message.text + ':changed' + +@bot.message_handler(commands=['start']) +def start(message): + # the message is already modified when it reaches message handler + assert message.another_text == message.text + ':changed' +``` +There are other examples using middleware handler in the [examples/middleware](examples/middleware) directory. #### TeleBot ```python