Permalink
Browse files

Added chatbot example.

  • Loading branch information...
1 parent 96fed3c commit cbd3d521f0bf80aecd03eb60815576db241b4c3d @primaryobjects committed Mar 23, 2016
Showing with 54 additions and 0 deletions.
  1. +54 −0 chatbot.js
View
@@ -0,0 +1,54 @@
+var readlineSync = require('readline-sync');
+var chatskills = require('./lib/chatskills');
+var mitsuku = require('mitsuku-api')();
+
+// Create a new skill.
+var chatbot = chatskills.add('chatbot');
+
+// Create intents.
+chatbot.intent('start', {
+ 'slots': {},
+ 'utterances': [ '{to |}{start|run|go|talk|chat}' ]
+ },
+ function(req, res) {
+ res.say('What shall we talk about?');
+
+ // Keep session alive.
+ return true;
+ }
+);
+
+chatbot.intent('*', {
+ 'slots': {},
+ 'utterances': [ '{*}' ]
+ },
+ function(req, res) {
+ mitsuku.send(req.input).then(function(response) {
+ res.say(response);
+ });
+
+ // Keep session alive.
+ return true;
+ }
+);
+
+//
+// Get input from the user and respond.
+// Note, since we're calling an async method (mitsuku.send) and we're using a sync method (readlineSync), we have to handle our main loop in an async fashion.
+//
+function main() {
+ var text = readlineSync.question('> ');
+
+ if (text != 'quit') {
+ // Respond to input.
+ chatskills.respond(text, function(response) {
+ console.log(response);
+ main();
+ });
+ }
+}
+
+// Start the 'chatbot' skill immediately, to fire up the chatbot.
+chatskills.respond('chatskills, ask chatbot to start', function(response) {
+ main();
+});

0 comments on commit cbd3d52

Please sign in to comment.