mirror of
https://github.com/eternnoir/pyTelegramBotAPI.git
synced 2023-08-10 21:12:57 +03:00
Create basic_example.py
This commit is contained in:
parent
403028bf35
commit
b292b275cb
32
examples/middleware/class_based/basic_example.py
Normal file
32
examples/middleware/class_based/basic_example.py
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
from telebot import TeleBot
|
||||||
|
from telebot.handler_backends import BaseMiddleware
|
||||||
|
|
||||||
|
bot = TeleBot('TOKEN', use_class_middlewares=True) # set use_class_middlewares to True!
|
||||||
|
# otherwise, class-based middlewares won't execute.
|
||||||
|
|
||||||
|
# You can use this classes for cancelling update or skipping handler:
|
||||||
|
# from telebot.handler_backends import CancelUpdate, SkipHandler
|
||||||
|
|
||||||
|
class Middleware(BaseMiddleware):
|
||||||
|
def __init__(self):
|
||||||
|
self.update_types = ['message']
|
||||||
|
def pre_process(self, message, data):
|
||||||
|
data['foo'] = 'Hello' # just for example
|
||||||
|
# we edited the data. now, this data is passed to handler.
|
||||||
|
# return SkipHandler() -> this will skip handler
|
||||||
|
# return CancelUpdate() -> this will cancel update
|
||||||
|
def post_process(self, message, data, exception=None):
|
||||||
|
print(data['foo'])
|
||||||
|
if exception: # check for exception
|
||||||
|
print(exception)
|
||||||
|
|
||||||
|
@bot.message_handler(commands=['start'])
|
||||||
|
def start(message, data: dict): # you don't have to put data parameter in handler if you don't need it.
|
||||||
|
bot.send_message(message.chat.id, data['foo'])
|
||||||
|
data['foo'] = 'Processed' # we changed value of data.. this data is now passed to post_process.
|
||||||
|
|
||||||
|
|
||||||
|
# Setup middleware
|
||||||
|
bot.setup_middleware(Middleware())
|
||||||
|
|
||||||
|
bot.infinity_polling()
|
Loading…
Reference in New Issue
Block a user