لوحة المفاتيح المضمنة في Telegram Bots

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; } } 


نقوم بإنشاء المتغيرات النهائية باسم بوت ورمزها ، وإضافة botUserName في طريقة getBotUsername () ، والرمز المميز في 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(); 

الآن نقوم ببناء موضع الأزرار.

نقوم بإنشاء كائن 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(); } } } } } 


نحن نحاول!



الآن نحن بحاجة إلى معالجته ، وإنشاء فرع جديد في حالة معالجة 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); } } 


Source: https://habr.com/ru/post/ar418905/


All Articles