TelegramBot的自定义键盘不起作用

时间:2017-08-28 10:45:52

标签: java telegram telegram-bot

我正在使用Java语言试验TelegramBot API。我创建了我的机器人,并尝试使用thisthe documentation here.

制作自定义键盘

代码本身有效,但自定义键盘根本没有出现(它应该在bot收到/ hello命令时出现。)

所以这是我的代码

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import org.telegram.telegrambots.api.methods.send.SendMessage;
import org.telegram.telegrambots.api.objects.Update;
import org.telegram.telegrambots.api.objects.replykeyboard.ReplyKeyboardMarkup;
import org.telegram.telegrambots.api.objects.replykeyboard.buttons.KeyboardButton;
import org.telegram.telegrambots.api.objects.replykeyboard.buttons.KeyboardRow;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.exceptions.TelegramApiException;

public class MyFirstBot  extends TelegramLongPollingBot{



@Override
public String getBotUsername() {
    // TODO Auto-generated method stub
    return "MyBotUsername";
}


@Override
public void onUpdateReceived(Update update) {
    // TODO Auto-generated method stub
     SendMessage message = new SendMessage()
                .setChatId(update.getMessage().getChatId());

     if(update.hasMessage() && update.getMessage().isCommand()){
         try{
             switch(update.getMessage().getText()){ //reads command
             case("/hello"):
                message.enableMarkdown(true);
                message.setReplyMarkup(getSettingsKeyboard());
                message.setReplyToMessageId(message.getReplyToMessageId());
                message.setChatId(message.getChatId().toString());
                message.setText(getSettingsMessage(null));


                break;
             case("/date"):
                message.setText(LocalDateTime.now().toString());
                sendMessage(message); //tells time and date
                break;

             default:
                message.setText("Invalid Value");
                sendMessage(message);} //in case no such command exists
         }
         catch(TelegramApiException e){
             e.printStackTrace();
         }
     }

}

private String getSettingsMessage(String string) {
    // TODO Auto-generated method stub
    return null;
}

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

   List<KeyboardRow> keyboardRow = new ArrayList<>();
   KeyboardRow keyboard =  new KeyboardRow();
   keyboard.add("Name");
   keyboard.add("Add");
   keyboardRow.add(keyboard);
   replyKeyboardMarkup.setKeyboard(keyboardRow);


   return replyKeyboardMarkup;
}




@Override
public String getBotToken() {
    // TODO Auto-generated method stub
    return "MyToken";
}



}

0 个答案:

没有答案