-
Notifications
You must be signed in to change notification settings - Fork 10
/
handler_gosquared_out.go
362 lines (310 loc) · 10.7 KB
/
handler_gosquared_out.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
package gosquared
import (
"encoding/json"
"errors"
"fmt"
"net/url"
"strings"
"time"
"github.com/grokify/chathooks/src/config"
"github.com/grokify/chathooks/src/handlers"
"github.com/grokify/chathooks/src/models"
cc "github.com/grokify/commonchat"
)
const (
DisplayName = "GoSquared"
HandlerKey = "gosquared"
DocumentationURL = "https://www.gosquared.com/customer/en/portal/articles/1996494-webhooks"
MessageBodyType = models.JSON
)
func NewHandler() handlers.Handler {
return handlers.Handler{MessageBodyType: MessageBodyType, Normalize: Normalize}
}
func Normalize(cfg config.Configuration, bytes []byte) (cc.Message, error) {
src, err := GosquaredOutBaseMessageFromBytes(bytes)
if err != nil {
return cc.NewMessage(), err
}
if len(src.Message.Id) > 0 {
return NormalizeLiveMessage(cfg, bytes)
} else if len(src.Person.Id) > 0 {
return NormalizeSmartGroup(cfg, bytes)
}
return NormalizeSiteTraffic(cfg, bytes)
}
func NormalizeSiteTraffic(cfg config.Configuration, bytes []byte) (cc.Message, error) {
ccMsg := cc.NewMessage()
iconURL, err := cfg.GetAppIconURL(HandlerKey)
if err == nil {
ccMsg.IconURL = iconURL.String()
}
src, err := GosquaredOutMessageSiteTrafficFromBytes(bytes)
if err != nil {
return ccMsg, err
}
if src.TriggeredAlert.Boundary == "upper" {
ccMsg.Activity = "Site traffic spike"
} else { // if src.TriggeredAlert.Boundary == "lower" {
ccMsg.Activity = "Site traffic dip"
}
pluralSuffix := "s"
if src.Concurrents == int64(1) {
pluralSuffix = ""
}
ccMsg.Title = fmt.Sprintf("[%s](%s) has [%v visitor%s online](%s)",
src.SiteDetails.SiteName,
src.SiteDetails.URL,
src.Concurrents,
pluralSuffix,
src.SiteDetails.DashboardURL())
return ccMsg, nil
}
func NormalizeSmartGroup(cfg config.Configuration, bytes []byte) (cc.Message, error) {
ccMsg := cc.NewMessage()
iconURL, err := cfg.GetAppIconURL(HandlerKey)
if err == nil {
ccMsg.IconURL = iconURL.String()
}
src, err := GosquaredOutMessageSmartGroupFromBytes(bytes)
if err != nil {
return ccMsg, err
}
verb := "exited"
if src.Boundary == "enter" {
verb = "entered"
}
ccMsg.Activity = fmt.Sprintf("User has %s Smart Group", verb)
ccMsg.Title = fmt.Sprintf("%s has %s [%s](%s)",
src.Person.Name,
verb,
src.Group.Name,
src.GroupURL())
return ccMsg, nil
}
func NormalizeLiveMessage(cfg config.Configuration, bytes []byte) (cc.Message, error) {
ccMsg := cc.NewMessage()
iconURL, err := cfg.GetAppIconURL(HandlerKey)
if err == nil {
ccMsg.IconURL = iconURL.String()
}
src, err := GosquaredOutLiveMessageFromBytes(bytes)
if err != nil {
return ccMsg, err
}
ccMsg.Activity = "Live chat message"
personInboxURL, errURL := src.PersonInboxURL()
person := src.Person.DisplayName(false, true)
if errURL == nil {
person = fmt.Sprintf("[%v](%v)", person, personInboxURL)
}
ccMsg.Title = fmt.Sprintf("%v sent a message", person)
attachment := cc.NewAttachment()
if len(strings.TrimSpace(src.Message.Content)) > 0 {
attachment.AddField(cc.Field{
Title: "Message",
Value: strings.TrimSpace(src.Message.Content)})
}
if src.Message.Timestamp > 0 {
epochMilli := src.Message.Timestamp
epochNano := epochMilli * 1000000
dt := time.Unix(0, epochNano)
attachment.AddField(cc.Field{
Title: "Time",
Value: dt.Format(time.RFC822)})
}
if len(attachment.Fields) > 0 {
ccMsg.AddAttachment(attachment)
}
return ccMsg, nil
}
type GosquaredOutBaseMessage struct {
TriggeredAlert GosquaredOutTriggeredAlert `json:"triggeredAlert,omitempty"`
Concurrents int64 `json:"concurrents,omitempty"`
Person GosquaredOutPerson `json:"person,omitempty"`
Message GosquaredOutLiveMessageMessage `json:"message,omitempty"`
}
func GosquaredOutBaseMessageFromBytes(bytes []byte) (GosquaredOutBaseMessage, error) {
msg := GosquaredOutBaseMessage{}
err := json.Unmarshal(bytes, &msg)
return msg, err
}
type GosquaredOutMessageSiteTraffic struct {
TriggeredAlert GosquaredOutTriggeredAlert `json:"triggeredAlert,omitempty"`
SiteDetails GosquaredOutSiteDetails `json:"siteDetails,omitempty"`
Concurrents int64 `json:"concurrents,omitempty"`
}
func GosquaredOutMessageSiteTrafficFromBytes(bytes []byte) (GosquaredOutMessageSiteTraffic, error) {
msg := GosquaredOutMessageSiteTraffic{}
err := json.Unmarshal(bytes, &msg)
return msg, err
}
type GosquaredOutLiveMessage struct {
Version string `json:"version,omitempty"`
SiteToken string `json:"site_token,omitempty"`
Timestamp string `json:"timestamp,omitempty"`
Person GosquaredOutPerson `json:"person,omitempty"`
Message GosquaredOutLiveMessageMessage `json:"message,omitempty"`
}
func (msg *GosquaredOutLiveMessage) PersonInboxURL() (string, error) {
if len(strings.TrimSpace(msg.SiteToken)) == 0 || len(strings.TrimSpace(msg.Person.Id)) == 0 {
return "", errors.New("Information missing for PersonInboxURL")
}
personIdEsc := url.QueryEscape(strings.TrimSpace(msg.Person.Id))
personInboxURL := fmt.Sprintf(
"https://www.gosquared.com/inbox/%v/inbox/%v",
strings.TrimSpace(msg.SiteToken),
personIdEsc)
return personInboxURL, nil
}
/*
https://www.gosquared.com/inbox/GSN-345166-V/inbox/Anon%20Chat:%20bba4b6264b073a17c74f1b0da7720114
The rule to build the url is
https://www.gosquared.com/inbox/<site_token>/inbox/<person id encoded>.
*/
func GosquaredOutLiveMessageFromBytes(bytes []byte) (GosquaredOutLiveMessage, error) {
msg := GosquaredOutLiveMessage{}
err := json.Unmarshal(bytes, &msg)
return msg, err
}
/*
{
"version":"0.0.1",
"site_token":"GSN-67890-A",
"timestamp":"2017-05-02T05:19:34.252Z",
"person":{
"id":"Anon Chat: bba4b6264b073a17c74f1b0da7720114",
"email":"",
"name":"",
"avatar":"",
"chat":{
"archived":null,
"latest":{
"message":{
"type":"message",
"id":"167c06cf-ff7c-4d09-880c-5533795f9673",
"content":"hello world",
"timestamp":1493702374252,
"from":"client",
"private":false,
"session":{
"title":"",
"href":"http://127.0.0.1:2015/"
},
*/
type GosquaredOutLiveMessageMessage struct {
Type string `json:"message,omitempty"`
Id string `json:"id,omitempty"`
Content string `json:"content,omitempty"`
Timestamp int64 `json:"timestamp,omitempty"`
From string `json:"from,omitempty"`
Private bool `json:"private,omitempty"`
Session GosquaredOutLiveMessageSession `json:"session,omitempty"`
Data interface{} `json:"data,omitempty"`
Entities []interface{} `json:"entities,omitempty"`
PersonID string `json:"person_id,omitempty"`
}
type GosquaredOutLiveMessageSession struct {
Title string `json:"title,omitempty"`
Href string `json:"href,omitempty"`
}
/*
"message":{
"type":"message",
"id":"167c06cf-ff7c-4d09-880c-5533795f9673",
"content":"hello world",
"timestamp":1493702374252,
"from":"client",
"private":false,
"session":{
"title":"",
"href":"http://127.0.0.1:2015/"
},
"data":{
},
"entities":[
],
"person_id":"Anon Chat: bba4b6264b073a17c74f1b0da7720114"
}
*/
type GosquaredOutMessageSmartGroup struct {
Version string `json:"version,omitempty"`
SiteToken string `json:"site_token,omitempty"`
Group GosquaredOutGroup `json:"group,omitempty"`
Boundary string `json:"boundary,omitempty"`
Person GosquaredOutPerson `json:"person,omitempty"`
}
func GosquaredOutMessageSmartGroupFromBytes(bytes []byte) (GosquaredOutMessageSmartGroup, error) {
msg := GosquaredOutMessageSmartGroup{}
err := json.Unmarshal(bytes, &msg)
return msg, err
}
type GosquaredOutGroup struct {
Name string `json:"name,omitempty"`
Id string `json:"id,omitempty"`
}
func (msg *GosquaredOutMessageSmartGroup) GroupURL() string {
// https://www.gosquared.com/people/GSN-466237-B/last-seen-1-day
return fmt.Sprintf("https://www.gosquared.com/people/%s/%s",
msg.SiteToken, msg.Group.Id)
}
type GosquaredOutPerson struct {
CreatedAt string `json:"created_at,omitempty"`
Phone string `json:"phone,omitempty"`
Avatar string `json:"avatar,omitempty"`
Description string `json:"description,omitempty"`
Username string `json:"username,omitempty"`
Email string `json:"email,omitempty"`
Name string `json:"name,omitempty"`
Id string `json:"id,omitempty"`
}
func (person *GosquaredOutPerson) DisplayName(extraEmail bool, anonymous bool) string {
displayName := ""
if len(strings.TrimSpace(person.Name)) > 0 {
displayName = strings.TrimSpace(person.Name)
} else if len(strings.TrimSpace(person.Username)) > 0 {
displayName = strings.TrimSpace(person.Username)
}
if len(strings.TrimSpace(person.Email)) > 0 {
if len(displayName) > 0 && extraEmail {
displayName = fmt.Sprintf("%v (%v)", displayName, strings.TrimSpace(person.Email))
} else {
displayName = strings.TrimSpace(person.Email)
}
}
if len(displayName) == 0 && anonymous {
displayName = "Anonymous user"
}
return displayName
}
type GosquaredOutMessageConcurrent struct {
TriggeredAlert GosquaredOutTriggeredAlert `json:"triggeredAlert,omitempty"`
Concurrents int64 `json:"concurrents,omitempty"`
SiteDetails GosquaredOutSiteDetails `json:"siteDetails,omitempty"`
}
type GosquaredOutTriggeredAlert struct {
Id int64 `json:"id,omitempty"`
Boundary string `json:"boundary,omitempty"`
Value string `json:"value,omitempty"`
Type string `json:"type,omitempty"`
}
type GosquaredOutSiteDetails struct {
UserId int64 `json:"user_id,omitempty"`
Acct string `json:"acct,omitempty"`
Email string `json:"email,omitempty"`
FirstName string `json:"first_name,omitempty"`
LastName string `json:"last_name,omitempty"`
SiteName string `json:"site_name,omitempty"`
Domain string `json:"domain,omitempty"`
URL string `json:"url,omitempty"`
Timezone string `json:"timezone,omitempty"`
}
func (site *GosquaredOutSiteDetails) DashboardURL() string {
return fmt.Sprintf("https://www.gosquared.com/now/%s", site.Acct)
}
func DashboardURL(siteToken string) string {
return fmt.Sprintf("https://www.gosquared.com/now/%s", siteToken)
}
func PeopleEveryoneURL(siteToken string) string {
// https://www.gosquared.com/people/GSN-466237-B/everyone
return fmt.Sprintf("https://www.gosquared.com/people/%s/everyone", siteToken)
}