-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
170 lines (143 loc) · 3.47 KB
/
index.js
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
const _debug = require('debug');
const get = require('lodash/get');
const invariant = require('invariant');
const debugAction = _debug('bottender:proposal:conversation');
// an object map to store actions and their getProps
const actions = Object.create(null);
// default behavior is to attach new key to the props when receiving text events
function defaultGetProps({ key, context, prevProps }) {
return {
...prevProps,
[key]: context.event.text,
};
}
/**
* register the action
*/
function registerAction(name, action) {
if (typeof action === 'function') {
actions[name] = {
action,
getProps: defaultGetProps,
};
} else {
actions[name] = action;
}
}
function getAction(name) {
invariant(
actions[name] && typeof actions[name].action === 'function',
`The ${name} action is not registered.`
);
return function SetCurrentAction(context, props) {
// FIXME: avoid using state to label current action
context.setState({ currentAction: name });
return actions[name].action(context, props);
};
}
/**
* data structure of prompt
*/
function prompt(key) {
return {
isPrompt: true,
key,
};
}
function setProps(context, props) {
context.setState({
bottender: {
lock: {
...context.state.bottender.lock,
props,
},
},
});
}
/**
* set the value of the field
*/
function setField(context, name, value) {
setProps(context, {
...context.state.bottender.lock.props,
[name]: value,
});
}
/**
* delete the value of the field
*/
function deleteField(context, nameOrNames) {
if (Array.isArray(nameOrNames)) {
const names = nameOrNames;
names.forEach((name) => {
setField(context, name, undefined);
});
return;
}
const name = nameOrNames;
setField(context, name, undefined);
}
/**
* runner extension of Bottender
*/
function run(action) {
registerAction('App', action);
return async (context, props) => {
// check if lock exists
const lock = get(context.state, 'bottender.lock');
let entryAction = action;
let entryProps = props;
if (lock) {
// TODO: we should handle more use case here, for example, line datetime payload
const lockAction = getAction(lock.actionName);
if (lockAction) {
const { getProps } = actions[lock.actionName];
entryAction = lockAction;
entryProps = getProps({
key: lock.promptName,
context,
prevProps: lock.props,
});
setProps(context, entryProps);
}
}
debugAction(`Current Action: ${entryAction.name || 'Anonymous'}`);
let next = await entryAction(context, entryProps);
while (typeof next === 'function') {
debugAction(`Current Action: ${next.name || 'Anonymous'}`);
// eslint-disable-next-line no-await-in-loop
next = await next(context, {});
}
if (next && next.isPrompt) {
const returnPrompt = next;
const newLock = {
// FIXME: avoid using state to label current action
actionName: context.state.currentAction,
promptName: returnPrompt.key,
props: lock ? context.state.bottender.lock.props : {},
};
context.setState({
bottender: {
lock: newLock,
},
});
return;
}
if (lock) {
context.setState({
bottender: {
lock: null,
},
});
}
return next;
};
}
module.exports = {
registerAction,
getAction,
prompt,
setField,
deleteField,
run,
};