from telegram import ReplyKeyboardMarkup, KeyboardButton from telegram.ext import Updater, CommandHandler, MessageHandler, Filters # Define the keyboard layout keyboard = [[KeyboardButton("Yes"), KeyboardButton("No")]] reply_markup = ReplyKeyboardMarkup(keyboard, resize_keyboard=True) # Define a function to handle messages def handle_message(update, context): text = update.message.text if text == "Yes": update.message.reply_text("Great, glad to hear it!", reply_markup=reply_markup) elif text == "No": update.message.reply_text("Sorry to hear that.", reply_markup=reply_markup) else: update.message.reply_text("Please choose 'Yes' or 'No'.", reply_markup=reply_markup) # Set up the bot and start polling for updates updater = Updater("YOUR_API_TOKEN") dispatcher = updater.dispatcher dispatcher.add_handler(MessageHandler(Filters.text, handle_message)) updater.start_polling()
from telegram import InlineKeyboardMarkup, InlineKeyboardButton from telegram.ext import Updater, CommandHandler, CallbackQueryHandler # Define the options as a list of tuples OPTIONS = [ ("Option 1", "This is information about option 1"), ("Option 2", "This is information about option 2"), ("Option 3", "This is information about option 3"), ("Option 4", "This is information about option 4"), ] # Define a function to handle the "start" command def handle_start(update, context): buttons = [] for option, info in OPTIONS: button = InlineKeyboardButton(option, callback_data=option) buttons.append([button]) reply_markup = InlineKeyboardMarkup(buttons) update.message.reply_text("Please choose an option:", reply_markup=reply_markup) # Define a function to handle button presses def handle_button_press(update, context): query = update.callback_query option = query.data for o, i in OPTIONS: if o == option: query.edit_message_text(i) # Set up the bot and start polling for updates updater = Updater("YOUR_API_TOKEN") dispatcher = updater.dispatcher dispatcher.add_handler(CommandHandler("start", handle_start)) dispatcher.add_handler(CallbackQueryHandler(handle_button_press)) updater.start_polling()Both examples use the `telegram` package library to create custom keyboards with `KeyboardButton` and `InlineKeyboardButton`. Example 1 uses a `ReplyKeyboardMarkup`, which displays the keyboard beneath the chat input field, and Example 2 uses an `InlineKeyboardMarkup`, which displays the keyboard as a separate message.