如何在 python-telegram-bot 中点击 InlineKeyboardButton 添加照片到消息?

问题描述 投票:0回答:1

所以我有一个使用 InlineKeyboard 并更新其消息的机器人。

问题很简单,是否可以在单击 InlineKeyboardButton 时将图像添加到消息中?我也可以用同样的方式清除邮件附件吗?

我的键盘调度程序方法:

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))

这是我为键盘按钮之一调度的方法之一,我想附加图像上传:

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
    )
python telegram telegram-bot python-telegram-bot
1个回答
0
投票

媒体消息不能编辑为纯文本消息,反之亦然。 Bot API 没有为此提供任何方法。请注意,这独立于您正在使用的 python 库(您标记为

python-telegram-bot
),但这是 Telegram 本身的限制。

© www.soinside.com 2019 - 2024. All rights reserved.