-
Notifications
You must be signed in to change notification settings - Fork 10
/
handler.go
245 lines (213 loc) · 6.5 KB
/
handler.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
package userlike
import (
"errors"
"fmt"
"strings"
cc "github.com/grokify/commonchat"
"github.com/grokify/chathooks/pkg/config"
"github.com/grokify/chathooks/pkg/handlers"
"github.com/grokify/chathooks/pkg/models"
)
const (
DisplayName = "Userlike"
HandlerKey = "userlike"
MessageDirection = "out"
MessageBodyType = models.JSON
)
var (
ChatMetaEvents = []string{"feedback", "forward", "rating", "receive", "start", "survey"}
OperatorEvents = []string{"away", "back", "offline", "online"}
)
func NewHandler() handlers.Handler {
return handlers.Handler{MessageBodyType: MessageBodyType, Normalize: Normalize}
}
func Normalize(cfg config.Configuration, hReq handlers.HandlerRequest) (cc.Message, error) {
bodyBytes := hReq.Body
srcMsgBase, err := UserlikeBaseOutMessageFromBytes(bodyBytes)
if err != nil {
return cc.Message{}, err
}
if srcMsgBase.Type == "offline_message" && srcMsgBase.Event == "receive" {
srcMsg, err := UserlikeOfflineMessageOutMessageFromBytes(bodyBytes)
if err != nil {
return cc.Message{}, err
}
return NormalizeOfflineMessage(cfg, srcMsg), nil
} else if srcMsgBase.Type == "chat_meta" {
srcMsg, err := UserlikeChatMetaStartOutMessageFromBytes(bodyBytes)
if err != nil {
return cc.Message{}, err
}
return NormalizeChatMeta(cfg, srcMsg), nil
} else if srcMsgBase.Type == "operator" {
srcMsg, err := UserlikeOperatorOutMessageFromBytes(bodyBytes)
if err != nil {
return cc.Message{}, err
}
return NormalizeOperator(cfg, srcMsg), nil
} else if srcMsgBase.Type == "chat_widget" {
srcMsg, err := UserlikeChatWidgetOutMessageFromBytes(bodyBytes)
if err != nil {
return cc.Message{}, err
}
return NormalizeChatWidget(cfg, srcMsg), nil
}
return cc.Message{}, errors.New("type not supported")
}
func GlipActivityForChat(event string, feedback string) string {
eventDisplay := event
eventMap := map[string]string{
"start": "session started",
"forward": "session forwarded",
"rating": "rating received",
"feedback": "feedback received",
"survey": "survey received",
"receive": "session ended",
"goal": "goal achieved"}
if event == "rating" && len(feedback) > 0 {
eventDisplay = eventMap["feedback"]
} else {
if displayTry, ok := eventMap[event]; ok {
eventDisplay = displayTry
}
}
return fmt.Sprintf("Chat %s", eventDisplay)
}
func NormalizeOfflineMessage(cfg config.Configuration, src UserlikeOfflineMessageOutMessage) cc.Message {
ccMsg := cc.NewMessage()
iconURL, err := cfg.GetAppIconURL(HandlerKey)
if err == nil {
ccMsg.IconURL = iconURL.String()
}
ccMsg.Activity = fmt.Sprintf("Offline message received%v", handlers.IntegrationActivitySuffix(DisplayName))
attachment := cc.NewAttachment()
if len(src.URL) > 0 {
attachment.AddField(cc.Field{
Title: "Message",
Value: fmt.Sprintf("[%s](%v)", src.Message, src.URL)})
}
if len(src.ClientName) > 0 {
attachment.AddField(cc.Field{
Title: "Client Name",
Value: src.ClientName})
}
if len(attachment.Fields) > 0 {
ccMsg.AddAttachment(attachment)
}
return ccMsg
}
func NormalizeChatMeta(cfg config.Configuration, src UserlikeChatMetaStartOutMessage) cc.Message {
ccMsg := cc.NewMessage()
iconURL, err := cfg.GetAppIconURL(HandlerKey)
if err == nil {
ccMsg.IconURL = iconURL.String()
}
ccMsg.Activity = fmt.Sprintf("%s%s",
GlipActivityForChat(src.Event, src.FeedbackMessage), handlers.IntegrationActivitySuffix(DisplayName))
attachment := cc.NewAttachment()
displayedURL := false
if src.Event == "rating" || src.Event == "survey" { // includes feedback
if len(src.FeedbackMessage) > 0 {
url, linked := LinkifyURL(src.FeedbackMessage, src.URL, displayedURL)
displayedURL = linked
attachment.AddField(cc.Field{
Title: "Feedback",
Value: url,
Short: false})
}
if len(src.PostSurveyOption) > 0 {
url, linked := LinkifyURL(src.PostSurveyOption, src.URL, displayedURL)
displayedURL = linked
attachment.AddField(cc.Field{
Title: "Rating",
Value: url,
Short: true})
}
}
if len(src.ClientName) > 0 {
url, _ := LinkifyURL(src.ClientName, src.URL, displayedURL)
attachment.AddField(cc.Field{
Title: "Client Name",
Value: url,
Short: true})
} else {
url, _ := LinkifyURL("Unknown", src.URL, displayedURL)
attachment.AddField(cc.Field{
Title: "Client Name",
Value: url,
Short: true})
}
if len(attachment.Fields) > 0 {
ccMsg.AddAttachment(attachment)
}
return ccMsg
}
func LinkifyURL(innerHTML string, url string, skipLinking bool) (string, bool) {
if len(innerHTML) == 0 && len(url) > 0 {
innerHTML = url
}
if skipLinking {
return innerHTML, skipLinking
}
if len(url) < 1 {
return innerHTML, false
}
if len(innerHTML) > 0 {
return fmt.Sprintf("[%s](%s)", innerHTML, url), true
}
return fmt.Sprintf("[%s](%s)", url, url), true
}
func NormalizeChatWidget(cfg config.Configuration, src UserlikeChatWidgetOutMessage) cc.Message {
ccMsg := cc.NewMessage()
iconURL, err := cfg.GetAppIconURL(HandlerKey)
if err == nil {
ccMsg.IconURL = iconURL.String()
}
ccMsg.Activity = fmt.Sprintf("Chat widget configuration updated%s", handlers.IntegrationActivitySuffix(DisplayName))
titleParts := []string{}
if len(src.StatusURL) > 0 {
titleParts = append(titleParts, fmt.Sprintf("[Check status](%s)", src.StatusURL))
}
if len(src.TestURL) > 0 {
titleParts = append(titleParts, fmt.Sprintf("[test widget](%s)", src.TestURL))
}
if len(titleParts) > 0 {
ccMsg.Title = strings.Join(titleParts, " and ")
}
attachment := cc.NewAttachment()
if len(src.Name) > 0 {
attachment.AddField(cc.Field{
Title: "Widget Name",
Value: fmt.Sprintf("[%s](%s)", src.Name, src.CustomURL),
Short: true})
}
attachment.AddField(cc.Field{
Title: "Widget Version",
Value: fmt.Sprintf("%v", src.WidgetVersion),
Short: true})
if len(src.WidgetExternalType) > 0 {
attachment.AddField(cc.Field{
Title: "Widget Type",
Value: fmt.Sprintf("%v", src.WidgetExternalType),
Short: true})
}
ccMsg.AddAttachment(attachment)
return ccMsg
}
func NormalizeOperator(cfg config.Configuration, src UserlikeOperatorOutMessage) cc.Message {
ccMsg := cc.NewMessage()
iconURL, err := cfg.GetAppIconURL(HandlerKey)
if err == nil {
ccMsg.IconURL = iconURL.String()
}
ccMsg.Activity = fmt.Sprintf("Operator is %s%s",
src.Event, handlers.IntegrationActivitySuffix(DisplayName))
attachment := cc.NewAttachment()
if len(src.DashboardURL) > 0 {
attachment.AddField(cc.Field{
Title: "Operator",
Value: fmt.Sprintf("[%s](%s)", src.Name, src.DashboardURL)})
}
ccMsg.AddAttachment(attachment)
return ccMsg
}