Skip to content
Advertisement

CallbackQueryHandler reply message instead of edit message – Python Telegram Bot

I’m developing a bot using python telegram bot

I would like to be able to send a reply message from a InlineKeyboardButton instead of having to edit the current message.

def callback(update, context):

    # this works (but I don't want to edit the current message but sending a new one)
    update.callback_query.edit_message_text("response")
    
    # this unfortunately does not work
    update.message.reply_text("response") # None has no attribute 'reply_text'


...


updater.dispatcher.add_handler(CallbackQueryHandler(self.callback))

Advertisement

Answer

Only one of the optional attributes of Update will be present at a time. If the update from pressing an inline button, then update.callback_query is present, but update.message is not. However, update.callback_query.message may be present, depending on whether or not the message with the inline button was sent by the bot itself or via inline mode. If it is present, you can also access it via the convenience property update.effective_message.


Dislaimer: I’ currently the maintainer of python-telegram-bot

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement