This repository has been archived by the owner on Nov 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
116 lines (93 loc) · 3.26 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
"use strict";
var restify = require('restify');
var builder = require('botbuilder');
var path = require('path');
var loaddir = require('./util/loaddir.js');
var books = require('./util/books.js');
var isitdown = require('./util/isitdown.js');
var allmenus = require('./util/allmenus.js');
console.log("microsoft_app_id: " + process.env.MICROSOFT_APP_ID);
//=========================================================
// load menus
//=========================================================
var menus = loaddir.requireDir(path.join(__dirname, "menus"));
var aliveReplies = ["I AM ALIVE!", "https://www.youtube.com/watch?v=oQwNN-0AgWc"];
//=========================================================
// Service Setup
//=========================================================
var server = restify.createServer();
server.listen(8080, function () {
console.log('%s listening to %s', server.name, server.url);
});
//a rest client for alexa to talk to
server.get('/menu/:name', function (req, res, next) {
var name = req.params.name;
menus[name].getMenu((result) => res.send(200, result.menu));
});
//=========================================================
// Bot Setup
//=========================================================
// 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);
server.post('/bot', connector.listen());
var intents = new builder.IntentDialog();
//=========================================================
// Bots Dialogs
//=========================================================
bot.dialog('/', intents);
//create intents for alle menu modules
var createIntent = function (m) {
intents.matches(m.intent, [
function (session) {
allmenus.sendSingleMenu(session, m);
}
]);
};
for (var menu in menus) {
createIntent(menus[menu]);
}
function getRandomElement(array) {
return array[Math.floor(Math.random() * array.length)]
}
intents.matches(/.*all.*/i, [
function (session) {
allmenus.sendAllMenus(session, menus);
}
]);
intents.matches(/ping|are you alive/i, [
function (session) {
session.send(getRandomElement(aliveReplies))
}
]);
intents.matches(/is .* down\?*/i, [
function(session, args) {
var url = /is (.*) down\?*/gi.exec(session.message.text)[1];
//console.log(url);
session.send(isitdown.isitdown(result => session.send(result), url));
}
]);
intents.matches(books.intent, [
function (session, args, next) {
session.send("Give me a second to look it up.");
var isbn = session.message.text.match(/[0-9]+/g)
//console.log(session.message.text);
if (isbn.length > 0 || !isbn[isbn.length - 1].trim()) {
books.query(result => session.send(result), isbn[isbn.length - 1])
} else {
session.send("Sorry couldnt find anything. Did you forget the isbn number?");
}
}
]);
intents.onDefault([
function (session, args, next) {
var commands = "";
for (var menu in menus) {
commands += `**${menu}**\n`
}
session.send(`Hey, I'm a pretty dumb bot (angel). Here are some commands I understand: \n*all**\n${commands}.`);
}
]);