InlineKeyboard-使用回调(CallbackQuery)附加到消息的键盘,而不是通过普通键盘发送消息。
创建机器人线框
首先,在Maven上创建一个项目并添加“
Telegram Bots ”存储库:
<dependency> <groupId>org.telegram</groupId> <artifactId>telegrambots</artifactId> <version>4.0.0</version> </dependency>
使用
BotFather,注册机器人并获取令牌:
接下来,创建Bot类,从TelegramLongPollingBot继承,并重写方法:
源代码 import org.telegram.telegrambots.bots.TelegramLongPollingBot; import org.telegram.telegrambots.meta.api.objects.Update; public class Bot extends TelegramLongPollingBot { @Override public void onUpdateReceived(Update update) { } @Override public String getBotUsername() { return null; } @Override public String getBotToken() { return null; } }
我们使用机器人名称和令牌创建最终变量,将其添加到getBotUsername()方法-botUserName,然后将其添加到getBotToken()-令牌。 在主要方法中,注册机器人:
源代码 import org.telegram.telegrambots.bots.TelegramLongPollingBot; import org.telegram.telegrambots.meta.TelegramBotsApi; import org.telegram.telegrambots.meta.api.objects.Update; import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException; public class Bot extends TelegramLongPollingBot { private static final String botUserName = "Habr_Inlinebot"; private static final String token = "632072575:AAG5YNl9tM9MJbnP5HwLB22rVzNCmY05MQI"; public static void main(String[] args) { ApiContextInitializer.init(); TelegramBotsApi telegramBotsApi = new TelegramBotsApi(); try { telegramBotsApi.registerBot(new Bot()); } catch (TelegramApiRequestException e) { e.printStackTrace(); } } @Override public void onUpdateReceived(Update update) { } @Override public String getBotUsername() { return botUserName; } @Override public String getBotToken() { return token; } }
机器人的框架已准备就绪! 现在,让我们用InlineKeyboard编写一个方法。
使用InlineKeyboard
创建一个键盘布局对象:
InlineKeyboardMarkup inlineKeyboardMarkup =new InlineKeyboardMarkup();
现在我们建立按钮的位置。
我们创建一个具有2个参数的InlineKeyboardButton对象:文本(将在按钮本身上写的内容)和CallBackData(单击按钮时将发送到服务器的内容)。
InlineKeyboardButton inlineKeyboardButton = new InlineKeyboardButton(); inlineKeyboardButton.setText(""); inlineKeyboardButton.setCallbackData("Button \"\" has been pressed");
将其添加到列表中,从而创建一个系列。
List<InlineKeyboardButton> keyboardButtonsRow1 = new ArrayList<>(); keyboardButtons.add(inlineKeyboardButton);
如果要创建另一行,只需创建另一个列表并向其添加新按钮。
List<InlineKeyboardButton> keyboardButtonsRow2 = new ArrayList<>(); keyboardButtons.add(inlineKeyboardButton2);
之后,我们需要统一行,因此创建一个行列表。
List<List<InlineKeyboardButton>> rowList= new ArrayList<>(); rowList.add(keyboardButtonsRow1); rowList.add(keyboardButtonsRow2);
特色开发人员照顾了我们,我们可以立即在列表中编写按钮,而无需创建变量。
keyboardButtonsRow1.add(new InlineKeyboardButton().setText("Fi4a") .setCallbackData("CallFi4a"));
现在我们可以在键盘布局对象中设置按钮。
inlineKeyboardMarkup.setKeyboard(rowList);
如果关于如何创建键盘的描述有些混乱,请参考以下图表:

仅此而已! 现在将标记添加到消息中:
SendMessage message = new SendMessage().setChatId(chatId).setText("") .setReplyMarkup(inlineKeyboardMarkup);
现在我们可以发送,这是一种现成的方法:
源代码 public static SendMessage sendInlineKeyBoardMessage(long chatId) { InlineKeyboardMarkup inlineKeyboardMarkup = new InlineKeyboardMarkup(); InlineKeyboardButton inlineKeyboardButton1 = new InlineKeyboardButton(); InlineKeyboardButton inlineKeyboardButton2 = new InlineKeyboardButton(); inlineKeyboardButton1.setText(""); inlineKeyboardButton1.setCallbackData("Button \"\" has been pressed"); inlineKeyboardButton2.setText("2"); inlineKeyboardButton2.setCallbackData("Button \"2\" has been pressed"); List<InlineKeyboardButton> keyboardButtonsRow1 = new ArrayList<>(); List<InlineKeyboardButton> keyboardButtonsRow2 = new ArrayList<>(); keyboardButtonsRow1.add(inlineKeyboardButton1); keyboardButtonsRow1.add(new InlineKeyboardButton().setText("Fi4a").setCallbackData("CallFi4a")); keyboardButtonsRow2.add(inlineKeyboardButton2); List<List<InlineKeyboardButton>> rowList = new ArrayList<>(); rowList.add(keyboardButtonsRow1); rowList.add(keyboardButtonsRow2); inlineKeyboardMarkup.setKeyboard(rowList); return new SendMessage().setChatId(chatId).setText("").setReplyMarkup(inlineKeyboardMarkup); }
我们将在onUpdateReceived请求处理程序中调用该方法时做出选择:
源代码 @Override public void onUpdateReceived(Update update) { if(update.hasMessage()){ if(update.getMessage().hasText()){ if(update.getMessage().getText().equals("Hello")){ try { execute(sendInlineKeyBoardMessage(update.getMessage().getChatId())); } catch (TelegramApiException e) { e.printStackTrace(); } } } } }
我们尝试!

