-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.go
257 lines (223 loc) · 5.42 KB
/
message.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
package gol
import (
"time"
"github.com/go-ozzo/ozzo-validation"
"github.com/pkg/errors"
)
// Types and targets
const (
System Type = "system"
Communication Type = "communication"
Audit Type = "audit"
Dashbot Target = "dashbot"
Chatbase Target = "chatbase"
Logstash Target = "logstash"
Sentry Target = "sentry"
File Target = "file"
Fallback Target = "fallback"
Incoming Source = "incoming"
Outgoing Source = "outgoing"
Facebook Platform = "Facebook"
SMS Platform = "SMS"
Web Platform = "Web"
Android Platform = "Android"
IOS Platform = "iOS"
Actions Platform = "Actions"
Alexa Platform = "Alexa"
Cortana Platform = "Cortana"
Kik Platform = "Kik"
Skype Platform = "Skype"
Twitter Platform = "Twitter"
Viber Platform = "Viber"
Telegram Platform = "Telegram"
Slack Platform = "Slack"
WhatsApp Platform = "WhatsApp"
WeChat Platform = "WeChat"
Line Platform = "Line"
Kakao Platform = "Kakao"
RBM Platform = "RBM"
)
var (
// Types contains the types of message.
Types = []interface{}{
System,
Communication,
Audit,
}
// Targets contains the targets of message.
Targets = []interface{}{
Dashbot,
Chatbase,
Logstash,
Sentry,
File,
Fallback,
}
// Targets contains the targets of message.
targetsNorm = []string{
string(Dashbot),
string(Chatbase),
string(Logstash),
string(Sentry),
string(File),
string(Fallback),
}
// Sources contains the sources of message.
Sources = []interface{}{
Incoming,
Outgoing,
}
)
// Type represents the message's type.
type Type string
// Target represents the message's target.
type Target string
// Source represents the message's source (incoming & outgoing)
type Source string
// Platform represents the message's platform.
type Platform string
// Message represents the log's message structure.
type Message struct {
Type Type `json:"type"`
Source *Source `json:"source"`
Platform *Platform `json:"platform"`
Targets []Target `json:"targets"`
RecipientID *string `json:"recipient_id"`
SenderID *string `json:"sender_id"`
AccessToken *string `json:"access_token"`
SessionID *string `json:"session_id"`
MessageID *string `json:"message_id"`
SentTime time.Time `json:"sent_time"`
Data interface{} `json:"data"`
Intent *Intent `json:"intent"`
NotHandled bool `json:"not_handled"`
Version *string `json:"version"`
}
func (m *Message) InTarget(target Target) bool {
if len(m.Targets) == 0 {
return false
}
for _, t := range m.Targets {
if t == target {
return true
}
}
return false
}
// Validate validates the message.
func (m Message) Validate() error {
return validation.ValidateStruct(
&m,
validation.Field(
&m.Type,
validation.Required,
validation.In(Types...),
),
validation.Field(
&m.Source,
validation.In(Sources...),
validation.By(func(value interface{}) error {
s, _ := value.(Source)
t := m.Type
if t == Communication {
if s != Incoming || s != Outgoing {
return errors.New("the source is required for the communication type")
}
if m.Platform == nil {
return errors.New("the platform is required for the communication type")
}
}
return nil
}),
),
validation.Field(
&m.Targets,
validation.Required,
validation.By(func(value interface{}) error {
for _, t := range m.Targets {
if !in(targetsNorm, string(t)) {
return errors.New("must be a valid value")
}
}
return nil
}),
validation.By(func(value interface{}) error {
var err error
t := m.Type
err = errors.New("the target is not valid for: " + string(t) + " type")
switch t {
case System:
if !m.InTarget(File) && !m.InTarget(Sentry) && !m.InTarget(Logstash) {
return err
}
case Communication:
if !m.InTarget(Dashbot) && !m.InTarget(Chatbase) && !m.InTarget(Logstash) {
return err
}
if m.RecipientID == nil {
return errors.New("the recipient id is required")
}
if m.SenderID == nil {
return errors.New("the sender id is required")
}
if m.AccessToken == nil {
return errors.New("the access token is required")
}
if m.SessionID == nil {
return errors.New("the session id is required")
}
if m.MessageID == nil {
return errors.New("the message id is required")
}
case Audit:
if !m.InTarget(Logstash) {
return err
}
}
return nil
}),
),
validation.Field(
&m.Data,
validation.Required,
),
validation.Field(
&m.SentTime,
validation.Required,
),
)
}
// Intent represents the intent structure.
type Intent struct {
Name string `json:"name"`
Inputs []Input `json:"inputs"`
}
// Validate validates the intent struct.
func (i Intent) Validate() error {
return validation.ValidateStruct(
&i,
validation.Field(&i.Name, validation.Required),
validation.Field(&i.Inputs),
)
}
// Input represents the input of intent.
type Input struct {
Name string `json:"name"`
Value string `json:"value"`
}
// Validate validates the input struct.
func (i Input) Validate() error {
return validation.ValidateStruct(
&i,
validation.Field(&i.Name, validation.Required),
validation.Field(&i.Value, validation.Required),
)
}
func in(array []string, value string) bool {
for _, v := range array {
if v == value {
return true
}
}
return false
}