如何使用“Python-Telegram-bot”在“InlineKeyboard”上使用“sendDice”

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

我正在尝试做一个

InlineKeyboard
,它有多个选项,然后根据您按下的按钮发送骰子

当我使用

CallbackQuery
时,唯一可用于发送某种响应的方法是
edit_text_
并且没有任何类型的“骰子”或贴纸可发送。有没有其他方法来处理按钮,以便我可以将骰子发送回用户?

async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
keyboard = [
    [
        InlineKeyboardButton("Dice", callback_data="1"),
        InlineKeyboardButton("Basket", callback_data="2"),
    ],
    [InlineKeyboardButton("test", callback_data="3")],
]

reply_markup = InlineKeyboardMarkup(keyboard)

await update.message.reply_text("Please choose:", reply_markup=reply_markup)



async def button(update: Update, context: ContextTypes.DEFAULT_TYPE):
    query = update.callback_query

    await query.answer()
    if query.data == '1':
        await update.callback_query.edit_message_text(query.data)
        #I want to do so that here it sends the dice back
    
    else:
        await query.edit_message_text(text=f"Selected option: {query.data}")
telegram telegram-bot python-telegram-bot
1个回答
0
投票

请注意,内联按钮可以附加到用户发送的消息中 - 即当这些消息通过内联模式生成时。在这些情况下,消息可以在机器人甚至不是参与者的聊天中发送。因此,

CallbackQuery
类没有任何直接的快捷方式来发送消息作为回复。

但是,如果回调查询源自附加到机器人本身发送的消息的按钮,则属性

CallbackQuery.message
Message
的实例,您可以使用
Message.reply_text 等快捷方式方法

除此之外,您当然可以调用

Bot
类的任何方法。该类的实例始终可以通过
context.bot
获得。


免责声明:我目前是

python-telegram-bot
的维护者。

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