-
Notifications
You must be signed in to change notification settings - Fork 5
/
flowdock.go
367 lines (325 loc) · 8.93 KB
/
flowdock.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package main
import (
"bufio"
"bytes"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"strings"
"time"
)
var availableFlows []flows
var currentUsers []user
func tokenFlowdock() string {
return base64.StdEncoding.EncodeToString([]byte(config.FlowdockAccessToken))
}
//ListenStream starts pulling the flowdock stream api
func listenStream() {
fetchFlows()
go fetchUserSchedule()
res := connectToFlow()
defer res.Body.Close()
for {
processFlowRow(parseFlowRow(bufio.NewReader(res.Body)))
}
}
//fetchFlows fetches all the flows we have access to
func fetchFlows() {
performGet("flows", parseAvailableFlows())
}
func parseAvailableFlows() parseCallback {
return func(payload []byte) {
err := json.Unmarshal(payload, &availableFlows)
if err != nil {
log.Fatalf("Error parsing flows data %+v", err)
}
}
}
func connectToFlow() *http.Response {
url := fmt.Sprintf("https://%[email protected]/flows?filter=%s", config.FlowdockAccessToken, config.Flows)
req, _ := http.NewRequest("GET", url, nil)
req.Header.Add("Authorization", fmt.Sprintf("Basic %s", tokenFlowdock()))
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
log.Fatalln("could not fetch streaming api ", err)
} else if res.StatusCode != 200 {
log.Fatalf("got error code: %+v from flowdock.\n", res.StatusCode)
}
return res
}
func parseFlowRow(reader *bufio.Reader) (flowdockMsg, []byte) {
line, err := reader.ReadBytes('\r')
if err != nil {
log.Fatalf("something went wrong reading the payload: %s", err)
}
line = bytes.TrimSpace(line)
jsonString := string(line[:])
if len(jsonString) < 4 {
log.Fatalln("got empty response from flowdock, shutting down")
os.Exit(1)
}
var message flowdockMsg
json.Unmarshal(line, &message)
return message, line
}
func processFlowRow(flowMessage flowdockMsg, line []byte) {
var flowUpdatedMessage flowdockUpdatedMsg
var flowComment flowdockComment
switch flowMessage.Event {
case "message":
intent, err := FetchIntent(flowMessage.Content)
if err != nil {
witErrorResponseToFlowdock(flowMessage, err)
} else {
ret := ProcessIntent(intent)
replyToFlow(ret, flowMessage.Id, flowMessage.Flow)
}
case "message-edit":
json.Unmarshal(line, &flowUpdatedMessage)
intent, err := FetchIntent(flowUpdatedMessage.Content.Updated_content)
if err != nil {
witErrorResponseToFlowdock(flowMessage, err)
} else {
ret := ProcessIntent(intent)
replyToFlow(ret, flowUpdatedMessage.Id, flowUpdatedMessage.Flow)
}
case "comment":
if flowMessage.User != "77156" {
var parentMessageID int64
json.Unmarshal(line, &flowComment)
intent, err := FetchIntent(flowComment.Content.Text)
if err != nil {
flowMessage.Id = parentMessageID
witErrorResponseToFlowdock(flowMessage, err)
} else {
ret := ProcessIntent(intent)
for _, v := range flowComment.Tags {
if strings.Contains(v, "influx") {
parentID, _ := strconv.ParseInt(strings.Split(v, ":")[1], 0, 64)
parentMessageID = parentID
}
}
replyToFlow(ret, parentMessageID, flowComment.Flow)
}
} else {
//log.Println("skipping Cortex's message.")
}
}
}
func witErrorResponseToFlowdock(flowMessage flowdockMsg, err error) {
ret :=
WitResponse{
WitArduinoResponse{},
WitTemperatureResponse{},
WitGithubResponse{},
witError{fmt.Sprintf("Error: %+v", err)},
}
replyToFlow(ret, flowMessage.Id, flowMessage.Flow)
}
//getFlowURL given a flow id as string, return the url for the flow
func getFlowURL(id string) (string, error) {
for _, flow := range availableFlows {
if flow.Id == id {
return flow.Url, nil
}
}
return "", errors.New("Flow url not found by key " + id)
}
//getFlowName given a flow id as string, return the name of the flow
func getFlowName(id string) (string, error) {
for _, flow := range availableFlows {
if flow.Id == id {
return flow.Parameterized_name, nil
}
}
return "", errors.New("Flow url not found by key " + id)
}
//getIssueURLForFlowName given a flow name, return the issues url for it
func getIssueURLForFlowName(parametizedName string) (string, error) {
for _, row := range config.FlowsTicketsUrls {
url, ok := row[parametizedName]
if ok {
return url, nil
}
}
return "", errors.New("Could not find issue url for flow: " + parametizedName)
}
func replyToFlow(ret WitResponse, originalMessageID int64, flowID string) {
if ret.Temperature.Unit != "" {
handleTemperature(ret, originalMessageID, flowID)
} else if len(ret.Github.issues) > 0 {
handleGithub(ret, originalMessageID, flowID)
} else if ret.Error.msg != "" {
flowdockPost(ret.Error.msg, originalMessageID, flowID)
}
}
func handleTemperature(ret WitResponse, originalMessageID int64, flowID string) {
temperature := ret.Temperature.Degrees
switch ret.Temperature.Unit {
case "C":
flowdockPost(fmt.Sprintf("Which is %+vF", cToF(temperature)), originalMessageID, flowID)
case "F":
flowdockPost(fmt.Sprintf("Which is %+vC", fToC(temperature)), originalMessageID, flowID)
}
}
func handleGithub(ret WitResponse, originalMessageID int64, flowID string) {
for _, issue := range ret.Github.issues {
flowParametizedName, error := getFlowName(flowID)
if error != nil {
log.Printf("Error trying to get parametized flow name for id %v", flowID)
}
issueURL, error := getIssueURLForFlowName(flowParametizedName)
if error != nil {
log.Printf("%s", error)
}
url := fmt.Sprintf("just click here: %+v%+v", issueURL, issue)
flowdockPost(url, originalMessageID, flowID)
}
}
func flowdockPost(message string, originalMessageID int64, flowID string) {
flowURL, err := getFlowURL(flowID)
if err != nil {
log.Printf("Error getting flow id, got %v", err)
return
}
url := fmt.Sprintf("%+v/messages/%+v/comments", flowURL, originalMessageID)
client := &http.Client{}
payload := []byte(`{
"event": "comment",
"content":"` + message + `"}`)
req, _ := http.NewRequest("POST", url, bytes.NewReader(payload))
req.Header.Add("Authorization", fmt.Sprintf("Basic %s", tokenFlowdock()))
req.Header.Add("Content-type", "application/json")
res, err := client.Do(req)
if err != nil {
log.Printf("Error posting a message to Flowdock: %v", err)
return
} else if res.StatusCode != 200 {
log.Printf("We got a non 200 code: %+v\n", res.StatusCode)
return
}
defer res.Body.Close()
value, err := ioutil.ReadAll(res.Body)
_ = value
if err != nil {
log.Fatalf("Could not read body, got: %v", err)
}
}
func fToC(f int) int {
return (f - 32) * 5 / 9
}
func cToF(c int) int {
return c*9/5 + 32
}
func fetchUserSchedule() {
for _ = range time.Tick(1 * time.Minute) {
fetchUsers()
}
}
func fetchUsers() {
performGet("users", parseUsers())
}
func performGet(path string, f parseCallback) {
url := fmt.Sprintf("https://%[email protected]/%s", config.FlowdockAccessToken, path)
res, err := http.Get(url)
if err != nil {
log.Fatalf("Error getting %+v: %v", path, err)
} else if res.StatusCode != 200 {
log.Fatalf("got status code %+v", res.StatusCode)
}
dataAsJson, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatalf("error reading body, got: %+v", err)
}
f(dataAsJson)
res.Body.Close()
}
func parseUsers() parseCallback {
return func(payload []byte) {
err := json.Unmarshal(payload, ¤tUsers)
if err != nil {
log.Printf("Unabled to parse users list, got %+v", err)
}
return
}
}
func getCortexUserID(email string) int64 {
for _, value := range currentUsers {
if value.Email == email {
return value.ID
}
}
return 0
}
//flowdockMsg struct all the information we care about from flowdock message of type message
type flowdockMsg struct {
Event string
Tags []string
Uuid string
Persist bool
Id int64
Flow string
Content string
Sent int64
User string
}
//flowdockUpdatedMsg struct all the information we care about from flowdock message of type update message
type flowdockUpdatedMsg struct {
Event string
Tags []string
Uuid string
Persist bool
Id int64
Flow string
Content flowdockContent
Sent int64
User string
}
type flowdockContent struct {
Message float64
Updated_content string
}
//flowdockComment struct all the information we care about from flowdock message of type comment
type flowdockComment struct {
Event string
Tags []string
Uuid string
Persist bool
Id int64
Flow string
Content flowdockCommentContent
Sent int64
User string
}
type flowdockCommentContent struct {
Title string
Text string
}
//flows struct that holds information about all the flows we can access
type flows struct {
Id string
Name string
Parameterized_name string
Email string
Description string
Url string
Web_url string
Unread_mentions int
}
//we fetch all the users currently logged in and use this struct to stuff them into
type user struct {
ID int64
Nick string
Email string
Avatar string
Mame string
Website string
}
type parseCallback func([]byte)