-
Notifications
You must be signed in to change notification settings - Fork 10
/
handler_enchant_out.go
142 lines (126 loc) · 3.72 KB
/
handler_enchant_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
package enchant
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"
"github.com/grokify/gotilla/type/stringsutil"
log "github.com/sirupsen/logrus"
)
const (
DisplayName = "Enchant"
HandlerKey = "enchant"
MessageDirection = "out"
MessageBodyType = models.JSON
)
func NewHandler() handlers.Handler {
return handlers.Handler{MessageBodyType: MessageBodyType, Normalize: Normalize}
}
func Normalize(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 := EnchantOutMessageFromBytes(bytes)
if err != nil {
return ccMsg, err
}
if len(src.ActorName) > 0 {
ccMsg.Activity = src.ActorName
}
attachment := cc.NewAttachment()
if len(src.Model.Subject) > 0 {
attachment.Text = src.Model.Subject
}
if len(src.Model.State) > 0 {
attachment.AddField(cc.Field{
Title: "State",
Value: stringsutil.ToUpperFirst(src.Model.State)})
}
ccMsg.AddAttachment(attachment)
return ccMsg, nil
}
type EnchantOutMessage struct {
Id string `json:"id,omitempty"`
AccountId string `json:"account_id,omitempty"`
AccountURL string `json:"account_url,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
Type string `json:"type,omitempty"`
Data EnchantData `json:"data,omitempty"`
ActorType string `json:"actor_type,omitempty"`
ActorId string `json:"actor_id,omitempty"`
ActorName string `json:"actor_name,omitempty"`
ModelType string `json:"model_type,omitempty"`
ModelId string `json:"model_id,omitempty"`
Model EnchantModel `json:"model,omitempty"`
}
type EnchantData struct {
LabelId string `json:"label_id,omitempty"`
LabelName string `json:"label_name,omitempty"`
LabelColor string `json:"label_color,omitempty"`
}
type EnchantModel struct {
Id string `json:"id,omitempty"`
Number int64 `json:"number,omitempty"`
UserId string `json:"user_id,omitempty"`
State string `json:"state,omitempty"`
Subject string `json:"subject,omitempty"`
LabelIds []string `json:"label_ids,omitempty"`
CustomerId string `json:"customer_id,omitempty"`
Type string `json:"type,omitempty"`
ReplyTo string `json:"reply_to,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
}
func EnchantOutMessageFromBytes(bytes []byte) (EnchantOutMessage, error) {
log.WithFields(log.Fields{
"type": "message.raw",
"message": string(bytes),
}).Debug(fmt.Sprintf("%v message.", DisplayName))
msg := EnchantOutMessage{}
err := json.Unmarshal(bytes, &msg)
if err != nil {
log.WithFields(log.Fields{
"type": "message.json.unmarshal",
"error": fmt.Sprintf("%v\n", err),
}).Warn(fmt.Sprintf("%v request unmarshal failure.", DisplayName))
}
return msg, err
}
/*
{
"id": "7f94629",
"account_id": "a91bb74",
"account_url": "company.enchant.com",
"created_at": "2016-10-17T19:52:43Z",
"type": "ticket.label_added",
"data": {
"label_id": "97b0a40",
"label_name": "High Priority",
"label_color": "red"
},
"actor_type": "user",
"actor_id": "a91bb75",
"actor_name": "Michelle Han",
"model_type": "ticket",
"model_id": "a52ec86",
"model": {
"id": "a52ec86",
"number": 53249,
"user_id": "a91bb75",
"state": "open",
"subject": "email from customer",
"label_ids": [
"97b0a3e",
"97b0a40"
],
"customer_id": "97b0a43",
"type": "email",
"reply_to": "john@smith.com",
"created_at": "2016-10-14T20:15:46Z",
... # truncated
}
}
*/