Newer
Older
wizarr-notify / index.js
const express = require('express');
const Pushover = require('pushover-notifications');
const mustache = require('mustache');
const fs = require('node:fs');
const path = require('node:path');

/*
{"event":"user_invited","data":{"auth":"WHo3P_ZCFdnPyWFKJvKR","code":"RLMUQK","created":"2024-11-21 21:30:24.728194","email":"bavis.mark+plex2@gmail.com","expires":null,"id":3,"token":"449061671","username":"bavis.ma"}}
{"event":"user_deleted","data":{"auth":"WHo3P_ZCFdnPyWFKJvKR","code":"RLMUQK","created":"2024-11-21 21:30:24.728194","email":"bavis.mark+plex2@gmail.com","expires":null,"id":3,"token":"449061671","username":"bavis.ma"}}
{"event":"invitation_created","data":{"allow_download":true,"code":"RLMUQK","created":"2024-11-21 21:11:29.928042","duration":null,"expires":"2024-11-22 21:11:29.928034","hide_user":true,"id":1,"live_tv":true,"plex_allow_sync":false,"sessions":0,"specific_libraries":[""],"unlimited":false,"used":false,"used_at":null,"used_by":null}}
{"event":"invitation_deleted","data":{"allow_download":true,"code":"RLMUQK","created":"2024-11-21 21:11:29.928042","duration":null,"expires":"2024-11-22 21:11:29.928034","hide_user":true,"id":1,"live_tv":true,"plex_allow_sync":false,"sessions":"0","specific_libraries":"","unlimited":false,"used":true,"used_at":"2024-11-21 13:30:35.743697","used_by":null}}
*/

const pushover = new Pushover(
    {
        user: process.env['PUSHOVER_USER'],
        token: process.env['PUSHOVER_TOKEN'],
    }
);

const notify_templates = new Map();

const templatesStat = fs.statSync("templates", {throwIfNoEntry: false});
if (!templatesStat) fs.mkdirSync("templates");

if (fs.readdirSync("templates").length == 0)
{
    for (let filename of fs.readdirSync("default_templates", {encoding:'utf8'}))
    {
        fs.copyFileSync(path.join("default_templates", filename), path.join("templates", filename));
    }
}

for (let filename of fs.readdirSync("templates", {encoding:'utf8'}))
{
    const is_comment = l => l.trim().startsWith("//");
    if (path.extname(filename) != '.json') continue;

    let lines = fs.readFileSync(path.join("templates", filename), {'encoding':'utf8'}).split(/[\r\n]/);
    if (lines.some(l => is_comment(l) && l.indexOf(":ignore") != -1)) continue;
    lines = lines.filter(l => !is_comment(l));

    notify_templates.set(path.parse(filename).name, lines.join("\n"));
}

console.log("Listening for: " + Array.from(notify_templates.keys()).join(","));

const app = express();



app.use(express.json());

app.post('/', (req, res) =>
{
    if ('event' in req.body)
    {
        console.log(JSON.stringify(req.body));

        if (notify_templates.has(req.body.event))
        {        
            const template_output = JSON.parse(mustache.render(notify_templates.get(req.body.event), {
                ...req.body.data,
                user: `${req.body.data.username} (${req.body.data.email})`
            }));

            if ('title' in template_output && 'message' in template_output)
            {
                pushover.send({
                    title: template_output.title,
                    message: template_output.message
                });
            }
        }
    }
    res.end();
})

app.use((req, res) => res.status(404).end());

app.listen(80);