/
mattermost.go
161 lines (142 loc) · 3.96 KB
/
mattermost.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package notify
import (
"encoding/json"
"fmt"
"strconv"
"github.com/infracloudio/botkube/pkg/config"
"github.com/infracloudio/botkube/pkg/events"
log "github.com/infracloudio/botkube/pkg/logging"
"github.com/mattermost/mattermost-server/model"
)
// Mattermost contains server URL and token
type Mattermost struct {
Client *model.Client4
Channel string
ClusterName string
NotifType string
}
// NewMattermost returns new Mattermost object
func NewMattermost(c *config.Config) (Notifier, error) {
// Load configurations
c, err := config.New()
if err != nil {
log.Logger.Fatal(fmt.Sprintf("Error in loading configuration. Error:%s", err.Error()))
}
// Set configurations for Mattermost server
client := model.NewAPIv4Client(c.Communications.Mattermost.URL)
client.SetOAuthToken(c.Communications.Mattermost.Token)
botTeam, resp := client.GetTeamByName(c.Communications.Mattermost.Team, "")
if resp.Error != nil {
log.Logger.Error("Error in connecting to Mattermost team ", c.Communications.Mattermost.Team, "\nError: ", resp.Error)
return nil, resp.Error
}
botChannel, resp := client.GetChannelByName(c.Communications.Mattermost.Channel, botTeam.Id, "")
if resp.Error != nil {
log.Logger.Error("Error in connecting to Mattermost channel ", c.Communications.Mattermost.Channel, "\nError: ", resp.Error)
return nil, resp.Error
}
return &Mattermost{
Client: client,
Channel: botChannel.Id,
ClusterName: c.Settings.ClusterName,
NotifType: c.Communications.Mattermost.NotifType,
}, nil
}
// SendEvent sends event notification to Mattermost
func (m *Mattermost) SendEvent(event events.Event) error {
log.Logger.Info(fmt.Sprintf(">> Sending to Mattermost: %+v", event))
var fields []*model.SlackAttachmentField
switch m.NotifType {
case "brief":
// set missing cluster name to event object
event.Cluster = m.ClusterName
fields = []*model.SlackAttachmentField{
{
Title: fmt.Sprintf("%s", event.Kind+" "+string(event.Type)),
Value: event.Message(),
},
}
default:
fields = []*model.SlackAttachmentField{
{
Title: "Kind",
Value: event.Kind,
Short: true,
},
{
Title: "Name",
Value: event.Name,
Short: true,
},
}
if event.Namespace != "" {
fields = append(fields, &model.SlackAttachmentField{
Title: "Namespace",
Value: event.Namespace,
Short: true,
})
}
if event.Reason != "" {
fields = append(fields, &model.SlackAttachmentField{
Title: "Reason",
Value: event.Reason,
Short: true,
})
}
if len(event.Messages) > 0 {
message := ""
for _, m := range event.Messages {
message = message + m
}
fields = append(fields, &model.SlackAttachmentField{
Title: "Message",
Value: message,
})
}
if event.Action != "" {
fields = append(fields, &model.SlackAttachmentField{
Title: "Action",
Value: event.Action,
})
}
if len(event.Recommendations) > 0 {
rec := ""
for _, r := range event.Recommendations {
rec = rec + r
}
fields = append(fields, &model.SlackAttachmentField{
Title: "Recommendations",
Value: rec,
})
}
// Add clustername in the message
fields = append(fields, &model.SlackAttachmentField{
Title: "Cluster",
Value: m.ClusterName,
})
}
attachment := []*model.SlackAttachment{{
Fields: fields,
Footer: "BotKube",
Timestamp: json.Number(strconv.FormatInt(event.TimeStamp.Unix(), 10)),
}}
post := &model.Post{}
post.ChannelId = m.Channel
post.Props = map[string]interface{}{
"attachments": attachment,
}
if _, resp := m.Client.CreatePost(post); resp.Error != nil {
log.Logger.Error("Failed to send message. Error: ", resp.Error)
}
return nil
}
// SendMessage sends message to Mattermost channel
func (m *Mattermost) SendMessage(msg string) error {
post := &model.Post{}
post.ChannelId = m.Channel
post.Message = msg
if _, resp := m.Client.CreatePost(post); resp.Error != nil {
log.Logger.Error("Failed to send message. Error: ", resp.Error)
}
return nil
}