-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbook.js
138 lines (116 loc) · 4.65 KB
/
book.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
/*-----------------------------------------------------------------------------
A simple "Hello World" bot for the Microsoft Bot Framework.
-----------------------------------------------------------------------------*/
var restify = require('restify');
var builder = require('botbuilder');
var request = require('request');
////
var ngrok = '74249d3f';
URL = 'http://' + ngrok + '.ngrok.io/try';
////
//=========================================================
// 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: '348a27a1-84b2-4533-a984-fa2cb9d5898a',
appPassword: 'NJbPtFQ6DFixkMasdFe7ijf'
});
var bot = new builder.UniversalBot(connector);
server.post('/api/messages', connector.listen());
//=========================================================
// Bots Dialogs
//=========================================================
var model = 'https://api.projectoxford.ai/luis/v1/application?id=10c76d78-ac03-4030-b196-4a24ed51ceac&subscription-key=136451a11017458d9e59cbfe7293fb4f';
var recognizer = new builder.LuisRecognizer(model);
var dialog = new builder.IntentDialog({ recognizers: [recognizer] });
bot.dialog('/', dialog);
// Add intent handlers
dialog.matches('TurnOn', [
function (session, args, next) {
// Resolve and store any entities passed from LUIS.
var appliance = builder.EntityRecognizer.findEntity(args.entities, 'Appliance');
session.dialogData.appliance = appliance;
// Prompt for title
if (!appliance) {
builder.Prompts.text(session, 'Which appliance do you want me to turn on?');
} else {
next();
}
},
function (session, results, next) {
var confirmation_msg = "Turning on the ";
var appliance = session.dialogData.appliance;
if (results.response) {
appliance = results.response;
confirmation_msg += appliance
}
else{
confirmation_msg += appliance.entity;
}
// Prompt for time (title will be blank if the user said cancel)
if (!appliance) {
builder.Prompts.text(session, 'Which appliance do you want me to turn on?');
} else {
if (session.userData[appliance.entity] == 'ON'){
session.send("It's already on.");
}
else{
var propertiesObject = {'number':1};
request({url:URL, qs:propertiesObject}, function(err, response, body) {
if(err) { console.log(err); return; }
console.log("Get response: " + response.statusCode);
});
session.userData[appliance.entity] = "ON";
session.send(confirmation_msg);
}
}
}
]);
dialog.matches('TurnOff', [
function (session, args, next) {
// Resolve and store any entities passed from LUIS.
var appliance = builder.EntityRecognizer.findEntity(args.entities, 'Appliance');
session.dialogData.appliance = appliance;
// Prompt for title
if (!appliance) {
builder.Prompts.text(session, 'Which appliance do you want me to turn off?');
} else {
next();
}
},
function (session, results, next) {
var confirmation_msg = "Turning off the ";
var appliance = session.dialogData.appliance;
if (results.response) {
appliance = results.response;
confirmation_msg += appliance
}
else{
confirmation_msg += appliance.entity;
}
// Prompt for time (title will be blank if the user said cancel)
if (!appliance) {
builder.Prompts.text(session, 'Which appliance do you want me to turn on?');
} else {
if (session.userData[appliance.entity] == 'OFF'){
session.send("It's already off.")
}
else{
var propertiesObject = {'number':2};
request({url:URL, qs:propertiesObject}, function(err, response, body) {
if(err) { console.log(err); return; }
console.log("Get response: " + response.statusCode);
});
session.userData[appliance.entity] = "OFF"
session.send(confirmation_msg);
}
}
}
]);
dialog.onDefault(builder.DialogAction.send("I'm sorry I didn't understand. I can only create & delete alarms."));