-
-
Notifications
You must be signed in to change notification settings - Fork 39
Handlers
Every update sent by Telegram runs through a number of handlers, classes that are attached to your bot to handle updates. There are several built in handlers, such as:
- CallbackQueryHandler
- ChosenInlineResultHandler
- CommandHandler
- EventHandler
- HearsHandler
- InlineQueryHandler
and more. Each has a specific task, and can be attached to your bot one of two ways.
Handlers can be attached to your bot using the built in #add_handler
client method. Each handler is different, but most all of them take a proc which runs when the handler is called. Let's use the CommandHandler
as an example, since this will likely be one of the most used handlers.
class EchoBot < Tourmaline::Client
include Tourmaline
def initialize
add_handler(CommandHandler.new("echo", ->echo_command(CommandHandlerContext))
end
def echo_command(ctx)
ctx.reply(ctx.text)
end
end
The add_handler
call also could've been made outside of the class, but you get it. Now this is nice and all, but I think we can do better. Enter: annotations.
class EchoBot < Tourmaline::Client
include Tourmaline
@[Command("echo")]
def echo_command(ctx)
ctx.reply(ctx.text)
end
end
Now isn't that nice? And all built in handlers come with an annotation attached. Better yet, you can make your own handlers as well.
Tourmaline (and many other projects) was written by watzon. It is free software, but if you feel so inclined you could sponsor its development on Patreon. Thank you for using my projects!
- Webhooks
- Inline Actions
- Reply Keyboards
- Games (coming soon)
- Stickers (coming soon)
- Payments