gpt4 book ai didi

java - 如何在用户单击时更改 InlineKeyboardButton 标签?

转载 作者:太空宇宙 更新时间:2023-11-04 10:41:14 25 4
gpt4 key购买 nike

我想在单击后更改由 setText() 方法初始化的 InlineKeyboardButton 对象的标签

例如,通过单击按钮将“label”字符串更改为“label-1”。

这是我的一段代码:

if (update.hasMessage() && update.getMessage().hasText()) {
if (update.getMessage().getText().equals("/start")) {
long chat_id = update.getMessage().getChatId();
message = new SendMessage()
.setChatId(chat_id)
.setText("You send /start");
InlineKeyboardButton dk1=new InlineKeyboardButton().setText("labe1").setCallbackData("change_the_label");
InlineKeyboardMarkup markupInline = new InlineKeyboardMarkup();
List<List<InlineKeyboardButton>> rowsInline = new ArrayList<>();
List<InlineKeyboardButton> rowInline = new ArrayList<>();
rowInline.add(dk1);
rowsInline.add(rowInline);
markupInline.setKeyboard(rowsInline);
message.setReplyMarkup(markupInline);
try {execute(message); catch (TelegramApiException e) {e.printStackTrace();}}}

最佳答案

我通过添加这段代码解决了这个问题使用 EditMessageReplyMarkup 对象 编辑现有的内联键盘:

   String call_data = update0.getCallbackQuery().getData();
long message_id = update0.getCallbackQuery().getMessage().getMessageId();
long chat_id = update0.getCallbackQuery().getMessage().getChatId();
String inline_message_id = update0.getCallbackQuery().getInlineMessageId();
if (call_data.equals("change_the_label")) {
String answer = "Updated message text";
EditMessageReplyMarkup new_message = new EditMessageReplyMarkup()
.setChatId(chat_id).setMessageId(toIntExact(message_id))
.setInlineMessageId(inline_message_id);
InlineKeyboardButton dk1=new InlineKeyboardButton();
dk1.setText("label1");
dk1.setCallbackData("change_the_label");
InlineKeyboardMarkup markupInline = new InlineKeyboardMarkup();
List<List<InlineKeyboardButton>> rowsInline = new ArrayList<>();
List<InlineKeyboardButton> rowInline = new ArrayList<>();

rowInline.add(dk1);

rowsInline.add(rowInline);

markupInline.setKeyboard(rowsInline);
new_message.setReplyMarkup(markupInline);

try {editMessageReplyMarkup(new_message);

} catch (TelegramApiException e) {
e.printStackTrace();
}

关于java - 如何在用户单击时更改 InlineKeyboardButton 标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48967168/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com