-
Notifications
You must be signed in to change notification settings - Fork 61
/
mattermost_json.go
54 lines (45 loc) · 1.21 KB
/
mattermost_json.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
package mattermost
import (
"encoding/json"
"regexp"
"github.com/containrrr/shoutrrr/pkg/types"
)
// JSON payload for mattermost notifications
type JSON struct {
Text string `json:"text"`
UserName string `json:"username,omitempty"`
Channel string `json:"channel,omitempty"`
IconEmoji string `json:"icon_emoji,omitempty"`
IconURL string `json:"icon_url,omitempty"`
}
var iconURLPattern = regexp.MustCompile(`https?://`)
// SetIcon sets the appropriate icon field in the payload based on whether the input is a URL or not
func (j *JSON) SetIcon(icon string) {
j.IconURL = ""
j.IconEmoji = ""
if icon != "" {
if iconURLPattern.MatchString(icon) {
j.IconURL = icon
} else {
j.IconEmoji = icon
}
}
}
// CreateJSONPayload for usage with the mattermost service
func CreateJSONPayload(config *Config, message string, params *types.Params) ([]byte, error) {
payload := JSON{
Text: message,
UserName: config.UserName,
Channel: config.Channel,
}
if params != nil {
if value, found := (*params)["username"]; found {
payload.UserName = value
}
if value, found := (*params)["channel"]; found {
payload.Channel = value
}
}
payload.SetIcon(config.Icon)
return json.Marshal(payload)
}