Skip to content

Commit

Permalink
v3.1.0 - updating action parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
patapizza committed Apr 15, 2016
1 parent 30f3888 commit b9ed1a4
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 27 deletions.
17 changes: 17 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
## v3.1.0

### breaking

- the `merge` action now takes 5 parameters: `sessionId`, `context`, `entities`, `message`, `cb`
- the `error` action now takes the context as second parameter
- custom actions now take 3 parameters: `sessionId`, `context`, `cb`

## v3.0.0

Bot Engine integration

### breaking

- the library now provides a Wit object
- `captureTextIntent` has been moved to `Wit.message` with no token
- audio not supported
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,17 @@ const actions = {
console.log(msg);
cb();
},
merge: (context, entities, cb) => {
merge: (sessionId, context, entities, message, cb) => {
cb(context);
},
error: (sessionid, msg) => {
error: (sessionId, context) => {
console.log('Oops, I don\'t know what to do.');
},
};
```

A custom action takes two parameters:
A custom action takes the following parameters:
* `sessionId` - a unique identifier describing the user session
* `context` - the object representing the session state
* `cb(context)` - a callback function to fire at the end of your action with the updated context.

Expand Down
6 changes: 3 additions & 3 deletions examples/joke.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const actions = {
console.log(msg);
cb();
},
merge: (context, entities, cb) => {
merge: (sessionId, context, entities, message, cb) => {
delete context.joke;
const category = firstEntityValue(entities, 'category');
if (category) {
Expand All @@ -60,10 +60,10 @@ const actions = {
}
cb(context);
},
error: (sessionId, msg) => {
error: (sessionId, context) => {
console.log('Oops, I don\'t know what to do.');
},
'select-joke': (context, cb) => {
'select-joke': (sessionId, context, cb) => {
const jokes = allJokes[context.cat || 'default'];
context.joke = jokes[Math.floor(Math.random() * jokes.length)];
cb(context);
Expand Down
4 changes: 2 additions & 2 deletions examples/messenger.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,10 @@ const actions = {
cb();
}
},
merge: (context, entities, cb) => {
merge: (sessionId, context, entities, message, cb) => {
cb(context);
},
error: (sessionid, msg) => {
error: (sessionId, context) => {
console.log('Oops, I don\'t know what to do.');
},
// You should implement your custom actions here
Expand Down
6 changes: 3 additions & 3 deletions examples/quickstart.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,18 @@ const actions = {
console.log(msg);
cb();
},
merge: (context, entities, cb) => {
merge: (sessionId, context, entities, message, cb) => {
// Retrieve the location entity and store it into a context field
const loc = firstEntityValue(entities, 'location');
if (loc) {
context.loc = loc;
}
cb(context);
},
error: (sessionId, msg) => {
error: (sessionId, context) => {
console.log('Oops, I don\'t know what to do.');
},
'fetch-weather': (context, cb) => {
'fetch-weather': (sessionId, context, cb) => {
// Here should go the api call, e.g.:
// context.forecast = apiCall(context.loc)
context.forecast = 'sunny';
Expand Down
4 changes: 2 additions & 2 deletions examples/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ const actions = {
console.log(msg);
cb();
},
merge: (context, entities, cb) => {
merge: (sessionId, context, entities, message, cb) => {
cb(context);
},
error: (sessionid, msg) => {
error: (sessionId, context) => {
console.log('Oops, I don\'t know what to do.');
},
};
Expand Down
26 changes: 13 additions & 13 deletions lib/wit.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ const validateActions = (actions) => {
}
if (key === 'say' && actions.say.length !== 3) {
throw new Error('The \'say\' action should accept 3 arguments: sessionId, message, callback. ' + learnMore);
} else if (key === 'merge' && actions.merge.length !== 3) {
throw new Error('The \'merge\' action should accept 3 arguments: context, entities, callback. ' + learnMore);
} else if (key === 'merge' && actions.merge.length !== 5) {
throw new Error('The \'merge\' action should accept 5 arguments: sessionId, context, entities, message, callback. ' + learnMore);
} else if (key === 'error' && actions.error.length !== 2) {
throw new Error('The \'error\' action should accept 2 arguments: sessionId, callback. ' + learnMore);
} else if (key !== 'say' && key !== 'merge' && actions[key].length !== 2) {
throw new Error('The \'' + key + '\' action should accept 2 arguments: context, callback. ' + learnMore);
throw new Error('The \'error\' action should accept 2 arguments: sessionId, context. ' + learnMore);
} else if (key !== 'say' && key !== 'merge' && key !== 'error' && actions[key].length !== 3) {
throw new Error('The \'' + key + '\' action should accept 3 arguments: sessionId, context, callback. ' + learnMore);
}
});
return actions;
Expand Down Expand Up @@ -112,7 +112,7 @@ const Wit = function(token, actions, logger) {
this.req(options, makeWitResponseHandler('converse', l, cb));
};

const makeCallback = (i, sessionId, context, cb) => {
const makeCallback = (i, sessionId, message, context, cb) => {
return (error, json) => {
let timeoutID;
l.debug('Context: ' + JSON.stringify(context));
Expand Down Expand Up @@ -166,7 +166,7 @@ const Wit = function(token, actions, logger) {
sessionId,
null,
context,
makeCallback(--i, sessionId, context, cb).bind(this)
makeCallback(--i, sessionId, message, context, cb).bind(this)
);
});
} else if (json.type === 'merge') {
Expand All @@ -180,7 +180,7 @@ const Wit = function(token, actions, logger) {
}
l.log('Executing merge action');
timeoutID = makeCallbackTimeout(CALLBACK_TIMEOUT_MS);
this.actions.merge(context, json.entities, (newContext) => {
this.actions.merge(sessionId, context, json.entities, message, (newContext) => {
if (timeoutID) {
clearTimeout(timeoutID);
timeoutID = null;
Expand All @@ -203,7 +203,7 @@ const Wit = function(token, actions, logger) {
sessionId,
null,
context,
makeCallback(--i, sessionId, context, cb).bind(this)
makeCallback(--i, sessionId, message, context, cb).bind(this)
);
});
} else if (json.type === 'action') {
Expand All @@ -220,7 +220,7 @@ const Wit = function(token, actions, logger) {
// Context might be updated in action call
l.log('Executing action: ' + action);
timeoutID = makeCallbackTimeout(CALLBACK_TIMEOUT_MS);
this.actions[action](context, (newContext) => {
this.actions[action](sessionId, context, (newContext) => {
if (timeoutID) {
clearTimeout(timeoutID);
timeoutID = null;
Expand All @@ -243,7 +243,7 @@ const Wit = function(token, actions, logger) {
sessionId,
null,
context,
makeCallback(--i, sessionId, context, cb).bind(this)
makeCallback(--i, sessionId, message, context, cb).bind(this)
);
});
} else { // error
Expand All @@ -256,7 +256,7 @@ const Wit = function(token, actions, logger) {
return;
}
l.log('Executing error action');
this.actions.error(sessionId, 'No \'error\' action found.');
this.actions.error(sessionId, context);
return;
}

Expand All @@ -269,7 +269,7 @@ const Wit = function(token, actions, logger) {
sessionId,
message,
context,
makeCallback(steps, sessionId, context, cb).bind(this)
makeCallback(steps, sessionId, message, context, cb).bind(this)
);
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-wit",
"version": "3.0.1",
"version": "3.1.0",
"description": "Wit.ai Node.js SDK",
"keywords": [
"wit",
Expand Down

0 comments on commit b9ed1a4

Please sign in to comment.