-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
119 lines (106 loc) · 4.46 KB
/
bot.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
var builder = require('botbuilder');
function create(connector) {
var bot = new builder.UniversalBot(connector);
// 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=c413b2ef-382c-45bd-8ff0-f76d60e2a821&subscription-key=d7b46a6c72bf46c1b67f2c4f21acf960&q=';
var recognizer = new builder.LuisRecognizer(model);
var dialog = new builder.IntentDialog({ recognizers: [recognizer] });
bot.dialog('/', dialog);
// Add intent handlers
// Add intent handlers
dialog.matches('builtin.intent.alarm.set_alarm', [
function (session, args, next) {
// Resolve and store any entities passed from LUIS.
var title = builder.EntityRecognizer.findEntity(args.entities, 'builtin.alarm.title');
var time = builder.EntityRecognizer.resolveTime(args.entities);
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;
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('builtin.intent.alarm.delete_alarm', [
function (session, args, next) {
// Resolve entities passed from LUIS.
var title;
var entity = builder.EntityRecognizer.findEntity(args.entities, 'builtin.alarm.title');
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);
return bot;
}
module.exports = { create }