forked from nyaruka/goflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bindings.go
325 lines (272 loc) · 8.05 KB
/
bindings.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package mobile
// To build an Android Archive:
//
// go get golang.org/x/mobile/cmd/gomobile
// gomobile bind -target android -javapkg=com.nyaruka.goflow -o mobile/goflow.aar github.com/nyaruka/goflow/mobile
import (
"encoding/json"
"time"
"github.com/nyaruka/gocommon/jsonx"
"github.com/nyaruka/gocommon/urns"
"github.com/nyaruka/goflow/assets"
"github.com/nyaruka/goflow/assets/static"
"github.com/nyaruka/goflow/envs"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/flows/definition"
"github.com/nyaruka/goflow/flows/definition/migrations"
"github.com/nyaruka/goflow/flows/engine"
"github.com/nyaruka/goflow/flows/resumes"
"github.com/nyaruka/goflow/flows/triggers"
"github.com/nyaruka/goflow/utils"
"github.com/Masterminds/semver"
)
// CurrentSpecVersion returns the current flow spec version
func CurrentSpecVersion() string {
return definition.CurrentSpecVersion.String()
}
// IsVersionSupported returns whether the given spec version is supported
func IsVersionSupported(version string) bool {
v, err := semver.NewVersion(version)
if err != nil {
return false
}
return definition.IsVersionSupported(v)
}
// Environment defines the environment for expression evaluation etc
type Environment struct {
target envs.Environment
}
// NewEnvironment creates a new environment.
func NewEnvironment(dateFormat string, timeFormat string, timezone string, allowedLanguages *StringSlice, defaultCountry string, redactionPolicy string) (*Environment, error) {
tz, err := time.LoadLocation(timezone)
if err != nil {
return nil, err
}
langs := make([]envs.Language, allowedLanguages.Length())
for i := 0; i < allowedLanguages.Length(); i++ {
langs[i] = envs.Language(allowedLanguages.Get(i))
}
return &Environment{
target: envs.NewBuilder().
WithDateFormat(envs.DateFormat(dateFormat)).
WithTimeFormat(envs.TimeFormat(timeFormat)).
WithTimezone(tz).
WithAllowedLanguages(langs).
WithDefaultCountry(envs.Country(defaultCountry)).
WithRedactionPolicy(envs.RedactionPolicy(redactionPolicy)).
Build(),
}, nil
}
// AssetsSource is a static asset source
type AssetsSource struct {
target *static.StaticSource
}
// NewAssetsSource creates a new static asset source
func NewAssetsSource(src string) (*AssetsSource, error) {
s, err := static.NewSource(json.RawMessage(src))
if err != nil {
return nil, err
}
return &AssetsSource{target: s}, nil
}
// SessionAssets provides optimized access to assets
type SessionAssets struct {
target flows.SessionAssets
}
// NewSessionAssets creates a new session assets
func NewSessionAssets(environment *Environment, source *AssetsSource) (*SessionAssets, error) {
s, err := engine.NewSessionAssets(environment.target, source.target, &migrations.Config{BaseMediaURL: ""})
if err != nil {
return nil, err
}
return &SessionAssets{target: s}, nil
}
// Contact represents a person who is interacting with a flow
type Contact struct {
target *flows.Contact
}
// NewEmptyContact creates a new contact
func NewEmptyContact(sa *SessionAssets) *Contact {
return &Contact{
target: flows.NewEmptyContact(sa.target, "", envs.NilLanguage, nil),
}
}
// MsgIn is an incoming message
type MsgIn struct {
target *flows.MsgIn
}
// NewMsgIn creates a new incoming message
func NewMsgIn(uuid string, text string, attachments *StringSlice) *MsgIn {
var convertedAttachments []utils.Attachment
if attachments != nil {
convertedAttachments = make([]utils.Attachment, attachments.Length())
for i := 0; i < attachments.Length(); i++ {
convertedAttachments[i] = utils.Attachment(attachments.Get(i))
}
}
return &MsgIn{
target: flows.NewMsgIn(flows.MsgUUID(uuid), urns.NilURN, nil, text, convertedAttachments),
}
}
func (m *MsgIn) Text() string {
return m.target.Text()
}
func (m *MsgIn) Attachments() *StringSlice {
attachments := NewStringSlice(len(m.target.Attachments()))
for _, attachment := range m.target.Attachments() {
attachments.Add(string(attachment))
}
return attachments
}
// FlowReference is a reference to a flow
type FlowReference struct {
uuid string
name string
}
// NewFlowReference creates a new flow reference
func NewFlowReference(uuid string, name string) *FlowReference {
return &FlowReference{uuid: uuid, name: name}
}
// Trigger represents something which can initiate a session
type Trigger struct {
target flows.Trigger
}
// NewManualTrigger creates a new manual trigger
func NewManualTrigger(environment *Environment, contact *Contact, flow *FlowReference) *Trigger {
flowRef := assets.NewFlowReference(assets.FlowUUID(flow.uuid), flow.name)
return &Trigger{
target: triggers.NewBuilder(environment.target, flowRef, contact.target).Manual().Build(),
}
}
// Resume represents something which can resume a session
type Resume struct {
target flows.Resume
}
// NewMsgResume creates a new message resume
func NewMsgResume(environment *Environment, contact *Contact, msg *MsgIn) *Resume {
var e envs.Environment
if environment != nil {
e = environment.target
}
var c *flows.Contact
if contact != nil {
c = contact.target
}
return &Resume{
target: resumes.NewMsg(e, c, msg.target),
}
}
type Event struct {
type_ string
payload string
}
func (e *Event) Type() string {
return e.type_
}
func (e *Event) Payload() string {
return e.payload
}
type Modifier struct {
type_ string
payload string
}
func (m *Modifier) Type() string {
return m.type_
}
func (m *Modifier) Payload() string {
return m.payload
}
// Sprint is an interaction with the engine - i.e. a start or resume of a session
type Sprint struct {
target flows.Sprint
}
// Modifiers returns the modifiers created during this sprint
func (s *Sprint) Modifiers() *ModifierSlice {
mods := NewModifierSlice(len(s.target.Modifiers()))
for _, mod := range s.target.Modifiers() {
marshaled, _ := jsonx.Marshal(mod)
mods.Add(&Modifier{type_: mod.Type(), payload: string(marshaled)})
}
return mods
}
// Events returns the events created during this sprint
func (s *Sprint) Events() *EventSlice {
events := NewEventSlice(len(s.target.Events()))
for _, event := range s.target.Events() {
marshaled, _ := jsonx.Marshal(event)
events.Add(&Event{type_: event.Type(), payload: string(marshaled)})
}
return events
}
// Session represents a session with the flow engine
type Session struct {
target flows.Session
}
// Status returns the status of this session
func (s *Session) Status() string {
return string(s.target.Status())
}
// Assets returns the assets associated with this session
func (s *Session) Assets() *SessionAssets {
return &SessionAssets{target: s.target.Assets()}
}
// Resume resumes this session
func (s *Session) Resume(resume *Resume) (*Sprint, error) {
sprint, err := s.target.Resume(resume.target)
if err != nil {
return nil, err
}
return &Sprint{target: sprint}, nil
}
// ToJSON serializes this session as JSON
func (s *Session) ToJSON() (string, error) {
data, err := jsonx.Marshal(s.target)
if err != nil {
return "", err
}
return string(data), nil
}
type Hint struct {
target flows.Hint
}
func (h *Hint) Type() string {
return string(h.target.Type())
}
type Engine struct {
target flows.Engine
}
func NewEngine() *Engine {
return &Engine{
target: engine.NewBuilder().Build(),
}
}
// NewSession creates a new session
func (e *Engine) NewSession(sa *SessionAssets, trigger *Trigger) (*SessionAndSprint, error) {
session, sprint, err := e.target.NewSession(sa.target, trigger.target)
if err != nil {
return nil, err
}
return &SessionAndSprint{
session: &Session{target: session},
sprint: &Sprint{target: sprint},
}, nil
}
// ReadSession reads an existing session from JSON
func (e *Engine) ReadSession(a *SessionAssets, data string) (*Session, error) {
s, err := e.target.ReadSession(a.target, []byte(data), assets.IgnoreMissing)
if err != nil {
return nil, err
}
return &Session{target: s}, nil
}
// SessionAndSprint holds a session and a sprint.. because a Java method can't return two values
type SessionAndSprint struct {
session *Session
sprint *Sprint
}
func (ss *SessionAndSprint) Session() *Session {
return ss.session
}
func (ss *SessionAndSprint) Sprint() *Sprint {
return ss.sprint
}