forked from MissionMarsFourthHorizon/operation-max
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
132 lines (110 loc) · 4.99 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
/* jshint esversion: 6 */
require('dotenv').config();
const restify = require('restify');
const fs = require('fs');
const builder = require('botbuilder');
const ticketsApi = require('./ticketsApi');
const listenPort = process.env.port || process.env.PORT || 3978;
const ticketSubmissionUrl = process.env.TICKET_SUBMISSION_URL || `http://localhost:${listenPort}`;
// 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.\n` +
`You can tell me things like _I need to reset my password_ or _I cannot print_.`);
session.send('First, please briefly describe your problem to me.');
}
).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);
};