-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathluis.js
162 lines (143 loc) · 5.32 KB
/
luis.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//=========================================================
// KSLHacks @KSLHacks
// MHacksChatBot using Node JS with LUIS.ai integration
//=========================================================
var restify = require('restify');
var builder = require('botbuilder');
var env = require('./env.js')
var quiz = require('./api.js');
var index = 0; // index for flashcard containers (terms + def)
var username; //= "gabrielle_crevecoeur"; // login info for user
(function () {
if (username)
quiz.GetSets(username); // I will invoke myself
})();
//=========================================================
// Bot Setup
//=========================================================
// Setup Restify Server
var server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function () {
console.log('%s listening to %s', server.name, server.url);
});
// Create chat bot
var connector = new builder.ChatConnector({
appId: process.env.APP_ID,
appPassword: process.env.APP_PASS
});
var bot = new builder.UniversalBot(connector);
server.post('/api/messages', connector.listen());
// Create LUIS recognizer that points at our model and add it as the root '/' dialog for our Cortana Bot.
var model = 'https://api.projectoxford.ai/luis/v1/application?id=9358489c-d440-488e-82de-d465b65b75f2&subscription-key=' + process.env.LUIS_KEY;
var recognizer = new builder.LuisRecognizer(model);
var dialog = new builder.IntentDialog({ recognizers: [recognizer] });
bot.dialog('/', dialog);
//=========================================================
// Bots Dialogs With LUIS.AI !
//=========================================================
// Greetings! Gather name from user so we can address them by name.
dialog.matches('Greeting', [
function (session, args, next) {
builder.Prompts.text(session, "Hello! Welcome to the Mhacks Quiz Bot. Would you like to study today?");
},
function (session, results, next) {
if ((results.response == "yes") || (results.response == "Yes")) {
// user wants to study
if (username) {
// user has username
next();
} else {
// user does NOT have username
session.beginDialog('/askName');
}
} else {
// user does not want to study
session.beginDialog('/Exit');
}
},
function (session, results) {
session.beginDialog('/PickTopic');
}
]);
// prompt the user for their name and store response -- Called from /Greeting
bot.dialog('/askName', [
function (session) {
builder.Prompts.text(session, "What is your quizlet username?");
},
function (session, results) {
// store the username in the privateConversationData.name
session.privateConversationData.name = results.response;
// call api to get sets with username
quiz.GetSets(results.response);
session.endDialogWithResult();
}
]);
// Choose a topic so we can load the flashcards from API
dialog.matches('PickTopic', [
function (session) {
session.beginDialog('/PickTopic');
}
]);
bot.dialog('/PickTopic', [
function (session) {
setTimeout(function() {
builder.Prompts.text(session, "What study set would you like today?" + quiz.Sets);
}, 2000)
},
function (session, results) {
// call api with selected set
quiz.GetTerms(results.response);
session.privateConversationData.topic = results.response;
session.send("Ok! I got your flashcards! Send 'ready' to begin. Send 'flip' for definition. Send 'next' for the next card. Send 'exit' when you are done");
session.endDialogWithResult();
}
]);
// Start the quiz --> display first flashcard
dialog.matches('StartQuiz', [
function (session) {
if (!session.privateConversationData.topic) {
session.beginDialog('/PickTopic');
} else {
session.send(quiz.Terms[index]);
session.endDialog();
}
}
]);
// Display the opposite side of the flashcard -- answer
dialog.matches('FlipCard', [
function(session) {
session.send(quiz.Def[index]);
session.endDialog();
}
]);
// Display the next flashcard
dialog.matches('NextCard', [
function(session) {
if(++index == quiz.Terms.length)
//all flashcards exhausted
session.send("You are all out of cards! Hope you had fun studying! :)");
else {
session.send(quiz.Terms[index])
}
}
]);
// TODO Score will be displayed "x out of y correct"
dialog.matches('ShowScore', builder.DialogAction.send('ShowScore'));
// User wishes to leave the session -- Data will be cleared for new user
dialog.matches('Exit', [
function (session) {
session.beginDialog('/Exit');
}
]);
bot.dialog('/Exit', [
function (session, results) {
// end conversation.. clear privateConversationData stack
if (username) {
session.endConversation("Hope you had fun studying. See ya later %s :)", session.privateConversationData.name);
} else {
session.endConversation("Hope you had fun studying. See ya later :)");
}
}
]);
// Waterfall fall through catch -- default messages
dialog.onDefault(builder.DialogAction.send("I'm sorry I didn't understand.."));