Skip to content
This repository has been archived by the owner on Jun 17, 2024. It is now read-only.

added intent validator, intent post processor, context handler and de… #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 56 additions & 3 deletions src/botkit-middleware-witai.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,63 @@ module.exports = function(config) {
config.minimum_confidence = 0.5;
}

// A context handler interface to get the context object and send it to wit
// It has a function with below signature
//function getContext(bot, message);
var contextHandler = null;
if (config.contextHandler)
{
contextHandler = config.contextHandler;
}

// If wit fails to give any intent then this may help to do some processing over wit
// may be a regex based botkit defaultEars can be used to do regex matching if wit fails
var defaultEars = null;
if (config.defaultEars) {
defaultEars = config.defaultEars;
}

// a Validate Intent interface to validate the intent from wit
// it requires to do some validation over it
var validateIntent = null;
if (config.validateIntent) {
validateIntent = config.validateIntent;
}

// a post process intent interface to do some post processing over wit intent
var postProcessIntent = null;
if (config.postProcessIntent) {
postProcessIntent = config.postProcessIntent;
}

var client = new Wit(config.token, actions);

var middleware = {};

middleware.receive = function(bot, message, next) {
// Only parse messages of type text and mention the bot.
// Otherwise it would send every single message to wit (probably don't want that).
if (message.text && message.text.indexOf(bot.identity.id) > -1) {
client.message(message.text, function(error, data) {
// removed message.text.indexOf(bot.identity.id) as id was missing from identity
if (message.text) {
// added to get the context object
var context = contextHandler != null ? contextHandler.getContext() : undefined;

client.message(message.text, context, function(error, data) {
if (error) {
next(error);
} else {
message.entities = data.entities;
console.log ("entities from wit ", data.entities);
// if validateIntent is not defined then add the entities to message,
// so that others can handle themselves
if ((validateIntent !== null && validateIntent (data.entities)) || validateIntent == null)
{
data.entities = postProcessIntent(data.entities);
message.entities = data.entities;
}
else
{
console.log ( "validation of intent is failed for entities - ", data.entities );
}
next();
}
});
Expand All @@ -50,6 +94,7 @@ module.exports = function(config) {

middleware.hears = function(tests, message) {
if (message.entities && message.entities.intent) {
console.log ("matching wit reposnse to ", tests);
for (var i = 0; i < message.entities.intent.length; i++) {
for (var t = 0; t < tests.length; t++) {
if (message.entities.intent[i].value == tests[t] &&
Expand All @@ -59,6 +104,14 @@ module.exports = function(config) {
}
}
}
else
{
console.log ("no intent is found, matching with regexes ", tests);
if (defaultEars && defaultEars !== null)
return defaultEars(tests, message);
else
console.error ("Missing defaultEars : No Intent is found in Wit Response, and there is no default handler for such response.");
}

return false;
};
Expand Down