现在我们需要对其进行处理,在if中创建一个新分支,然后处理CallbackQuery:
源代码 @Override public void onUpdateReceived(Update update) { if(update.hasMessage()){ if(update.getMessage().hasText()){ if(update.getMessage().getText().equals("Hello")){ try { execute(sendInlineKeyBoardMessage(update.getMessage().getChatId())); } catch (TelegramApiException e) { e.printStackTrace(); } } } }else if(update.hasCallbackQuery()){ try { execute(new SendMessage().setText( update.getCallbackQuery().getData()) .setChatId(update.getCallbackQuery().getMessage().getChatId())); } catch (TelegramApiException e) { e.printStackTrace(); } } }
检查!

可能就这些,谢谢您的关注!
所有源代码:
整个代码 import org.telegram.telegrambots.ApiContextInitializer; import org.telegram.telegrambots.bots.TelegramLongPollingBot; import org.telegram.telegrambots.meta.ApiContext; import org.telegram.telegrambots.meta.TelegramBotsApi; import org.telegram.telegrambots.meta.api.methods.send.SendMessage; import org.telegram.telegrambots.meta.api.objects.Update; import org.telegram.telegrambots.meta.api.objects.replykeyboard.InlineKeyboardMarkup; import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.InlineKeyboardButton; import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.KeyboardButton; import org.telegram.telegrambots.meta.exceptions.TelegramApiException; import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException; import java.util.ArrayList; import java.util.List; public class Bot extends TelegramLongPollingBot { private static final String botUserName = "Habr_Inlinebot"; private static final String token = "632072575:AAG5YNl9tM9MJbnP5HwLB22rVzNCmY05MQI"; public static void main(String[] args) { ApiContextInitializer.init(); TelegramBotsApi telegramBotsApi = new TelegramBotsApi(); try { telegramBotsApi.registerBot(new Bot()); } catch (TelegramApiRequestException e) { e.printStackTrace(); } } @Override public void onUpdateReceived(Update update) { if(update.hasMessage()){ if(update.getMessage().hasText()){ if(update.getMessage().getText().equals("Hello")){ try { execute(sendInlineKeyBoardMessage(update.getMessage().getChatId())); } catch (TelegramApiException e) { e.printStackTrace(); } } } }else if(update.hasCallbackQuery()){ try { execute(new SendMessage().setText( update.getCallbackQuery().getData()) .setChatId(update.getCallbackQuery().getMessage().getChatId())); } catch (TelegramApiException e) { e.printStackTrace(); } } } @Override public String getBotUsername() { return botUserName; } @Override public String getBotToken() { return token; } public static SendMessage sendInlineKeyBoardMessage(long chatId) { InlineKeyboardMarkup inlineKeyboardMarkup = new InlineKeyboardMarkup(); InlineKeyboardButton inlineKeyboardButton1 = new InlineKeyboardButton(); InlineKeyboardButton inlineKeyboardButton2 = new InlineKeyboardButton(); inlineKeyboardButton1.setText(""); inlineKeyboardButton1.setCallbackData("Button \"\" has been pressed"); inlineKeyboardButton2.setText("2"); inlineKeyboardButton2.setCallbackData("Button \"2\" has been pressed"); List<InlineKeyboardButton> keyboardButtonsRow1 = new ArrayList<>(); List<InlineKeyboardButton> keyboardButtonsRow2 = new ArrayList<>(); keyboardButtonsRow1.add(inlineKeyboardButton1); keyboardButtonsRow1.add(new InlineKeyboardButton().setText("Fi4a").setCallbackData("CallFi4a")); keyboardButtonsRow2.add(inlineKeyboardButton2); List<List<InlineKeyboardButton>> rowList = new ArrayList<>(); rowList.add(keyboardButtonsRow1); rowList.add(keyboardButtonsRow2); inlineKeyboardMarkup.setKeyboard(rowList); return new SendMessage().setChatId(chatId).setText("").setReplyMarkup(inlineKeyboardMarkup); } }