InlineKeyboard - un clavier attaché à un message, utilisant un rappel (CallbackQuery), au lieu d'envoyer un message à partir d'un clavier ordinaire.
Création d'un filaire de bot
Créez d'abord un projet sur Maven et ajoutez le référentiel "
Telegram Bots ":
<dependency> <groupId>org.telegram</groupId> <artifactId>telegrambots</artifactId> <version>4.0.0</version> </dependency>
En utilisant
BotFather, enregistrez le bot et obtenez un jeton:
Ensuite, créez la classe Bot, héritez de TelegramLongPollingBot et remplacez les méthodes:
Code source 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; } }
Nous créons des variables finales avec le nom du bot et le jeton, ajoutons botUserName dans la méthode getBotUsername () et jeton dans getBotToken (). Dans la méthode principale, enregistrez le bot:
Code source 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; } }
Le cadre du bot est prĂȘt! Ăcrivons maintenant une mĂ©thode avec InlineKeyboard.
Travailler avec InlineKeyboard
Créez un objet de disposition de clavier:
InlineKeyboardMarkup inlineKeyboardMarkup =new InlineKeyboardMarkup();
Maintenant, nous construisons la position des boutons.
Nous crĂ©ons un objet InlineKeyboardButton qui a 2 paramĂštres: Texte (ce qui sera Ă©crit sur le bouton lui-mĂȘme) et CallBackData (ce qui sera envoyĂ© au serveur lorsque le bouton est cliquĂ©).
InlineKeyboardButton inlineKeyboardButton = new InlineKeyboardButton(); inlineKeyboardButton.setText(""); inlineKeyboardButton.setCallbackData("Button \"\" has been pressed");
Ajoutez-le à la liste, créant ainsi une série.
List<InlineKeyboardButton> keyboardButtonsRow1 = new ArrayList<>(); keyboardButtons.add(inlineKeyboardButton);
Si vous souhaitez créer une autre ligne, créez simplement une autre liste et ajoutez-y de nouveaux boutons.
List<InlineKeyboardButton> keyboardButtonsRow2 = new ArrayList<>(); keyboardButtons.add(inlineKeyboardButton2);
AprÚs cela, nous devons unir les lignes, alors créez une liste de lignes.
List<List<InlineKeyboardButton>> rowList= new ArrayList<>(); rowList.add(keyboardButtonsRow1); rowList.add(keyboardButtonsRow2);
FonctionnalitéLe développeur a pris soin de nous et nous pouvons immédiatement écrire des boutons dans la liste sans créer de variable.
keyboardButtonsRow1.add(new InlineKeyboardButton().setText("Fi4a") .setCallbackData("CallFi4a"));
Maintenant, nous pouvons définir les boutons dans l'objet de disposition du clavier.
inlineKeyboardMarkup.setKeyboard(rowList);
Si la description de la création d'un clavier est un peu déroutante, voici un diagramme:

C'est tout! Ajoutez maintenant le balisage au message:
SendMessage message = new SendMessage().setChatId(chatId).setText("") .setReplyMarkup(inlineKeyboardMarkup);
Maintenant, nous pouvons envoyer, voici une mĂ©thode prĂȘte Ă l'emploi:
Code source 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); }
Nous faisons une option lorsque la mĂ©thode sera appelĂ©e dans le gestionnaire de requĂȘtes onUpdateReceived:
Code source @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(); } } } } }
Nous essayons!

Maintenant, nous devons le traiter, créer une nouvelle branche dans if et traiter CallbackQuery:
Code source @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(); } } }
Vérifiez!

Câest probablement tout, merci de votre attention!
Tout le code source:
Code entier 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); } }