-
Notifications
You must be signed in to change notification settings - Fork 1
/
openmessaging_message_structured.go
92 lines (82 loc) · 2.77 KB
/
openmessaging_message_structured.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
package gcloudcx
import (
"encoding/json"
"github.com/gildas/go-core"
"github.com/gildas/go-errors"
"github.com/gildas/go-logger"
)
// OpenMessageStructed describes a structured message
//
// See: https://developer.genesys.cloud/api/rest/v2/conversations/#post-api-v2-conversations-messages-inbound-open
type OpenMessageStructured struct {
ID string `json:"id,omitempty"` // Can be anything
Channel OpenMessageChannel `json:"channel"`
Direction string `json:"direction"` // inbound or outbound
Text string `json:"text"`
Content []OpenMessageContent `json:"content,omitempty"`
OriginatingEntity string `json:"originatingEntity,omitempty"` // Bot or Human
Metadata map[string]string `json:"metadata,omitempty"`
KeysToRedact []string `json:"-"`
}
// init initializes this type
func init() {
openMessageRegistry.Add(OpenMessageStructured{})
}
// GetType tells the type of this OpenMessage
//
// implements core.TypeCarrier
func (message OpenMessageStructured) GetType() string {
return "Structured"
}
// GetID gets the identifier of this
//
// implements OpenMessage
func (message OpenMessageStructured) GetID() string {
return message.ID
}
// Redact redacts sensitive data
//
// implements logger.Redactable
func (message OpenMessageStructured) Redact() interface{} {
redacted := message
redacted.Channel = message.Channel.Redact().(OpenMessageChannel)
if core.GetEnvAsBool("REDACT_MESSAGE_TEXT", true) && len(message.Text) > 0 {
redacted.Text = logger.RedactWithHash(message.Text)
}
for _, key := range message.KeysToRedact {
if value, found := redacted.Metadata[key]; found {
redacted.Metadata[key] = logger.RedactWithHash(value)
}
}
return redacted
}
// MarshalJSON marshals this into JSON
func (message OpenMessageStructured) MarshalJSON() ([]byte, error) {
type surrogate OpenMessageStructured
data, err := json.Marshal(struct {
surrogate
Type string `json:"type"`
}{
surrogate: surrogate(message),
Type: message.GetType(),
})
return data, errors.JSONMarshalError.Wrap(err)
}
// UnmarshalJSON unmarshals JSON into this
func (message *OpenMessageStructured) UnmarshalJSON(data []byte) (err error) {
type surrogate OpenMessageStructured
var inner struct {
surrogate
Type string `json:"type"`
KeysToRedact []string `json:"keysToRedact"`
}
if err = json.Unmarshal(data, &inner); err != nil {
return errors.JSONUnmarshalError.Wrap(err)
}
if inner.Type != (OpenMessageStructured{}).GetType() {
return errors.JSONUnmarshalError.Wrap(errors.InvalidType.With(inner.Type))
}
*message = OpenMessageStructured(inner.surrogate)
message.KeysToRedact = append(message.KeysToRedact, inner.KeysToRedact...)
return
}