ts-notify-pushover/index.js

64 lines
1.6 KiB
JavaScript

const io = require('socket.io-client');
const push = require('pushover-notifications');
const util = require('util');
const fs = require('fs');
const configjson = require('./config.example.json');
const ts_notify = configjson["ts-notify"];
var p = new push({
user: configjson.pushover.user,
token: configjson.pushover.token
});
var address = "http://" + ts_notify.address + ":" + ts_notify.port;
var socket = io(address);
socket.on('connect', function() {
util.log("Connected to " + address);
socket.emit('apiaccess', ts_notify.apikey);
});
socket.on('event', function(evt) {
var friends = [];
if(configjson.follow_file != undefined || fs.existsSync(configjson.follow_file)) {
try {
var follow_file = fs.readFileSync(configjson.follow_file, {encoding: "UTF-8"});
friends = follow_file.trim().split('\n');
} catch(e) {
util.log("Error while reading friends file: " + err.message);
return;
}
}
if(friends.indexOf(evt.uid) != -1) {
util.log(evt.name + " " + evt.status);
var message = evt.name + " " + evt.status;
if (ts_notify.friendly_name != undefined || ts_notify.friendly_name != "") {
if (evt.status == "connected") {
message += " to ";
} else if (evt.status == "disconnected") {
message += " from ";
} else {
message += " at ";
}
message += ts_notify.friendly_name;
}
p.send({
message: message
}, function(err, result) {
if(err) {
util.log("Error while sending push: " + err.message);
}
});
}
});
function htmlEntities(str) {
return String(str).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(new RegExp('"', 'g'), '&quot;');
}