-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
73 lines (61 loc) · 2.17 KB
/
index.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
var express = require('express');
var axios = require('axios');
var bodyParser = require('body-parser')
var app = express();
app.use(bodyParser.text());
function handleNotification(notification) {
switch(notification["operation"]) {
case "create":
case "update":
// Handle notification content here!
console.log(JSON.stringify(notification));
break;
case "ping":
console.log("Received ping from webhook.");
break;
}
// Only return false if you failed to process this notification and you want the webhook to retry.
return true;
}
app.post('/webhook', function(req, res) {
var request;
try {
request = JSON.parse(req.body);
} catch (err) {
res.status(400).send("Could not parse JSON!");
return;
}
switch(request["Type"]) {
case "SubscriptionConfirmation":
console.log("SubscriptionConfirmation received, need to make web request onwards to: " + request["SubscribeURL"]);
axios.get(request["SubscribeURL"])
.then(function (response) {
console.log("Subscription confirmed!");
res.status(200).send("Subscribed!");
})
.catch(function (error) {
res.status(500).send("Error making webhook call! " + error);
});
break;
case "Notification":
console.log("Notification received!");
var body;
try {
body = JSON.parse(request['Message'])
} catch (err) {
res.status(400).send("Could not parse internal JSON!");
return;
}
if (handleNotification(body)) {
res.status(200).send("Processed!");
} else {
res.status(500).send("Unknown error! Something went wrong, retry if you want to!");
}
break;
default:
console.log("Unknown type received: " + request["Type"]);
res.status(200).send("Unknown type, ignored!");
}
});
app.listen(3000);
console.log("Server running...");