-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage.go
58 lines (49 loc) · 1.75 KB
/
message.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
package slack15
import "fmt"
// Envelope describes destination and sender (as visiable in Slack).
// Defaults are defined by webhook settings in Slack and this structure
// allows for overwritting these.
// More info at https://api.slack.com/incoming-webhooks
type Envelope struct {
// Destination channel in slack, e.g. "#log" or "@bobby"
Channel string `json:"channel,omitempty"`
// Username
Username string `json:"username,omitempty"`
// Icon URL, e.g. "https://slack.com/img/icons/app-57.png"
IconURL string `json:"icon_url,omitempty"`
// Icon emoji, e.g. ":rabbit:"
IconEmoji string `json:"icon_emoji,omitempty"`
}
// Field represents a key-value pair within slack message
type Field struct {
Title string `json:"title"` // Field title (key)
Value string `json:"value"` // Text
Short bool `json:"short,omitempty"` // If false field will occupy the whole row
}
// String returns field's value, so it's correctly printed in other formatters
func (f Field) String() string {
return f.Value
}
// Long returns Field which will occupy the whole row in Slack message.
// It can be used as a value of key-value pair passed to logger.
//
// Example:
// log.Info("got message", "source", ip, "data", slack15.Long(data)).
func Long(value interface{}) Field {
return Field{
Title: "", // will use key from log context
Value: fmt.Sprint(value),
Short: false,
}
}
// Message represents a single slack message.
// It's serialised to JSON and send to incoming webhook.
type message struct {
Envelope
Attachments [1]struct {
Text string `json:"text"`
Fallback string `json:"fallback,omitempty"` // Required plain-text summary
Color string `json:"color,omitempty"`
Fields []Field `json:"fields,omitempty"`
} `json:"attachments"`
}