/
chat.go
245 lines (225 loc) · 5.88 KB
/
chat.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package chat
import (
"fmt"
"strings"
"unicode/utf16"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/kelseyhightower/envconfig"
"github.com/mattermost/mattermost-server/v6/model"
"github.com/slack-go/slack"
"k8s.io/klog"
)
// CrashNotification contains all data to print out a informative crash note.
type CrashNotification struct {
Title string
Message string
Reason string
Logs string
}
// Client informs a communication channel about a crash.
type Client interface {
Send(*CrashNotification)
}
type MattermostConfig struct {
Token string
URL string
Team, Channel string
}
type MattermostClient struct {
mattermost *model.Client4
channel *model.Channel
}
// Send constructs a new mattermost message containing information about the crash.
func (client *MattermostClient) Send(note *CrashNotification) {
attachment := &model.SlackAttachment{
Color: "#AD2200",
Text: note.Message,
Title: note.Title,
Fields: []*model.SlackAttachmentField{
{
Title: "Logs",
Value: "```\n" + note.Logs + "```",
},
},
}
// Check for termination message
if note.Reason != "" {
attachment.Fields = append(attachment.Fields, &model.SlackAttachmentField{
Title: "Reason",
Value: note.Reason,
})
}
client.sendAttachments(attachment)
}
func (client *MattermostClient) sendAttachments(attachements ...*model.SlackAttachment) {
post := &model.Post{ChannelId: client.channel.Id}
model.ParseSlackAttachment(post, attachements)
_, _, err := client.mattermost.CreatePost(post)
if err != nil {
klog.Warningf("Failed to notify Mattermost: %v", err)
}
}
type ClientConfig struct {
Type string `default:"mattermost"`
}
// NewClientFromEnv instantiates a new chat client using env configuration.
func NewClientFromEnv() (Client, error) {
var (
cfg ClientConfig
client Client
err error
)
if err := envconfig.Process("informer", &cfg); err != nil {
return nil, err
}
switch cfg.Type {
case "mattermost":
client, err = NewMattermostClientFromEnv()
case "slack":
client, err = NewSlackClientFromEnv()
case "telegram":
client, err = NewTelegramClientFromEnv()
default:
err = fmt.Errorf("unknown client type: %s", cfg.Type)
}
if err != nil {
return nil, fmt.Errorf("create client from env: %w", err)
}
return client, nil
}
// NewMattermostClientFromEnv instantiates and configures a Mattermost client.
func NewMattermostClientFromEnv() (*MattermostClient, error) {
var cfg MattermostConfig
if err := envconfig.Process("mattermost", &cfg); err != nil {
return nil, err
}
client := model.NewAPIv4Client(cfg.URL)
client.SetToken(cfg.Token)
team, _, err := client.GetTeamByName(cfg.Team, "")
if err != nil {
return nil, fmt.Errorf("get team: %w", err)
}
channel, _, err := client.GetChannelByName(cfg.Channel, team.Id, "")
if err != nil {
return nil, fmt.Errorf("get channel: %w", err)
}
return &MattermostClient{client, channel}, nil
}
type SlackConfig struct {
Token string
Channel string
}
type SlackClient struct {
Client *slack.Client
Channel string
}
func (client *SlackClient) Send(note *CrashNotification) {
blocks := []slack.Block{
slack.NewHeaderBlock(&slack.TextBlockObject{
Type: slack.PlainTextType,
Text: note.Title,
}),
slack.NewSectionBlock(&slack.TextBlockObject{
Type: slack.MarkdownType,
Text: note.Message,
}, []*slack.TextBlockObject{}, nil),
slack.NewDividerBlock(),
slack.NewSectionBlock(&slack.TextBlockObject{
Type: slack.MarkdownType,
Text: "*Logs*\n```\n" + note.Logs + "\n```",
}, []*slack.TextBlockObject{}, nil),
}
if note.Reason != "" {
blocks = append(blocks, slack.NewSectionBlock(&slack.TextBlockObject{
Type: slack.MarkdownType,
Text: "*Reason*\n```\n" + note.Reason + "\n```",
}, []*slack.TextBlockObject{}, nil))
}
_, _, _, err := client.Client.SendMessage(client.Channel, slack.MsgOptionBlocks(blocks...))
if err != nil {
klog.Warningf("Failed to notify Slack: %v", err)
}
}
// NewSlackClientFromEnv instantiates and configures a Slack client.
func NewSlackClientFromEnv() (*SlackClient, error) {
var cfg SlackConfig
if err := envconfig.Process("slack", &cfg); err != nil {
return nil, err
}
api := slack.New(cfg.Token)
return &SlackClient{
Client: api,
Channel: cfg.Channel,
}, nil
}
type TelegramConfig struct {
Token string
ChatID int64
}
type TelegramClient struct {
chat tgbotapi.Chat
bot *tgbotapi.BotAPI
}
func (client *TelegramClient) Send(note *CrashNotification) {
// Generate text
type entityMarker struct {
Type string
Content string
}
contents := []entityMarker{
{"bold", note.Title},
{"", note.Message},
{"bold", "Logs"},
{"pre", note.Logs},
{"bold", "Reason"},
{"pre", note.Reason},
}
// Generate text
cleartext := []string{}
for _, c := range contents {
cleartext = append(cleartext, c.Content)
}
// Get all joined groups
message := tgbotapi.NewMessage(client.chat.ID, strings.Join(cleartext, "\n"))
offset := 0
// Generate list of entities
for _, c := range contents {
length := len(utf16.Encode([]rune(c.Content)))
if c.Type != "" {
message.Entities = append(message.Entities, tgbotapi.MessageEntity{
Type: c.Type,
Offset: offset,
Length: length,
})
}
offset += length + 1
}
_, err := client.bot.Send(message)
if err != nil {
fmt.Println(err)
}
}
// NewTelegramClientFromEnv instantiates and configures a Telegram client.
func NewTelegramClientFromEnv() (*TelegramClient, error) {
var cfg TelegramConfig
if err := envconfig.Process("telegram", &cfg); err != nil {
return nil, err
}
bot, err := tgbotapi.NewBotAPI(cfg.Token)
if err != nil {
return nil, fmt.Errorf("init bot: %w", err)
}
// Get chat to verify
chat, err := bot.GetChat(tgbotapi.ChatInfoConfig{
ChatConfig: tgbotapi.ChatConfig{
ChatID: cfg.ChatID,
},
})
if err != nil {
return nil, fmt.Errorf("get chat: %w", err)
}
return &TelegramClient{
chat: chat,
bot: bot,
}, nil
}