|
|
@@ -30,27 +30,20 @@ var ChatSkillsManager = { |
|
|
return this.apps[namespace];
|
|
|
},
|
|
|
|
|
|
- respond: function(input, sessionId, callback) {
|
|
|
- if (typeof sessionId == 'function') {
|
|
|
- // No sessionId provided, default to 1 (localhost).
|
|
|
- callback = sessionId;
|
|
|
- sessionId = 1;
|
|
|
- }
|
|
|
-
|
|
|
- var session = this.sessions[sessionId];
|
|
|
- var app = null;
|
|
|
-
|
|
|
+ session: function(input, sessionId) {
|
|
|
var regEx = new RegExp(this.id + '[,\\-\\!\\? ]+ask ([a-zA-Z0-9]+)[,\\. ](.*)', 'i');
|
|
|
var matches = input.match(regEx);
|
|
|
|
|
|
// Determine a skill to start: "[bot], ask [namespace] [input]".
|
|
|
if (matches && matches.length == 3) {
|
|
|
+ // It's a request for our bot.
|
|
|
var namespace = matches[1];
|
|
|
input = matches[2];
|
|
|
|
|
|
- // It's a request for our bot.
|
|
|
- app = this.apps[namespace];
|
|
|
+ // See if a skill exists with this namespace.
|
|
|
+ var app = this.apps[namespace];
|
|
|
if (!app) {
|
|
|
+ // Skill not found.
|
|
|
if (this.verbose) {
|
|
|
console.log("Error: The skill '" + namespace + "' doesn't exist. Add one using: chatskills.add('" + namespace + "')");
|
|
|
}
|
|
|
@@ -61,91 +54,107 @@ var ChatSkillsManager = { |
|
|
this.sessions[sessionId] = {
|
|
|
id: sessionId,
|
|
|
app: app,
|
|
|
+ input: input,
|
|
|
slots: {}
|
|
|
};
|
|
|
|
|
|
- session = this.sessions[sessionId];
|
|
|
-
|
|
|
if (this.verbose) {
|
|
|
console.log('Session ' + sessionId + ' started.');
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- else if (!session) {
|
|
|
- // Not a request for our bot.
|
|
|
+ else if (!this.sessions[sessionId]) {
|
|
|
+ // Not a request for our bot and no existing session.
|
|
|
if (this.verbose) {
|
|
|
console.log("Info: Ignoring. Example request: '" + this.id + ", ask SKILL_NAME text'.");
|
|
|
}
|
|
|
return;
|
|
|
}
|
|
|
else {
|
|
|
// Continue existing session.
|
|
|
- app = session.app;
|
|
|
+ this.sessions[sessionId].input = input;
|
|
|
}
|
|
|
|
|
|
- // Go through each intent in the skill to find a valid response.
|
|
|
- for (var i in Object.keys(app.intents)) {
|
|
|
- var key = Object.keys(app.intents)[i];
|
|
|
-
|
|
|
- // Get utterances for this intent.
|
|
|
- var utterances = [];
|
|
|
- app.utterances().split('\n').forEach(function(template) {
|
|
|
- // Get the intent name from this template line.
|
|
|
- var matches = template.match(/([a-zA-Z0-9]+)\t/);
|
|
|
- if (matches && matches[1] == key) {
|
|
|
- // The intent matches ours, let's use it. First, strip out intent name.
|
|
|
- var start = template.indexOf('\t');
|
|
|
- template = template.substring(start + 1);
|
|
|
-
|
|
|
- // Add this utterance for processing.
|
|
|
- utterances.push(template);
|
|
|
- }
|
|
|
- });
|
|
|
+ return this.sessions[sessionId];
|
|
|
+ },
|
|
|
|
|
|
- var result = ChatSkillsManager.parse(input, utterances);
|
|
|
- if (result.isValid) {
|
|
|
- // This intent is valid for the input. Set slots.
|
|
|
- for (var j in result.pairs) {
|
|
|
- var pair = result.pairs[j];
|
|
|
- session.slots[pair.name] = pair.value;
|
|
|
- };
|
|
|
+ respond: function(input, sessionId, callback) {
|
|
|
+ if (typeof sessionId == 'function') {
|
|
|
+ // No sessionId provided, default to 1 (localhost).
|
|
|
+ callback = sessionId;
|
|
|
+ sessionId = 1;
|
|
|
+ }
|
|
|
|
|
|
- // Call intent.
|
|
|
- var continueSession = app.intents[key]['function'](
|
|
|
- {
|
|
|
- // Request
|
|
|
- input: input,
|
|
|
- slots: session.slots,
|
|
|
- variables: result.pairs,
|
|
|
+ // Get a new or existing session.
|
|
|
+ var session = this.session(input, sessionId);
|
|
|
+ if (session) {
|
|
|
+ var app = session.app;
|
|
|
+ input = session.input;
|
|
|
+
|
|
|
+ // Go through each intent in the skill to find a valid response.
|
|
|
+ for (var i in Object.keys(app.intents)) {
|
|
|
+ var key = Object.keys(app.intents)[i];
|
|
|
+
|
|
|
+ // Get utterances for this intent.
|
|
|
+ var utterances = [];
|
|
|
+ app.utterances().split('\n').forEach(function(template) {
|
|
|
+ // Get the intent name from this template line.
|
|
|
+ var matches = template.match(/([a-zA-Z0-9]+)\t/);
|
|
|
+ if (matches && matches[1] == key) {
|
|
|
+ // The intent matches ours, let's use it. First, strip out intent name.
|
|
|
+ var start = template.indexOf('\t');
|
|
|
+ template = template.substring(start + 1);
|
|
|
+
|
|
|
+ // Add this utterance for processing.
|
|
|
+ utterances.push(template);
|
|
|
+ }
|
|
|
+ });
|
|
|
|
|
|
- get: function(key) {
|
|
|
- return session.slots[key];
|
|
|
+ var result = ChatSkillsManager.parse(input, utterances);
|
|
|
+ if (result.isValid) {
|
|
|
+ // This intent is valid for the input. Set slots.
|
|
|
+ for (var j in result.pairs) {
|
|
|
+ var pair = result.pairs[j];
|
|
|
+ session.slots[pair.name] = pair.value;
|
|
|
+ };
|
|
|
+
|
|
|
+ // Call intent.
|
|
|
+ var continueSession = app.intents[key]['function'](
|
|
|
+ {
|
|
|
+ // Request
|
|
|
+ input: input,
|
|
|
+ slots: session.slots,
|
|
|
+ variables: result.pairs,
|
|
|
+
|
|
|
+ get: function(key) {
|
|
|
+ return session.slots[key];
|
|
|
+ },
|
|
|
+
|
|
|
+ set: function(key, value) {
|
|
|
+ session.slots[key] = value;
|
|
|
+ }
|
|
|
},
|
|
|
-
|
|
|
- set: function(key, value) {
|
|
|
- session.slots[key] = value;
|
|
|
- }
|
|
|
- },
|
|
|
- {
|
|
|
- // Response
|
|
|
- say: function(text) {
|
|
|
- if (callback) {
|
|
|
- callback(text);
|
|
|
+ {
|
|
|
+ // Response
|
|
|
+ say: function(text) {
|
|
|
+ if (callback) {
|
|
|
+ callback(text);
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
- });
|
|
|
+ });
|
|
|
|
|
|
- if (!continueSession) {
|
|
|
- // Intent returned false, so end the session.
|
|
|
- delete this.sessions[sessionId];
|
|
|
+ if (!continueSession) {
|
|
|
+ // Intent returned false, so end the session.
|
|
|
+ delete this.sessions[sessionId];
|
|
|
|
|
|
- if (this.verbose) {
|
|
|
- console.log('Session ' + sessionId + ' ended.');
|
|
|
+ if (this.verbose) {
|
|
|
+ console.log('Session ' + sessionId + ' ended.');
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
- // We've already found a valid intent, skip processing the rest. This prevents multiple responses from the same text.
|
|
|
- break;
|
|
|
+ // We've already found a valid intent, skip processing the rest. This prevents multiple responses from the same text.
|
|
|
+ break;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
},
|
|
|
|
0 comments on commit
820162b