-
-
Notifications
You must be signed in to change notification settings - Fork 39
Reply Keyboards
Sometimes it might be nice to get user input via predefined options. This is where a reply keyboard comes in handy. Reply keyboards allow you to show a keyboard with a set of buttons that perform different actions. Let's go into what some of those actions are.
Tourmaline comes with a built in class called Model::ReplyKeyboardMarkup
. Any message can be sent with a ReplyKeyboardMarkup
instance attached to attach a keyboard to the message. Here's a simple example:
REPLY_MARKUP = Tourmaline::Model::ReplyKeyboardMarkup.new([
[Tourmaline::Model::KeyboardButton.new("/kitty")],
[Tourmaline::Model::KeyboardButton.new("/kittygif")],
])
@[Command(["start", "help"])]
def help_command(message, params)
message.chat.send_message("😺 Use commands: /kitty and /kittygif", reply_markup: REPLY_MARKUP)
end
You should end up with a keyboard that looks like this:
As you may have noticed, the keyboard creation process is pretty verbose. Luckily there is a module to help you out. The Markup
module includes a number of keyboard related convenience methods. Let's modify the previous code block using Markup
instead.
REPLY_MARKUP = Markup.buttons([
["/kitty"], ["/kittygif"],
]).keyboard
@[Command(["start", "help"])]
def help_command(message, params)
message.chat.send_message("😺 Use commands: /kitty and /kittygif", reply_markup: REPLY_MARKUP)
end
You can check out the documentation for the Markup
module for more information.
Telegram also allows the creation of inline keyboards. Inline keyboards, as the name states, are displayed inline right below the message they're attached to, rather than being attached below the message area. Let's try to do the same thing as before, but with an inline keyboard.
REPLY_MARKUP = Markup.inline_buttons([
Markup.callback_button("pic", "pic")
Markup.callback_button("gif", "gif"),
]).inline_keyboard
@[Command(["start", "help"])]
def help_command(message, params)
message.chat.send_message("😺 Choose an option below", reply_markup: REPLY_MARKUP)
end
@[OnCallbackQuery]
def kitty_command(ctx)
# The time hack is to get around Telegram's image cache
api = API_URL + "?time=#{Time.utc.to_unix}&format=src&type="
ctx.chat.send_chat_action(:upload_photo)
case ctx.data
when "pic"
ctx.respond_with_photo(api + "jpg")
when "gif"
ctx.respond_with_document(api + "gif")
end
end
With that code you should end up with this:
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