在Java应用程序中按虚拟TelegramBot按钮上的按钮后获取用户消息文本

时间:2018-10-09 13:30:02

标签: java telegram chatbot

那是我的代码:

public class MyBot extends AbilityBot {
    private final ResponseHandler responseHandler;
    protected MyBot(String botToken, String botUsername, DefaultBotOptions options) {
        super(botToken, botUsername, options);
        responseHandler = new ResponseHandler(sender, db);
    }
   ...
    public Ability hello() {
        return Ability.builder()
                .name("test")
                ..............
                .action(ctx -> responseHandler.replyToStart(ctx.chatId(), ctx.update()))
                .build();
    }

    public Reply replyToButtons() {
        Consumer<Update> action = upd -> responseHandler.replyToButtons(getChatId(upd), upd.getCallbackQuery().getData());
        return Reply.of(action, Flag.CALLBACK_QUERY);
    }

我的responseHandler是:

public class ResponseHandler {

    private final MessageSender sender;
    private final Map<Long, State> chatStates;

    public ResponseHandler(MessageSender sender, DBContext db) {
        this.sender = sender;
        chatStates = db.getMap(Constants.CHAT_STATES);
    }

    public void replyToStart(long chatId, Update update) {
        try {
            sender.execute(new SendMessage()
                    .setText(Constants.START_REPLY)
                    .setChatId(chatId));
            sender.execute(new SendMessage()
                    .setText(Constants.FIND_TRAINING_DATE)
                    .setChatId(chatId)
                    .setReplyMarkup(KeyboardFactory.withTodayTomorrowButtons()));
            chatStates.put(chatId, State.AWAITING_TRAINING_DAY);
        } catch (TelegramApiException e) {
            e.printStackTrace();
        }
    }

    public void replyToButtons(long chatId, String buttonId) {
        System.out.println(buttonId);
        try {
            switch (buttonId) {
                case Constants.TRAINING_TODAY:
                    replyToTrainingToday(chatId);
                    break;
                case 
                    ....
....
            }
        } catch (TelegramApiException e) {
            e.printStackTrace();
        }
    }

    private void replyToTrainingToday(long chatId) throws TelegramApiException {
        if (chatStates.get(chatId).equals(State.AWAITING_TRAINING_DAY)) {
            sender.execute(new SendMessage()
                    .setText(Constants.TRAINING_TODAY_REPLY)
                    .setChatId(chatId));
            chatStates.put(chatId, State.TODAY_IS_TRAINING_DAY);
        }
    }

    private void replyToTrainingTomorrow(long chatId) throws TelegramApiException {
        ...
    }
}

我想做什么?现在,用户编写 / test ,然后他会收到3个虚拟按钮。当他按下其中一些按钮时,他会收到简单的消息。但是我想显示一个邀请输入数字。例如,用户按下按钮“ x + 1”,我的chatBot邀请用户输入该数字,然后,聊天机器人将其输入的数字+1加到其上并显示为回复消息。

我该怎么办?我应该如何更改代码?

0 个答案:

没有答案