搜索
您的当前位置:首页正文

Slack Bot Message receive and s

来源:二三娱乐

What is Slack?

依赖

Maven

Step 1. Add the JitPack repository to your build file

<repository>
    <id>jitpack.io</id>
    <url>https://jitpack.io</url>
</repository>

Step 2. Add the dependency in the form

<dependency>
    <groupId>com.github.allbegray</groupId>
    <artifactId>slack-api</artifactId>
    <version>v1.3.0.RELEASE</version>
</dependency>

代码

package com.test.slack;

import allbegray.slack.SlackClientFactory;
import allbegray.slack.SlackTextBuilder;
import allbegray.slack.rtm.Event;
import allbegray.slack.rtm.EventListener;
import allbegray.slack.rtm.SlackRealTimeMessagingClient;
import allbegray.slack.webapi.SlackWebApiClient;
import com.fasterxml.jackson.databind.JsonNode;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * Created by cocky on 2017/3/8.
 */
public class MainTest {

    public static String exec(String cmd){
        InputStream in = null;
        StringBuffer sb= new StringBuffer();
        try {
            Process pro = Runtime.getRuntime().exec(cmd);
            pro.waitFor();
            in = pro.getInputStream();
            BufferedReader read = new BufferedReader(new InputStreamReader(in));
            String result = null;
            while ((result=read.readLine())!=null){
                sb.append(result+"\n");
            }

            return sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sb.toString();
    }

    public static void main(String[] args) {

        listening();
        //System.out.println(exec("ls"));


    }

    static String message = SlackTextBuilder.create()
            .text("text message")
            .newline()
            
             "Slack")
            .newline()
            .bold("bold message")
            .italic("italic message")
            .strike("strike message")
            .newline()
            
             "TestMail")
            .newline()
            .code("code block")
            .preformatted("public class SlackWebhookClientTest() {\n\n\tpublic static void main(String args[]) {\n\n\t}\n}")
            .quote("quote message")
            .build();

    private static void listening() {
        final SlackRealTimeMessagingClient rtmClient = SlackClientFactory.createSlackRealTimeMessagingClient("xoxb-151982105191-Fnk3VsRbyigJDDyupEvSQMla");
        final SlackWebApiClient webApiClient = SlackClientFactory.createWebApiClient("xoxb-151982105191-Fnk3VsRbyigJDDyupEvSQMla");

        rtmClient.addListener(Event.MESSAGE, new EventListener() {
            public void handleMessage(JsonNode jsonNode) {
                System.out.println(jsonNode);

                System.out.println("Send=====: "+ jsonNode.get("text").asText());

                if(jsonNode.has("subtype") && "bot_message".equals(jsonNode.get("subtype").asText())) {
                    return;
                }


                try {
                    //webApiClient.postMessage("#test", exec(jsonNode.get("text").asText()));
                    webApiClient.postMessage("#test", message);
                }catch (Exception e){
                    e.printStackTrace();
                }

            }
        });

        rtmClient.connect();

        try {
            Thread.sleep(1000 * 1000);
            rtmClient.close();
            webApiClient.shutdown();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

效果

image.png
Top