This repository has been archived by the owner on Sep 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
bot.go
265 lines (236 loc) · 6.11 KB
/
bot.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package main
import (
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"strings"
"sync/atomic"
"github.com/digitalrebar/provision/v4/models"
"github.com/gorilla/websocket"
)
type Bot struct {
msgId int64
slackToken string
slackConn *websocket.Conn
auth *apiAuth
channels []apiChannel
groups []apiGroup
}
// base type for incoming messages. This will be embedded into other types.
type evtBase struct {
Type string `json:"type,omitempty"`
Subtype string `json:"subtype,omitempty"`
}
/* Not allowed by RTM - could be used with ChatAPI
type Field struct {
Title string `json:"title:omitempty"`
Value string `json:"value:omitempty"`
Short bool `json:"short:omitempty"`
}
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"`
AutherIcon string `json:"author_icon,omitempty"`
Title string `json:"title,omitempty"`
TitleLink string `json:"title_link,omitempty"`
Text string `json:"text,omitempty"`
Fields []Field `json:"fields,omitempty"`
ImageURL string `json:"image_url,omitempty"`
ThumbURL string `json:"thumb_url,omitempty"`
footer string `json:"footer,omitempty"`
footerIcon string `json:"footer_icon,omitempty"`
Time int64 `json:"ts,omitempty"`
}
*/
// All outgoing messages to Slack will be of this type.
type Msg struct {
ID int64 `json:"id"`
Channel string `json:"channel,omitempty"`
Text string `json:"text,omitempty"`
Type string `json:"type,omitempty"`
}
type apiBase struct {
OK bool `json:"ok"`
Error string `json:"error"`
Warning string `json:"warning"`
}
func (sb *apiBase) Ok() bool {
return sb.OK
}
func (sb *apiBase) ErrOrNil() error {
if sb.Ok() {
return nil
}
return errors.New(sb.Error)
}
type Apier interface {
Ok() bool
ErrOrNil() error
}
type apiConnResp struct {
apiBase
URL string `json:"url"`
Team struct {
ID string `json:"id"`
Name string `json:"name"`
Domain string `json:"domain"`
EnterpriseId string `json:"enterprise_id"`
EnterpriseName string `json:"enterprise_name"`
} `json:"team"`
Self struct {
ID string `json:"id"`
Name string `json:"name"`
} `json:"self"`
}
type apiAuth struct {
apiBase
Team string `json:"team"`
User string `json:"user"`
TeamID string `json:"team_id"`
UserID string `json:"user_id"`
}
// this only includes the data we care about.
type apiChannel struct {
ID string `json:"id"`
Name string `json:"name"`
IsArchived bool `json:"is_archived"`
IsMember bool `json:"is_member"`
}
type apiChannels struct {
apiBase
Channels []apiChannel `json:"channels"`
}
type apiGroup struct {
ID string `json:"id"`
Name string `json:"name"`
IsArchived bool `json:"is_archived"`
}
type apiGroups struct {
apiBase
Groups []apiGroup `json:"groups"`
}
func (s *Bot) apiGet(v Apier, api string, args ...string) error {
url := fmt.Sprintf("https://slack.com/api/%s?token=%s", api, s.slackToken)
if len(args) > 0 {
url = fmt.Sprintf("%s&%s", url, strings.Join(args, "&"))
}
resp, err := http.Get(url)
if err != nil {
return err
}
if resp.Body != nil {
defer resp.Body.Close()
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("Slack API failed with status %d", resp.StatusCode)
}
dec := json.NewDecoder(resp.Body)
if err := dec.Decode(&v); err != nil {
return err
}
return v.ErrOrNil()
}
func (s *Bot) wsEventLoop() {
for {
msg := map[string]interface{}{}
if err := s.slackConn.ReadJSON(&msg); err != nil {
log.Fatalf("Error reading frame from Slack: %v", err)
}
log.Printf("Received: %v", msg)
msgType, ok := msg["type"].(string)
if !ok || msgType == "" {
log.Printf("Invalid message received from Slack, ignoring")
}
// Add handling code for appropriate message types here
if msgType == "message" {
message := msg["text"].(string)
log.Printf("Message: %s\n", message)
} else {
log.Printf("Message type %s received", msgType)
}
}
}
func (s *Bot) Send(msg Msg) error {
msg.ID = atomic.AddInt64(&s.msgId, 1)
if msg.Type == "" {
msg.Type = "message"
}
return s.slackConn.WriteJSON(&msg)
}
func (s *Bot) Publish(e *models.Event) error {
m := e.Text()
log.Printf("Sending event: %v\n", m)
for _, channel := range s.channels {
if err := s.Send(Msg{Channel: channel.ID, Text: m}); err != nil {
return err
}
}
for _, group := range s.groups {
if err := s.Send(Msg{Channel: group.ID, Text: m}); err != nil {
return err
}
}
return nil
}
func (s *Bot) DialSlack() error {
whoami := &apiAuth{}
channels := &apiChannels{}
groups := &apiGroups{}
if err := s.apiGet(whoami, "auth.test"); err != nil {
return err
}
s.auth = whoami
if err := s.apiGet(channels, "channels.list", "exclude_archived=true", "exclude_members=true"); err != nil {
return err
}
s.channels = []apiChannel{}
for _, channel := range channels.Channels {
if !channel.IsMember {
continue
}
log.Printf("%s is a member of channel %s", s.auth.User, channel.Name)
s.channels = append(s.channels, channel)
}
if err := s.apiGet(groups, "groups.list", "exclude_archived=true"); err != nil {
return err
}
s.groups = groups.Groups
for _, group := range groups.Groups {
log.Printf("%s is a member of group %s", s.auth.User, group.Name)
}
wsConn := &apiConnResp{}
if err := s.apiGet(wsConn, "rtm.connect"); err != nil {
return err
}
var dialer *websocket.Dialer
ws, _, err := dialer.Dial(wsConn.URL, nil)
if err != nil {
return err
}
s.slackConn = ws
go s.wsEventLoop()
for _, channel := range s.channels {
s.Send(Msg{Channel: channel.ID, Text: fmt.Sprintf("%s lives!", s.auth.User)})
}
for _, group := range s.groups {
s.Send(Msg{Channel: group.ID, Text: fmt.Sprintf("%s lives!", s.auth.User)})
}
return nil
}
func (s *Bot) Shutdown() error {
return s.slackConn.Close()
}
func NewSlackBot(slackToken string) (*Bot, error) {
res := &Bot{
slackToken: slackToken,
}
if err := res.DialSlack(); err != nil {
return nil, err
}
return res, nil
}