maven

        <dependency>
            <groupId>net.gpedro.integrations.slack</groupId>
            <artifactId>slack-webhook</artifactId>
            <version>1.2.1</version>
        </dependency>

构造field和attachment

public static SlackAttachment newAttachment(String title, String titleLink, String text, List<SlackField> fields){
        SlackAttachment slackAttachment = new SlackAttachment();
        slackAttachment.setFallback(text);
        slackAttachment.setColor("danger");
        slackAttachment.setFields(fields);
        slackAttachment.setTitle(title);
        slackAttachment.setTitleLink(titleLink);
        slackAttachment.setText(text);
        return slackAttachment;
    }

    public static SlackField newField(String name,Object value){
        SlackField field = new SlackField();
        field.setTitle(name);
        field.setValue(Objects.toString(value));
        field.setShorten(false);
        return field;
    }

发送

public void sendWithAttachment(String title,String link,String text,SlackAttachment attachment){
        try{
            SlackMessage slackMessage = new SlackMessage("");
            slackMessage.setChannel("#" + channel);
            slackMessage.setUsername(String.format("[%s] - Bot",appId));
            slackMessage.setIcon(":exclamation:");
            slackMessage.setAttachments(Collections.singletonList(attachment));
            slackApi.call(slackMessage);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

docs


codecraft
11.9k 声望2k 粉丝

当一个代码的工匠回首往事时,不因虚度年华而悔恨,也不因碌碌无为而羞愧,这样,当他老的时候,可以很自豪告诉世人,我曾经将代码注入生命去打造互联网的浪潮之巅,那是个很疯狂的时代,我在一波波的浪潮上留下...


引用和评论

0 条评论