-
Notifications
You must be signed in to change notification settings - Fork 2
/
slack.go
88 lines (84 loc) · 2.7 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
package outputs
import (
"fmt"
"sort"
"strings"
"github.com/hexbotio/hex/models"
"github.com/hexbotio/hex/parse"
"github.com/nlopes/slack"
)
// Slack struct
type Slack struct {
}
// Output function
func (x Slack) Write(message models.Message, config models.Config) {
api := slack.New(config.SlackToken)
msg := ""
params := slack.NewPostMessageParameters()
params.Username = config.BotName
image := config.SlackIcon
if image == "" {
image = ":nut_and_bolt:"
}
if message.Attributes["hex.rule.threaded"] == "true" && message.Attributes["hex.slack.response"] != "" {
params.ThreadTimestamp = message.Attributes["hex.slack.response"]
}
params.IconEmoji = image
for _, output := range message.Outputs {
if message.Debug && parse.EitherMember(config.Admins, message.Attributes["hex.user"], message.Attributes["hex.channel"]) {
output.Response = output.Response + "\n\n[ Debug: " + output.Command + " ]"
}
if message.Attributes["hex.rule.format"] == "true" {
msg = "*" + message.Attributes["hex.rule.name"] + "*"
color := "grey"
if output.Success {
color = "good"
} else {
color = "danger"
}
attachment := slack.Attachment{
Text: "```" + output.Response + "```",
Color: color,
MarkdownIn: []string{"text"},
}
params.Attachments = append(params.Attachments, attachment)
} else {
msg = msg + output.Response + "\n"
}
}
if message.Debug {
keys := make([]string, 0, len(message.Attributes))
for key, _ := range message.Attributes {
keys = append(keys, key)
}
sort.Strings(keys)
out := fmt.Sprintf("\nMESSAGE DEBUG (%d sec to complete)\n", message.EndTime-message.StartTime)
for _, key := range keys {
if strings.HasPrefix(key, "hex.var.") {
out = out + fmt.Sprintf(" %s: '%s'\n", key, "********")
} else {
out = out + fmt.Sprintf(" %s: '%s'\n", key, message.Attributes[key])
}
}
attachment := slack.Attachment{
Text: "```" + out + "```",
Color: "grey",
MarkdownIn: []string{"text"},
}
params.Attachments = append(params.Attachments, attachment)
}
if message.Attributes["hex.channel"] != "" {
_, respTimestamp, err := api.PostMessage(message.Attributes["hex.channel"], msg, params)
message.Attributes["hex.slack.response"] = respTimestamp
if err != nil {
config.Logger.Error("Slack Message Send Error: " + err.Error())
}
}
if message.Attributes["hex.rule.channel"] != "" && message.Attributes["hex.channel"] != message.Attributes["hex.rule.channel"] {
_, respTimestamp, err := api.PostMessage(message.Attributes["hex.rule.channel"], msg, params)
message.Attributes["hex.slack.response"] = respTimestamp
if err != nil {
config.Logger.Error("Slack Message Send Error: " + err.Error())
}
}
}