-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfreshservice.js
197 lines (163 loc) · 5.49 KB
/
freshservice.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
var express = require('express');
var router = express.Router();
const axios = require('axios').default;
const _ = require('lodash');
const COMMENTER_ID = 123456789;
// Process all the webhooks from PagerTree and update Freshservice tickets accordingly
router.post('/', function(req, res, next) {
pagertree_payload = req.body;
console.log(pagertree_payload);
switch(req.body.type){
case 'alert.open':
processAlertOpen(req, res, next);
break;
case 'alert.acknowledged':
processAlertAcknowledged(req, res, next);
break;
case 'alert.resolved':
processAlertResolved(req, res, next);
break;
default:
console.log('Unknown Event Type');
break;
}
});
// Post a comment to a Freshservice ticket when an alert is opened in PagerTree
async function processAlertOpen(req, res, next){
res.send('OK');
console.log('Processing Alert Open');
let freshserviceTicketId = _.get(req.body, 'data.s_log.content.params.freshdesk_webhook.ticket_id');
if(!freshserviceTicketId){
console.log('No Freshservice Ticket ID found in PagerTree payload');
return;
}
let freshserviceTicketIdTokens = freshserviceTicketId.split('-');
let freshserviceTicketIdNumber = _.last(freshserviceTicketIdTokens);
let freshserviceApiUrl = `https://demo.freshservice.com/api/v2/tickets/${freshserviceTicketIdNumber}`;
try {
freshserviceResponse = await axios({
method: 'post',
url: freshserviceApiUrl,
data: {
"body": "Alert Opened in PagerTree",
"private": true,
"user_id": 1
},
auth: {
username: process.env.FRESHSERVICE_API_KEY,
password: ''
}
});
console.log(freshserviceResponse.data);
} catch (error) {
console.log(error);
}
}
// Add a note on the Freshservice ticket when an alert is acknowledged in PagerTree
async function processAlertAcknowledged(req, res, next){
res.send('OK');
// find out who acknowledged the alert in pagertree
let pagertreeAcknowledgedBy = req.body.data.d_user;
let pagertreeAcknowledgedByName = pagertreeAcknowledgedBy.name;
let pagertreeAcknowledgedByEmail = _.first(pagertreeAcknowledgedBy.emails).email;
// map that to a user in freshservice
// https://api.freshservice.com/#filter_agents
let freshserviceApiUrl = `https://demo.freshservice.com/api/v2/agents?query="email:'${pagertreeAcknowledgedByEmail}'"`;
let freshserviceResponse = null;
try {
freshserviceResponse = await axios({
method: 'get',
url: freshserviceApiUrl,
auth: {
username: process.env.FRESHSERVICE_API_KEY,
password: ''
}
});
} catch (error) {
console.log(error);
return;
}
let freshserviceAgent = _.get(freshserviceResponse, 'data.agents.0');
if(!freshserviceAgent){
console.log('No Freshservice Agent found for PagerTree Acknowledged By');
return;
}
// create a note in freshservice
// https://api.freshservice.com/#create_note
let freshserviceTicketId = _.get(req.body, 'data.s_log.content.params.freshdesk_webhook.ticket_id');
if(!freshserviceTicketId){
console.log('No Freshservice Ticket ID found in PagerTree payload');
return;
}
let freshserviceTicketIdTokens = freshserviceTicketId.split('-');
let freshserviceTicketIdNumber = _.last(freshserviceTicketIdTokens);
freshserviceApiUrl = `https://demo.freshservice.com/api/v2/tickets/${freshserviceTicketIdNumber}?bypass_mandatory=true`;
try {
freshserviceResponse = await axios({
method: 'put',
url: freshserviceApiUrl,
data: {
"responder_id": freshserviceAgent.id
},
auth: {
username: process.env.FRESHSERVICE_API_KEY,
password: ''
}
});
console.log(freshserviceResponse.data);
} catch (error) {
console.log(error);
}
freshserviceApiUrl = `https://demo.freshservice.com/api/v2/tickets/${freshserviceTicketIdNumber}/notes`;
try {
freshserviceResponse = await axios({
method: 'post',
url: freshserviceApiUrl,
data: {
"body": `Alert Acknowledged by ${pagertreeAcknowledgedByName} in PagerTree`,
"private": true,
"user_id": COMMENTER_ID
},
auth: {
username: process.env.FRESHSERVICE_API_KEY,
password: ''
}
});
console.log(freshserviceResponse.data);
} catch (error) {
console.log(error);
}
}
// Update the Freshservice ticket status to resolved when an alert is resolved in PagerTree
async function processAlertResolved(req, res, next){
res.send('OK');
// set ticket status to resolved in freshservice
// https://api.freshservice.com/#update_ticket_priority
console.log('Processing Alert Resolved');
let freshserviceTicketId = _.get(req.body, 'data.s_log.content.params.freshdesk_webhook.ticket_id');
if(!freshserviceTicketId){
console.log('No Freshservice Ticket ID found in PagerTree payload');
res.send('OK');
return;
}
let freshserviceTicketIdTokens = freshserviceTicketId.split('-');
let freshserviceTicketIdNumber = _.last(freshserviceTicketIdTokens);
freshserviceApiUrl = `https://demo.freshservice.com/api/v2/tickets/${freshserviceTicketIdNumber}?bypass_mandatory=true`;
try {
freshserviceResponse = await axios({
method: 'put',
url: freshserviceApiUrl,
data: {
"status": 4
},
auth: {
username: process.env.FRESHSERVICE_API_KEY,
password: ''
}
});
console.log(freshserviceResponse.data);
} catch (error) {
console.log(error);
}
}
module.exports = router;