Socket
Socket
Sign inDemoInstall

node-wit

Package Overview
Dependencies
6
Maintainers
4
Versions
30
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 3.2.2 to 3.3.0

6

CHANGES.md

@@ -0,1 +1,7 @@

## v3.3.0
- callbacks are not called asynchronously by default, choice is left to the developer (use process.nextTick in your callback to emulate the previous behavior)
- using `node-fetch` instead of `requests`
- the `message()` API takes now an optional context as second parameter
## v3.2.2

@@ -2,0 +8,0 @@

8

examples/joke.js

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

const actions = {
say: (sessionId, context, message, cb) => {
say(sessionId, context, message, cb) {
console.log(message);
cb();
},
merge: (sessionId, context, entities, message, cb) => {
merge(sessionId, context, entities, message, cb) {
delete context.joke;

@@ -64,6 +64,6 @@ const category = firstEntityValue(entities, 'category');

},
error: (sessionId, context, error) => {
error(sessionId, context, error) {
console.log(error.message);
},
'select-joke': (sessionId, context, cb) => {
['select-joke'](sessionId, context, cb) {
const jokes = allJokes[context.cat || 'default'];

@@ -70,0 +70,0 @@ context.joke = jokes[Math.floor(Math.random() * jokes.length)];

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

// 2. Download and install ngrok from https://ngrok.com/download
// 3. ./ngrok -http 8445
// 3. ./ngrok http 8445
// 4. WIT_TOKEN=your_access_token FB_PAGE_ID=your_page_id FB_PAGE_TOKEN=your_page_token FB_VERIFY_TOKEN=verify_token node examples/messenger.js

@@ -115,3 +115,3 @@ // 5. Subscribe your page to the Webhooks using verify_token and `https://<your_ngrok_io>/fb` as callback URL.

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

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

},
merge: (sessionId, context, entities, message, cb) => {
merge(sessionId, context, entities, message, cb) {
cb(context);
},
error: (sessionId, context, error) => {
error(sessionId, context, error) {
console.log(error.message);

@@ -148,0 +148,0 @@ },

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

const actions = {
say: (sessionId, context, message, cb) => {
say(sessionId, context, message, cb) {
console.log(message);
cb();
},
merge: (sessionId, context, entities, message, cb) => {
merge(sessionId, context, entities, message, cb) {
// Retrieve the location entity and store it into a context field

@@ -44,6 +44,6 @@ const loc = firstEntityValue(entities, 'location');

},
error: (sessionId, context, error) => {
error(sessionId, context, error) {
console.log(error.message);
},
'fetch-weather': (sessionId, context, cb) => {
['fetch-weather'](sessionId, context, cb) {
// Here should go the api call, e.g.:

@@ -50,0 +50,0 @@ // context.forecast = apiCall(context.loc)

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

const actions = {
say: (sessionId, context, message, cb) => {
say(sessionId, context, message, cb) {
console.log(message);
cb();
},
merge: (sessionId, context, entities, message, cb) => {
merge(sessionId, context, entities, message, cb) {
cb(context);
},
error: (sessionId, context, err) => {
error(sessionId, context, err) {
console.log(err.message);

@@ -26,0 +26,0 @@ },

'use strict';
const request = require('request');
const fetch = require('node-fetch');
const readline = require('readline');

@@ -14,25 +14,34 @@ const uuid = require('node-uuid');

const makeWitResponseHandler = (endpoint, l, cb) => (
(error, response, data) => {
const err = error ||
data.error ||
response.statusCode !== 200 && data.body + ' (' + response.statusCode + ')'
;
const makeWitResponseHandler = (endpoint, l, cb) => {
const error = err => {
l.error('[' + endpoint + '] Error: ' + err);
if (cb) {
cb(err);
}
};
return rsp => {
if (rsp instanceof Error) {
return error(rsp);
}
const json = rsp[0];
const status = rsp[1];
if (json instanceof Error) {
return error(json);
}
const err = json.error || status !== 200 && json.body + ' (' + status + ')';
if (err) {
l.error('[' + endpoint + '] Error: ' + err);
if (cb) {
process.nextTick(() => {
cb(err);
});
}
return;
return error(err)
}
l.debug('[' + endpoint + '] Response: ' + JSON.stringify(data));
l.debug('[' + endpoint + '] Response: ' + JSON.stringify(json));
if (cb) {
process.nextTick(() => {
cb(null, data);
});
cb(null, json);
}
}
);
};

@@ -79,5 +88,3 @@ const validateActions = (actions) => {

if (cb) {
process.nextTick(() => {
cb('No \'' + action + '\' action found.');
});
cb('No \'' + action + '\' action found.');
}

@@ -106,10 +113,8 @@ return true;

const Wit = function(token, actions, logger) {
this.req = request.defaults({
baseUrl: process.env.WIT_URL || 'https://api.wit.ai',
strictSSL: false,
json: true,
headers: {
'Authorization': 'Bearer ' + token,
},
});
const baseURL = process.env.WIT_URL || 'https://api.wit.ai';
const headers = {
'Authorization': 'Bearer ' + token,
'Accept': 'application/json',
'Content-Type': 'application/json',
};
if (logger) {

@@ -120,22 +125,37 @@ l = logger;

this.message = (message, cb) => {
const options = {
uri: '/message',
this.message = (message, context, cb) => {
if (typeof context === 'function') {
cb = context;
context = undefined;
}
let qs = 'q=' + encodeURIComponent(message);
if (context) {
qs += '&context=' + encodeURIComponent(JSON.stringify(context));
}
const handler = makeWitResponseHandler('message', l, cb);
fetch(baseURL + '/message?' + qs, {
method: 'GET',
qs: { q: message },
};
this.req(options, makeWitResponseHandler('message', l, cb));
headers: headers,
})
.then(response => Promise.all([response.json(), response.status]))
.then(handler)
.catch(handler)
;
};
this.converse = (sessionId, message, context, cb) => {
const options = {
uri: '/converse',
method: 'POST',
qs: { 'session_id': sessionId },
json: context,
};
const handler = makeWitResponseHandler('converse', l, cb);
let qs = 'session_id=' + sessionId;
if (message) {
options.qs.q = message;
qs += '&q=' + encodeURIComponent(message);
}
this.req(options, makeWitResponseHandler('converse', l, cb));
fetch(baseURL + '/converse?' + qs, {
method: 'POST',
headers: headers,
body: JSON.stringify(context),
})
.then(response => Promise.all([response.json(), response.status]))
.then(handler)
.catch(handler)
;
};

@@ -207,5 +227,3 @@

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

@@ -219,5 +237,3 @@ return;

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

@@ -224,0 +240,0 @@ return;

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

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

"dependencies": {
"node-uuid": "^1.4.7",
"request": "^2.69.0"
"node-fetch": "^1.5.1",
"node-uuid": "^1.4.7"
}
}

@@ -1,2 +0,2 @@

# Wit Node.js SDK
# Wit Node.js SDK [![npm](https://img.shields.io/npm/v/node-wit.svg)](https://www.npmjs.com/package/node-wit)

@@ -46,10 +46,10 @@ `node-wit` is the Node.js SDK for [Wit.ai](https://wit.ai).

const actions = {
say: (sessionId, context, message, cb) => {
say(sessionId, context, message, cb) {
console.log(message);
cb();
},
merge: (sessionId, context, entities, message, cb) => {
merge(sessionId, context, entities, message, cb) {
cb(context);
},
error: (sessionId, context, error) => {
error(sessionId, context, error) {
console.log(error.message);

@@ -93,2 +93,3 @@ },

* `message` - the text you want Wit.ai to extract the information from
* `context` - (optional) the object representing the session state
* `cb(error, data)` - a callback function with the JSON response

@@ -98,3 +99,4 @@

```js
client.message('what is the weather in London?', (error, data) => {
const context = {};
client.message('what is the weather in London?', context, (error, data) => {
if (error) {

@@ -181,3 +183,3 @@ console.log('Oops! Got an error: ' + error);

```bash
npm install body-parser express request
npm install body-parser express node-fetch
```

@@ -192,3 +194,3 @@

```bash
./ngrok -http 8445
./ngrok http 8445
```

@@ -195,0 +197,0 @@

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc