Last active
May 7, 2019 18:47
-
-
Save elektron9/6b7ce6e6d4000519d12d3c6cb5efb47e to your computer and use it in GitHub Desktop.
RoboSlack Logging Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.palantir.roboslack.api.MessageRequest; | |
import com.palantir.roboslack.api.ParseMode; | |
import com.palantir.roboslack.api.attachments.Attachment; | |
import com.palantir.roboslack.api.attachments.components.Color; | |
import com.palantir.roboslack.api.attachments.components.Title; | |
import com.palantir.roboslack.api.markdown.SlackMarkdown; | |
import com.palantir.roboslack.webhook.SlackWebHookService; | |
import com.palantir.roboslack.webhook.api.model.WebHookToken; | |
import com.palantir.roboslack.webhook.api.model.response.ResponseCode; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
/** | |
* @author abukhari | |
* @since v0.0.1 | |
*/ | |
public class SlackLoggerExample { | |
public static void main(String[] args) { | |
WebHookToken token = WebHookToken.fromEnvironment(); | |
MessageRequest msg = MessageRequest.builder() | |
.username("Slack Oncall Service") | |
.iconEmoji(SlackMarkdown.EMOJI.decorate("sos")) | |
.channel("slack-dev-msgs") | |
.text("[CRITICAL] Error Logged in MailService - " + SlackMarkdown.LINK.decorate(url | |
("https://localhost:9600/?pretty"), "[Logstash Link]")) | |
.addAttachments(Attachment.builder() | |
.fallback("Logged Alert Details") | |
.title(Title.of("Logged Alert Details")) | |
.text(SlackMarkdown.PREFORMAT_MULTILINE.decorate("21:49:09.059 WARN [PacketReportingService]" + | |
" c.p" + | |
".w.c.s.r.email.ManagedEmailFolder - Error occurred while trying to get message " + | |
"count\n" + | |
"javax.mail.FolderClosedException: * BYE Connection closed.\n" + | |
"\tat com.sun.mail.imap.IMAPFolder.getMessageCount(IMAPFolder.java:1582) ~[javax" + | |
".mail-1.5.6.jar:1.5.6]\n...<stack trace continues>")) | |
.color(Color.danger()) | |
.imageUrl(url("http://image_url.png")) | |
.build() | |
) | |
.parse(ParseMode.NONE) | |
.markdownEnabled(true) | |
.unfurlLinks(false) | |
.unfurlMedia(false) | |
.build(); | |
ResponseCode code = SlackWebHookService.with(token).sendMessage(msg); | |
System.out.println(code); | |
} | |
private static URL url(String url) { | |
try { | |
return new URL(url); | |
} catch (MalformedURLException e) { | |
throw new IllegalArgumentException(e.getLocalizedMessage(), e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment