-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
159 lines (138 loc) · 5.66 KB
/
app.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
var restify = require('restify');
var builder = require('botbuilder');
var instrumentation = require('botbuilder-instrumentation');
//=========================================================
// 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.MICROSOFT_APP_ID,
appPassword: process.env.MICROSOFT_APP_PASSWORD
});
var bot = new builder.UniversalBot(connector);
// Setting up advanced instrumentation
let logging = new instrumentation.BotFrameworkInstrumentation({
instrumentationKey: process.env.APPINSIGHTS_INSTRUMENTATIONKEY,
sentimentKey: process.env.CG_SENTIMENT_KEY,
customFields: {
dialogData: [ 'BotBuilder.Data.Intent' ]
}
});
logging.monitor(bot);
server.post('/api/messages', connector.listen());
//=========================================================
// Bots Dialogs
//=========================================================
// 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=0a2cc164-5a19-47b7-b85e-41914d9037ba&subscription-key=d7b46a6c72bf46c1b67f2c4f21acf960&q=';
var model = process.env.BOT_LUIS_MODEL;
var recognizer = new builder.LuisRecognizer(model);
var dialog = new builder.IntentDialog({ recognizers: [recognizer] });
bot.dialog('/', dialog);
// Add intent handlers
// Add intent handlers
dialog.matches('alarm.set', [
function (session, args, next) {
logging.trackCustomEvent('Try.This', { prop: 'val' }, session);
logging.trackEvent({ prop: 'val' }, session);
console.log('setting alarm...');
logging.startTransaction(session, 'create alarm');
// Resolve and store any entities passed from LUIS.
var title = builder.EntityRecognizer.findEntity(args.entities, 'AlarmName');
var timeEntity = builder.EntityRecognizer.recognizeTime(session.message.text);
var time = timeEntity && builder.EntityRecognizer.resolveTime([timeEntity]);
var alarm = session.dialogData.alarm = {
title: title ? title.entity : null,
timestamp: time ? time.getTime() : null
};
// Prompt for title
if (!alarm.title) {
builder.Prompts.text(session, 'What would you like to call your alarm?');
} else {
next();
}
},
function (session, results, next) {
var alarm = session.dialogData.alarm;
if (results.response) {
alarm.title = results.response;
}
// Prompt for time (title will be blank if the user said cancel)
if (alarm.title && !alarm.timestamp) {
builder.Prompts.time(session, 'What time would you like to set the alarm for?');
} else {
next();
}
},
function (session, results) {
var alarm = session.dialogData.alarm;
if (results.response) {
var time = builder.EntityRecognizer.resolveTime([results.response]);
alarm.timestamp = time ? time.getTime() : null;
}
// Set the alarm (if title or timestamp is blank the user said cancel)
if (alarm.title && alarm.timestamp) {
// Save address of who to notify and write to scheduler.
alarm.address = session.message.address;
alarms[alarm.title] = alarm;
// Send confirmation to user
var date = new Date(alarm.timestamp);
var isAM = date.getHours() < 12;
console.log('alarm set!');
logging.endTransaction(session, 'create alarm', true);
session.send('Creating alarm named "%s" for %d/%d/%d %d:%02d%s',
alarm.title,
date.getMonth() + 1, date.getDate(), date.getFullYear(),
isAM ? date.getHours() : date.getHours() - 12, date.getMinutes(), isAM ? 'am' : 'pm');
} else {
session.send('Ok... no problem.');
}
}
]);
dialog.matches('alarm.delete', [
function (session, args, next) {
// Resolve entities passed from LUIS.
var title;
var entity = builder.EntityRecognizer.findEntity(args.entities, 'AlarmName');
if (entity) {
// Verify its in our set of alarms.
title = builder.EntityRecognizer.findBestMatch(alarms, entity.entity);
}
// Prompt for alarm name
if (!title) {
builder.Prompts.choice(session, 'Which alarm would you like to delete?', alarms);
} else {
next({ response: title });
}
},
function (session, results) {
// If response is null the user canceled the task
if (results.response) {
delete alarms[results.response.entity];
session.send("Deleted the '%s' alarm.", results.response.entity);
} else {
session.send('Ok... no problem.');
}
}
]);
dialog.onDefault(builder.DialogAction.send("I'm sorry I didn't understand. I can only create & delete alarms."));
// Very simple alarm scheduler
var alarms = {};
setInterval(function () {
var now = new Date().getTime();
for (var key in alarms) {
var alarm = alarms[key];
if (now >= alarm.timestamp) {
var msg = new builder.Message()
.address(alarm.address)
.text("Here's your '%s' alarm.", alarm.title);
bot.send(msg);
delete alarms[key];
}
}
}, 15000);