-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
chatbot.js
54 lines (46 loc) · 1.35 KB
/
chatbot.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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();
});