2021-06-20 09:54:50 -04:00
|
|
|
const fs = require('fs');
|
|
|
|
const ini = require('ini');
|
|
|
|
const mastodon = require('mastodon');
|
|
|
|
const mqtt = require('mqtt');
|
|
|
|
const request = require('request');
|
|
|
|
let Parser = require('rss-parser');
|
|
|
|
let parser = new Parser();
|
|
|
|
const xmpp = require('simple-xmpp');
|
|
|
|
|
2021-12-12 09:15:09 -05:00
|
|
|
try {
|
|
|
|
fs.readFileSync('./config.ini', 'utf-8');
|
|
|
|
|
2021-06-20 09:54:50 -04:00
|
|
|
const config = ini.parse(fs.readFileSync('./config.ini', 'utf-8'));
|
|
|
|
const api_pass = config.api_pass;
|
|
|
|
const api_url = config.api_url;
|
|
|
|
const api_user = config.api_user;
|
|
|
|
const app_port = config.app_port;
|
|
|
|
const asterisk_callfrom = config.asterisk_callfrom;
|
2021-12-07 16:17:31 -05:00
|
|
|
const asterisk_callto = config.asterisk_callerto;
|
2021-06-20 09:54:50 -04:00
|
|
|
const asterisk_callerID = config.asterisk_callerID;
|
|
|
|
const asterisk_context = config.asterisk_context;
|
|
|
|
const asterisk_host = config.asterisk_host;
|
|
|
|
const asterisk_password = config.asterisk_password;
|
|
|
|
const asterisk_port = config.asterisk_port;
|
|
|
|
const asterisk_user = config.asterisk_user;
|
|
|
|
const bot_hostname = config.bot_hostname;
|
|
|
|
const bot_jid = config.bot_jid;
|
|
|
|
const bot_password = config.bot_password;
|
2021-12-07 16:17:31 -05:00
|
|
|
const bot_port = config.bot_port;
|
2021-06-20 09:54:50 -04:00
|
|
|
const city_code = config.city_code;
|
|
|
|
const city_name = config.city_name;
|
|
|
|
const gotify_api_key = config.gotify_api_key;
|
|
|
|
const gotify_message = config.gotify_message;
|
|
|
|
const gotify_priority = config.gotify_priority;
|
|
|
|
const gotify_title = config.gotify_title;
|
|
|
|
const gotify_url = config.gotify_url;
|
|
|
|
const mastodon_token = config.mastodon_token;
|
|
|
|
const mastodon_toot = config.mastodon_toot;
|
|
|
|
const mastodon_url = config.mastodon_url;
|
|
|
|
const mqtt_message = config.mqtt_message;
|
|
|
|
const mqtt_password = config.mqtt_password;
|
|
|
|
const mqtt_server = config.mqtt_server;
|
|
|
|
const mqtt_topic = config.mqtt_topic;
|
|
|
|
const mqtt_user = config.mqtt_user;
|
|
|
|
const rpc_url = config.rpc_url;
|
2021-12-07 16:17:31 -05:00
|
|
|
const rss_url = config.rss_url;
|
2022-01-27 10:48:21 -05:00
|
|
|
const tuya_name = config.tuya_name;
|
|
|
|
const tuya_status_url = config.tuya_status_url;
|
|
|
|
const tuya_toggle_url = config.tuya_toggle_url;
|
2021-06-20 09:54:50 -04:00
|
|
|
const user_jid = config.user_jid;
|
|
|
|
const user_name = config.user_name;
|
|
|
|
const vfc_url = config.vfc_url;
|
|
|
|
const weather_api_key = config.weather_api_key;
|
|
|
|
const weather_units = config.weather_units;
|
|
|
|
|
2021-12-07 16:43:46 -05:00
|
|
|
// thx: https://stackoverflow.com/questions/48750528/get-direction-from-degrees
|
|
|
|
function getDirection(angle) {
|
|
|
|
var directions = ['North', 'North-East', 'East', 'South-East', 'South', 'South-West', 'West', 'North-West'];
|
|
|
|
var index = Math.round(((angle %= 360) < 0 ? angle + 360 : angle) / 45) % 8;
|
|
|
|
return directions[index];
|
|
|
|
}
|
|
|
|
|
2021-06-20 09:54:50 -04:00
|
|
|
var ami = new require('asterisk-manager')(asterisk_port, asterisk_host, asterisk_user, asterisk_password, false);
|
|
|
|
let weather_url = `https://api.openweathermap.org/data/2.5/weather?id=${city_code}&appid=${weather_api_key}&units=${weather_units}`
|
|
|
|
var client = mqtt.connect('tcp://' + mqtt_user + ':' + mqtt_password + '@' + mqtt_server);
|
|
|
|
var M = new mastodon({
|
|
|
|
access_token: `${mastodon_token}`,
|
|
|
|
timeout_ms: 60*1000,
|
|
|
|
api_url: `${mastodon_url}`,
|
|
|
|
})
|
|
|
|
// Once we're online, we'll send a message alerting admins
|
|
|
|
xmpp.on('online', function(data, to) {
|
|
|
|
console.log('Connected with JID: ' + data.jid.user);
|
|
|
|
xmpp.send(user_jid, "[ONLINE]", false);
|
|
|
|
client.publish(mqtt_topic, mqtt_message);
|
|
|
|
M.post('statuses', { status: `${mastodon_toot}` });
|
|
|
|
});
|
|
|
|
|
|
|
|
xmpp.on('chat', function(from, message) {
|
|
|
|
|
|
|
|
// HELP
|
|
|
|
if (message === 'help' || message === 'Help') {
|
2021-12-07 16:17:31 -05:00
|
|
|
xmpp.send(from, 'https://wiki.vanderwarker.family/doku.php?id=code:vxmppb:commands');
|
2021-12-07 16:43:46 -05:00
|
|
|
console.log('help');
|
2021-06-20 09:54:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// UNLOCKS
|
|
|
|
else if (message === 'unlocks' || message === 'Unlocks') {
|
|
|
|
var d = new Date();
|
|
|
|
var weekday = new Array(7);
|
|
|
|
weekday[0] = "Sunday";
|
|
|
|
weekday[1] = "Monday";
|
|
|
|
weekday[2] = "Tuesday";
|
|
|
|
weekday[3] = "Wednesday";
|
|
|
|
weekday[4] = "Thursday";
|
|
|
|
weekday[5] = "Friday";
|
|
|
|
weekday[6] = "Saturday";
|
|
|
|
var n = weekday[d.getDay()];
|
|
|
|
|
|
|
|
request(api_url + "?columns=" + n, (error, res, body) => {
|
|
|
|
if (error) {
|
2021-12-07 16:43:46 -05:00
|
|
|
return console.log(error)
|
2021-06-20 09:54:50 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
if (!error && res.statusCode == 200) {
|
|
|
|
var ulb = new RegExp(':(.*)}');
|
|
|
|
var ule = body.match(ulb);
|
|
|
|
const ur = user_name + " has " + ule[1] + " unlocks for " + n;
|
|
|
|
xmpp.send(from, ur);
|
2021-12-07 16:43:46 -05:00
|
|
|
console.log(ur);
|
2021-06-20 09:54:50 -04:00
|
|
|
}
|
|
|
|
if (!error && res.statusCode == 401) {
|
2021-12-07 16:43:46 -05:00
|
|
|
console.log('Invalid credentials');
|
2021-06-20 09:54:50 -04:00
|
|
|
xmpp.send(from, 'Invalid credentials');
|
|
|
|
}
|
|
|
|
if (!error && res.statusCode == 404) {
|
2021-12-07 16:43:46 -05:00
|
|
|
console.log('Record not found');
|
2021-06-20 09:54:50 -04:00
|
|
|
xmpp.send(from, 'Record not found');
|
|
|
|
}
|
|
|
|
if (!error && res.statusCode == 500) {
|
2021-12-07 16:43:46 -05:00
|
|
|
console.log('Server error.');
|
2021-06-20 09:54:50 -04:00
|
|
|
xmpp.send(from, 'Server error. Please ping admin');
|
|
|
|
}
|
|
|
|
});
|
2021-12-07 16:43:46 -05:00
|
|
|
console.log('unlocks');
|
2021-06-20 09:54:50 -04:00
|
|
|
}
|
2022-01-27 10:48:21 -05:00
|
|
|
// Tuya light control
|
|
|
|
else if (message === 'lights' || message === 'Lights') {
|
|
|
|
request(tuya_status_url,(error, res, body) => {
|
|
|
|
if (error) {
|
|
|
|
return console.log(error)
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!error && res.statusCode == 200) {
|
|
|
|
if (res.body === "true"){
|
|
|
|
const stat = tuya_name + ": On";
|
|
|
|
xmpp.send(from, stat);
|
|
|
|
console.log(stat);
|
|
|
|
} else if (res.body === "false") {
|
|
|
|
const stat = tuya_name + ": Off";
|
|
|
|
xmpp.send(from, stat);
|
|
|
|
console.log(stat);
|
|
|
|
} else {
|
|
|
|
xmpp.send(from, "Unknown reply. Please reply ping to reach an admin.");
|
|
|
|
console.log("Tuya API: Unknown reply!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!error && res.statusCode == 401) {
|
|
|
|
console.log('Invalid credentials');
|
|
|
|
xmpp.send(from, 'Invalid credentials');
|
|
|
|
}
|
|
|
|
if (!error && res.statusCode == 404) {
|
|
|
|
console.log('Record not found');
|
|
|
|
xmpp.send(from, 'Record not found');
|
|
|
|
}
|
|
|
|
if (!error && res.statusCode == 500) {
|
|
|
|
console.log('Server error.');
|
|
|
|
xmpp.send(from, 'Server error. Please ping admin');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
console.log('lights status');
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (message === tuya_name + ' on') {
|
|
|
|
request(tuya_toggle_url + "?stat=true&devname=" + tuya_name,(error, res, body) => {
|
|
|
|
if (error) {
|
|
|
|
return console.log(error)
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!error && res.statusCode == 200) {
|
|
|
|
const stat = tuya_name + " has been turned on";
|
|
|
|
xmpp.send(from, stat);
|
|
|
|
console.log(stat);
|
|
|
|
}
|
|
|
|
if (!error && res.statusCode == 401) {
|
|
|
|
console.log('Invalid credentials');
|
|
|
|
xmpp.send(from, 'Invalid credentials');
|
|
|
|
}
|
|
|
|
if (!error && res.statusCode == 404) {
|
|
|
|
console.log('Record not found');
|
|
|
|
xmpp.send(from, 'Record not found');
|
|
|
|
}
|
|
|
|
if (!error && res.statusCode == 500) {
|
|
|
|
console.log('Server error.');
|
|
|
|
xmpp.send(from, 'Server error. Please ping admin');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
console.log('lights on');
|
|
|
|
}
|
|
|
|
else if (message === tuya_name + ' off') {
|
|
|
|
request(tuya_toggle_url + "?stat=false&devname=" + tuya_name,(error, res, body) => {
|
|
|
|
if (error) {
|
|
|
|
return console.log(error)
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!error && res.statusCode == 200) {
|
|
|
|
const stat = tuya_name + " has been turned off";
|
|
|
|
xmpp.send(from, stat);
|
|
|
|
console.log(stat);
|
|
|
|
}
|
|
|
|
if (!error && res.statusCode == 401) {
|
|
|
|
console.log('Invalid credentials');
|
|
|
|
xmpp.send(from, 'Invalid credentials');
|
|
|
|
}
|
|
|
|
if (!error && res.statusCode == 404) {
|
|
|
|
console.log('Record not found');
|
|
|
|
xmpp.send(from, 'Record not found');
|
|
|
|
}
|
|
|
|
if (!error && res.statusCode == 500) {
|
|
|
|
console.log('Server error.');
|
|
|
|
xmpp.send(from, 'Server error. Please ping admin');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
console.log('lights on');
|
|
|
|
}
|
2021-06-20 09:54:50 -04:00
|
|
|
|
2021-12-07 16:17:31 -05:00
|
|
|
// Check VFC balance
|
2021-06-20 09:54:50 -04:00
|
|
|
else if (message === "vfc" || message === "Vfc") {
|
|
|
|
ami.action({
|
|
|
|
'action':'originate',
|
|
|
|
'channel':'SIP/' + asterisk_callfrom,
|
|
|
|
'context': asterisk_context,
|
|
|
|
'callerID' : asterisk_callerID,
|
2021-12-07 16:17:31 -05:00
|
|
|
'exten' : asterisk_callto,
|
2021-06-20 09:54:50 -04:00
|
|
|
'priority' : '1',
|
|
|
|
});
|
|
|
|
request(vfc_url, (error, res, body) => {
|
|
|
|
if (error) {
|
2021-12-07 16:43:46 -05:00
|
|
|
return console.log(error)
|
2021-06-20 09:54:50 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
if (!error && res.statusCode == 200) {
|
|
|
|
const balance = user_name + " has " + res.body + " VFC";
|
|
|
|
xmpp.send(from, balance);
|
2021-12-07 16:43:46 -05:00
|
|
|
console.log(balance);
|
2021-06-20 09:54:50 -04:00
|
|
|
}
|
|
|
|
});
|
2021-12-07 16:43:46 -05:00
|
|
|
console.log('vfc');
|
2021-06-20 09:54:50 -04:00
|
|
|
}
|
|
|
|
|
2021-12-07 16:17:31 -05:00
|
|
|
// Get latest RSS item from feed
|
2021-06-20 09:54:50 -04:00
|
|
|
else if (message === "rss" || message === "Rss") {
|
|
|
|
(async () => {
|
|
|
|
let feed = await parser.parseURL(rss_url);
|
|
|
|
// console.log(feed.title);
|
|
|
|
feed.items.forEach(item => {
|
|
|
|
console.log(item.title + ':' + item.link)
|
|
|
|
xmpp.send(from, item.title)
|
|
|
|
});
|
2021-12-07 16:17:31 -05:00
|
|
|
xmpp.send(from, 'See https://vanderwarker.social/@stephen for full entries.')
|
2021-06-20 09:54:50 -04:00
|
|
|
})();
|
|
|
|
}
|
|
|
|
|
2021-12-07 16:17:31 -05:00
|
|
|
// Ping a user to let them know you would like to communitcate
|
2021-06-20 09:54:50 -04:00
|
|
|
else if (message === "ping" || message === 'Ping') {
|
|
|
|
xmpp.send(from, "Pinging " + user_name + "! Please stand by.");
|
|
|
|
xmpp.send(user_jid, from + " has pinged you!");
|
|
|
|
client.publish(mqtt_topic, from + ' has pinged you!');
|
|
|
|
ami.action({
|
|
|
|
'action':'originate',
|
|
|
|
'channel':'SIP/' + asterisk_callfrom,
|
|
|
|
'context': asterisk_context,
|
|
|
|
'callerID' : asterisk_callerID,
|
|
|
|
'data': 'googletts.agi, \"hello. ' + from + ' has pinged you!\"',
|
|
|
|
'application': 'agi'
|
|
|
|
});
|
2021-12-07 16:43:46 -05:00
|
|
|
console.log('ping');
|
2021-06-20 09:54:50 -04:00
|
|
|
let guf = `${gotify_url}/message?token=${gotify_api_key}&message=${from} ${gotify_message}&title=${gotify_title}&priority=${gotify_priority}`;
|
|
|
|
let gufe = encodeURI(guf);
|
|
|
|
request.post(gufe, function (err, response, body) {
|
|
|
|
if(err){
|
2021-12-07 16:43:46 -05:00
|
|
|
console.log('error:', err);
|
2021-06-20 09:54:50 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-12-07 16:17:31 -05:00
|
|
|
// Check the weather using weatherunderground API
|
2021-06-20 09:54:50 -04:00
|
|
|
else if (message === "weather" || message === 'Weather') {
|
|
|
|
xmpp.send(from, "Checking weather. Please hold...\r\n \r\n");
|
|
|
|
request(weather_url, function (err, response, body) {
|
|
|
|
if(err){
|
2021-12-07 16:43:46 -05:00
|
|
|
console.log('error:', err);
|
2021-06-20 09:54:50 -04:00
|
|
|
} else {
|
|
|
|
let w = JSON.parse(body);
|
|
|
|
let wm = city_name + " Weather: \r\n Tempature: " + w.main.temp + " degrees. \r\n Conditions: " + w.weather[0].main + "\r\n Pressure: " +
|
2021-12-07 16:43:46 -05:00
|
|
|
w.main.pressure + " \r\n Wind Direction: " + getDirection(w.wind.deg) + "\r\n Wind Speed: " + w.wind.speed + " m.p.h. \r\n";
|
2021-06-20 09:54:50 -04:00
|
|
|
let wmc = city_name + " Weather: Tempature: " + w.main.temp + " degrees. Conditions: " + w.weather[0].main + ". Pressure: " +
|
2021-12-07 16:43:46 -05:00
|
|
|
w.main.pressure + ". Wind Direction: " + getDirection(w.wind.deg) + ". Wind Speed: " + w.wind.speed + " miles per hour \r\n";
|
|
|
|
console.log(wm);
|
2021-06-20 09:54:50 -04:00
|
|
|
client.publish('weather/temp', 'w.main.temp');
|
|
|
|
client.publish('weather/conditions', 'w.weather[0].main');
|
|
|
|
client.publish('weather/pressure', 'w.main.pressure');
|
2021-12-07 16:43:46 -05:00
|
|
|
client.publish('weather/wind/direction', 'getDirection(w.wind.deg)');
|
2021-06-20 09:54:50 -04:00
|
|
|
client.publish('weather/wind/speed', 'w.wind.speed');
|
|
|
|
xmpp.send(from, wm);
|
|
|
|
/*
|
|
|
|
ami.action({
|
|
|
|
'action':'originate',
|
|
|
|
'channel':'SIP/' + asterisk_callfrom,
|
|
|
|
'context': asterisk_context,
|
|
|
|
'callerID' : asterisk_callerID,
|
|
|
|
'data': 'googletts.agi,' + wmc,
|
|
|
|
'application': 'agi'
|
|
|
|
});
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
});
|
2021-12-07 16:43:46 -05:00
|
|
|
console.log('weather');
|
2021-06-20 09:54:50 -04:00
|
|
|
}
|
|
|
|
|
2021-12-07 16:43:46 -05:00
|
|
|
// Sends a nice goodbye message
|
|
|
|
else if (message === 'bye' || message === "Bye"){
|
2021-06-20 09:54:50 -04:00
|
|
|
xmpp.send(from, "Bye, have a beautiful time!");
|
2021-12-07 16:43:46 -05:00
|
|
|
console.log('bye');
|
|
|
|
|
|
|
|
}
|
|
|
|
// Shutsdown bot if admin messages "goodbye"
|
|
|
|
else if (message === "goodbye" || from === user_jid){
|
2021-06-20 09:54:50 -04:00
|
|
|
xmpp.send(from, "Alrighty, shutting down. Goodbye!");
|
2021-12-07 16:43:46 -05:00
|
|
|
console.log('Admin shutdown.');
|
|
|
|
process.exit(1);
|
2021-06-20 09:54:50 -04:00
|
|
|
}
|
|
|
|
|
2021-12-07 16:17:31 -05:00
|
|
|
// Get users vCard (WIP)
|
2021-06-20 09:54:50 -04:00
|
|
|
else if (message === 'vcard' && from === user_jid){
|
|
|
|
xmpp.getVCard(from, function (vcard) {
|
|
|
|
xmpp.send(from, vcard);
|
2021-12-07 16:43:46 -05:00
|
|
|
console.log(vcard)
|
2021-06-20 09:54:50 -04:00
|
|
|
})
|
|
|
|
}
|
2022-01-27 10:48:21 -05:00
|
|
|
|
2021-12-07 16:17:31 -05:00
|
|
|
// If the user sends something we don't have logic for, send them info about the project
|
2022-01-27 10:48:21 -05:00
|
|
|
else {
|
|
|
|
xmpp.send(from, "This is an auto replying bot. Reply \"help\" for more info, or https://wiki.vanderwarker.family/doku.php?id=code:vxmppb:start");
|
|
|
|
console.log('autoreply');
|
2021-06-20 09:54:50 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-12-07 16:17:31 -05:00
|
|
|
// Catch and log XMPP errors
|
2021-06-20 09:54:50 -04:00
|
|
|
xmpp.on('error', function(err) {
|
|
|
|
console.error(err);
|
|
|
|
});
|
|
|
|
|
|
|
|
// EXTENDING XEPS (WIP)
|
|
|
|
/*
|
|
|
|
xmpp.on('chatstate', function(from, state) {
|
2021-12-07 16:43:46 -05:00
|
|
|
console.log(state);
|
2021-06-20 09:54:50 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
xmpp.on('buddy', function(jid, state, statusText, resource) {
|
2021-12-07 16:43:46 -05:00
|
|
|
console.log(state + statusText + resource);
|
2021-06-20 09:54:50 -04:00
|
|
|
});
|
|
|
|
*/
|
|
|
|
|
2021-12-07 16:17:31 -05:00
|
|
|
// Alas, we connect to XMPP!
|
2021-06-20 09:54:50 -04:00
|
|
|
xmpp.connect({
|
|
|
|
jid: bot_jid,
|
|
|
|
password: bot_password,
|
|
|
|
host: bot_hostname,
|
2021-12-07 16:17:31 -05:00
|
|
|
port: bot_port
|
2021-06-20 09:54:50 -04:00
|
|
|
});
|
|
|
|
|
2021-12-07 16:17:31 -05:00
|
|
|
// Checks the XMPP server for new 'friend' requests
|
2021-12-07 16:43:46 -05:00
|
|
|
console.log(xmpp.getRoster());
|
2021-12-12 09:15:09 -05:00
|
|
|
} catch(e) {
|
|
|
|
console.log("Please create a config.ini");
|
2021-12-12 09:17:22 -05:00
|
|
|
console.log("See https://wiki.vanderwarker.family/doku.php?id=code:vxmppb:install:start");
|
|
|
|
}
|