-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslack.go
More file actions
192 lines (162 loc) · 5.73 KB
/
slack.go
File metadata and controls
192 lines (162 loc) · 5.73 KB
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
package main
// handles slack API interface for sending webhooks back with responses
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"github.com/parnurzeal/gorequest"
)
// Field - struct
type Field struct {
Title string `json:"title"`
Value string `json:"value"`
Short bool `json:"short"`
}
// BotDMPayload - struct for bot DMs
type BotDMPayload struct {
Token string `json:"token,omitempty"`
Channel string `json:"channel,omitempty"`
Text string `json:"text,omitempty"`
AsUser bool `json:"as_user,omitempty"`
Attachments []Attachment `json:"attachments,omitempty"`
IconEmoji string `json:"icon_emoji,omitempty"`
IconURL string `json:"icon_url,omitempty"`
LinkNames bool `json:"link_names,omitempty"`
Mkrdwn bool `json:"mrkdwn,omitempty"`
Parse string `json:"parse,omitempty"`
ReplyBroadcast bool `json:"reply_broadcast,omitempty"`
ThreadTS string `json:"thread_ts,omitempty"`
UnfurlLinks bool `json:"unfurl_links,omitempty"`
UnfurlMedia bool `json:"unfurl_media,omitempty"`
Username string `json:"username,omitempty"`
}
// Attachment - struct
type Attachment struct {
Fallback string `json:"fallback,omitempty"`
Color string `json:"color,omitempty"`
PreText string `json:"pretext,omitempty"`
AuthorName string `json:"author_name,omitempty"`
AuthorLink string `json:"author_link,omitempty"`
AuthorIcon string `json:"author_icon,omitempty"`
Title string `json:"title,omitempty"`
TitleLink string `json:"title_link,omitempty"`
Text string `json:"text,omitempty"`
ImageURL string `json:"image_url,omitempty"`
Fields []*Field `json:"fields,omitempty"`
Footer string `json:"footer,omitempty"`
FooterIcon string `json:"footer_icon,omitempty"`
Timestamp int64 `json:"ts,omitempty"`
MarkdownIn []string `json:"mrkdwn_in,omitempty"`
}
// Payload - struct
type Payload struct {
Parse string `json:"parse,omitempty"`
Username string `json:"username,omitempty"`
IconURL string `json:"icon_url,omitempty"`
IconEmoji string `json:"icon_emoji,omitempty"`
Channel string `json:"channel,omitempty"`
Text string `json:"text,omitempty"`
LinkNames string `json:"link_names,omitempty"`
Attachments []Attachment `json:"attachments,omitempty"`
UnfurlLinks bool `json:"unfurl_links,omitempty"`
UnfurlMedia bool `json:"unfurl_media,omitempty"`
}
// ReactionPayload - payload to send an emoji reaction to a message
type ReactionPayload struct {
Token string `json:"token"`
Name string `json:"name"`
Channel string `json:"channel"`
TimeStamp string `json:"timestamp"`
}
const (
reactionAddURL string = "https://slack.com/api/reactions.add"
)
// AddField - add fields
func (attachment *Attachment) AddField(field Field) *Attachment {
attachment.Fields = append(attachment.Fields, &field)
return attachment
}
func redirectPolicyFunc(req gorequest.Request, via []gorequest.Request) error {
return fmt.Errorf("incorrect token (redirection)")
}
// Send - send message
func Send(webhookURL string, proxy string, payload Payload) []error {
request := gorequest.New().Proxy(proxy)
resp, _, err := request.
Post(webhookURL).
RedirectPolicy(redirectPolicyFunc).
Send(payload).
End()
if err != nil {
return err
}
if resp.StatusCode >= 400 {
return []error{fmt.Errorf("error sending msg. Status: %v", resp.Status)}
}
return nil
}
// Wrangler - wrangle slack webhook calls
func Wrangler(webhookURL string, message string, myChannel string, attachments Attachment) {
payload := Payload{
Text: message,
Username: "tagger",
Channel: myChannel,
IconEmoji: ":spray-paint:",
Attachments: []Attachment{attachments},
}
err := Send(webhookURL, "", payload)
if len(err) > 0 {
fmt.Printf("Slack Messaging Error in Wrangler function in slack.go: %s\n", err)
}
}
// WranglerDM - Send chat.Post API DM messages "as the bot"
func WranglerDM(myBot TagBot, payload BotDMPayload) error {
url := "https://slack.com/api/chat.postMessage"
payload.Token = myBot.SlackBotToken
payload.AsUser = true
jsonStr, err := json.Marshal(&payload)
if err != nil {
Logit("error attempting to marshal struct to json for slack BotDMPayload: "+err.Error(), false, "err")
return err
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
if err != nil {
Logit("error in http.NewRequest in WranglerDM Func: "+err.Error(), false, "err")
return err
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", "Bearer "+myBot.SlackBotToken)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
Logit("error in client.Do in WranglerDM: "+err.Error(), false, "err")
return err
}
defer resp.Body.Close()
return err
}
// AddReaction - add an emoji reaction to a message (expects proper ReactionPayload struct)
func AddReaction(tagbot TagBot, payload ReactionPayload) error {
payload.Token = tagbot.SlackBotToken
jsonStr, err := json.Marshal(&payload)
if err != nil {
Logit("Error attempting to marshal struct to json for slack AddReaction", false, "err")
return err
}
req, err := http.NewRequest("POST", reactionAddURL, bytes.NewBuffer(jsonStr))
if err != nil {
Logit("Error in http.NewRequest in `AddReaction` in `slack.go`", false, "err")
return err
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", "Bearer "+tagbot.SlackBotToken)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
Logit("Error in client.Do in `CreateList` in `trello.go`", false, "err")
return err
}
defer resp.Body.Close()
return err
}