2019-09-13 20:43:03 +02:00
|
|
|
#!/usr/bin/env python
|
2022-05-15 14:08:40 +02:00
|
|
|
# pylint: disable=unused-argument, wrong-import-position
|
2020-10-31 16:33:34 +01:00
|
|
|
# This program is dedicated to the public domain under the CC0 license.
|
|
|
|
|
2019-09-13 20:43:03 +02:00
|
|
|
"""Simple inline keyboard bot with multiple CallbackQueryHandlers.
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
This Bot uses the Application class to handle the bot.
|
2019-09-13 20:43:03 +02:00
|
|
|
First, a few callback functions are defined as callback query handler. Then, those functions are
|
2022-04-24 12:38:09 +02:00
|
|
|
passed to the Application and registered at their respective places.
|
2019-09-13 20:43:03 +02:00
|
|
|
Then, the bot is started and runs until we press Ctrl-C on the command line.
|
|
|
|
Usage:
|
|
|
|
Example of a bot that uses inline keyboard that has multiple CallbackQueryHandlers arranged in a
|
|
|
|
ConversationHandler.
|
|
|
|
Send /start to initiate the conversation.
|
|
|
|
Press Ctrl-C on the command line to stop the bot.
|
|
|
|
"""
|
|
|
|
import logging
|
2022-05-05 09:27:54 +02:00
|
|
|
|
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, "
|
2022-06-09 17:22:32 +02:00
|
|
|
f"visit https://docs.python-telegram-bot.org/en/v{TG_VER}/examples.html"
|
2022-05-15 14:08:40 +02:00
|
|
|
)
|
2020-10-31 16:33:34 +01:00
|
|
|
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update
|
|
|
|
from telegram.ext import (
|
2022-04-24 12:38:09 +02:00
|
|
|
Application,
|
2022-05-05 09:27:54 +02:00
|
|
|
CallbackQueryHandler,
|
|
|
|
CommandHandler,
|
2022-05-12 19:36:25 +02:00
|
|
|
ContextTypes,
|
2022-05-05 09:27:54 +02:00
|
|
|
ConversationHandler,
|
2020-10-31 16:33:34 +01:00
|
|
|
)
|
2019-09-13 20:43:03 +02:00
|
|
|
|
|
|
|
# Enable logging
|
2020-10-09 17:22:07 +02:00
|
|
|
logging.basicConfig(
|
2022-05-05 17:40:22 +02:00
|
|
|
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
|
2020-10-09 17:22:07 +02:00
|
|
|
)
|
2019-09-13 20:43:03 +02:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
# Stages
|
2022-04-24 12:38:09 +02:00
|
|
|
START_ROUTES, END_ROUTES = range(2)
|
2019-09-13 20:43:03 +02:00
|
|
|
# Callback data
|
|
|
|
ONE, TWO, THREE, FOUR = range(4)
|
|
|
|
|
|
|
|
|
2022-05-12 19:36:25 +02:00
|
|
|
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
|
2019-09-13 20:43:03 +02:00
|
|
|
"""Send message on `/start`."""
|
|
|
|
# Get user that sent /start and log his name
|
|
|
|
user = update.message.from_user
|
|
|
|
logger.info("User %s started the conversation.", user.first_name)
|
|
|
|
# Build InlineKeyboard where each button has a displayed text
|
|
|
|
# and a string as callback_data
|
|
|
|
# The keyboard is a list of button rows, where each row is in turn
|
2019-10-11 20:10:21 +02:00
|
|
|
# a list (hence `[[...]]`).
|
2019-09-13 20:43:03 +02:00
|
|
|
keyboard = [
|
2020-10-09 17:22:07 +02:00
|
|
|
[
|
|
|
|
InlineKeyboardButton("1", callback_data=str(ONE)),
|
|
|
|
InlineKeyboardButton("2", callback_data=str(TWO)),
|
|
|
|
]
|
2019-09-13 20:43:03 +02:00
|
|
|
]
|
|
|
|
reply_markup = InlineKeyboardMarkup(keyboard)
|
|
|
|
# Send message with text and appended InlineKeyboard
|
2022-04-24 12:38:09 +02:00
|
|
|
await update.message.reply_text("Start handler, Choose a route", reply_markup=reply_markup)
|
2019-10-11 20:10:21 +02:00
|
|
|
# Tell ConversationHandler that we're in state `FIRST` now
|
2022-04-24 12:38:09 +02:00
|
|
|
return START_ROUTES
|
2019-09-13 20:43:03 +02:00
|
|
|
|
|
|
|
|
2022-05-12 19:36:25 +02:00
|
|
|
async def start_over(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
|
2019-09-13 20:43:03 +02:00
|
|
|
"""Prompt same text & keyboard as `start` does but not as new message"""
|
|
|
|
# Get CallbackQuery from Update
|
|
|
|
query = update.callback_query
|
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()
|
2019-09-13 20:43:03 +02:00
|
|
|
keyboard = [
|
2020-10-09 17:22:07 +02:00
|
|
|
[
|
|
|
|
InlineKeyboardButton("1", callback_data=str(ONE)),
|
|
|
|
InlineKeyboardButton("2", callback_data=str(TWO)),
|
|
|
|
]
|
2019-09-13 20:43:03 +02:00
|
|
|
]
|
|
|
|
reply_markup = InlineKeyboardMarkup(keyboard)
|
|
|
|
# Instead of sending a new message, edit the message that
|
|
|
|
# originated the CallbackQuery. This gives the feeling of an
|
|
|
|
# interactive menu.
|
2022-04-24 12:38:09 +02:00
|
|
|
await query.edit_message_text(text="Start handler, Choose a route", reply_markup=reply_markup)
|
|
|
|
return START_ROUTES
|
2019-09-13 20:43:03 +02:00
|
|
|
|
|
|
|
|
2022-05-12 19:36:25 +02:00
|
|
|
async def one(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
|
2019-09-13 20:43:03 +02:00
|
|
|
"""Show new choice of buttons"""
|
|
|
|
query = update.callback_query
|
2022-04-24 12:38:09 +02:00
|
|
|
await query.answer()
|
2019-09-13 20:43:03 +02:00
|
|
|
keyboard = [
|
2020-10-09 17:22:07 +02:00
|
|
|
[
|
|
|
|
InlineKeyboardButton("3", callback_data=str(THREE)),
|
|
|
|
InlineKeyboardButton("4", callback_data=str(FOUR)),
|
|
|
|
]
|
2019-09-13 20:43:03 +02:00
|
|
|
]
|
|
|
|
reply_markup = InlineKeyboardMarkup(keyboard)
|
2022-04-24 12:38:09 +02:00
|
|
|
await query.edit_message_text(
|
2020-10-09 17:22:07 +02:00
|
|
|
text="First CallbackQueryHandler, Choose a route", reply_markup=reply_markup
|
2019-09-13 20:43:03 +02:00
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
return START_ROUTES
|
2019-09-13 20:43:03 +02:00
|
|
|
|
|
|
|
|
2022-05-12 19:36:25 +02:00
|
|
|
async def two(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
|
2019-09-13 20:43:03 +02:00
|
|
|
"""Show new choice of buttons"""
|
|
|
|
query = update.callback_query
|
2022-04-24 12:38:09 +02:00
|
|
|
await query.answer()
|
2019-09-13 20:43:03 +02:00
|
|
|
keyboard = [
|
2020-10-09 17:22:07 +02:00
|
|
|
[
|
|
|
|
InlineKeyboardButton("1", callback_data=str(ONE)),
|
|
|
|
InlineKeyboardButton("3", callback_data=str(THREE)),
|
|
|
|
]
|
2019-09-13 20:43:03 +02:00
|
|
|
]
|
|
|
|
reply_markup = InlineKeyboardMarkup(keyboard)
|
2022-04-24 12:38:09 +02:00
|
|
|
await query.edit_message_text(
|
2020-10-09 17:22:07 +02:00
|
|
|
text="Second CallbackQueryHandler, Choose a route", reply_markup=reply_markup
|
2019-09-13 20:43:03 +02:00
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
return START_ROUTES
|
2019-09-13 20:43:03 +02:00
|
|
|
|
|
|
|
|
2022-05-12 19:36:25 +02:00
|
|
|
async def three(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
|
2022-04-24 12:38:09 +02:00
|
|
|
"""Show new choice of buttons. This is the end point of the conversation."""
|
2019-09-13 20:43:03 +02:00
|
|
|
query = update.callback_query
|
2022-04-24 12:38:09 +02:00
|
|
|
await query.answer()
|
2019-09-13 20:43:03 +02:00
|
|
|
keyboard = [
|
2020-10-09 17:22:07 +02:00
|
|
|
[
|
|
|
|
InlineKeyboardButton("Yes, let's do it again!", callback_data=str(ONE)),
|
|
|
|
InlineKeyboardButton("Nah, I've had enough ...", callback_data=str(TWO)),
|
|
|
|
]
|
2019-09-13 20:43:03 +02:00
|
|
|
]
|
|
|
|
reply_markup = InlineKeyboardMarkup(keyboard)
|
2022-04-24 12:38:09 +02:00
|
|
|
await query.edit_message_text(
|
2020-10-09 17:22:07 +02:00
|
|
|
text="Third CallbackQueryHandler. Do want to start over?", reply_markup=reply_markup
|
2019-09-13 20:43:03 +02:00
|
|
|
)
|
|
|
|
# Transfer to conversation state `SECOND`
|
2022-04-24 12:38:09 +02:00
|
|
|
return END_ROUTES
|
2019-09-13 20:43:03 +02:00
|
|
|
|
|
|
|
|
2022-05-12 19:36:25 +02:00
|
|
|
async def four(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
|
2019-09-13 20:43:03 +02:00
|
|
|
"""Show new choice of buttons"""
|
|
|
|
query = update.callback_query
|
2022-04-24 12:38:09 +02:00
|
|
|
await query.answer()
|
2019-09-13 20:43:03 +02:00
|
|
|
keyboard = [
|
2020-10-09 17:22:07 +02:00
|
|
|
[
|
|
|
|
InlineKeyboardButton("2", callback_data=str(TWO)),
|
2021-03-28 18:53:44 +02:00
|
|
|
InlineKeyboardButton("3", callback_data=str(THREE)),
|
2020-10-09 17:22:07 +02:00
|
|
|
]
|
2019-09-13 20:43:03 +02:00
|
|
|
]
|
|
|
|
reply_markup = InlineKeyboardMarkup(keyboard)
|
2022-04-24 12:38:09 +02:00
|
|
|
await query.edit_message_text(
|
2020-10-09 17:22:07 +02:00
|
|
|
text="Fourth CallbackQueryHandler, Choose a route", reply_markup=reply_markup
|
2019-09-13 20:43:03 +02:00
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
return START_ROUTES
|
2019-09-13 20:43:03 +02:00
|
|
|
|
|
|
|
|
2022-05-12 19:36:25 +02:00
|
|
|
async def end(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
|
2019-09-13 20:43:03 +02:00
|
|
|
"""Returns `ConversationHandler.END`, which tells the
|
2021-05-27 20:34:58 +02:00
|
|
|
ConversationHandler that the conversation is over.
|
|
|
|
"""
|
2019-09-13 20:43:03 +02:00
|
|
|
query = update.callback_query
|
2022-04-24 12:38:09 +02:00
|
|
|
await query.answer()
|
|
|
|
await query.edit_message_text(text="See you next time!")
|
2019-09-13 20:43:03 +02:00
|
|
|
return ConversationHandler.END
|
|
|
|
|
|
|
|
|
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()
|
2019-09-13 20:43:03 +02:00
|
|
|
|
|
|
|
# Setup conversation handler with the states FIRST and SECOND
|
2019-10-11 20:10:21 +02:00
|
|
|
# Use the pattern parameter to pass CallbackQueries with specific
|
2019-09-13 20:43:03 +02:00
|
|
|
# data pattern to the corresponding handlers.
|
|
|
|
# ^ means "start of line/string"
|
|
|
|
# $ means "end of line/string"
|
|
|
|
# So ^ABC$ will only allow 'ABC'
|
|
|
|
conv_handler = ConversationHandler(
|
2022-05-05 17:40:22 +02:00
|
|
|
entry_points=[CommandHandler("start", start)],
|
2019-09-13 20:43:03 +02:00
|
|
|
states={
|
2022-04-24 12:38:09 +02:00
|
|
|
START_ROUTES: [
|
2022-05-05 17:40:22 +02:00
|
|
|
CallbackQueryHandler(one, pattern="^" + str(ONE) + "$"),
|
|
|
|
CallbackQueryHandler(two, pattern="^" + str(TWO) + "$"),
|
|
|
|
CallbackQueryHandler(three, pattern="^" + str(THREE) + "$"),
|
|
|
|
CallbackQueryHandler(four, pattern="^" + str(FOUR) + "$"),
|
2020-10-09 17:22:07 +02:00
|
|
|
],
|
2022-04-24 12:38:09 +02:00
|
|
|
END_ROUTES: [
|
2022-05-05 17:40:22 +02:00
|
|
|
CallbackQueryHandler(start_over, pattern="^" + str(ONE) + "$"),
|
|
|
|
CallbackQueryHandler(end, pattern="^" + str(TWO) + "$"),
|
2020-10-09 17:22:07 +02:00
|
|
|
],
|
2019-09-13 20:43:03 +02:00
|
|
|
},
|
2022-05-05 17:40:22 +02:00
|
|
|
fallbacks=[CommandHandler("start", start)],
|
2019-09-13 20:43:03 +02:00
|
|
|
)
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
# Add ConversationHandler to application that will be used for handling updates
|
|
|
|
application.add_handler(conv_handler)
|
2019-09-13 20:43:03 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
# Run the bot until the user presses Ctrl-C
|
|
|
|
application.run_polling()
|
2019-09-13 20:43:03 +02:00
|
|
|
|
|
|
|
|
2022-05-05 17:40:22 +02:00
|
|
|
if __name__ == "__main__":
|
2019-09-13 20:43:03 +02:00
|
|
|
main()
|