This repository has been archived by the owner on Jun 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.mjs
64 lines (56 loc) · 1.47 KB
/
index.mjs
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
import express from "express";
import { client } from "./matrix.mjs";
import fetch from "node-fetch";
import { ticketToBody } from "./helpers.mjs";
import * as crypto from "crypto";
import { store } from "./store.mjs";
global.fetch = fetch;
const port = 3000;
const app = express();
const router = express.Router();
router
.use(
express.json({
verify(req, res, buf, encoding) {
req.rawBody = buf;
},
})
)
.use((req, res, next) => {
const hmac = crypto.createHmac('sha1', store.taiga.webhookSecret);
hmac.write(req.rawBody);
hmac.end();
const hash = hmac.read().toString('hex');
if (req.headers['x-taiga-webhook-signature'] !== hash) {
res.status(403);
res.send({ message: 'unauthorized' });
return;
}
next();
});
app.use('/api/v1', router);
router.post('/hook/:roomId/event', async (req, res) => {
const { roomId } = req.params;
const ticket = req.body;
if (!ticket) {
res.status(400);
res.send({ message: 'event is missing data' });
return;
}
try {
const body = ticketToBody(ticket);
if (!body) throw new Error('Wrong ticket action');
await client.sendMessage(roomId, {
body,
"msgtype": "m.notice",
});
res.status(200);
res.send({ message: 'successfully sent matrix event' });
} catch (e) {
res.status(500);
res.send({ message: 'failed to sent matrix event' });
console.error(e);
}
});
app.listen(port);
console.log('express server started');