-
Notifications
You must be signed in to change notification settings - Fork 20
/
msg.go
183 lines (148 loc) · 6.04 KB
/
msg.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
package flows
import (
"fmt"
"github.com/nyaruka/gocommon/urns"
"github.com/nyaruka/gocommon/uuids"
"github.com/nyaruka/goflow/assets"
"github.com/nyaruka/goflow/envs"
"github.com/nyaruka/goflow/utils"
validator "gopkg.in/go-playground/validator.v9"
)
func init() {
utils.RegisterValidatorAlias("msg_topic", "eq=event|eq=account|eq=purchase|eq=agent", func(validator.FieldError) string {
return "is not a valid message topic"
})
}
// MsgTopic is the topic, as required by some channel types
type MsgTopic string
// possible msg topic values
const (
NilMsgTopic MsgTopic = ""
MsgTopicEvent MsgTopic = "event"
MsgTopicAccount MsgTopic = "account"
MsgTopicPurchase MsgTopic = "purchase"
MsgTopicAgent MsgTopic = "agent"
)
// BaseMsg represents a incoming or outgoing message with the session contact
type BaseMsg struct {
UUID_ MsgUUID `json:"uuid"`
ID_ MsgID `json:"id,omitempty"`
URN_ urns.URN `json:"urn,omitempty" validate:"omitempty,urn"`
Channel_ *assets.ChannelReference `json:"channel,omitempty"`
Text_ string `json:"text"`
Attachments_ []utils.Attachment `json:"attachments,omitempty"`
}
// MsgIn represents a incoming message from the session contact
type MsgIn struct {
BaseMsg
ExternalID_ string `json:"external_id,omitempty"`
}
// MsgOut represents a outgoing message to the session contact
type MsgOut struct {
BaseMsg
QuickReplies_ []string `json:"quick_replies,omitempty"`
Templating_ *MsgTemplating `json:"templating,omitempty"`
Topic_ MsgTopic `json:"topic,omitempty"`
TextLanguage envs.Language `json:"text_language,omitempty"`
}
// NewMsgIn creates a new incoming message
func NewMsgIn(uuid MsgUUID, urn urns.URN, channel *assets.ChannelReference, text string, attachments []utils.Attachment) *MsgIn {
return &MsgIn{
BaseMsg: BaseMsg{
UUID_: uuid,
URN_: urn,
Channel_: channel,
Text_: text,
Attachments_: attachments,
},
}
}
// NewMsgOut creates a new outgoing message
func NewMsgOut(urn urns.URN, channel *assets.ChannelReference, text string, attachments []utils.Attachment, quickReplies []string, templating *MsgTemplating, topic MsgTopic) *MsgOut {
return &MsgOut{
BaseMsg: BaseMsg{
UUID_: MsgUUID(uuids.New()),
URN_: urn,
Channel_: channel,
Text_: text,
Attachments_: attachments,
},
QuickReplies_: quickReplies,
Templating_: templating,
Topic_: topic,
}
}
// NewIVRMsgOut creates a new outgoing message for IVR
func NewIVRMsgOut(urn urns.URN, channel *assets.ChannelReference, text string, textLanguage envs.Language, audioURL string) *MsgOut {
var attachments []utils.Attachment
if audioURL != "" {
attachments = []utils.Attachment{utils.Attachment(fmt.Sprintf("audio:%s", audioURL))}
}
return &MsgOut{
BaseMsg: BaseMsg{
UUID_: MsgUUID(uuids.New()),
URN_: urn,
Channel_: channel,
Text_: text,
Attachments_: attachments,
},
QuickReplies_: nil,
Templating_: nil,
Topic_: NilMsgTopic,
TextLanguage: textLanguage,
}
}
// UUID returns the UUID of this message
func (m *BaseMsg) UUID() MsgUUID { return m.UUID_ }
// ID returns the internal ID of this message
func (m *BaseMsg) ID() MsgID { return m.ID_ }
// SetID sets the internal ID of this message
func (m *BaseMsg) SetID(id MsgID) { m.ID_ = id }
// URN returns the URN of this message
func (m *BaseMsg) URN() urns.URN { return m.URN_ }
// SetURN returns the URN of this message
func (m *BaseMsg) SetURN(urn urns.URN) { m.URN_ = urn }
// Channel returns the channel of this message
func (m *BaseMsg) Channel() *assets.ChannelReference { return m.Channel_ }
// Text returns the text of this message
func (m *BaseMsg) Text() string { return m.Text_ }
// Attachments returns the attachments of this message
func (m *BaseMsg) Attachments() []utils.Attachment { return m.Attachments_ }
// ExternalID returns the optional external ID of this incoming message
func (m *MsgIn) ExternalID() string { return m.ExternalID_ }
// SetExternalID sets the external ID of this message
func (m *MsgIn) SetExternalID(id string) { m.ExternalID_ = id }
// QuickReplies returns the quick replies of this outgoing message
func (m *MsgOut) QuickReplies() []string { return m.QuickReplies_ }
// Templating returns the templating to use to send this message (if any)
func (m *MsgOut) Templating() *MsgTemplating { return m.Templating_ }
// Topic returns the topic to use to send this message (if any)
func (m *MsgOut) Topic() MsgTopic { return m.Topic_ }
// MsgTemplating represents any substituted message template that should be applied when sending this message
type MsgTemplating struct {
Template_ *assets.TemplateReference `json:"template"`
Language_ envs.Language `json:"language"`
Country_ envs.Country `json:"country"`
Variables_ []string `json:"variables,omitempty"`
Namespace_ string `json:"namespace"`
}
// Template returns the template this msg template is for
func (t MsgTemplating) Template() *assets.TemplateReference { return t.Template_ }
// Language returns the language that should be used for the template
func (t MsgTemplating) Language() envs.Language { return t.Language_ }
// Country returns the country that should be used for the template
func (t MsgTemplating) Country() envs.Country { return t.Country_ }
// Variables returns the variables that should be substituted in the template
func (t MsgTemplating) Variables() []string { return t.Variables_ }
// Namespace returns the namespace that should be for the template
func (t MsgTemplating) Namespace() string { return t.Namespace_ }
// NewMsgTemplating creates and returns a new msg template
func NewMsgTemplating(template *assets.TemplateReference, language envs.Language, country envs.Country, variables []string, namespace string) *MsgTemplating {
return &MsgTemplating{
Template_: template,
Language_: language,
Country_: country,
Variables_: variables,
Namespace_: namespace,
}
}