-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebhook.js
70 lines (62 loc) · 2.09 KB
/
webhook.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
var http = require('http');
var config = require('./config');
var events = require('events').EventEmitter;
var secret= config.hookpassword
function webhook() {
events.EventEmitter.call(this);
var that = this;
var server = http.createServer(handleRequest);
server.listen(config.hookport);
function handleRequest(req, res) {
if(req.url == '/webhook?secret=' + secret){
var fullBody = ""
req.on('data', function(chunk) {
// append the current chunk of data to the fullBody variable
fullBody += chunk.toString();
});
// var decodedBody = querystring.parse(fullBody);
req.on('end', function() {
if (fullBody.length > 5) {
console.log("Incoming webhook request")
console.log(fullBody)
parser(JSON.parse(fullBody));
}
res.end("Gotcha");
});
}else{
res.end("Denied!")
}
}
function parser(payload) {
that.emit('data', payload);
console.log(payload);
if (payload.eventId == 'mediaItem.live') {
that.emit('live', payload);
console.log("New live media with id " + payload.payload.id);
}
if (payload.eventId == 'mediaItem.showOver') {
that.emit('showOver', payload);
that.emit('end' + payload.payload.id, payload);
console.log("Show now over, id " + payload.payload.id);
}
if (payload.eventId == 'mediaItem.notLive') {
that.emit('notLive', payload);
that.emit('end' + payload.payload.id, payload);
console.log("Media item listed as not live " + payload.payload.id);
}
if (payload.eventId == 'mediaItem.vodAvailable') {
that.emit('vod', payload);
console.log("New VOD media with id " + payload.payload.id);
}
if (payload.eventId == 'degradedService.stateChanged') {
that.emit('degradedServiceStateChanged', payload.payload.enabled);
console.log("Degraded service state changed.");
}
if (payload.eventId == 'test') {
that.emit('test', payload);
console.log("Test response with message " + payload.payload.info);
}
}
}
webhook.prototype.__proto__ = events.EventEmitter.prototype;
module.exports = webhook;