Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

node-wit

Package Overview
Dependencies
Maintainers
4
Versions
30
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-wit - npm Package Compare versions

Comparing version 3.1.0 to 3.2.0

11

CHANGES.md

@@ -0,3 +1,14 @@

## v3.2.0
Unifying action parameters.
### breaking
- the `say` action now takes 4 parameters: `sessionId`, `context`, `message`, `cb`
- the `error` action now takes 3 parameters: `sessionId`, `context`, `error`
## v3.1.0
Updating action parameters.
### breaking

@@ -4,0 +15,0 @@

8

examples/joke.js

@@ -45,4 +45,4 @@ 'use strict';

const actions = {
say: (sessionId, msg, cb) => {
console.log(msg);
say: (sessionId, context, message, cb) => {
console.log(message);
cb();

@@ -64,4 +64,4 @@ },

},
error: (sessionId, context) => {
console.log('Oops, I don\'t know what to do.');
error: (sessionId, context, error) => {
console.log(error.message);
},

@@ -68,0 +68,0 @@ 'select-joke': (sessionId, context, cb) => {

@@ -114,3 +114,3 @@ 'use strict';

const actions = {
say: (sessionId, msg, cb) => {
say: (sessionId, context, message, cb) => {
// Our bot has something to say!

@@ -122,3 +122,3 @@ // Let's retrieve the Facebook user whose session belongs to

// Let's forward our bot response to her.
fbMessage(recipientId, msg, (err, data) => {
fbMessage(recipientId, message, (err, data) => {
if (err) {

@@ -145,4 +145,4 @@ console.log(

},
error: (sessionId, context) => {
console.log('Oops, I don\'t know what to do.');
error: (sessionId, context, error) => {
console.log(error.message);
},

@@ -149,0 +149,0 @@ // You should implement your custom actions here

@@ -31,4 +31,4 @@ 'use strict';

const actions = {
say: (sessionId, msg, cb) => {
console.log(msg);
say: (sessionId, context, message, cb) => {
console.log(message);
cb();

@@ -44,4 +44,4 @@ },

},
error: (sessionId, context) => {
console.log('Oops, I don\'t know what to do.');
error: (sessionId, context, error) => {
console.log(error.message);
},

@@ -48,0 +48,0 @@ 'fetch-weather': (sessionId, context, cb) => {

@@ -16,4 +16,4 @@ 'use strict';

const actions = {
say: (sessionId, msg, cb) => {
console.log(msg);
say: (sessionId, context, message, cb) => {
console.log(message);
cb();

@@ -24,4 +24,4 @@ },

},
error: (sessionId, context) => {
console.log('Oops, I don\'t know what to do.');
error: (sessionId, context, err) => {
console.log(err.message);
},

@@ -28,0 +28,0 @@ };

@@ -60,8 +60,8 @@ 'use strict';

}
if (key === 'say' && actions.say.length !== 3) {
throw new Error('The \'say\' action should accept 3 arguments: sessionId, message, callback. ' + learnMore);
if (key === 'say' && actions.say.length !== 4) {
throw new Error('The \'say\' action should accept 4 arguments: sessionId, context, message, 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, context. ' + learnMore);
} else if (key === 'error' && actions.error.length !== 3) {
throw new Error('The \'error\' action should accept 3 arguments: sessionId, context, error. ' + learnMore);
} else if (key !== 'say' && key !== 'merge' && key !== 'error' && actions[key].length !== 3) {

@@ -80,2 +80,22 @@ throw new Error('The \'' + key + '\' action should accept 3 arguments: sessionId, context, callback. ' + learnMore);

const cbIfActionMissing = (actions, action, cb) => {
if (!actions.hasOwnProperty(action)) {
if (cb) {
process.nextTick(() => {
cb('No \'' + action + '\' action found.');
});
}
return true;
}
return false;
};
const clone = (obj) => {
const newObj = {};
Object.keys(obj).forEach(k => {
newObj[k] = typeof obj[k] === 'object' ? clone(obj[k]) : obj[k];
});
return newObj;
};
const Wit = function(token, actions, logger) {

@@ -118,4 +138,61 @@ this.req = request.defaults({

const makeCallback = (i, sessionId, message, context, cb) => {
let timeoutID;
const makeActionCallback = () => {
timeoutID = makeCallbackTimeout(CALLBACK_TIMEOUT_MS);
return (newContext) => {
if (timeoutID) {
clearTimeout(timeoutID);
timeoutID = null;
}
const context = newContext || {};
l.debug('Context\': ' + JSON.stringify(context));
if (i <= 0) {
l.warn('Max steps reached, halting.');
if (cb) {
cb(null, context);
}
return;
}
// Retrieving action sequence
this.converse(
sessionId,
null,
context,
makeCallback(--i, sessionId, message, context, cb).bind(this)
);
};
};
const makeSayCallback = () => {
timeoutID = makeCallbackTimeout(CALLBACK_TIMEOUT_MS);
return function() {
if (arguments.length > 0) {
throw new Error('The \'say\' callback should not have any arguments!');
}
if (timeoutID) {
clearTimeout(timeoutID);
timeoutID = null;
}
if (i <= 0) {
l.warn('Max steps reached, halting.');
if (cb) {
cb(null, context);
}
return;
}
// Retrieving action sequence
this.converse(
sessionId,
null,
context,
makeCallback(--i, sessionId, message, context, cb).bind(this)
);
};
};
return (error, json) => {
let timeoutID;
l.debug('Context: ' + JSON.stringify(context));

@@ -126,3 +203,3 @@ error = error || !json.type && 'Couldn\'t find type in Wit response';

process.nextTick(() => {
cb(error, context);
cb(error);
});

@@ -133,3 +210,3 @@ }

// TODO(jodent) refactor
var clonedContext = clone(context);
if (json.type === 'stop') {

@@ -144,123 +221,26 @@ // End of turn

} else if (json.type === 'msg') {
if (!this.actions.say) {
if (cb) {
process.nextTick(() => {
cb('No \'say\' action found.');
});
}
if (cbIfActionMissing(this.actions, 'say', cb)) {
return;
}
timeoutID = makeCallbackTimeout(CALLBACK_TIMEOUT_MS);
l.log('Executing say with message: ' + json.msg);
this.actions.say(sessionId, json.msg, () => {
if (timeoutID) {
clearTimeout(timeoutID);
timeoutID = null;
}
if (i <= 0) {
l.warn('Max steps reached, halting.');
if (cb) {
process.nextTick(() => {
cb(null, context);
});
}
return;
}
// Retrieving action sequence
this.converse(
sessionId,
null,
context,
makeCallback(--i, sessionId, message, context, cb).bind(this)
);
});
this.actions.say(sessionId, clonedContext, json.msg, makeSayCallback().bind(this));
} else if (json.type === 'merge') {
if (!this.actions.merge) {
if (cb) {
process.nextTick(() => {
cb('No \'merge\' action found.');
});
}
if (cbIfActionMissing(this.actions, 'merge', cb)) {
return;
}
l.log('Executing merge action');
timeoutID = makeCallbackTimeout(CALLBACK_TIMEOUT_MS);
this.actions.merge(sessionId, context, json.entities, message, (newContext) => {
if (timeoutID) {
clearTimeout(timeoutID);
timeoutID = null;
}
const context = newContext || {};
l.debug('Context\': ' + JSON.stringify(context));
if (i <= 0) {
l.warn('Max steps reached, halting.');
if (cb) {
process.nextTick(() => {
cb(null, context);
});
}
return;
}
// Retrieving action sequence
this.converse(
sessionId,
null,
context,
makeCallback(--i, sessionId, message, context, cb).bind(this)
);
});
this.actions.merge(sessionId, clonedContext, json.entities, message, makeActionCallback());
} else if (json.type === 'action') {
const action = json.action;
if (!this.actions.hasOwnProperty(action)) {
if (cb) {
process.nextTick(() => {
cb('No \'' + action + '\' action found.', context);
});
}
if (cbIfActionMissing(this.actions, action, cb)) {
return;
}
// Context might be updated in action call
l.log('Executing action: ' + action);
timeoutID = makeCallbackTimeout(CALLBACK_TIMEOUT_MS);
this.actions[action](sessionId, context, (newContext) => {
if (timeoutID) {
clearTimeout(timeoutID);
timeoutID = null;
}
const context = newContext || {};
l.debug('Context\': ' + JSON.stringify(context));
if (i <= 0) {
l.warn('Max steps reached, halting.');
if (cb) {
process.nextTick(() => {
cb(null, context);
});
}
return;
}
// Retrieving action sequence
this.converse(
sessionId,
null,
context,
makeCallback(--i, sessionId, message, context, cb).bind(this)
);
});
this.actions[action](sessionId, clonedContext, makeActionCallback());
} else { // error
if (!this.actions.error) {
if (cb) {
process.nextTick(() => {
cb('No \'error\' action found.');
});
}
if (cbIfActionMissing(this.actions, 'error', cb)) {
return;
}
l.log('Executing error action');
this.actions.error(sessionId, context);
this.actions.error(sessionId, clonedContext, new Error('Oops, I don\'t know what to do.'));
return;

@@ -267,0 +247,0 @@ }

{
"name": "node-wit",
"version": "3.1.0",
"version": "3.2.0",
"description": "Wit.ai Node.js SDK",

@@ -5,0 +5,0 @@ "keywords": [

@@ -15,14 +15,8 @@ # Wit Node.js SDK

Copy `examples/template.js` to `app.js`:
Run in your terminal:
```bash
cp examples/template.js app.js
node examples/template.js <your_token>
```
Then run in your terminal:
```bash
node app.js
```
See `examples` folder for more examples.

@@ -53,4 +47,4 @@

const actions = {
say: (sessionId, msg, cb) => {
console.log(msg);
say: (sessionId, context, message, cb) => {
console.log(message);
cb();

@@ -61,4 +55,4 @@ },

},
error: (sessionId, context) => {
console.log('Oops, I don\'t know what to do.');
error: (sessionId, context, error) => {
console.log(error.message);
},

@@ -65,0 +59,0 @@ };

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc