Slackbot chat.postMessage不停地发布消息

时间:2020-01-11 18:03:57

标签: java chatbot resttemplate slack slack-api

我创建了一个与Java应用程序交互的Slack机器人。每当机器人在私人聊天中或作为app_mention收到消息时,我都希望它回复该消息。我已经编写了以下服务来接收消息:

        ObjectNode objectNode = objectMapper.createObjectNode();
        if (!StringUtils.isEmpty(eventDto.getChallenge())) {
            objectNode.put("challenge", eventDto.getChallenge());
        } else if (eventDto.getEvent().has("type")
                && "message".equalsIgnoreCase(eventDto.getEvent().get("type").asText())) {
            objectNode.put("text", "Bot called in a private chat.");
            objectNode.put("channel", eventDto.getEvent().get("channel").asText());
            slackWebApiService.postMessageInChat(botAuthToken, objectNode);
        } else {
            objectNode.put("text", "Bot is called in a channel");
            objectNode.put("channel", eventDto.getEvent().get("channel").asText());
            slackWebApiService.postMessageInChat(botAuthToken, objectNode);
        }
        return objectNode;
    }

SlackWebApiService.postMessageInChat如下:

        requestHeader.add("Authorization", "Bearer " + token);
        requestHeader.add("Content-Type", MediaType.APPLICATION_JSON_VALUE);

        HttpEntity<ObjectNode> requestEntity = new HttpEntity<>(message, requestHeader);
        try {
            ResponseEntity<ObjectNode> responseFromSlack = restTemplate.exchange(
                    "https://slack.com/api/chat.postMessage", HttpMethod.POST, requestEntity, ObjectNode.class);
            log.info("[postMessageInChat], response: {}", responseFromSlack.getBody().asText());
        } catch (Exception e) {
            log.error("[postMessageInChat] ", e.getMessage());
        }

当我在本地计算机上运行此程序时,它工作正常,但在将其部署到环境时,机器人会不停地回复。

为什么机器人不断回复?

0 个答案:

没有答案