-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
129 lines (118 loc) · 4.76 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
var restify = require('restify');
var builder = require('botbuilder');
var userOptions = require('./userOptions');
//=========================================================
// 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);
server.post('/api/messages', connector.listen());
//=========================================================
// Bots Dialogs
//=========================================================
var validUserNames = new Array('max', 'moritz', 'hana');
bot.dialog('/', [
function(session) {
session.beginDialog('auth');
},
function(session, results) {
session.beginDialog('main');
}
]);
bot.dialog('auth', [
function(session) {
builder.Prompts.text(session, "Hallo, hier ist der Finanzmanager. Bitte loggen Sie sich ein (Max, Moritz, Hana)");
},
function(session, results) {
if (validUserNames.indexOf(results.response.toLowerCase()) != -1) {
session.send("Hallo %s", results.response);
session.userData.flows = [];
session.userData.level = [];
session.userData.name = results.response;
session.endDialog();
} else {
session.send("Username ungültig. (Max, Moritz, Hana)");
session.replaceDialog('auth');
}
}
]);
bot.dialog('main', [
function(session) {
var currentTreePosition = userOptions.userOptions;
var lastElement = "Welches Szenario?";
session.userData.lastType = "options";
session.userData.level.forEach(function(element) {
session.userData.lastType = currentTreePosition[element]['type'];
currentTreePosition = currentTreePosition[element]['values'];
lastElement = element;
}, this);
if (session.userData.lastType == "flow") {
Object.keys(currentTreePosition).forEach(function(element) {
session.userData.level.push(element);
if (session.userData.flows.indexOf(element) != -1) {
session.userData.flows.pop();
session.userData.level.pop();
session.userData.level.pop();
} else {
session.userData.flows.push(element);
session.userData.lastType = currentTreePosition[element]['type'];
currentTreePosition = currentTreePosition[element]['values'];
lastElement = element;
}
});
}
if (session.userData.lastType == "end") {
session.send("Vielen Dank für die Nutzung des Finanzmanagers. Ihr Auftrag wurde ausgeführt.")
session.userData.level = [];
session.userData.lastType = null;
currentTreePosition = null;
lastElement = null;
session.endDialog();
}
if (session.userData.lastType == "options" && session.userData.level.length > 0) {
currentTreePosition["Zurück"] = {};
}
if (session.userData.lastType == 'options') {
builder.Prompts.choice(session, lastElement, currentTreePosition, {
listStyle: builder.ListStyle['button']
});
} else if (session.userData.lastType == 'number') {
builder.Prompts.number(session, lastElement);
}
else if(session.userData.lastType != 'end'){
session.replaceDialog('main');
}
},
function(session, results) {
if (results && results.response) {
// options
if (results.response.entity) {
if (results.response.entity == "Zurück") {
session.userData.level.pop();
} else {
session.userData.level.push(results.response.entity);
}
} else if (results.response) {
session.userData.requestedValue = results.response;
}
}
console.log(session.userData.lastType);
if (session.userData.level.length > 0 && (session.userData.lastType == "end" || session.userData.lastType == "number")) {
session.send("Vielen Dank für die Nutzung des Finanzmanagers. Ihr Auftrag wurde ausgeführt.")
session.userData.level = [];
session.userData.lastType = null;
session.endDialog();
} else {
session.replaceDialog('main');
}
}
]);