python-telegram-bot/examples/inlinekeyboard.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

80 lines
2.8 KiB
Python
Raw Normal View History

2016-04-16 20:32:44 +02:00
#!/usr/bin/env python
2022-05-15 14:08:40 +02:00
# 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.
"""
2016-04-16 20:32:44 +02:00
import logging
2022-05-15 14:08:40 +02:00
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://github.com/python-telegram-bot/python-telegram-bot/tree/v{TG_VER}/examples"
)
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update
from telegram.ext import Application, CallbackQueryHandler, CommandHandler, ContextTypes
2016-04-16 20:32:44 +02:00
# Enable logging
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
2016-07-20 00:15:03 +02:00
)
logger = logging.getLogger(__name__)
2016-04-16 20:32:44 +02:00
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Sends a message with three inline buttons attached."""
2016-07-20 00:15:03 +02:00
keyboard = [
[
2016-07-20 00:15:03 +02:00
InlineKeyboardButton("Option 1", callback_data="1"),
InlineKeyboardButton("Option 2", callback_data="2"),
],
[InlineKeyboardButton("Option 3", callback_data="3")],
]
2016-04-16 20:32:44 +02:00
2016-07-20 00:15:03 +02:00
reply_markup = InlineKeyboardMarkup(keyboard)
2016-04-16 20:32:44 +02:00
await update.message.reply_text("Please choose:", reply_markup=reply_markup)
2016-04-16 20:32:44 +02:00
async def button(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Parses the CallbackQuery and updates the message text."""
2016-04-16 20:55:43 +02:00
query = update.callback_query
2016-07-20 00:15:03 +02:00
# 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}")
2016-04-16 20:32:44 +02:00
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.")
2016-04-16 20:32:44 +02:00
def main() -> None:
"""Run the bot."""
# Create the Application and pass it your bot's token.
application = Application.builder().token("TOKEN").build()
2016-04-16 20:32:44 +02:00
application.add_handler(CommandHandler("start", start))
application.add_handler(CallbackQueryHandler(button))
application.add_handler(CommandHandler("help", help_command))
2016-07-20 00:15:03 +02:00
# Run the bot until the user presses Ctrl-C
application.run_polling()
2016-04-16 20:32:44 +02:00
if __name__ == "__main__":
main()