2019-09-13 20:43:03 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""Simple inline keyboard bot with multiple CallbackQueryHandlers.
|
|
|
|
|
|
|
|
This Bot uses the Updater class to handle the bot.
|
|
|
|
First, a few callback functions are defined as callback query handler. Then, those functions are
|
|
|
|
passed to the Dispatcher and registered at their respective places.
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
|
|
|
|
from telegram.ext import Updater, CommandHandler, CallbackQueryHandler, ConversationHandler
|
|
|
|
import logging
|
|
|
|
|
|
|
|
# Enable logging
|
|
|
|
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
|
|
|
level=logging.INFO)
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
# Stages
|
|
|
|
FIRST, SECOND = range(2)
|
|
|
|
# Callback data
|
|
|
|
ONE, TWO, THREE, FOUR = range(4)
|
|
|
|
|
|
|
|
|
|
|
|
def start(update, context):
|
|
|
|
"""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 = [
|
|
|
|
[InlineKeyboardButton("1", callback_data=str(ONE)),
|
|
|
|
InlineKeyboardButton("2", callback_data=str(TWO))]
|
|
|
|
]
|
|
|
|
reply_markup = InlineKeyboardMarkup(keyboard)
|
|
|
|
# Send message with text and appended InlineKeyboard
|
|
|
|
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
|
2019-09-13 20:43:03 +02:00
|
|
|
return FIRST
|
|
|
|
|
|
|
|
|
|
|
|
def start_over(update, context):
|
|
|
|
"""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
|
|
|
|
query.answer()
|
2019-09-13 20:43:03 +02:00
|
|
|
keyboard = [
|
|
|
|
[InlineKeyboardButton("1", callback_data=str(ONE)),
|
|
|
|
InlineKeyboardButton("2", callback_data=str(TWO))]
|
|
|
|
]
|
|
|
|
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.
|
2020-03-28 12:07:23 +01:00
|
|
|
query.edit_message_text(
|
2019-09-13 20:43:03 +02:00
|
|
|
text="Start handler, Choose a route",
|
|
|
|
reply_markup=reply_markup
|
|
|
|
)
|
|
|
|
return FIRST
|
|
|
|
|
|
|
|
|
|
|
|
def one(update, context):
|
|
|
|
"""Show new choice of buttons"""
|
|
|
|
query = update.callback_query
|
2020-03-28 12:07:23 +01:00
|
|
|
query.answer()
|
2019-09-13 20:43:03 +02:00
|
|
|
keyboard = [
|
|
|
|
[InlineKeyboardButton("3", callback_data=str(THREE)),
|
|
|
|
InlineKeyboardButton("4", callback_data=str(FOUR))]
|
|
|
|
]
|
|
|
|
reply_markup = InlineKeyboardMarkup(keyboard)
|
2020-03-28 12:07:23 +01:00
|
|
|
query.edit_message_text(
|
2019-09-13 20:43:03 +02:00
|
|
|
text="First CallbackQueryHandler, Choose a route",
|
|
|
|
reply_markup=reply_markup
|
|
|
|
)
|
|
|
|
return FIRST
|
|
|
|
|
|
|
|
|
|
|
|
def two(update, context):
|
|
|
|
"""Show new choice of buttons"""
|
|
|
|
query = update.callback_query
|
2020-03-28 12:07:23 +01:00
|
|
|
query.answer()
|
2019-09-13 20:43:03 +02:00
|
|
|
keyboard = [
|
|
|
|
[InlineKeyboardButton("1", callback_data=str(ONE)),
|
|
|
|
InlineKeyboardButton("3", callback_data=str(THREE))]
|
|
|
|
]
|
|
|
|
reply_markup = InlineKeyboardMarkup(keyboard)
|
2020-03-28 12:07:23 +01:00
|
|
|
query.edit_message_text(
|
2019-09-13 20:43:03 +02:00
|
|
|
text="Second CallbackQueryHandler, Choose a route",
|
|
|
|
reply_markup=reply_markup
|
|
|
|
)
|
|
|
|
return FIRST
|
|
|
|
|
|
|
|
|
|
|
|
def three(update, context):
|
|
|
|
"""Show new choice of buttons"""
|
|
|
|
query = update.callback_query
|
2020-03-28 12:07:23 +01:00
|
|
|
query.answer()
|
2019-09-13 20:43:03 +02:00
|
|
|
keyboard = [
|
|
|
|
[InlineKeyboardButton("Yes, let's do it again!", callback_data=str(ONE)),
|
|
|
|
InlineKeyboardButton("Nah, I've had enough ...", callback_data=str(TWO))]
|
|
|
|
]
|
|
|
|
reply_markup = InlineKeyboardMarkup(keyboard)
|
2020-03-28 12:07:23 +01:00
|
|
|
query.edit_message_text(
|
2019-09-13 20:43:03 +02:00
|
|
|
text="Third CallbackQueryHandler. Do want to start over?",
|
|
|
|
reply_markup=reply_markup
|
|
|
|
)
|
|
|
|
# Transfer to conversation state `SECOND`
|
|
|
|
return SECOND
|
|
|
|
|
|
|
|
|
|
|
|
def four(update, context):
|
|
|
|
"""Show new choice of buttons"""
|
|
|
|
query = update.callback_query
|
2020-03-28 12:07:23 +01:00
|
|
|
query.answer()
|
2019-09-13 20:43:03 +02:00
|
|
|
keyboard = [
|
|
|
|
[InlineKeyboardButton("2", callback_data=str(TWO)),
|
|
|
|
InlineKeyboardButton("4", callback_data=str(FOUR))]
|
|
|
|
]
|
|
|
|
reply_markup = InlineKeyboardMarkup(keyboard)
|
2020-03-28 12:07:23 +01:00
|
|
|
query.edit_message_text(
|
2019-09-13 20:43:03 +02:00
|
|
|
text="Fourth CallbackQueryHandler, Choose a route",
|
|
|
|
reply_markup=reply_markup
|
|
|
|
)
|
|
|
|
return FIRST
|
|
|
|
|
|
|
|
|
|
|
|
def end(update, context):
|
|
|
|
"""Returns `ConversationHandler.END`, which tells the
|
|
|
|
ConversationHandler that the conversation is over"""
|
|
|
|
query = update.callback_query
|
2020-03-28 12:07:23 +01:00
|
|
|
query.answer()
|
|
|
|
query.edit_message_text(
|
2019-09-13 20:43:03 +02:00
|
|
|
text="See you next time!"
|
|
|
|
)
|
|
|
|
return ConversationHandler.END
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
# Create the Updater and pass it your bot's token.
|
|
|
|
updater = Updater("TOKEN", use_context=True)
|
|
|
|
|
|
|
|
# Get the dispatcher to register handlers
|
|
|
|
dp = updater.dispatcher
|
|
|
|
|
|
|
|
# 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(
|
|
|
|
entry_points=[CommandHandler('start', start)],
|
|
|
|
states={
|
|
|
|
FIRST: [CallbackQueryHandler(one, pattern='^' + str(ONE) + '$'),
|
|
|
|
CallbackQueryHandler(two, pattern='^' + str(TWO) + '$'),
|
|
|
|
CallbackQueryHandler(three, pattern='^' + str(THREE) + '$'),
|
|
|
|
CallbackQueryHandler(four, pattern='^' + str(FOUR) + '$')],
|
|
|
|
SECOND: [CallbackQueryHandler(start_over, pattern='^' + str(ONE) + '$'),
|
|
|
|
CallbackQueryHandler(end, pattern='^' + str(TWO) + '$')]
|
|
|
|
},
|
|
|
|
fallbacks=[CommandHandler('start', start)]
|
|
|
|
)
|
|
|
|
|
2019-10-11 20:10:21 +02:00
|
|
|
# Add ConversationHandler to dispatcher that will be used for handling
|
2019-09-13 20:43:03 +02:00
|
|
|
# updates
|
|
|
|
dp.add_handler(conv_handler)
|
|
|
|
|
|
|
|
# Start the Bot
|
|
|
|
updater.start_polling()
|
|
|
|
|
|
|
|
# Run the bot until you press Ctrl-C or the process receives SIGINT,
|
|
|
|
# SIGTERM or SIGABRT. This should be used most of the time, since
|
|
|
|
# start_polling() is non-blocking and will stop the bot gracefully.
|
|
|
|
updater.idle()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|