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
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。