-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
72 lines (60 loc) · 2.11 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
const express = require('express');
const dotenv = require('dotenv');
const pushover = require('pushover-notifications')
dotenv.config();
const app = express();
const PORT = process.env.PORT || 3000;
app.use(express.json());
app.post('/webhook', (req, res) => {
console.log("Incoming Webhook ...");
if (req.body.secret !== process.env.WEBHOOK_SECRET) {
console.error("Webhook secret doesn't match");
res.status(401).send('Wrong Webhook secret');
return;
}
let msg;
if (req.body.post) {
let wmType, wmTypeVerb;
switch (req.body.post["wm-property"]) {
case "in-reply-to": wmType = "Reply"; wmTypeVerb = "replied"; break;
case "like-of": wmType = "Like"; wmTypeVerb = "liked"; break;
case "repost-of": wmType = "Repost"; wmTypeVerb = "reposted"; break;
case "bookmark-of": wmType = "Bookmark"; wmTypeVerb = "bookmarked"; break;
case "mention-of": wmType = "Mention"; wmTypeVerb = "mentioned"; break;
default: break;
}
msg = {
title: `webmention.io ${wmType}`,
message: `${req.body.post.author.name} ${wmTypeVerb} ${req.body.target}`,
url: req.body.source,
timestamp: new Date(req.body.post.published).getTime()
}
} else if(req.body.deleted) {
msg = {
title: "webmention.io Delete",
message: `Webmention on ${req.body.source} for ${req.body.target} deleted`,
url: req.body.source
}
}
if (msg) {
console.log("Pushover message: " + JSON.stringify(msg));
let push = new pushover( {
user: process.env['PUSHOVER_USER'],
token: process.env['PUSHOVER_TOKEN']
});
push.send(msg, function(error, result) {
if (error) {
console.error("Pushover failed: " + error)
}
console.log("Pushover send: " + result)
})
res.status(200).send('OK');
} else {
console.error("Wrong content: " + JSON.stringify(req.body));
res.status(422).send("Unprocessable content: Neither post or deleted webmention");
}
});
// ---------------------------------------------------------------
app.listen(PORT, () => {
console.log(`Webhook receiver listening on port ${PORT}`);
});