gpt4 book ai didi

java - 如何使用 Java 为 Telegram Bot 键盘编写一组自定义命令?

转载 作者:行者123 更新时间:2023-12-02 03:20:29 35 4
gpt4 key购买 nike

因此,我查看了我所喜欢的黑手党帮派机器人的示例,他们的自定义键盘让我感到惊讶:

enter image description here

我发现我必须添加这个 Simple Java API for Telegram Bots jar 才能实现它。我确实添加了 jar ,但我不知道该怎么做。我想使用 Java 创建自定义键盘。这是源代码:

public class Pizza {
private static final Logger LOG = Logger.getLogger(Pizza.class.getName());

public static void main(String... args) throws Exception {
TelegramBotsApi telegramBotsApi = new TelegramBotsApi();
telegramBotsApi.registerBot(new TelegramLongPollingBot() {

@Override
public void onUpdateReceived(Update update) {
Message message = update.getMessage();
Long chatId = message.getChatId();
String input = message.getText();
if ("/recommend".equals(input)) {
SendMessage request = new SendMessage();
request.setChatId(chatId.toString());
request.setText("Peperoni");
try {
sendMessage(request);
} catch (TelegramApiException e) {
LOG.log(Level.SEVERE, "Could not send message", e);
}
}
}

@Override
public String getBotUsername() {
return "USERNAME_BOT";
}

@Override
public String getBotToken() {
return "BOT_TOKEN";
}
});
}
}

最佳答案

https://github.com/rubenlagus/TelegramBotsExample 有一些该库的使用示例。更准确地说,您可以在https://github.com/rubenlagus/TelegramBotsExample/blob/master/src/main/java/org/telegram/updateshandlers/WeatherHandlers.java查看自定义键盘。 (@weatherbot)。

在该文件中,您可以看到,例如,设置键盘是通过以下方法生成的:

private static ReplyKeyboardMarkup getSettingsKeyboard(String language) {
ReplyKeyboardMarkup replyKeyboardMarkup = new ReplyKeyboardMarkup();
replyKeyboardMarkup.setSelective(true);
replyKeyboardMarkup.setResizeKeyboard(true);
replyKeyboardMarkup.setOneTimeKeyboad(false);

List<KeyboardRow> keyboard = new ArrayList<>();
KeyboardRow keyboardFirstRow = new KeyboardRow();
keyboardFirstRow.add(getLanguagesCommand(language));
keyboardFirstRow.add(getUnitsCommand(language));
KeyboardRow keyboardSecondRow = new KeyboardRow();
keyboardSecondRow.add(getAlertsCommand(language));
keyboardSecondRow.add(getBackCommand(language));
keyboard.add(keyboardFirstRow);
keyboard.add(keyboardSecondRow);
replyKeyboardMarkup.setKeyboard(keyboard);

return replyKeyboardMarkup;
}

您可以在 SendMessage 方法中使用 ReplyKeyboardMarkup,如下所示:

    SendMessage message = new SendMessage();
message.enableMarkdown(true);
message.setReplyMarkup(getSettingsKeyboard(language));
message.setReplyToMessageId(message.getMessageId());
message.setChatId(message.getChatId().toString());
message.setText(getSettingsMessage(language));

关于java - 如何使用 Java 为 Telegram Bot 键盘编写一组自定义命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39674019/

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