-
Notifications
You must be signed in to change notification settings - Fork 20
/
msg.go
183 lines (139 loc) · 4.55 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 waits
import (
"encoding/json"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/flows/events"
"github.com/nyaruka/goflow/flows/resumes"
"github.com/nyaruka/goflow/flows/routers/waits/hints"
"github.com/nyaruka/goflow/flows/triggers"
"github.com/nyaruka/goflow/utils"
"github.com/nyaruka/goflow/utils/jsonx"
"github.com/pkg/errors"
)
func init() {
registerType(TypeMsg, readMsgWait, readActivatedMsgWait)
}
// TypeMsg is the type of our message wait
const TypeMsg string = "msg"
// MsgWait is a wait which waits for an incoming message (i.e. a msg_received event)
type MsgWait struct {
baseWait
// Message waits can indicate to the caller what kind of message the flow is expecting. In the case of flows of type
// messaging_offline, this should be considered a requirement and the client should only reply with a message containing
// an attachment of that type. In the case of other flow types this should be considered only a hint to the channel,
// which may or may not support prompting the contact for media of that type.
hint flows.Hint
}
// NewMsgWait creates a new message wait
func NewMsgWait(timeout *Timeout, hint flows.Hint) *MsgWait {
return &MsgWait{
baseWait: newBaseWait(TypeMsg, timeout),
hint: hint,
}
}
// Hint returns the hint (optional)
func (w *MsgWait) Hint() flows.Hint { return w.hint }
// Begin beings waiting at this wait
func (w *MsgWait) Begin(run flows.FlowRun, log flows.EventCallback) flows.ActivatedWait {
var timeoutSeconds *int
if w.timeout != nil {
seconds := w.timeout.Seconds()
timeoutSeconds = &seconds
}
// if we have a msg trigger and we're the first thing to happen... then we skip ourselves
triggerHasMsg := run.Session().Trigger().Type() == triggers.TypeMsg
if triggerHasMsg && len(run.Session().Runs()) == 1 && len(run.Path()) == 1 {
return nil
}
log(events.NewMsgWait(timeoutSeconds, w.hint))
return NewActivatedMsgWait(timeoutSeconds, w.hint)
}
// End ends this wait or returns an error
func (w *MsgWait) End(resume flows.Resume) error {
// if we have a message we can definitely resume
if resume.Type() == resumes.TypeMsg {
return nil
}
return w.baseWait.End(resume)
}
var _ flows.Wait = (*MsgWait)(nil)
type ActivatedMsgWait struct {
baseActivatedWait
hint flows.Hint
}
func NewActivatedMsgWait(timeoutSeconds *int, hint flows.Hint) *ActivatedMsgWait {
return &ActivatedMsgWait{
baseActivatedWait: baseActivatedWait{type_: TypeMsg, timeoutSeconds: timeoutSeconds},
hint: hint,
}
}
// Hint returns the hint (optional)
func (w *ActivatedMsgWait) Hint() flows.Hint { return w.hint }
var _ flows.ActivatedWait = (*ActivatedMsgWait)(nil)
//------------------------------------------------------------------------------------------
// JSON Encoding / Decoding
//------------------------------------------------------------------------------------------
type msgWaitEnvelope struct {
baseWaitEnvelope
Hint json.RawMessage `json:"hint,omitempty"`
}
func readMsgWait(data json.RawMessage) (flows.Wait, error) {
e := &msgWaitEnvelope{}
if err := utils.UnmarshalAndValidate(data, e); err != nil {
return nil, err
}
w := &MsgWait{}
var err error
if e.Hint != nil {
if w.hint, err = hints.ReadHint(e.Hint); err != nil {
return nil, errors.Wrap(err, "unable to read hint")
}
}
return w, w.unmarshal(&e.baseWaitEnvelope)
}
// MarshalJSON marshals this wait into JSON
func (w *MsgWait) MarshalJSON() ([]byte, error) {
e := &msgWaitEnvelope{}
if err := w.marshal(&e.baseWaitEnvelope); err != nil {
return nil, err
}
var err error
if w.hint != nil {
if e.Hint, err = jsonx.Marshal(w.hint); err != nil {
return nil, err
}
}
return jsonx.Marshal(e)
}
type activatedMsgWaitEnvelope struct {
baseActivatedWaitEnvelope
Hint json.RawMessage `json:"hint,omitempty"`
}
func readActivatedMsgWait(data json.RawMessage) (flows.ActivatedWait, error) {
e := &activatedMsgWaitEnvelope{}
if err := utils.UnmarshalAndValidate(data, e); err != nil {
return nil, err
}
w := &ActivatedMsgWait{}
var err error
if e.Hint != nil {
if w.hint, err = hints.ReadHint(e.Hint); err != nil {
return nil, errors.Wrap(err, "unable to read hint")
}
}
return w, w.unmarshal(&e.baseActivatedWaitEnvelope)
}
// MarshalJSON marshals this wait into JSON
func (w *ActivatedMsgWait) MarshalJSON() ([]byte, error) {
e := &activatedMsgWaitEnvelope{}
if err := w.marshal(&e.baseActivatedWaitEnvelope); err != nil {
return nil, err
}
var err error
if w.hint != nil {
if e.Hint, err = jsonx.Marshal(w.hint); err != nil {
return nil, err
}
}
return jsonx.Marshal(e)
}