From 224ff3ca081cf6fb669ad6fd4a9de567d5f365ba Mon Sep 17 00:00:00 2001 From: Matt Martz Date: Wed, 10 Sep 2014 07:40:59 -0500 Subject: [PATCH] Use slack message attachments for color coding --- pkg/plugin/notify/slack.go | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/pkg/plugin/notify/slack.go b/pkg/plugin/notify/slack.go index 13109ed49..708618d28 100644 --- a/pkg/plugin/notify/slack.go +++ b/pkg/plugin/notify/slack.go @@ -57,25 +57,40 @@ func getMessage(context *Context, message string) string { } func (s *Slack) sendStarted(context *Context) error { - return s.send(getMessage(context, slackStartedMessage)) + return s.send(getMessage(context, slackStartedMessage), "warning") } func (s *Slack) sendSuccess(context *Context) error { - return s.send(getMessage(context, slackSuccessMessage)) + return s.send(getMessage(context, slackSuccessMessage), "good") } func (s *Slack) sendFailure(context *Context) error { - return s.send(getMessage(context, slackFailureMessage)) + return s.send(getMessage(context, slackFailureMessage), "danger") } // helper function to send HTTP requests -func (s *Slack) send(msg string) error { +func (s *Slack) send(msg string, color string) error { + type Attachment struct { + Fallback string `json:"fallback"` + Text string `json:"text"` + Color string `json:"color"` + MrkdwnIn []string `json:"mrkdwn_in"` + } + + attachments := []Attachment{ + Attachment{ + msg, + msg, + color, + []string{"fallback", "text"}, + }, + } // data will get posted in this format data := struct { - Channel string `json:"channel"` - Username string `json:"username"` - Text string `json:"text"` - }{s.Channel, s.Username, msg} + Channel string `json:"channel"` + Username string `json:"username"` + Attachments []Attachment `json:"attachments"` + }{s.Channel, s.Username, attachments} // data json encoded payload, err := json.Marshal(data)