skip to Main Content

So I have a bot that uses InlineKeyboard and updates it’s message.

The question is pretty simple, is it possible to add image to message on InlineKeyboardButton click? And also can I clear message attachments same way?

My keyboard dispatcher method:

async def keyboard_dispatcher(update: Update, context: ContextTypes.DEFAULT_TYPE):
    query = update.callback_query
    query.answer()
    user = update.effective_user

    try:
        button_data = keyboard_callback_schema.loads(query.data)
    except ValidationError as e:
        logging.error(
            f"Validation error ocurred while processing keyboard callback data by user {user.username}, seems like it's wrong callback!",
            exc_info=True,
        )
        await handle_invalid_button(update, context, is_exception=True)
        return

    try:
        await state_methods[button_data["state"]](update, context, data=button_data['data'])
    except KeyError:
        await handle_invalid_button(update, context, is_exception=True)
        return
    except Exception as e:
        await handle_invalid_button(update, context, is_exception=True)
        return

def add_handlers(application: Application):
    application.add_handler(CommandHandler("start", start))
    application.add_handler(CommandHandler("menu", menu))
    application.add_handler(CallbackQueryHandler(handle_invalid_button, pattern=InvalidCallbackData))
    application.add_handler(CallbackQueryHandler(keyboard_dispatcher))

Here’s one of my dispatched method for one of keyboard’s button, that i want to append with image upload:

async def product_view(
    update: Update, context: ContextTypes.DEFAULT_TYPE, data: dict[str, any]
):
    """Product view"""
    query = update.callback_query

    product_slug = data["product"]

    product = await api.get.product(product_slug)

    text = "product description"

    

    keyboard = [
        [
            InlineKeyboardButton(
                text="add to cart",
                callback_data=generate_button_callback_data(
                    MenuStates.EMPTY_ACTION,
                )
            )
        ],
        [
            InlineKeyboardButton(
                text="back to categories",
                callback_data=generate_button_callback_data(
                    state=MenuStates.CATEGORIES_SELECT
                )
            )
        ]
    ]

    await query.edit_message_text(
        text=text,
        reply_markup=InlineKeyboardMarkup(keyboard),
        parse_mode=ParseMode.HTML
    )

2

Answers


  1. Media messages can not be edited to be text-only messages or vice versa. The Bot API does not provide any methods for that. Note that this is independent of the python library that you’re using (you tagged python-telegram-bot), but is a restriction of Telegram itself.

    Login or Signup to reply.
  2. You can’t add to text message some media by editing telegram method, but in this situation, you should just delete previous message and send new with photo or video.

    On the other hand, if you sent a message with a picture, you can use the "editMessageMedia" method to remove the text from the previous messages, but not the picture. Method you can find here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search