-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
95 lines (78 loc) · 3.74 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
/* jshint esversion: 6 */
"use strict";
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.sendTyping();
session.send('Hi! I\'m the help desk bot and I can help you create a ticket.');
builder.Prompts.text(session, 'First, please briefly describe your problem to me.');
},
(session, result, next) => {
session.dialogData.description = result.response;
var choices = ['high', 'normal', 'low'];
builder.Prompts.choice(session, 'Which is the severity of this problem?', choices, { listStyle: builder.ListStyle.button });
},
(session, result, next) => {
session.dialogData.severity = result.response.entity;
builder.Prompts.text(session, 'Which would be the category for this ticket (software, hardware, networking, security or other)?');
},
(session, result, next) => {
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) => {
session.sendTyping();// send 'typing' to user
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('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.');
}
}
]);
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);
};