简体   繁体   English

如何使用 Java 从 slack 中读取消息

[英]How to read messages from slack using Java

java.net.ConnectException: invalid_auth java.net.ConnectException:invalid_auth

getting this error even after fetching the token for my app即使在为我的应用获取令牌后也出现此错误

SlackSession session = SlackSessionFactory.createWebSocketSlackSession("token");
        session.connect();
        //ChannelHistoryModule channelHistoryModule = ChannelHistoryModuleFactory.createChannelHistoryModule(session);
        SlackChannel slackChannel=session.findChannelByName("notification-hub-test"); 
        session.sendMessage(slackChannel, "Test Message");
        //List<SlackMessagePosted> messages = channelHistoryModule.fetchHistoryOfChannel(slackChannel.getId(),10);

If you created a Slack bot channel successfully, you should be able to send messages to Slack as follows:如果您成功创建了 Slack 机器人通道,您应该能够向 Slack 发送消息,如下所示:

String payload = "{\"text\":\"" + message + "\"}";
Slack slack = Slack.getInstance(); // More on Slack class later
WebhookResponse response = slack.send(WEBHOOK, payload);

At this point, you can do whatever you need to do with the response.此时,您可以对响应做任何您需要做的事情。 At the very least, you should get the response code to see if the message was sent OK.至少,您应该获得响应代码以查看消息是否发送正常。 To do that, simply call response.getCode() .为此,只需调用response.getCode()

WEBHOOK is the webhook URL for the channel you created. WEBHOOK是您创建的频道的 webhook URL。

To get access to the Slack class, you need to import slack-api-client into your project.要访问Slack class,您需要将slack-api-client导入到您的项目中。 I use Maven to manage my dependencies.我使用 Maven 来管理我的依赖项。 Which, for Slack is其中,对于 Slack 来说

<dependency>
    <groupId>com.slack.api</groupId>
    <artifactId>slack-api-client</artifactId>
    <version>1.10.0</version>
</dependency>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM