From 76597b5c619649c2457cb876a2c749e9c7a4a248 Mon Sep 17 00:00:00 2001 From: clarkzjw Date: Mon, 20 Feb 2023 01:03:57 -0800 Subject: add python-telegram-bot inline keyboard example --- bot.py | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 12 +++++++++ 2 files changed, 91 insertions(+) create mode 100644 bot.py create mode 100644 requirements.txt diff --git a/bot.py b/bot.py new file mode 100644 index 0000000..517f810 --- /dev/null +++ b/bot.py @@ -0,0 +1,79 @@ +#!/usr/bin/env python +# pylint: disable=unused-argument, wrong-import-position +# This program is dedicated to the public domain under the CC0 license. + +""" +Basic example for a bot that uses inline keyboards. For an in-depth explanation, check out + https://github.com/python-telegram-bot/python-telegram-bot/wiki/InlineKeyboard-Example. +""" +import logging + +from telegram import __version__ as TG_VER + +try: + from telegram import __version_info__ +except ImportError: + __version_info__ = (0, 0, 0, 0, 0) # type: ignore[assignment] + +if __version_info__ < (20, 0, 0, "alpha", 1): + raise RuntimeError( + f"This example is not compatible with your current PTB version {TG_VER}. To view the " + f"{TG_VER} version of this example, " + f"visit https://docs.python-telegram-bot.org/en/v{TG_VER}/examples.html" + ) +from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update +from telegram.ext import Application, CallbackQueryHandler, CommandHandler, ContextTypes + +# Enable logging +logging.basicConfig( + format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO +) +logger = logging.getLogger(__name__) + + +async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: + """Sends a message with three inline buttons attached.""" + keyboard = [ + [ + InlineKeyboardButton("Option 1", callback_data="1"), + InlineKeyboardButton("Option 2", callback_data="2"), + ], + [InlineKeyboardButton("Option 3", callback_data="3")], + ] + + reply_markup = InlineKeyboardMarkup(keyboard) + + await update.message.reply_text("Please choose:", reply_markup=reply_markup) + + +async def button(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: + """Parses the CallbackQuery and updates the message text.""" + query = update.callback_query + + # CallbackQueries need to be answered, even if no notification to the user is needed + # Some clients may have trouble otherwise. See https://core.telegram.org/bots/api#callbackquery + await query.answer() + + await query.edit_message_text(text=f"Selected option: {query.data}") + + +async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: + """Displays info on how to use the bot.""" + await update.message.reply_text("Use /start to test this bot.") + + +def main() -> None: + """Run the bot.""" + # Create the Application and pass it your bot's token. + application = Application.builder().token("TOKEN").build() + + application.add_handler(CommandHandler("start", start)) + application.add_handler(CallbackQueryHandler(button)) + application.add_handler(CommandHandler("help", help_command)) + + # Run the bot until the user presses Ctrl-C + application.run_polling() + + +if __name__ == "__main__": + main() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..33a4479 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,12 @@ +anyio==3.6.2 +certifi==2022.12.7 +h11==0.14.0 +h2==4.1.0 +hpack==4.0.0 +httpcore==0.16.3 +httpx==0.23.3 +hyperframe==6.0.1 +idna==3.4 +python-telegram-bot==20.1 +rfc3986==1.5.0 +sniffio==1.3.0 -- cgit v1.2.3