Skip to content

Add support for keyboards and buttons #66

@emilyalbini

Description

@emilyalbini

Keyboards and inline keyboards are a great way for users to interact with your bot: they allows you to create rich user interfaces, integrated in the Telegram client.

In botogram, keyboards and buttons (I prefer this name than "inline keyboards") will be implemented simoultaneously, sharing most of their code. This creates a coerent API, and allows you to switch between keyboards and buttons really easily.

Both keyboards are buttons are built around the concept of callbacks: when someone clicks a key/button, a specific function you define will be called. Buttons natively supports this feature, while for keyboards this will be implemented by botogram.

@bot.callback("color_chosen")
def color_chosen_callback(chat, user, callback, args):
    chat.send("%s chosen %s!" % (user.name, args[0]))

@bot.callback("exit")
def exit_callback(chat, user, callback):
    callback.notify("Bye, I will miss you :(", alert=True)

@bot.command("ask")
def ask_command(chat, message, args):
    btns = botogram.buttons()
    btns[0].callback("Red", "color_chosen", ["red"])
    btns[0].callback("Blue", "color_chosen", ["blue"])
    btns[1].callback("Exit", "exit")

    chat.send("Which color do you prefer?", attach=btns)

The code above will create a /ask command, which sends the "Which color do you prefer" message to the chat. Below that message, there will be two rows of buttons: the first row with "Red" and "Blue", and the second row with "Exit". Clicking on one of the color will invoke color_chosen_callback with the color as an argument, while clicking on the exit button will display an alert.

The API for keyboards will be almost the same (callbacks mocked in by botogram, other features such as callback.notify unavailable).

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions