-
Notifications
You must be signed in to change notification settings - Fork 20
/
msg.go
118 lines (94 loc) · 3.99 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
package flows
import (
"github.com/nyaruka/gocommon/urns"
"github.com/nyaruka/goflow/assets"
"github.com/nyaruka/goflow/utils"
)
// 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"`
}
// 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) *MsgOut {
return &MsgOut{
BaseMsg: BaseMsg{
UUID_: MsgUUID(utils.NewUUID()),
URN_: urn,
Channel_: channel,
Text_: text,
Attachments_: attachments,
},
QuickReplies_: quickReplies,
Templating_: templating,
}
}
// 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_ }
// 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_ }
// MsgTemplating represents any substituted message template that should be applied when sending this message
type MsgTemplating struct {
Template_ *assets.TemplateReference `json:"template"`
Language_ utils.Language `json:"language"`
Variables_ []string `json:"variables"`
}
// 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() utils.Language { return t.Language_ }
// Variables returns the variables that should be substituted in the template
func (t MsgTemplating) Variables() []string { return t.Variables_ }
// NewMsgTemplating creates and returns a new msg template
func NewMsgTemplating(template *assets.TemplateReference, language utils.Language, variables []string) *MsgTemplating {
return &MsgTemplating{
Template_: template,
Language_: language,
Variables_: variables,
}
}