mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2025-03-26 08:32:58 +01:00
Answer CQs and use edit_message_text in examples (#1721)
This commit is contained in:
parent
157652cfdf
commit
f94ea9acbb
3 changed files with 31 additions and 26 deletions
|
@ -29,6 +29,10 @@ def start(update, context):
|
||||||
def button(update, context):
|
def button(update, context):
|
||||||
query = update.callback_query
|
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="Selected option: {}".format(query.data))
|
query.edit_message_text(text="Selected option: {}".format(query.data))
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -55,8 +55,9 @@ def start_over(update, context):
|
||||||
"""Prompt same text & keyboard as `start` does but not as new message"""
|
"""Prompt same text & keyboard as `start` does but not as new message"""
|
||||||
# Get CallbackQuery from Update
|
# Get CallbackQuery from Update
|
||||||
query = update.callback_query
|
query = update.callback_query
|
||||||
# Get Bot from CallbackContext
|
# CallbackQueries need to be answered, even if no notification to the user is needed
|
||||||
bot = context.bot
|
# Some clients may have trouble otherwise. See https://core.telegram.org/bots/api#callbackquery
|
||||||
|
query.answer()
|
||||||
keyboard = [
|
keyboard = [
|
||||||
[InlineKeyboardButton("1", callback_data=str(ONE)),
|
[InlineKeyboardButton("1", callback_data=str(ONE)),
|
||||||
InlineKeyboardButton("2", callback_data=str(TWO))]
|
InlineKeyboardButton("2", callback_data=str(TWO))]
|
||||||
|
@ -65,9 +66,7 @@ def start_over(update, context):
|
||||||
# Instead of sending a new message, edit the message that
|
# Instead of sending a new message, edit the message that
|
||||||
# originated the CallbackQuery. This gives the feeling of an
|
# originated the CallbackQuery. This gives the feeling of an
|
||||||
# interactive menu.
|
# interactive menu.
|
||||||
bot.edit_message_text(
|
query.edit_message_text(
|
||||||
chat_id=query.message.chat_id,
|
|
||||||
message_id=query.message.message_id,
|
|
||||||
text="Start handler, Choose a route",
|
text="Start handler, Choose a route",
|
||||||
reply_markup=reply_markup
|
reply_markup=reply_markup
|
||||||
)
|
)
|
||||||
|
@ -77,15 +76,13 @@ def start_over(update, context):
|
||||||
def one(update, context):
|
def one(update, context):
|
||||||
"""Show new choice of buttons"""
|
"""Show new choice of buttons"""
|
||||||
query = update.callback_query
|
query = update.callback_query
|
||||||
bot = context.bot
|
query.answer()
|
||||||
keyboard = [
|
keyboard = [
|
||||||
[InlineKeyboardButton("3", callback_data=str(THREE)),
|
[InlineKeyboardButton("3", callback_data=str(THREE)),
|
||||||
InlineKeyboardButton("4", callback_data=str(FOUR))]
|
InlineKeyboardButton("4", callback_data=str(FOUR))]
|
||||||
]
|
]
|
||||||
reply_markup = InlineKeyboardMarkup(keyboard)
|
reply_markup = InlineKeyboardMarkup(keyboard)
|
||||||
bot.edit_message_text(
|
query.edit_message_text(
|
||||||
chat_id=query.message.chat_id,
|
|
||||||
message_id=query.message.message_id,
|
|
||||||
text="First CallbackQueryHandler, Choose a route",
|
text="First CallbackQueryHandler, Choose a route",
|
||||||
reply_markup=reply_markup
|
reply_markup=reply_markup
|
||||||
)
|
)
|
||||||
|
@ -95,15 +92,13 @@ def one(update, context):
|
||||||
def two(update, context):
|
def two(update, context):
|
||||||
"""Show new choice of buttons"""
|
"""Show new choice of buttons"""
|
||||||
query = update.callback_query
|
query = update.callback_query
|
||||||
bot = context.bot
|
query.answer()
|
||||||
keyboard = [
|
keyboard = [
|
||||||
[InlineKeyboardButton("1", callback_data=str(ONE)),
|
[InlineKeyboardButton("1", callback_data=str(ONE)),
|
||||||
InlineKeyboardButton("3", callback_data=str(THREE))]
|
InlineKeyboardButton("3", callback_data=str(THREE))]
|
||||||
]
|
]
|
||||||
reply_markup = InlineKeyboardMarkup(keyboard)
|
reply_markup = InlineKeyboardMarkup(keyboard)
|
||||||
bot.edit_message_text(
|
query.edit_message_text(
|
||||||
chat_id=query.message.chat_id,
|
|
||||||
message_id=query.message.message_id,
|
|
||||||
text="Second CallbackQueryHandler, Choose a route",
|
text="Second CallbackQueryHandler, Choose a route",
|
||||||
reply_markup=reply_markup
|
reply_markup=reply_markup
|
||||||
)
|
)
|
||||||
|
@ -113,15 +108,13 @@ def two(update, context):
|
||||||
def three(update, context):
|
def three(update, context):
|
||||||
"""Show new choice of buttons"""
|
"""Show new choice of buttons"""
|
||||||
query = update.callback_query
|
query = update.callback_query
|
||||||
bot = context.bot
|
query.answer()
|
||||||
keyboard = [
|
keyboard = [
|
||||||
[InlineKeyboardButton("Yes, let's do it again!", callback_data=str(ONE)),
|
[InlineKeyboardButton("Yes, let's do it again!", callback_data=str(ONE)),
|
||||||
InlineKeyboardButton("Nah, I've had enough ...", callback_data=str(TWO))]
|
InlineKeyboardButton("Nah, I've had enough ...", callback_data=str(TWO))]
|
||||||
]
|
]
|
||||||
reply_markup = InlineKeyboardMarkup(keyboard)
|
reply_markup = InlineKeyboardMarkup(keyboard)
|
||||||
bot.edit_message_text(
|
query.edit_message_text(
|
||||||
chat_id=query.message.chat_id,
|
|
||||||
message_id=query.message.message_id,
|
|
||||||
text="Third CallbackQueryHandler. Do want to start over?",
|
text="Third CallbackQueryHandler. Do want to start over?",
|
||||||
reply_markup=reply_markup
|
reply_markup=reply_markup
|
||||||
)
|
)
|
||||||
|
@ -132,15 +125,13 @@ def three(update, context):
|
||||||
def four(update, context):
|
def four(update, context):
|
||||||
"""Show new choice of buttons"""
|
"""Show new choice of buttons"""
|
||||||
query = update.callback_query
|
query = update.callback_query
|
||||||
bot = context.bot
|
query.answer()
|
||||||
keyboard = [
|
keyboard = [
|
||||||
[InlineKeyboardButton("2", callback_data=str(TWO)),
|
[InlineKeyboardButton("2", callback_data=str(TWO)),
|
||||||
InlineKeyboardButton("4", callback_data=str(FOUR))]
|
InlineKeyboardButton("4", callback_data=str(FOUR))]
|
||||||
]
|
]
|
||||||
reply_markup = InlineKeyboardMarkup(keyboard)
|
reply_markup = InlineKeyboardMarkup(keyboard)
|
||||||
bot.edit_message_text(
|
query.edit_message_text(
|
||||||
chat_id=query.message.chat_id,
|
|
||||||
message_id=query.message.message_id,
|
|
||||||
text="Fourth CallbackQueryHandler, Choose a route",
|
text="Fourth CallbackQueryHandler, Choose a route",
|
||||||
reply_markup=reply_markup
|
reply_markup=reply_markup
|
||||||
)
|
)
|
||||||
|
@ -151,10 +142,8 @@ def end(update, context):
|
||||||
"""Returns `ConversationHandler.END`, which tells the
|
"""Returns `ConversationHandler.END`, which tells the
|
||||||
ConversationHandler that the conversation is over"""
|
ConversationHandler that the conversation is over"""
|
||||||
query = update.callback_query
|
query = update.callback_query
|
||||||
bot = context.bot
|
query.answer()
|
||||||
bot.edit_message_text(
|
query.edit_message_text(
|
||||||
chat_id=query.message.chat_id,
|
|
||||||
message_id=query.message.message_id,
|
|
||||||
text="See you next time!"
|
text="See you next time!"
|
||||||
)
|
)
|
||||||
return ConversationHandler.END
|
return ConversationHandler.END
|
||||||
|
|
|
@ -66,6 +66,7 @@ def start(update, context):
|
||||||
|
|
||||||
# If we're starting over we don't need do send a new message
|
# If we're starting over we don't need do send a new message
|
||||||
if context.user_data.get(START_OVER):
|
if context.user_data.get(START_OVER):
|
||||||
|
update.callback_query.answer()
|
||||||
update.callback_query.edit_message_text(text=text, reply_markup=keyboard)
|
update.callback_query.edit_message_text(text=text, reply_markup=keyboard)
|
||||||
else:
|
else:
|
||||||
update.message.reply_text('Hi, I\'m FamiliyBot and here to help you gather information'
|
update.message.reply_text('Hi, I\'m FamiliyBot and here to help you gather information'
|
||||||
|
@ -83,6 +84,7 @@ def adding_self(update, context):
|
||||||
button = InlineKeyboardButton(text='Add info', callback_data=str(MALE))
|
button = InlineKeyboardButton(text='Add info', callback_data=str(MALE))
|
||||||
keyboard = InlineKeyboardMarkup.from_button(button)
|
keyboard = InlineKeyboardMarkup.from_button(button)
|
||||||
|
|
||||||
|
update.callback_query.answer()
|
||||||
update.callback_query.edit_message_text(text=text, reply_markup=keyboard)
|
update.callback_query.edit_message_text(text=text, reply_markup=keyboard)
|
||||||
|
|
||||||
return DESCRIBING_SELF
|
return DESCRIBING_SELF
|
||||||
|
@ -118,6 +120,7 @@ def show_data(update, context):
|
||||||
]]
|
]]
|
||||||
keyboard = InlineKeyboardMarkup(buttons)
|
keyboard = InlineKeyboardMarkup(buttons)
|
||||||
|
|
||||||
|
update.callback_query.answer()
|
||||||
update.callback_query.edit_message_text(text=text, reply_markup=keyboard)
|
update.callback_query.edit_message_text(text=text, reply_markup=keyboard)
|
||||||
ud[START_OVER] = True
|
ud[START_OVER] = True
|
||||||
|
|
||||||
|
@ -133,6 +136,8 @@ def stop(update, context):
|
||||||
|
|
||||||
def end(update, context):
|
def end(update, context):
|
||||||
"""End conversation from InlineKeyboardButton."""
|
"""End conversation from InlineKeyboardButton."""
|
||||||
|
update.callback_query.answer()
|
||||||
|
|
||||||
text = 'See you around!'
|
text = 'See you around!'
|
||||||
update.callback_query.edit_message_text(text=text)
|
update.callback_query.edit_message_text(text=text)
|
||||||
|
|
||||||
|
@ -151,6 +156,8 @@ def select_level(update, context):
|
||||||
InlineKeyboardButton(text='Back', callback_data=str(END))
|
InlineKeyboardButton(text='Back', callback_data=str(END))
|
||||||
]]
|
]]
|
||||||
keyboard = InlineKeyboardMarkup(buttons)
|
keyboard = InlineKeyboardMarkup(buttons)
|
||||||
|
|
||||||
|
update.callback_query.answer()
|
||||||
update.callback_query.edit_message_text(text=text, reply_markup=keyboard)
|
update.callback_query.edit_message_text(text=text, reply_markup=keyboard)
|
||||||
|
|
||||||
return SELECTING_LEVEL
|
return SELECTING_LEVEL
|
||||||
|
@ -172,8 +179,9 @@ def select_gender(update, context):
|
||||||
InlineKeyboardButton(text='Show data', callback_data=str(SHOWING)),
|
InlineKeyboardButton(text='Show data', callback_data=str(SHOWING)),
|
||||||
InlineKeyboardButton(text='Back', callback_data=str(END))
|
InlineKeyboardButton(text='Back', callback_data=str(END))
|
||||||
]]
|
]]
|
||||||
|
|
||||||
keyboard = InlineKeyboardMarkup(buttons)
|
keyboard = InlineKeyboardMarkup(buttons)
|
||||||
|
|
||||||
|
update.callback_query.answer()
|
||||||
update.callback_query.edit_message_text(text=text, reply_markup=keyboard)
|
update.callback_query.edit_message_text(text=text, reply_markup=keyboard)
|
||||||
|
|
||||||
return SELECTING_GENDER
|
return SELECTING_GENDER
|
||||||
|
@ -201,6 +209,8 @@ def select_feature(update, context):
|
||||||
if not context.user_data.get(START_OVER):
|
if not context.user_data.get(START_OVER):
|
||||||
context.user_data[FEATURES] = {GENDER: update.callback_query.data}
|
context.user_data[FEATURES] = {GENDER: update.callback_query.data}
|
||||||
text = 'Please select a feature to update.'
|
text = 'Please select a feature to update.'
|
||||||
|
|
||||||
|
update.callback_query.answer()
|
||||||
update.callback_query.edit_message_text(text=text, reply_markup=keyboard)
|
update.callback_query.edit_message_text(text=text, reply_markup=keyboard)
|
||||||
# But after we do that, we need to send a new message
|
# But after we do that, we need to send a new message
|
||||||
else:
|
else:
|
||||||
|
@ -215,6 +225,8 @@ def ask_for_input(update, context):
|
||||||
"""Prompt user to input data for selected feature."""
|
"""Prompt user to input data for selected feature."""
|
||||||
context.user_data[CURRENT_FEATURE] = update.callback_query.data
|
context.user_data[CURRENT_FEATURE] = update.callback_query.data
|
||||||
text = 'Okay, tell me.'
|
text = 'Okay, tell me.'
|
||||||
|
|
||||||
|
update.callback_query.answer()
|
||||||
update.callback_query.edit_message_text(text=text)
|
update.callback_query.edit_message_text(text=text)
|
||||||
|
|
||||||
return TYPING
|
return TYPING
|
||||||
|
|
Loading…
Add table
Reference in a new issue