api-ai-botkit-facebook
Advanced tools
Comparing version 1.0.2 to 1.1.0
@@ -6,3 +6,4 @@ /* jshint node: true */ | ||
apiai = require('apiai'), | ||
uuid = require('node-uuid'), | ||
ware = require('ware'), | ||
uuidV1 = require('uuid/v1'), | ||
Entities = require('html-entities').XmlEntities, | ||
@@ -37,2 +38,7 @@ decoder = new Entities() | ||
worker.middleware = { | ||
query: ware(), | ||
response: ware() | ||
}; | ||
worker.action = function (action, callback) { | ||
@@ -62,34 +68,45 @@ if (worker.actionCallbacks[action]) { | ||
if (!(channel in worker.sessionIds)) { | ||
worker.sessionIds[channel] = uuid.v1(); | ||
worker.sessionIds[channel] = uuidV1(); | ||
} | ||
var request = worker.apiaiService.textRequest( | ||
requestText, | ||
{ sessionId: worker.sessionIds[channel] } | ||
); | ||
var options = { | ||
sessionId: worker.sessionIds[channel] | ||
}; | ||
worker.middleware.query.run(requestText, options, function(err, query, options) { | ||
var request = worker.apiaiService.textRequest( | ||
query, | ||
options | ||
); | ||
request.on('response', function (response) { | ||
request.on('response', function (response) { | ||
worker.middleware.response.run(message, response, bot, | ||
function(err, message, response, bot) { | ||
if (err) { | ||
console.error(err); | ||
} | ||
worker.allCallback.forEach(function (callback) { | ||
callback(message, response, bot); | ||
}); | ||
worker.allCallback.forEach(function (callback) { | ||
callback(message, response, bot); | ||
if (isDefined(response.result)) { | ||
var action = response.result.action; | ||
// set action to null if action is not defined or used | ||
action = isDefined(action) && worker.actionCallbacks[action] ? | ||
action : null; | ||
if (worker.actionCallbacks[action]) { | ||
worker.actionCallbacks[action].forEach(function (callback) { | ||
callback(message, response, bot); | ||
}); | ||
} | ||
} | ||
} | ||
); | ||
}); | ||
if (isDefined(response.result)) { | ||
var action = response.result.action; | ||
// set action to null if action is not defined or used | ||
action = isDefined(action) && worker.actionCallbacks[action] ? | ||
action : null; | ||
request.on('error', function (error) { | ||
console.error(error); | ||
}); | ||
if (worker.actionCallbacks[action]) { | ||
worker.actionCallbacks[action].forEach(function (callback) { | ||
callback(message, response, bot); | ||
}); | ||
} | ||
} | ||
request.end(); | ||
}); | ||
request.on('error', function (error) { | ||
console.error(error); | ||
}); | ||
request.end(); | ||
} | ||
@@ -96,0 +113,0 @@ } catch (err) { |
{ | ||
"name": "api-ai-botkit-facebook", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "Utility lib for creating Facebook Messenger bots with Botkit and api.ai", | ||
@@ -27,4 +27,5 @@ "main": "api-ai-botkit-facebook.js", | ||
"html-entities": "^1.2.0", | ||
"node-uuid": "^1.4.7" | ||
"uuid": "^3.0.1", | ||
"ware": "^1.3.0" | ||
} | ||
} |
@@ -51,1 +51,36 @@ # api-ai-botkit-facebook | ||
``` | ||
## Middleware | ||
The functionality can be extended using middleware functions. These functions can plugin to the api.ai running processes at couple useful places and make changes to both the query or response. | ||
### Middleware Endpoints | ||
The module currently supports middleware insertion in two places: | ||
* When sending a query, before the query is sent to api.ai | ||
* When receiving a response, before triggering any events | ||
Query and Response middleware functions are added to the module using an Express-style "use" syntax. Each function receives a set of parameters and a next function which must be called to continue processing the middleware stack. | ||
### Query Middleware | ||
Query middleware can be used to do things like preprocess the query or options before it gets sent out to api.ai. | ||
```js | ||
apiai.middleware.query.use((query, options, next) => { | ||
// do something... | ||
// options.contexts.resetContexts = true; | ||
next(); | ||
}); | ||
``` | ||
### Response Middleware | ||
Response middleware can be used to do things like preprocess the response content. Additional information can be added to the response object for use down the chain. | ||
```js | ||
apiai.middleware.response.use((message, response, bot, next) => { | ||
// do something... | ||
// response.extrainfo = 'bar'; | ||
next(); | ||
}); | ||
``` |
7749
6
95
86
4
+ Addeduuid@^3.0.1
+ Addedware@^1.3.0
+ Addedco@3.1.0(transitive)
+ Addeduuid@3.4.0(transitive)
+ Addedware@1.3.0(transitive)
+ Addedwrap-fn@0.1.5(transitive)
- Removednode-uuid@^1.4.7
- Removednode-uuid@1.4.8(transitive)