-
Notifications
You must be signed in to change notification settings - Fork 0
/
slack.go
98 lines (83 loc) · 2.28 KB
/
slack.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package slack
import (
"encoding/json"
"fmt"
"log"
"os"
"github.com/Jeffail/gabs"
"github.com/aws/aws-lambda-go/events"
)
type slackAttachmentField struct {
Title string `json:"title,omitempty"`
Value string `json:"value,omitempty"`
Short bool `json:"short,omitempty"`
}
// MessageAttachments is the Slack message stracture
type MessageAttachments struct {
Color string `json:"color,omitempty"`
Pretext string `json:"pretext,omitempty"`
Username string `json:"username,omitempty"`
Icon string `json:"icon_emoji,omitempty"`
Fields []slackAttachmentField `json:"fields,omitempty"`
}
func mapColor(status string) string {
var colorCode string
switch status {
case "ALARM":
colorCode = "danger"
case "INSUFFICIENT_DATA":
colorCode = "warning"
default:
colorCode = "good"
}
return colorCode
}
// CreateSlackMessagAttachment is a function to create slack message
func CreateSlackMessagAttachment(snsEvent events.SNSEvent) string {
log.Println("snsEvent", snsEvent)
records := snsEvent.Records
snsRecord := records[0].SNS
jsonParsed, _ := gabs.ParseJSON([]byte(snsRecord.Message))
NewStateValue, _ := jsonParsed.Path("NewStateValue").Data().(string)
NewStateReason, _ := jsonParsed.Path("NewStateReason").Data().(string)
AlarmName, _ := jsonParsed.Path("AlarmName").Data().(string)
Region, _ := jsonParsed.Path("Region").Data().(string)
slackAttachmentFields := []slackAttachmentField{
slackAttachmentField{
Title: "Alarm",
Value: AlarmName,
Short: true,
},
slackAttachmentField{
Title: "Status",
Value: NewStateValue,
Short: true,
},
slackAttachmentField{
Title: "Reason",
Value: NewStateReason,
Short: false,
},
}
pretext := fmt.Sprintf("%s: %s in %s", NewStateValue, AlarmName, Region)
username := os.Getenv("USERNAME")
if username == "" {
username = "AWS-bot"
}
icon := os.Getenv("ICON")
if icon == "" {
icon = ":loudspeaker:"
}
slackMessageAttachments := MessageAttachments{
Color: mapColor(NewStateValue),
Pretext: pretext,
Username: username,
Icon: icon,
Fields: slackAttachmentFields,
}
resp, err := json.Marshal(slackMessageAttachments)
if err != nil {
log.Fatal("Error building Slack attachments", err)
}
return string(resp)
}