Keyboard
With a Keyboard we can show an array of buttons with some callback
data to send
when selected.
Build a keyboard
InlineKeyboardMarkup inlineKeyboardMarkup;
List<List<InlineKeyboardButton>> keyboard;
List<InlineKeyboardButton> row;
InlineKeyboardButton button;
inlineKeyboardMarkup = new InlineKeyboardMarkup();
keyboard = new ArrayList();
row = new ArrayList();
button = new InlineKeyboardButton();
button.setText("Hi");
button.setCallback_data("hi");
row.add(button);
keyboard.add(row);
inlineKeyboardMarkup.setInline_keyboard(keyboard);
Send a keyboard
Message message = new Message();
message.setChat_id( update.getMessage().getChat().getId() );
message.setText("A keyboard");
message.setReply_markup(inlineKeyboardMarkup);
telegramBot.sendMessage(message).blockingGet();
Remove a keyboard
telegramBot.deleteMessage( chatId, messageId).blockingGet();
Controller
Now the controller can receive a "direct" message from user or a "callback"
message from a button in the Update
parameter
@Post
String index( Update update){
if( update.getMessage() != null ){
sendKeyboard(update);
}else{
answerKeyboard(update);
}
return "done";
}
Exercise
Build a bot who show an InlineKeyboard when the user send some text.
Build a keyboard with two buttons:
-
"Hi" who answer "goodbye" when pressed
-
"Hola" who answer "adios" when pressed
-
"Cancel" who remove the keyboard