电报机器人的响应问题

时间:2018-05-09 09:32:53

标签: java multithreading telegram-bot

问题:

在机器人同时请求时,用户可以收到另一个答案。我试图将每个处理程序分成一个单独的线程,我尝试了sendMessageAsync。但没有任何结果。也许还有其他一些方法?我正在写Java长轮询机器人。 Rubenlagus / TelegramBots图书馆。提前谢谢。

执行人服务:

public synchronized void onUpdateReceived(Update update) {

    boolean isFound = false;

    if (update.hasMessage() && update.getMessage().hasText()) {

        if (update.getMessage().getText().equals("/stats")) {
            executorService.execute(() -> logger.getStats(update, this));
            return;
        }

        for (User user : usersList) {
            if (user.getId().equals(update.getMessage().getFrom().getId())) {
                executorService.execute(() -> user.handle(update));
                isFound = true;
                break;
            }
        }

        if (isFound == false) {
            logger.addUser(update);
            User newUser = new User(update, new CommandHandler(this));
            usersList.add(newUser);
            executorService.execute(() -> newUser.handle(update));
        }
    }


}

sendMessageAsync:

 public void onUpdateReceived(Update update) {

    boolean isFound = false;
    SendMessage message = new SendMessage()
            .setChatId(update.getMessage().getChatId())
            .setText("I don't understand you:(");

    if (update.hasMessage() && update.getMessage().hasText()) {

        if (update.getMessage().getText().equals("/stats")) {
            logger.getStats(update, this);
            return;
        }

        for (User user : usersList) {
            if (user.getId().equals(update.getMessage().getFrom().getId())) {
                message = user.handle(update);
                isFound = true;
                break;
            }
        }

        if (isFound == false) {
            logger.addUser(update);
            User newUser = new User(update, new CommandHandler());
            usersList.add(newUser);
            message = newUser.handle(update);
        }
    }

    throwMessage(message);
}

public void throwMessage(SendMessage message) {
    try {
        this.executeAsync(message, new SentCallback<Message>() {
            @Override
            public void onResult(BotApiMethod<Message> botApiMethod, Message message) {

            }

            @Override
            public void onError(BotApiMethod<Message> botApiMethod, TelegramApiRequestException e) {

            }

            @Override
            public void onException(BotApiMethod<Message> botApiMethod, Exception e) {

            }
        });
    } catch (TelegramApiException e) {
        e.printStackTrace();
    }
}

0 个答案:

没有答案