node-wit
Advanced tools
Comparing version 3.2.2 to 3.3.0
@@ -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 @@ |
@@ -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 @@ }, |
116
lib/wit.js
'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 @@ |
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
28942
669
212
2
+ Addednode-fetch@^1.5.1
+ Addedencoding@0.1.13(transitive)
+ Addediconv-lite@0.6.3(transitive)
+ Addedis-stream@1.1.0(transitive)
+ Addednode-fetch@1.7.3(transitive)
- Removedrequest@^2.69.0
- Removedajv@6.12.6(transitive)
- Removedasn1@0.2.6(transitive)
- Removedassert-plus@1.0.0(transitive)
- Removedasynckit@0.4.0(transitive)
- Removedaws-sign2@0.7.0(transitive)
- Removedaws4@1.13.0(transitive)
- Removedbcrypt-pbkdf@1.0.2(transitive)
- Removedcaseless@0.12.0(transitive)
- Removedcombined-stream@1.0.8(transitive)
- Removedcore-util-is@1.0.2(transitive)
- Removeddashdash@1.14.1(transitive)
- Removeddelayed-stream@1.0.0(transitive)
- Removedecc-jsbn@0.1.2(transitive)
- Removedextend@3.0.2(transitive)
- Removedextsprintf@1.3.0(transitive)
- Removedfast-deep-equal@3.1.3(transitive)
- Removedfast-json-stable-stringify@2.1.0(transitive)
- Removedforever-agent@0.6.1(transitive)
- Removedform-data@2.3.3(transitive)
- Removedgetpass@0.1.7(transitive)
- Removedhar-schema@2.0.0(transitive)
- Removedhar-validator@5.1.5(transitive)
- Removedhttp-signature@1.2.0(transitive)
- Removedis-typedarray@1.0.0(transitive)
- Removedisstream@0.1.2(transitive)
- Removedjsbn@0.1.1(transitive)
- Removedjson-schema@0.4.0(transitive)
- Removedjson-schema-traverse@0.4.1(transitive)
- Removedjson-stringify-safe@5.0.1(transitive)
- Removedjsprim@1.4.2(transitive)
- Removedmime-db@1.52.0(transitive)
- Removedmime-types@2.1.35(transitive)
- Removedoauth-sign@0.9.0(transitive)
- Removedperformance-now@2.1.0(transitive)
- Removedpsl@1.9.0(transitive)
- Removedpunycode@2.3.1(transitive)
- Removedqs@6.5.3(transitive)
- Removedrequest@2.88.2(transitive)
- Removedsafe-buffer@5.2.1(transitive)
- Removedsshpk@1.18.0(transitive)
- Removedtough-cookie@2.5.0(transitive)
- Removedtunnel-agent@0.6.0(transitive)
- Removedtweetnacl@0.14.5(transitive)
- Removeduri-js@4.4.1(transitive)
- Removeduuid@3.4.0(transitive)
- Removedverror@1.10.0(transitive)