2016-04-16 20:32:44 +02:00
|
|
|
#!/usr/bin/env python
|
2021-06-06 12:16:23 +02:00
|
|
|
# pylint: disable=C0116,W0613
|
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
|
|
|
|
https://git.io/JOmFw.
|
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
|
|
|
|
from telegram.ext import Updater, CommandHandler, CallbackQueryHandler, CallbackContext
|
2016-04-16 20:32:44 +02:00
|
|
|
|
2020-10-09 17:22:07 +02:00
|
|
|
logging.basicConfig(
|
|
|
|
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO
|
|
|
|
)
|
2017-10-20 20:24:00 +02:00
|
|
|
logger = logging.getLogger(__name__)
|
2016-04-16 20:32:44 +02:00
|
|
|
|
|
|
|
|
2021-06-06 12:16:23 +02:00
|
|
|
def start(update: Update, context: CallbackContext) -> None:
|
2021-05-27 20:34:58 +02:00
|
|
|
"""Sends a message with three inline buttons attached."""
|
2020-10-09 17:22:07 +02:00
|
|
|
keyboard = [
|
|
|
|
[
|
|
|
|
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
|
|
|
|
2016-09-24 15:32:22 +02:00
|
|
|
update.message.reply_text('Please choose:', reply_markup=reply_markup)
|
2016-04-16 20:32:44 +02:00
|
|
|
|
|
|
|
|
2021-06-06 12:16:23 +02:00
|
|
|
def button(update: Update, context: CallbackContext) -> 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
|
|
|
|
query.answer()
|
|
|
|
|
2020-11-23 22:09:29 +01:00
|
|
|
query.edit_message_text(text=f"Selected option: {query.data}")
|
2016-04-16 20:32:44 +02:00
|
|
|
|
|
|
|
|
2021-06-06 12:16:23 +02:00
|
|
|
def help_command(update: Update, context: CallbackContext) -> None:
|
2021-05-27 20:34:58 +02:00
|
|
|
"""Displays info on how to use the bot."""
|
2016-09-24 15:32:22 +02:00
|
|
|
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."""
|
2017-10-20 20:24:00 +02:00
|
|
|
# Create the Updater and pass it your bot's token.
|
2021-02-01 17:59:39 +01:00
|
|
|
updater = Updater("TOKEN")
|
2016-04-16 20:32:44 +02:00
|
|
|
|
2017-10-20 20:24:00 +02:00
|
|
|
updater.dispatcher.add_handler(CommandHandler('start', start))
|
|
|
|
updater.dispatcher.add_handler(CallbackQueryHandler(button))
|
2020-06-16 17:07:05 +02:00
|
|
|
updater.dispatcher.add_handler(CommandHandler('help', help_command))
|
2016-07-20 00:15:03 +02:00
|
|
|
|
2017-10-20 20:24:00 +02:00
|
|
|
# Start the Bot
|
|
|
|
updater.start_polling()
|
2016-04-16 20:32:44 +02:00
|
|
|
|
2017-10-20 20:24:00 +02:00
|
|
|
# Run the bot until the user presses Ctrl-C or the process receives SIGINT,
|
|
|
|
# SIGTERM or SIGABRT
|
|
|
|
updater.idle()
|
2016-04-16 20:32:44 +02:00
|
|
|
|
|
|
|
|
2017-10-20 20:24:00 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|