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