How to make a menu in the Bota Telegram Python

Create a Telegram bot menu with Python: learn how to create a menu of options for users to choose from, with an example.

Creating a Menu in Bot for Telegram using Python

Creating a menu in a bot for Telegram using Python is an easy and straightforward process. Firstly, it is important to set up the bot on Telegram. This can be done by going to the BotFather bot in Telegram, and then following the instructions given there. Once the bot is set up, it is time to write the code for the menu.

In order to create a menu, the telegram.ext.Updater class from the telegram.ext package must be imported. Then, the bot's token must be obtained from the BotFather. It is important to keep this token a secret.


import telegram
from telegram.ext import Updater

# get bot token from BotFather
bot_token = "XXXXXXXXXXXXXXXXXXXXXXX"

# create an updater object
updater = Updater(token=bot_token, use_context=True)

Once the updater object is created, the add_handler() method can be used to add a command handler to the bot. This command handler will be used to handle the menu command. The add_handler() method takes four arguments, the first one being the command handler, the second one being the command to be handled, the third one being the callback function to be executed, and finally, the fourth one being the pass_user_data argument. This argument is set to True to allow the user data to be accessed in the callback function.


# add command handler
updater.dispatcher.add_handler(CommandHandler(
    'menu', menu, pass_user_data=True))

The callback function for the menu command is then written. This callback function should check for the user's input and call the appropriate function based on the input. For example, if the user types in the command /menu, the callback function should call the menu() function, which in turn displays the menu. The menu function should then display the menu options as a list to the user.


def menu(update, context):
    user = update.message.from_user

    # display menu options
    options = ["Option 1", "Option 2", "Option 3"]
    message = "n".join(options)

    update.message.reply_text(
        message,
        reply_markup=ReplyKeyboardMarkup(
            [[item] for item in options],
            one_time_keyboard=True
        )
    )

The ReplyKeyboardMarkup class from the telegram.replykeyboardmarkup module is then used to display the menu options to the user. This class takes two arguments, the first one being an array of options and the second one being the one_time_keyboard argument which is set to True. This argument is used to ensure that the keyboard is only displayed for a single time and then it is removed.

Once the menu is displayed, the user's input can be handled. This can be done by adding a message handler to the bot. The message handler should handle the user's input and call the appropriate function based on the input. For example, if the user selects the first option, then the handle_option_1() should be called.


# add message handler
updater.dispatcher.add_handler(MessageHandler(
    Filters.text, handle_message,
    pass_user_data=True
))

def handle_message(update, context):
    user = update.message.from_user
    text = update.message.text

    if text == "Option 1":
        handle_option_1(update, context)
    elif text == "Option 2":
        handle_option_2(update, context)
    elif text == "Option 3":
        handle_option_3(update, context)

Finally, the functions that are called by the message handler can be written. These functions should do the appropriate task based on the user's input. For example, if the user selects Option 1, then the handle_option_1() function should do the task associated with Option 1.


def handle_option_1(update, context):
    user = update.message.from_user

    # do something
    update.message.reply_text(
        'Option 1 selected',
        reply_markup=ReplyKeyboardRemove()
    )

The ReplyKeyboardRemove class from the telegram.replykeyboardremove module is then used to remove the menu from the chat. This class takes no arguments.

In this way, a menu can be created in a Bot for Telegram using Python.

Answers (0)