/
handler_gosquared2_out.go
192 lines (159 loc) · 5.77 KB
/
handler_gosquared2_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
package gosquared2
import (
"encoding/json"
"fmt"
"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.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
}
ccMsg.Activity = "Site traffic change"
attachment := cc.NewAttachment()
attachment.AddField(cc.Field{
Title: "Site",
Value: fmt.Sprintf("[%s](%s)", src.SiteDetails.SiteName, src.SiteDetails.URL)})
attachment.AddField(cc.Field{
Title: "Concurrent Users Online",
Value: fmt.Sprintf("%v", src.Concurrents)})
attachment.AddField(cc.Field{
Title: "Trigger Boundary",
Value: fmt.Sprintf("%v", src.TriggeredAlert.Boundary)})
ccMsg.AddAttachment(attachment)
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
}
ccMsg.Activity = "Smart Group update"
attachment := cc.NewAttachment()
attachment.AddField(cc.Field{
Title: "Smart Group",
Value: fmt.Sprintf("[%v](%v)", src.Group.Name, src.GroupURL())})
attachment.AddField(cc.Field{
Title: "Person",
Value: src.Person.Name})
attachment.AddField(cc.Field{
Title: "Action",
Value: src.Boundary})
ccMsg.AddAttachment(attachment)
return ccMsg, nil
}
type GosquaredOutBaseMessage struct {
TriggeredAlert GosquaredOutTriggeredAlert `json:"triggeredAlert,omitempty"`
Concurrents int64 `json:"concurrents,omitempty"`
Person GosquaredOutPerson `json:"person,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 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"`
}
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)
}