-
Notifications
You must be signed in to change notification settings - Fork 0
/
obs.js
97 lines (90 loc) · 3.31 KB
/
obs.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
function obsFunc(app) {
const express = require('express')
const axios = require('axios')
const OBSWebSocket = require('obs-websocket-js')
const fs = require('fs')
const obs = new OBSWebSocket()
let webhooks = require('./obs-webhooks.json')
let events = require('./obs-events.json')
for (item in events.subscribed) {
obs.on(events.subscribed[item], onEvent(events.subscribed[item]))
}
app.post('/obs/sendRequest/:request', (req, resp) => {
obs.send(req.params.request, req.body).then(data => {
resp.send(JSON.stringify(data, null, 4))
}).catch(err => {
resp.status(400)
resp.send(JSON.stringify(err, null, 4))
})
})
app.post('/obs/addWebhook', (req, resp) => {
if (!req.body.url) {
resp.status(400)
resp.send('URL not sent.')
return
} else if (!req.body.event) {
resp.status(400)
resp.send('Event not sent.')
return
}
for (item in webhooks.subscribed) {
if (webhooks.subscribed[item].url == req.body.url && webhooks.subscribed[item].event == req.body.event) {
resp.status(400)
resp.send('Webhook already exists.')
return
}
}
webhooks.subscribed.push({
url: req.body.url,
event: req.body.event
})
if (!events.subscribed.includes(req.body.event)) {
events.subscribed.push(
req.body.event
)
fs.writeFileSync('obs-events.json', JSON.stringify(events, null, 4))
obs.on(req.body.event, onEvent(req.body.event))
}
fs.writeFileSync('obs-webhooks.json', JSON.stringify(webhooks, null, 4))
resp.send('Webhook set!')
})
app.post('/obs/deleteWebhook', (req, resp) => {
if (!req.body.url) {
resp.status(400)
resp.send('URL not sent.')
return
} else if (!req.body.event) {
resp.status(400)
resp.send('Event not sent.')
return
}
for (item in webhooks.subscribed) {
if (webhooks.subscribed[item].url == req.body.url && webhooks.subscribed[item].event == req.body.event) {
webhooks.subscribed.splice(item, 1)
fs.writeFileSync('obs-webhooks.json', JSON.stringify(webhooks, null, 4))
resp.send('Webhook removed!')
for (microItem in webhooks.subscribed) {
if (webhooks.subscribed[microItem].event == req.body.event) {
return
}
}
events.subscribed.splice(events.subscribed.indexOf(req.body.event), 1)
fs.writeFileSync('obs-events.json', JSON.stringify(events, null, 4))
return
}
}
resp.status(400)
resp.send('Webhook not found.')
})
function onEvent(event) {
return (data) => {
for (microItem in webhooks.subscribed) {
if (webhooks.subscribed[microItem].event == event) {
axios.post(webhooks.subscribed[microItem].url, data)
}
}
}
}
return obs.connect({ address: '127.0.0.1:4444' })
}
module.exports = obsFunc