forked from fuselabs/echobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
183 lines (149 loc) · 5.88 KB
/
server.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
var restify = require('restify');
var builder = require('botbuilder');
var api = require('./apiutil');
var monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
];
// Get secrets from server environment
var botConnectorOptions = {
appId: process.env.BOTFRAMEWORK_APPID,
appPassword: process.env.BOTFRAMEWORK_APPSECRET
//appId: "12345",
//appPassword: "12345"
};
// Create bot
var connector = new builder.ChatConnector(botConnectorOptions);
var bot = new builder.UniversalBot(connector);
//bot.dialog('/', function (session) {
//respond with user's message
// session.send("You said " + session.message.text);
//});
//=========================================================
// Bots Dialogs
//=========================================================
//bot.dialog('/', function (session) {
// session.send("Hello World");
//});
//
// LUIS Integration
//
// Create LUIS recognizer that points at our model and add it as the root '/' dialog for our Cortana Bot.
var model = 'https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/384145fc-8d4b-4e8a-9d1e-796d2f5e86f3?subscription-key=5cee307472f44e8db7e26968ae4a0f8b&verbose=true';
var recognizer = new builder.LuisRecognizer(model);
var dialog = new builder.IntentDialog({ recognizers: [recognizer] });
bot.dialog('/', dialog);
// Add intent handlers
//dialog.matches('KPI', builder.DialogAction.send('You asked about a KPI!'));
dialog.matches('KPI', [
function (session, args, next) {
// Resolve and store any entities passed from LUIS.
var kpi = builder.EntityRecognizer.findEntity(args.entities, 'KPI');
var data = session.dialogData.Data = {
kpi: kpi
};
var date = builder.EntityRecognizer.findEntity(args.entities, 'builtin.datetime.date');
session.dialogData.Data.date = date;
if (session.dialogData.Data.kpi === null)
{
session.send('I know you are asking about a KPI. However I have not yet learnt how to fetch that information!');
}
else if (session.dialogData.Data.date === null || session.dialogData.Data.date === undefined)
{
session.send('I know you are asking about a KPI. However you also need to specify a time period. Please specify a time period and try again');
}
else
{
var kpi = session.dialogData.Data.kpi.entity;
var dateval = session.dialogData.Data.date.resolution.date;
var ss = dateval.split("-");
var year = ss[0];
var monthNum = Number(ss[1]);
if (year === null || monthNum === null)
{
session.send('I know you are asking about a KPI. However you also need to specify a valid time period. Please specify a time period and try again');
}
var month = monthNames[monthNum-1];
var formattedDate = month + ':' + 'FY' + year;
session.send('You asked about ' + kpi + ' for ' + formattedDate + '. I am fetching that information...');
api.getKpis(session, kpi, formattedDate);
}
},
function (session, results) {
session.send('I am working on this ...');
var query = 'Cost';
if (session.dialogData.Data.kpi === null)
{
session.send('I know you are asking about a KPI. However I have not yet learnt how to fetch that information!');
}
else
{
var kpi = session.dialogData.Data.kpi.entity;
session.send('You asked about ' + kpi + '. I am fetching that information...');
api.getKpis(session, kpi);
/*if (kpi === 'cost variance')
{
query = 'Cost';
api.getKpis(session, query, kpi);
}
else if (kpi === 'variable cost')
{
query = '% Variable';
api.getKpis(session, query, kpi);
}
else if ((kpi === 'capital spend') || (kpi === 'capital cost'))
{
query = '% Capital Spend';
api.getKpis(session, query, kpi);
}
else
{
session.send('I know you are asking about a KPI. However I have not yet learnt how to fetch that information!');
}*/
}
}
]);
/*bot.dialog('/costcenter', [
function (session) {
builder.Prompts.text(session, "I would be happy to get that for you. Please wait while I ");
},
function (session, results) {
session.send("I will get it for you now and remember that Cost Center for future reviews.");
session.endDialogWithResult(results);
}
]);*/
//function (session) {
//session.send("Please wait while I retrieve your KPI.");
//api.getKpis(session, 'Cost');
// api.getKpis(session, 'Cost YTD');
// api.getKpis(session, 'Current FY Plan Remaining');
// api.getKpis(session, '% Variable');
// api.getKpis(session, '% Capital Spend');
//});
dialog.matches('None', builder.DialogAction.send("I'm sorry I didn't understand. I can only fetch KPIs!"));
dialog.onDefault(builder.DialogAction.send("I'm sorry I didn't understand. I can only fetch KPIs!"));
// Setup Restify Server
var server = restify.createServer();
// Handle Bot Framework messages
server.post('/api/messages', connector.listen());
// Serve a static web page
server.get(/.*/, restify.serveStatic({
'directory': '.',
'default': 'index.html'
}));
server.post('/api/notify', function (req, res) {
// Process posted notification
var address = JSON.parse('{"id":"6ba09821805c443ba54aa27c082b7ee8","channelId":"slack","user":{"id":"U4FPSE0UC:T4FLMJ675","name":"hshah"},"conversation":{"isGroup":false,"id":"B4F2ZNURX:T4FLMJ675:D4F24VB88"},"bot":{"id":"B4F2ZNURX:T4FLMJ675","name":"apptiobot"},"serviceUrl":"https://slack.botframework.com","useAuth":true}');
var notification ='March data has been loaded and verified in Apptio. You can begin your financial review!';
// Send notification as a proactive message
var msg = new builder.Message()
.address(address)
.text(notification);
bot.send(msg, function (err) {
// Return success/failure
res.status(err ? 500 : 200);
res.end();
});
});
server.listen(process.env.port || 3978, function () {
console.log('%s listening to %s', server.name, server.url);
});