mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2024-11-22 23:27:49 +01:00
76 lines
2.3 KiB
Python
76 lines
2.3 KiB
Python
#!/usr/bin/env python
|
|
# pylint: disable=missing-function-docstring, unused-argument
|
|
# 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://git.io/JOmFw.
|
|
"""
|
|
import logging
|
|
|
|
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update
|
|
from telegram.ext import (
|
|
CommandHandler,
|
|
CallbackQueryHandler,
|
|
Updater,
|
|
CallbackContext,
|
|
)
|
|
|
|
|
|
# Enable logging
|
|
logging.basicConfig(
|
|
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO
|
|
)
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def start(update: Update, context: CallbackContext.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)
|
|
|
|
update.message.reply_text('Please choose:', reply_markup=reply_markup)
|
|
|
|
|
|
def button(update: Update, context: CallbackContext.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
|
|
query.answer()
|
|
|
|
query.edit_message_text(text=f"Selected option: {query.data}")
|
|
|
|
|
|
def help_command(update: Update, context: CallbackContext.DEFAULT_TYPE) -> None:
|
|
"""Displays info on how to use the bot."""
|
|
update.message.reply_text("Use /start to test this bot.")
|
|
|
|
|
|
def main() -> None:
|
|
"""Run the bot."""
|
|
# Create the Updater and pass it your bot's token.
|
|
updater = Updater.builder().token("TOKEN").build()
|
|
|
|
updater.dispatcher.add_handler(CommandHandler('start', start))
|
|
updater.dispatcher.add_handler(CallbackQueryHandler(button))
|
|
updater.dispatcher.add_handler(CommandHandler('help', help_command))
|
|
|
|
# Start the Bot
|
|
updater.start_polling()
|
|
|
|
# Run the bot until the user presses Ctrl-C or the process receives SIGINT,
|
|
# SIGTERM or SIGABRT
|
|
updater.idle()
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|