-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
208 lines (168 loc) · 5.87 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
import express from 'express';
import crypto from 'crypto';
import bodyParser from 'body-parser';
import zipcode from 'zipcode';
import request from "request";
// Watson Work Services URL
const watsonWork = "https://api.watsonwork.ibm.com";
//
const responses=["It is certain",
"It is decidedly so",
"Without a doubt",
"Yes, definitely",
"You may rely on it",
"As I see it, yes",
"Most likely",
"Yes",
"Signs point to yes",
"Reply hazy try again",
"Ask again later",
"Better not tell you now",
"Concentrate and ask again",
"Don't count on it",
"My reply is no",
"My sources say no",
"Outlook not so good",
"Very doubtful",
"Ask Anton",
"Visit Cork, I hear its lovely this time of year!"]
// Application Id, obtained from registering the application at https://developer.watsonwork.ibm.com
const appId = "2ca25542-be23-4ae6-852d-98e60191f487";
//Application secret. Obtained from registration of application.
const appSecret = "szr274khnvkbeccuyiub0xki98wrt8s";
//Webhook secret. Obtained from registration of a webhook.
const webhookSecret = "l1ojkhio8fzw2fivutju2n8yq2t66zxk";
// Keyword to "listen" for when receiving outbound webhook calls.
const webhookKeyword = "@magic8ball";
const failMessage =
`magic 8 ball fell and broke`;
const app = express();
// Send 200 and empty body for requests that won't be processed.
const ignoreMessage = (res) => {
res.status(200).end();
}
// Process webhook verification requests
const verifyCallback = (req, res) => {
console.log("Verifying challenge");
console.log(req.body);
const bodyToSend = {
response: req.body.challenge
};
// Create a HMAC-SHA256 hash of the recieved body, using the webhook secret
// as the key, to confirm webhook endpoint.
const hashToSend =
crypto.createHmac('sha256', webhookSecret)
.update(JSON.stringify(bodyToSend))
.digest('hex');
res.set('X-OUTBOUND-TOKEN', hashToSend);
res.send(bodyToSend).end();
};
// Validate events coming through and process only message-created or verification events.
const validateEvent = (req, res, next) => {
// Event to Event Handler mapping
const processEvent = {
'verification': verifyCallback,
'message-created': () => next()
};
// If event exists in processEvent, execute handler. If not, ignore message.
return (processEvent[req.body.type]) ?
processEvent[req.body.type](req, res) : ignoreMessage(res);
};
// Authenticate Application
const authenticateApp = (callback) => {
// Authentication API
const authenticationAPI = 'oauth/token';
const authenticationOptions = {
"method": "POST",
"url": `${watsonWork}/${authenticationAPI}`,
"auth": {
"user": appId,
"pass": appSecret
},
"form": {
"grant_type": "client_credentials"
}
};
request(authenticationOptions, (err, response, body) => {
// If can't authenticate just return
if (response.statusCode != 200) {
console.log("Error authentication application. Exiting.");
process.exit(1);
}
callback(JSON.parse(body).access_token);
});
};
// Send message to Watson Workspace
const sendMessage = (spaceId, title, message, state) => {
// Spaces API
const spacesAPI = `v1/spaces/${spaceId}/messages`;
// Photos API
const photosAPI = `photos`;
let colorHex = '#1DA1F2';
// Format for sending messages to Workspace
const messageData = {
type: "appMessage",
version: 1.0,
annotations: [
{
type: "generic",
version: 1.0,
color: colorHex,
title: title,
text: message
}
]
};
// Authenticate application and send message.
authenticateApp( (jwt) => {
const sendMessageOptions = {
"method": "POST",
"url": `${watsonWork}/${spacesAPI}`,
"headers": {
"Authorization": `Bearer ${jwt}`
},
"json": messageData
};
request(sendMessageOptions, (err, response, body) => {
if(response.statusCode != 201) {
console.log("Error posting newrelic information.");
console.log(response.statusCode);
console.log(err);
}
});
});
};
// Ensure we can parse JSON when listening to requests
app.use(bodyParser.json());
app.get('/', (req, res) => {
res.send('IBM Watson Workspace Integration for magic 8 ball is alive and happy!');
});
// This is callback URI that Watson Workspace will call when there's a new message created
app.post('/webhook', validateEvent, (req, res) => {
// Check if the first part of the message is '@magic8ball'.
// This lets us "listen" for the '@magic8ball' keyword.
if (req.body.content.indexOf(webhookKeyword) != 0) {
ignoreMessage(res);
return;
}
// Send status back to Watson Work to confirm receipt of message
res.status(200).end();
// Id of space where outbound event originated from.
const spaceId = req.body.spaceId;
// Parse newrelic query from message body.
// Expected format: <keyword>
sendMessage(spaceId,'Hmmm ..', responses[Math.floor(Math.random()*responses.length)]);
});
app.post('/alert/:spaceId', (req, res) => {
console.log(JSON.stringify(req.body));
// Send Respone back to New Relic.
res.status(200).end();
// TODO: Check for precence of condition_name
const targets = req.body.targets;
sendMessage(req.params.spaceId,'Incident '+req.body.current_state+' '+targets[0].name+' '+req.body.condition_name,req.body.details+'\n'+'Link: '+'[Incident '+req.body.incident_id+']('+req.body.incident_url+')',req.body.current_state);
});
console.log('VERSION: 1.00');
// Kickoff the main process to listen to incoming requests
app.listen(process.env.PORT || 3000, () => {
console.log('NewRelic app is listening on the port');
});