-
Notifications
You must be signed in to change notification settings - Fork 63
/
app.js
235 lines (204 loc) · 9.21 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/* jshint esversion: 6 */
require('dotenv').config();
const restify = require('restify');
const fs = require('fs');
const builder = require('botbuilder');
const ticketsApi = require('./ticketsApi');
const azureSearch = require('./azureSearchApiClient');
const listenPort = process.env.port || process.env.PORT || 3978;
const ticketSubmissionUrl = process.env.TICKET_SUBMISSION_URL || `http://localhost:${listenPort}`;
const azureSearchQuery = azureSearch({
searchName: process.env.AZURE_SEARCH_ACCOUNT,
indexName: process.env.AZURE_SEARCH_INDEX,
searchKey: process.env.AZURE_SEARCH_KEY
});
// Setup Restify Server
const server = restify.createServer();
server.listen(listenPort, () => {
console.log('%s listening to %s', server.name, server.url);
});
// Setup body parser and sample tickets api
server.use(restify.bodyParser());
server.post('/api/tickets', ticketsApi);
// Create chat connector for communicating with the Bot Framework Service
var connector = new builder.ChatConnector({
appId: process.env.MICROSOFT_APP_ID,
appPassword: process.env.MICROSOFT_APP_PASSWORD
});
// Listen for messages from users
server.post('/api/messages', connector.listen());
var bot = new builder.UniversalBot(connector, (session, args, next) => {
session.endDialog(`I'm sorry, I did not understand '${session.message.text}'.\nType 'help' to know more about me :)`);
});
var luisRecognizer = new builder.LuisRecognizer(process.env.LUIS_MODEL_URL).onEnabled(function (context, callback) {
var enabled = context.dialogStack().length === 0;
callback(null, enabled);
});
bot.recognizer(luisRecognizer);
bot.dialog('Help',
(session, args, next) => {
session.endDialog(`I'm the help desk bot and I can help you create a ticket or explore the knowledge base.\n` +
`You can tell me things like _I need to reset my password_ or _explore hardware articles_.`);
}
).triggerAction({
matches: 'Help'
});
bot.dialog('SubmitTicket', [
(session, args, next) => {
var category = builder.EntityRecognizer.findEntity(args.intent.entities, 'category');
var severity = builder.EntityRecognizer.findEntity(args.intent.entities, 'severity');
if (category && category.resolution.values.length > 0) {
session.dialogData.category = category.resolution.values[0];
}
if (severity && severity.resolution.values.length > 0) {
session.dialogData.severity = severity.resolution.values[0];
}
session.dialogData.description = session.message.text;
if (!session.dialogData.severity) {
var choices = ['high', 'normal', 'low'];
builder.Prompts.choice(session, 'Which is the severity of this problem?', choices, { listStyle: builder.ListStyle.button });
} else {
next();
}
},
(session, result, next) => {
if (!session.dialogData.severity) {
session.dialogData.severity = result.response.entity;
}
if (!session.dialogData.category) {
builder.Prompts.text(session, 'Which would be the category for this ticket (software, hardware, networking, security or other)?');
} else {
next();
}
},
(session, result, next) => {
if (!session.dialogData.category) {
session.dialogData.category = result.response;
}
var message = `Great! I'm going to create a "${session.dialogData.severity}" severity ticket in the "${session.dialogData.category}" category. ` +
`The description I will use is "${session.dialogData.description}". Can you please confirm that this information is correct?`;
builder.Prompts.confirm(session, message, { listStyle: builder.ListStyle.button });
},
(session, result, next) => {
if (result.response) {
var data = {
category: session.dialogData.category,
severity: session.dialogData.severity,
description: session.dialogData.description,
};
const client = restify.createJsonClient({ url: ticketSubmissionUrl });
client.post('/api/tickets', data, (err, request, response, ticketId) => {
if (err || ticketId == -1) {
session.send('Ooops! Something went wrong while I was saving your ticket. Please try again later.');
} else {
session.send(new builder.Message(session).addAttachment({
contentType: "application/vnd.microsoft.card.adaptive",
content: createCard(ticketId, data)
}));
}
session.endDialog();
});
} else {
session.endDialog('Ok. The ticket was not created. You can start again if you want.');
}
}
]).triggerAction({
matches: 'SubmitTicket'
});
const createCard = (ticketId, data) => {
var cardTxt = fs.readFileSync('./cards/ticket.json', 'UTF-8');
cardTxt = cardTxt.replace(/{ticketId}/g, ticketId)
.replace(/{severity}/g, data.severity)
.replace(/{category}/g, data.category)
.replace(/{description}/g, data.description);
return JSON.parse(cardTxt);
};
bot.dialog('ExploreKnowledgeBase', [
(session, args, next) => {
var category = builder.EntityRecognizer.findEntity(args.intent.entities, 'category');
if (!category) {
// retrieve facets
azureSearchQuery('facet=category', (error, result) => {
if (error) {
session.endDialog('Ooops! Something went wrong while contacting Azure Search. Please try again later.');
} else {
var choices = result['@search.facets'].category.map(item=> `${item.value} (${item.count})`);
builder.Prompts.choice(session, 'Let\'s see if I can find something in the knowledge base for you. Which category is your question about?', choices, { listStyle: builder.ListStyle.button });
}
});
} else {
if (!session.dialogData.category) {
session.dialogData.category = category.entity;
}
next();
}
},
(session, args) => {
var category;
if (session.dialogData.category) {
category = session.dialogData.category;
} else {
category = args.response.entity.replace(/\s\([^)]*\)/,'');
}
// search by category
azureSearchQuery('$filter=' + encodeURIComponent(`category eq '${category}'`), (error, result) => {
if (error) {
session.endDialog('Ooops! Something went wrong while contacting Azure Search. Please try again later.');
} else {
session.replaceDialog('ShowKBResults', { result, originalText: category });
}
});
}
]).triggerAction({
matches: 'ExploreKnowledgeBase'
});
bot.dialog('DetailsOf', [
(session, args) => {
var title = session.message.text.substring('show me the article '.length);
azureSearchQuery('$filter=' + encodeURIComponent(`title eq '${title}'`), (error, result) => {
if (error || !result.value[0]) {
session.endDialog('Sorry, I could not find that article.');
} else {
session.endDialog(result.value[0].text);
}
});
}
]).triggerAction({
matches: /^show me the article (.*)/i
});
bot.dialog('SearchKB', [
(session) => {
session.sendTyping();
azureSearchQuery(`search=${encodeURIComponent(session.message.text.substring('search about '.length))}`, (err, result) => {
if (err) {
session.send('Ooops! Something went wrong while contacting Azure Search. Please try again later.');
return;
}
session.replaceDialog('ShowKBResults', { result, originalText: session.message.text });
});
}
])
.triggerAction({
matches: /^search about (.*)/i
});
bot.dialog('ShowKBResults', [
(session, args) => {
if (args.result.value.length > 0) {
var msg = new builder.Message(session).attachmentLayout(builder.AttachmentLayout.carousel);
args.result.value.forEach((faq, i) => {
msg.addAttachment(
new builder.ThumbnailCard(session)
.title(faq.title)
.subtitle(`Category: ${faq.category} | Search Score: ${faq['@search.score']}`)
.text(faq.text.substring(0, Math.min(faq.text.length, 50) + '...'))
.images([builder.CardImage.create(session, 'https://raw.githubusercontent.com/GeekTrainer/help-desk-bot-lab/master/assets/botimages/head-smiling-medium.png')])
.buttons([{ title: 'More details', value: `show me the article ${faq.title}`, type: 'postBack' }])
);
});
session.send(`These are some articles I\'ve found in the knowledge base for _'${args.originalText}'_, click **More details** to read the full article:`);
session.endDialog(msg);
} else {
session.endDialog(`Sorry, I could not find any results in the knowledge base for _'${args.originalText}'_`);
}
}
]);