2016-04-16 20:32:44 +02:00
|
|
|
#!/usr/bin/env python
|
2023-09-09 23:12:30 +02:00
|
|
|
# pylint: disable=unused-argument
|
2017-10-20 20:24:00 +02:00
|
|
|
# This program is dedicated to the public domain under the CC0 license.
|
2019-02-13 13:38:07 +01:00
|
|
|
|
|
|
|
"""
|
2021-04-30 10:47:41 +02:00
|
|
|
Basic example for a bot that uses inline keyboards. For an in-depth explanation, check out
|
2022-01-21 16:51:03 +01:00
|
|
|
https://github.com/python-telegram-bot/python-telegram-bot/wiki/InlineKeyboard-Example.
|
2017-10-20 20:24:00 +02:00
|
|
|
"""
|
2016-04-16 20:32:44 +02:00
|
|
|
import logging
|
2018-09-21 08:57:01 +02:00
|
|
|
|
2020-10-31 16:33:34 +01:00
|
|
|
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update
|
2022-05-12 19:36:25 +02:00
|
|
|
from telegram.ext import Application, CallbackQueryHandler, CommandHandler, ContextTypes
|
2016-04-16 20:32:44 +02:00
|
|
|
|
2021-10-09 13:56:50 +02:00
|
|
|
# Enable logging
|
2016-05-23 22:45:01 +02:00
|
|
|
logging.basicConfig(
|
|
|
|
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
|
2016-07-20 00:15:03 +02:00
|
|
|
)
|
2023-06-07 22:32:04 +02:00
|
|
|
# set higher logging level for httpx to avoid all GET and POST requests being logged
|
|
|
|
logging.getLogger("httpx").setLevel(logging.WARNING)
|
|
|
|
|
2017-10-20 20:24:00 +02:00
|
|
|
logger = logging.getLogger(__name__)
|
2016-04-16 20:32:44 +02:00
|
|
|
|
|
|
|
|
2022-05-12 19:36:25 +02:00
|
|
|
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
2021-05-27 20:34:58 +02:00
|
|
|
"""Sends a message with three inline buttons attached."""
|
2016-07-20 00:15:03 +02:00
|
|
|
keyboard = [
|
2020-10-09 17:22:07 +02:00
|
|
|
[
|
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
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
await update.message.reply_text("Please choose:", reply_markup=reply_markup)
|
2016-04-16 20:32:44 +02:00
|
|
|
|
|
|
|
|
2022-05-12 19:36:25 +02:00
|
|
|
async def button(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
2021-05-27 20:34:58 +02:00
|
|
|
"""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
|
|
|
|
2020-03-28 12:07:23 +01: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
|
2022-04-24 12:38:09 +02:00
|
|
|
await query.answer()
|
2020-03-28 12:07:23 +01:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
await query.edit_message_text(text=f"Selected option: {query.data}")
|
2016-04-16 20:32:44 +02:00
|
|
|
|
|
|
|
|
2022-05-12 19:36:25 +02:00
|
|
|
async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
2021-05-27 20:34:58 +02:00
|
|
|
"""Displays info on how to use the bot."""
|
2022-04-24 12:38:09 +02:00
|
|
|
await update.message.reply_text("Use /start to test this bot.")
|
2016-04-16 20:32:44 +02:00
|
|
|
|
|
|
|
|
2021-03-13 16:21:03 +01:00
|
|
|
def main() -> None:
|
2021-05-27 20:34:58 +02:00
|
|
|
"""Run the bot."""
|
2022-04-24 12:38:09 +02:00
|
|
|
# Create the Application and pass it your bot's token.
|
|
|
|
application = Application.builder().token("TOKEN").build()
|
2016-04-16 20:32:44 +02:00
|
|
|
|
2022-04-24 12:38:09 +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
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
# Run the bot until the user presses Ctrl-C
|
2023-06-04 17:11:58 +02:00
|
|
|
application.run_polling(allowed_updates=Update.ALL_TYPES)
|
2016-04-16 20:32:44 +02:00
|
|
|
|
|
|
|
|
2017-10-20 20:24:00 +02:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|