'How to delete answered inline query when pressing the InlineKeyboardButton? (python-telegram-bot)

In a nutshell, I want to add "Cancel" button into InlineKeyboardMarkup. But I have problems when adding it when using inline mode.

I want to delete a message created by InlineQueryResultArticle in InlineQueryHandler's function. I'm passing reply_markup with some InlineKeyboardButtons. The last button has text='Cancel' and callback_data that is an object of my custom class CancelMessage() (I don't think the problem is related to this class).

In main function there is CallbackQueryHandler(cancel, pattern=CancelMessage) and InlineQueryHandler(post_issue_inline)

Logger says that update.effective_chat and update.message are both None, thus, I can't use context.bot.delete_message(). Possible reason that I think I can't delete message is because

  1. when I type @bot's_username in telegram app, the text I'm typing pops up, then I push/choose it, then the message (that I want to delete) with buttons is sent by me, not by bot
  2. this leads to None type of update.message (and maybe update.effective_chat)
  3. -> I can't delete this message.

I've tried a lot of variants of deleting. I was only able to edit the text of this message by update.callback_query.edit_message_text('some text')

def cancel(update: Update, context: CallbackContext) -> None:
    context.bot.delete_message(update.effective_chat.id, update.callback_query.message.message_id) # doesn't work

def post_issue_inline(update: Update, context: CallbackContext) -> None:
    query = update.inline_query.query
    if query != '':
        if '.' in query:
            summary = query[:query.find('.')].strip()
            description = query[query.find('.'):].strip()
        else:
            summary = query
            description = ''
        project = SetProjectMessage(summary, description)
        names_ids = projects_name_id(update, context, project)
        article = InlineQueryResultArticle(
            id=str(uuid4()),
            title=query,
            input_message_content=InputTextMessageContent(query),
            reply_markup=InlineKeyboardMarkup(make_keyboard(names_ids, extra_buttons=1))
        )
        update.inline_query.answer([article])

Function projects_name_id(...) returns me a list of tuples [(to_text, to_callback_data), ...] from which I make_keyboard() with InlineKeyboardButtons

I found that I can delete message if it was sent by bot. How can I send message with reply_markup (that was in InlineQueryResultArticle) by bot in post_issue_inline?



Solution 1:[1]

The issue here is that the bot may not be part of the chat where the message is sent. E.g. you can use @botusername … in a private chat with another person. In these cases, the bot can't possibly be allowed to delete messages of chats that it's not even a member of. This is probably the reason whay deleteMessage does not accept an inline_message_id (in contrast to edit_message_text).

Note that even in cases where the bot is a member of the chat where the message is sent, update.callback_query.message will still be None.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 CallMeStag