-
Notifications
You must be signed in to change notification settings - Fork 20
/
msg_wait.go
90 lines (73 loc) · 2.47 KB
/
msg_wait.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
package events
import (
"encoding/json"
"time"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/flows/routers/waits/hints"
"github.com/nyaruka/goflow/utils"
"github.com/pkg/errors"
)
func init() {
registerType(TypeMsgWait, func() flows.Event { return &MsgWaitEvent{} })
}
// TypeMsgWait is the type of our msg wait event
const TypeMsgWait string = "msg_wait"
// MsgWaitEvent events are created when a flow pauses waiting for a response from
// a contact. If a timeout is set, then the caller should resume the flow after
// the number of seconds in the timeout to resume it.
//
// {
// "type": "msg_wait",
// "created_on": "2022-01-03T13:27:30Z",
// "timeout_seconds": 300,
// "expires_on": "2022-02-02T13:27:30Z",
// "hint": {
// "type": "image"
// }
// }
//
// @event msg_wait
type MsgWaitEvent struct {
BaseEvent
// when this wait times out and we can proceed assuming router has a timeout category. This value is relative
// because we want it to start counting when the last message is actually sent, which the engine can't know.
TimeoutSeconds *int `json:"timeout_seconds,omitempty"`
// When this wait expires and the whole run can be expired
ExpiresOn *time.Time `json:"expires_on,omitempty"`
Hint flows.Hint `json:"hint,omitempty"`
}
// NewMsgWait returns a new msg wait with the passed in timeout
func NewMsgWait(timeoutSeconds *int, expiresOn *time.Time, hint flows.Hint) *MsgWaitEvent {
return &MsgWaitEvent{
BaseEvent: NewBaseEvent(TypeMsgWait),
TimeoutSeconds: timeoutSeconds,
ExpiresOn: expiresOn,
Hint: hint,
}
}
//------------------------------------------------------------------------------------------
// JSON Encoding / Decoding
//------------------------------------------------------------------------------------------
type msgWaitEnvelope struct {
BaseEvent
TimeoutSeconds *int `json:"timeout_seconds,omitempty"`
ExpiresOn *time.Time `json:"expires_on,omitempty"`
Hint json.RawMessage `json:"hint,omitempty"`
}
// UnmarshalJSON unmarshals this event from the given JSON
func (e *MsgWaitEvent) UnmarshalJSON(data []byte) error {
v := &msgWaitEnvelope{}
if err := utils.UnmarshalAndValidate(data, v); err != nil {
return err
}
e.BaseEvent = v.BaseEvent
e.TimeoutSeconds = v.TimeoutSeconds
e.ExpiresOn = v.ExpiresOn
var err error
if v.Hint != nil {
if e.Hint, err = hints.ReadHint(v.Hint); err != nil {
return errors.Wrap(err, "unable to read hint")
}
}
return nil
}