-
Notifications
You must be signed in to change notification settings - Fork 20
/
msg.go
136 lines (113 loc) · 3.83 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
package triggers
import (
"encoding/json"
"github.com/nyaruka/goflow/assets"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/flows/events"
"github.com/nyaruka/goflow/flows/inputs"
"github.com/nyaruka/goflow/utils"
)
func init() {
RegisterType(TypeMsg, readMsgTrigger)
}
// TypeMsg is the type for message triggered sessions
const TypeMsg string = "msg"
// MsgTrigger is used when a session was triggered by a message being recieved by the caller
//
// {
// "type": "msg",
// "flow": {"uuid": "50c3706e-fedb-42c0-8eab-dda3335714b7", "name": "Registration"},
// "contact": {
// "uuid": "9f7ede93-4b16-4692-80ad-b7dc54a1cd81",
// "name": "Bob",
// "created_on": "2018-01-01T12:00:00.000000Z"
// },
// "msg": {
// "uuid": "2d611e17-fb22-457f-b802-b8f7ec5cda5b",
// "channel": {"uuid": "61602f3e-f603-4c70-8a8f-c477505bf4bf", "name": "Twilio"},
// "urn": "tel:+12065551212",
// "text": "hi there",
// "attachments": ["https://s3.amazon.com/mybucket/attachment.jpg"]
// },
// "keyword_match": {
// "type": "first_word",
// "keyword": "start"
// },
// "triggered_on": "2000-01-01T00:00:00.000000000-00:00"
// }
//
// @trigger msg
type MsgTrigger struct {
baseTrigger
msg *flows.MsgIn
match *KeywordMatch
}
// KeywordMatchType describes how the message matched a keyword
type KeywordMatchType string
// the different types of keyword match
const (
KeywordMatchTypeFirstWord KeywordMatchType = "first_word"
KeywordMatchTypeOnlyWord KeywordMatchType = "only_word"
)
// KeywordMatch describes why the message triggered a session
type KeywordMatch struct {
Type KeywordMatchType `json:"type" validate:"required"`
Keyword string `json:"keyword" validate:"required"`
}
// NewKeywordMatch creates a new keyword match
func NewKeywordMatch(typeName KeywordMatchType, keyword string) *KeywordMatch {
return &KeywordMatch{Type: typeName, Keyword: keyword}
}
// NewMsgTrigger creates a new message trigger
func NewMsgTrigger(env utils.Environment, flow *assets.FlowReference, contact *flows.Contact, msg *flows.MsgIn, match *KeywordMatch) flows.Trigger {
return &MsgTrigger{
baseTrigger: newBaseTrigger(TypeMsg, env, flow, contact, nil, nil),
msg: msg,
match: match,
}
}
// InitializeRun performs additional initialization when we visit our first node
func (t *MsgTrigger) InitializeRun(run flows.FlowRun, logEvent flows.EventCallback) error {
// update our input
input, err := inputs.NewMsgInput(run.Session().Assets(), t.msg, t.triggeredOn)
if err != nil {
return err
}
run.Session().SetInput(input)
logEvent(events.NewMsgReceivedEvent(t.msg))
return t.baseTrigger.InitializeRun(run, logEvent)
}
var _ flows.Trigger = (*MsgTrigger)(nil)
//------------------------------------------------------------------------------------------
// JSON Encoding / Decoding
//------------------------------------------------------------------------------------------
type msgTriggerEnvelope struct {
baseTriggerEnvelope
Msg *flows.MsgIn `json:"msg" validate:"required,dive"`
Match *KeywordMatch `json:"keyword_match,omitempty" validate:"omitempty,dive"`
}
func readMsgTrigger(sessionAssets flows.SessionAssets, data json.RawMessage, missing assets.MissingCallback) (flows.Trigger, error) {
e := &msgTriggerEnvelope{}
if err := utils.UnmarshalAndValidate(data, e); err != nil {
return nil, err
}
t := &MsgTrigger{
msg: e.Msg,
match: e.Match,
}
if err := t.unmarshal(sessionAssets, &e.baseTriggerEnvelope, missing); err != nil {
return nil, err
}
return t, nil
}
// MarshalJSON marshals this trigger into JSON
func (t *MsgTrigger) MarshalJSON() ([]byte, error) {
e := &msgTriggerEnvelope{
Msg: t.msg,
Match: t.match,
}
if err := t.marshal(&e.baseTriggerEnvelope); err != nil {
return nil, err
}
return json.Marshal(e)
}