Handler is a class that defines the logic for processing incoming updates from the server.
For example, handling the /start command:
import telekit
class MyHandler(telekit.Handler):
@classmethod
def init_handler(cls) -> None:
# here we’ll define message triggers
cls.on.command("start").invoke(cls.handle)
def handle(self):
self.chain.sender.set_text("Hello!")
self.chain.send()
telekit.Server(BOT_TOKEN).polling()Here:
-
We define a handler class
MyHandlerthat inherits fromtelekit.Handler.
The class name can be anything, but there must not be two handlers with the same name in the project. -
In the special class method
init_handler, we register a trigger that listens for the/startcommand
and specifies that thehandlemethod should be invoked when this command is received. -
In the
handlemethod, we construct a simple"Hello!"message and send it to the user. -
The
init_handlermethod is called when an instance of theServerclass is created.
Important
The trigger automatically creates an instance of the MyHandler class.
That is why handle is defined as an instance method (using self),
but is passed to the trigger via cls.handle.