Skip to content

Commit ba8fcd7

Browse files
authored
[wit] support actions that do not return promises (wit-ai#111)
* [wit] support actions that do not return promises * changes * feedback
1 parent 5b4ceeb commit ba8fcd7

File tree

4 files changed

+26
-26
lines changed

4 files changed

+26
-26
lines changed

CHANGES.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
- support actions that do not return promises
2+
- support the case where an action does not return a Promise
3+
- update uuid to version 3.0.0
4+
- Support older versions of node
5+
- 'Use strict' on interactive.js
6+
- Check for bot's message in messenger example
7+
18
## v4.1.0
29

310
- Support for different JS environments

examples/basic.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,8 @@ const actions = {
2323
send(request, response) {
2424
const {sessionId, context, entities} = request;
2525
const {text, quickreplies} = response;
26-
return new Promise(function(resolve, reject) {
27-
console.log('user said...', request.text);
28-
console.log('sending...', JSON.stringify(response));
29-
return resolve();
30-
});
26+
console.log('user said...', request.text);
27+
console.log('sending...', JSON.stringify(response));
3128
},
3229
};
3330

examples/quickstart.js

+10-15
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,18 @@ const actions = {
3838
send(request, response) {
3939
const {sessionId, context, entities} = request;
4040
const {text, quickreplies} = response;
41-
return new Promise(function(resolve, reject) {
42-
console.log('sending...', JSON.stringify(response));
43-
return resolve();
44-
});
41+
console.log('sending...', JSON.stringify(response));
4542
},
4643
getForecast({context, entities}) {
47-
return new Promise(function(resolve, reject) {
48-
var location = firstEntityValue(entities, 'location')
49-
if (location) {
50-
context.forecast = 'sunny in ' + location; // we should call a weather API here
51-
delete context.missingLocation;
52-
} else {
53-
context.missingLocation = true;
54-
delete context.forecast;
55-
}
56-
return resolve(context);
57-
});
44+
var location = firstEntityValue(entities, 'location');
45+
if (location) {
46+
context.forecast = 'sunny in ' + location; // we should call a weather API here
47+
delete context.missingLocation;
48+
} else {
49+
context.missingLocation = true;
50+
delete context.forecast;
51+
}
52+
return context;
5853
},
5954
};
6055

lib/wit.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,12 @@ function Wit(opts) {
9797
text: message,
9898
entities: json.entities,
9999
};
100-
101100
if (json.type === 'msg') {
102-
throwIfActionMissing(actions, 'send');
103101
const response = {
104102
text: json.msg,
105103
quickreplies: json.quickreplies,
106104
};
107-
return actions.send(request, response).then(ctx => {
105+
return runAction(actions, 'send', request, response).then(ctx => {
108106
if (ctx) {
109107
throw new Error('Cannot update context after \'send\' action');
110108
}
@@ -116,8 +114,7 @@ function Wit(opts) {
116114
);
117115
});
118116
} else if (json.type === 'action') {
119-
throwIfActionMissing(actions, json.action);
120-
return actions[json.action](request).then(ctx => {
117+
return runAction(actions, json.action, request).then(ctx => {
121118
const nextContext = ctx || {};
122119
if (currentRequest !== this._sessions[sessionId]) {
123120
return nextContext;
@@ -136,7 +133,6 @@ function Wit(opts) {
136133
this.runActions = function(sessionId, message, context, maxSteps) {
137134
if (!actions) throwMustHaveActions();
138135
const steps = maxSteps ? maxSteps : DEFAULT_MAX_STEPS;
139-
140136
// Figuring out whether we need to reset the last turn.
141137
// Each new call increments an index for the session.
142138
// We only care about the last call to runActions.
@@ -194,6 +190,11 @@ const throwIfActionMissing = (actions, action) => {
194190
}
195191
};
196192

193+
const runAction = (actions, name, ...rest) => {
194+
throwIfActionMissing(actions, name);
195+
return Promise.resolve(actions[name](...rest));
196+
};
197+
197198
const validate = (opts) => {
198199
if (!opts.accessToken) {
199200
throw new Error('Could not find access token, learn more at https://wit.ai/docs');

0 commit comments

Comments
 (0)