This commit is contained in:
Stephen Vanderwarker 2022-03-31 14:15:12 -04:00
parent 5330154584
commit 3abe2454c6
Signed by: stephen
GPG Key ID: EF429EF847868C14

320
index.js
View File

@ -9,112 +9,114 @@ const xmpp = require('simple-xmpp');
try { try {
fs.readFileSync('./config.ini', 'utf-8'); fs.readFileSync('./config.ini', 'utf-8');
const config = ini.parse(fs.readFileSync('./config.ini', 'utf-8')); const config = ini.parse(fs.readFileSync('./config.ini', 'utf-8'));
const debugging = config.debug; const debugging = config.debug;
// Asterisk config // Asterisk config
const asterisk_callfrom = config.asterisk.callfrom; const asterisk_callfrom = config.asterisk.callfrom;
const asterisk_callerID = config.asterisk.callerID; const asterisk_callerID = config.asterisk.callerID;
const asterisk_callto = config.asterisk.callerto; const asterisk_callto = config.asterisk.callerto;
const asterisk_context = config.asterisk.context; const asterisk_context = config.asterisk.context;
const asterisk_host = config.asterisk.host; const asterisk_host = config.asterisk.host;
const asterisk_password = config.asterisk.password; const asterisk_password = config.asterisk.password;
const asterisk_port = config.asterisk.port; const asterisk_port = config.asterisk.port;
const asterisk_user = config.asterisk.user; const asterisk_user = config.asterisk.user;
// Bot config // Bot config
const bot_hostname = config.bot.hostname; const bot_hostname = config.bot.hostname;
const bot_jid = config.bot.jid; const bot_jid = config.bot.jid;
const bot_password = config.bot.password; const bot_password = config.bot.password;
const bot_port = config.bot.port; const bot_port = config.bot.port;
// Gotify config // Gotify config
const gotify_api_url = config.gotify.api_url; const gotify_api_url = config.gotify.api_url;
const gotify_key = config.gotify.key; const gotify_key = config.gotify.key;
const gotify_message = config.gotify.message; const gotify_message = config.gotify.message;
const gotify_priority = config.gotify.priority; const gotify_priority = config.gotify.priority;
const gotify_title = config.gotify.title; const gotify_title = config.gotify.title;
// Mastodon config // Mastodon config
const mastodon_api_url = config.mastodon.api_url; const mastodon_api_url = config.mastodon.api_url;
const mastodon_token = config.mastodon.token; const mastodon_token = config.mastodon.token;
const mastodon_toot = config.mastodon.toot; const mastodon_toot = config.mastodon.toot;
// MQTT config // MQTT config
const mqtt_message = config.mqtt.message; const mqtt_message = config.mqtt.message;
const mqtt_password = config.mqtt.password; const mqtt_password = config.mqtt.password;
const mqtt_server = config.mqtt.server; const mqtt_server = config.mqtt.server;
const mqtt_topic = config.mqtt.topic; const mqtt_topic = config.mqtt.topic;
const mqtt_user = config.mqtt.user; const mqtt_user = config.mqtt.user;
// Tuya (Smart Lights) config // Tuya (Smart Lights) config
const tuya_name = config.tuya.name; const tuya_name = config.tuya.name;
const tuya_bright_url = config.tuya.bright_url; const tuya_bright_url = config.tuya.bright_url;
const tuya_color_url = config.tuya.color_url; const tuya_color_url = config.tuya.color_url;
const tuya_status_url = config.tuya.status_url; const tuya_status_url = config.tuya.status_url;
const tuya_toggle_url = config.tuya.toggle_url; const tuya_toggle_url = config.tuya.toggle_url;
// Unlocks API config // Unlocks API config
const unlocks_api_url = config.unlocks.api_url; const unlocks_api_url = config.unlocks.api_url;
const unlocks_pass = config.unlocks.pass; const unlocks_pass = config.unlocks.pass;
const unlocks_user = config.unlocks.user; const unlocks_user = config.unlocks.user;
// User config (authorized user) // User config (authorized user)
const user_jid = config.user.jid //.split('/')[0]; const user_jid = config.user.jid //.split('/')[0];
const user_name = config.user.name; const user_name = config.user.name;
const user_server = config.user.server; const user_server = config.user.server;
// Weather config // Weather config
const weather_key = config.weather.key; const weather_key = config.weather.key;
const weather_city_code = config.weather.city_code; const weather_city_code = config.weather.city_code;
const weather_city_name = config.weather.city_name; const weather_city_name = config.weather.city_name;
const weather_units = config.weather.units; const weather_units = config.weather.units;
// Private channel creation (uses friends secion of config.ini to invite them to a private chat) // Private channel creation (uses friends secion of config.ini to invite them to a private chat)
const xmpp_api_url = config.xmpp.api_url; const xmpp_api_url = config.xmpp.api_url;
const xmpp_auth = config.xmpp.key; const xmpp_auth = config.xmpp.key;
const xmpp_muc_name = config.xmpp.muc_name; const xmpp_muc_name = config.xmpp.muc_name;
const xmpp_muc_password = config.xmpp.muc_password; const xmpp_muc_password = config.xmpp.muc_password;
const xmpp_muc_server= config.xmpp.muc_server; const xmpp_muc_server = config.xmpp.muc_server;
let pc = 'This is a private command. Please see https://wiki.vanderwarker.family/doku.php?id=code:vxmppb:start for your own instance!'; let pc = 'This is a private command. Please see https://wiki.vanderwarker.family/doku.php?id=code:vxmppb:start for your own instance!';
let weather_url = `https://api.openweathermap.org/data/2.5/weather?id=${weather_city_code}&appid=${weather_key}&units=${weather_units}` let weather_url = `https://api.openweathermap.org/data/2.5/weather?id=${weather_city_code}&appid=${weather_key}&units=${weather_units}`
var ami = new require('asterisk-manager')(asterisk_port, asterisk_host, asterisk_user, asterisk_password, false); var ami = new require('asterisk-manager')(asterisk_port, asterisk_host, asterisk_user, asterisk_password, false);
var client = mqtt.connect('tcp://' + mqtt_user + ':' + mqtt_password + '@' + mqtt_server); var client = mqtt.connect('tcp://' + mqtt_user + ':' + mqtt_password + '@' + mqtt_server);
var M = new mastodon({ var M = new mastodon({
access_token: `${mastodon_token}`, access_token: `${mastodon_token}`,
timeout_ms: 60*1000, timeout_ms: 60 * 1000,
api_url: `${mastodon_api_url}`, api_url: `${mastodon_api_url}`,
}) })
// thx: https://stackoverflow.com/questions/48750528/get-direction-from-degrees // thx: https://stackoverflow.com/questions/48750528/get-direction-from-degrees
function getDirection(angle) { function getDirection(angle) {
var directions = ['North', 'North-East', 'East', 'South-East', 'South', 'South-West', 'West', 'North-West']; 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; var index = Math.round(((angle %= 360) < 0 ? angle + 360 : angle) / 45) % 8;
return directions[index]; return directions[index];
} }
// Once we're online, we'll send a message alerting admins // Once we're online, we'll send a message alerting admins
xmpp.on('online', function(data, to) { xmpp.on('online', function(data, to) {
console.log('Connected with JID: ' + bot_jid); console.log('Connected with JID: ' + bot_jid);
xmpp.send(user_jid, "[ONLINE]", false); xmpp.send(user_jid, "[ONLINE]", false);
client.publish(mqtt_topic, mqtt_message); client.publish(mqtt_topic, mqtt_message);
M.post('statuses', { status: `${mastodon_toot}` }); M.post('statuses', {
}); status: `${mastodon_toot}`
});
});
xmpp.on('chat', function(from, message) { xmpp.on('chat', function(from, message) {
function debug(){ function debug() {
if (debugging === '1'){ if (debugging === '1') {
console.log('-------------'); console.log('-------------');
console.log(message); console.log(message);
console.log(from); console.log(from);
console.log('-------------'); console.log('-------------');
}; };
} }
const friends = Object.keys(config.friends); const friends = Object.keys(config.friends);
let gotifyURL = `${gotify_api_url}/message?token=${gotify_key}&message=${from} ${gotify_message}&title=${gotify_title}&priority=${gotify_priority}`; let gotifyURL = `${gotify_api_url}/message?token=${gotify_key}&message=${from} ${gotify_message}&title=${gotify_title}&priority=${gotify_priority}`;
let gotifyEUrl = encodeURI(gotifyURL); let gotifyEUrl = encodeURI(gotifyURL);
// HELP // HELP
if (message === '!help') { if (message === '!help') {
xmpp.send(from, 'https://wiki.vanderwarker.family/doku.php?id=code:vxmppb:commands'); xmpp.send(from, 'https://wiki.vanderwarker.family/doku.php?id=code:vxmppb:commands');
@ -123,7 +125,7 @@ let gotifyEUrl = encodeURI(gotifyURL);
// UNLOCKS // UNLOCKS
else if (message === '!unlocks') { else if (message === '!unlocks') {
if (from != user_jid){ if (from != user_jid) {
xmpp.send(from, pc); xmpp.send(from, pc);
} else { } else {
var d = new Date(); var d = new Date();
@ -163,19 +165,20 @@ let gotifyEUrl = encodeURI(gotifyURL);
} }
}); });
debug(); debug();
}} }
}
// Tuya light control // Tuya light control
else if (message === '!lights') { else if (message === '!lights') {
if (from != user_jid){ if (from != user_jid) {
xmpp.send(from, pc); xmpp.send(from, pc);
} else { } else {
request(tuya_status_url,(error, res, body) => { request(tuya_status_url, (error, res, body) => {
if (error) { if (error) {
return console.log(error) return console.log(error)
}; };
if (!error && res.statusCode == 200) { if (!error && res.statusCode == 200) {
if (res.body === "true"){ if (res.body === "true") {
const stat = tuya_name + ": On"; const stat = tuya_name + ": On";
xmpp.send(from, stat); xmpp.send(from, stat);
console.log(stat); console.log(stat);
@ -202,12 +205,12 @@ let gotifyEUrl = encodeURI(gotifyURL);
} }
}); });
debug(); debug();
}} }
else if (message === tuya_name + ' on') { } else if (message === tuya_name + ' on') {
if (from != user_jid){ if (from != user_jid) {
xmpp.send(from, pc); xmpp.send(from, pc);
} else { } else {
request(tuya_toggle_url + "?stat=true&devname=" + tuya_name,(error, res, body) => { request(tuya_toggle_url + "?stat=true&devname=" + tuya_name, (error, res, body) => {
if (error) { if (error) {
return console.log(error) return console.log(error)
}; };
@ -231,12 +234,12 @@ let gotifyEUrl = encodeURI(gotifyURL);
} }
}); });
debug(); debug();
}} }
else if (message === tuya_name + ' off') { } else if (message === tuya_name + ' off') {
if (from != user_jid){ if (from != user_jid) {
xmpp.send(from, pc); xmpp.send(from, pc);
} else { } else {
request(tuya_toggle_url + "?stat=false&devname=" + tuya_name,(error, res, body) => { request(tuya_toggle_url + "?stat=false&devname=" + tuya_name, (error, res, body) => {
if (error) { if (error) {
return console.log(error) return console.log(error)
}; };
@ -260,14 +263,14 @@ let gotifyEUrl = encodeURI(gotifyURL);
} }
}); });
debug(); debug();
}} }
else if (message.includes(tuya_name + ' brightness')) { } else if (message.includes(tuya_name + ' brightness')) {
if (from != user_jid){ if (from != user_jid) {
xmpp.send(from, pc); xmpp.send(from, pc);
} else { } else {
var per=message.substring(message.indexOf("brightness") + 10); var per = message.substring(message.indexOf("brightness") + 10);
var perc=(per*10) var perc = (per * 10)
request(tuya_bright_url + "?brite=" + perc + "&devname=" + tuya_name,(error, res, body) => { request(tuya_bright_url + "?brite=" + perc + "&devname=" + tuya_name, (error, res, body) => {
if (error) { if (error) {
return console.log(error) return console.log(error)
}; };
@ -289,22 +292,23 @@ let gotifyEUrl = encodeURI(gotifyURL);
xmpp.send(from, 'Server error. Please ping admin'); xmpp.send(from, 'Server error. Please ping admin');
} }
}); });
if (config.debug === '1'){ if (config.debug === '1') {
console.log('lights turned to ' + per + "%");} console.log('lights turned to ' + per + "%");
}} }
else if (message.includes(tuya_name + ' color')) { }
if (from != user_jid){ } else if (message.includes(tuya_name + ' color')) {
if (from != user_jid) {
xmpp.send(from, pc); xmpp.send(from, pc);
} else { } else {
request(tuya_toggle_url + "?stat=true&devname=" + tuya_name,(error, res, body) => {}) request(tuya_toggle_url + "?stat=true&devname=" + tuya_name, (error, res, body) => {})
var color=message.substring(message.indexOf("color") + 6); var color = message.substring(message.indexOf("color") + 6);
var colorRgb=rgbcolor(color); var colorRgb = rgbcolor(color);
console.log(color) console.log(color)
if (colorRgb.isValid()) { if (colorRgb.isValid()) {
var obj = colorRgb.channels(); var obj = colorRgb.channels();
console.log(obj.r + ', ' + obj.g + ', ' + obj.b); console.log(obj.r + ', ' + obj.g + ', ' + obj.b);
} }
request(tuya_color_url + "?r=" + colorRgb.r + "&g=" + colorRgb.g + "&b=" + colorRgb.b + "&devname=" + tuya_name,(error, res, body) => { request(tuya_color_url + "?r=" + colorRgb.r + "&g=" + colorRgb.g + "&b=" + colorRgb.b + "&devname=" + tuya_name, (error, res, body) => {
if (error) { if (error) {
return console.log(error) return console.log(error)
}; };
@ -326,24 +330,26 @@ let gotifyEUrl = encodeURI(gotifyURL);
xmpp.send(from, 'Server error. Please ping admin'); xmpp.send(from, 'Server error. Please ping admin');
} }
}); });
if (config.debug === '1'){ if (config.debug === '1') {
console.log(tuya_name + "set to " + color);} console.log(tuya_name + "set to " + color);
}} }
}
}
// Ping a user to let them know you would like to communicate // Ping a user to let them know you would like to communicate
else if (message === '!ping') { else if (message === '!ping') {
xmpp.send(from, "Pinging " + user_name + "! Please stand by."); xmpp.send(from, "Pinging " + user_name + "! Please stand by.");
xmpp.send(user_jid, from + " has pinged you!"); xmpp.send(user_jid, from + " has pinged you!");
client.publish(mqtt_topic, from + ' has pinged you!'); client.publish(mqtt_topic, from + ' has pinged you!');
request.post(gotifyEUrl, function (err, response, body) { request.post(gotifyEUrl, function(err, response, body) {
if(err){ if (err) {
console.log('error:', err); console.log('error:', err);
} }
}); });
ami.action({ ami.action({
'action':'originate', 'action': 'originate',
'channel':'SIP/' + asterisk_callfrom, 'channel': 'SIP/' + asterisk_callfrom,
'context': asterisk_context, 'context': asterisk_context,
'callerID' : asterisk_callerID, 'callerID': asterisk_callerID,
'data': 'googletts.agi, \"hello. ' + from + ' has pinged you!\"', 'data': 'googletts.agi, \"hello. ' + from + ' has pinged you!\"',
'application': 'agi' 'application': 'agi'
}); });
@ -352,12 +358,12 @@ let gotifyEUrl = encodeURI(gotifyURL);
// Check the weather using weatherunderground API // Check the weather using weatherunderground API
else if (message === '!weather') { else if (message === '!weather') {
if (from != user_jid){ if (from != user_jid) {
xmpp.send(from, pc); xmpp.send(from, pc);
} else { } else {
xmpp.send(from, "Checking weather. Please hold...\r\n \r\n"); xmpp.send(from, "Checking weather. Please hold...\r\n \r\n");
request(weather_url, function (err, response, body) { request(weather_url, function(err, response, body) {
if(err){ if (err) {
console.log('error:', err); console.log('error:', err);
} else { } else {
let w = JSON.parse(body); let w = JSON.parse(body);
@ -384,18 +390,19 @@ let gotifyEUrl = encodeURI(gotifyURL);
} }
}); });
debug(); debug();
}} }
}
// Sends a nice goodbye message // Sends a nice goodbye message
else if (message === '!bye'){ else if (message === '!bye') {
xmpp.send(from, "Bye, have a beautiful time!"); xmpp.send(from, "Bye, have a beautiful time!");
debug(); debug();
} }
// Share-A-Command (#13?) // Share-A-Command (#13?)
else if (message === '!meet'){ else if (message === '!meet') {
if (from != user_jid){ if (from != user_jid) {
xmpp.send(from, pc); xmpp.send(from, pc);
} else { } else {
const createRoom = { const createRoom = {
@ -427,7 +434,7 @@ let gotifyEUrl = encodeURI(gotifyURL);
}, },
json: true json: true
}; };
request(createRoom, function (error, response, body) { request(createRoom, function(error, response, body) {
if (error) throw new Error(error); if (error) throw new Error(error);
console.log(body); console.log(body);
}); });
@ -465,22 +472,24 @@ let gotifyEUrl = encodeURI(gotifyURL);
}, },
json: true json: true
}; };
request(addMember, function (error, response, body) { request(addMember, function(error, response, body) {
if (error) throw new Error(error); if (error) throw new Error(error);
console.log(body); console.log(body);
}); });
request(inviteMember, function (error, response, body) { request(inviteMember, function(error, response, body) {
if (error) throw new Error(error); if (error) throw new Error(error);
console.log(body); console.log(body);
}); });
}}, 2000); }
/////////////////////////////////////// }, 2000);
///////////////////////////////////////
debug(); debug();
}} }
}
// End meeting // End meeting
else if (message === '!endmeet'){ else if (message === '!endmeet') {
if (from != user_jid){ if (from != user_jid) {
xmpp.send(from, pc); xmpp.send(from, pc);
} else { } else {
const endMeeting = { const endMeeting = {
@ -490,10 +499,13 @@ let gotifyEUrl = encodeURI(gotifyURL);
'Content-Type': 'application/json', 'Content-Type': 'application/json',
Authorization: xmpp_auth Authorization: xmpp_auth
}, },
body: {name: xmpp_muc_name, service: xmpp_muc_server}, body: {
name: xmpp_muc_name,
service: xmpp_muc_server
},
json: true json: true
}; };
request(endMeeting, function (error, response, body) { request(endMeeting, function(error, response, body) {
if (error) throw new Error(error); if (error) throw new Error(error);
console.log(body); console.log(body);
}); });
@ -501,60 +513,64 @@ let gotifyEUrl = encodeURI(gotifyURL);
for (const key in config.friends) { for (const key in config.friends) {
xmpp.send(key, "Meeting ended"); xmpp.send(key, "Meeting ended");
} }
}debug();} }
debug();
}
// Shutsdown bot if admin sends shutdown command (linux) // Shutsdown bot if admin sends shutdown command (linux)
else if (message === "sudo shutdown now -h"){ else if (message === "sudo shutdown now -h") {
if (from != user_jid){ if (from != user_jid) {
xmpp.send(from, pc); xmpp.send(from, pc);
} else { } else {
xmpp.send(from, "Alrighty, shutting down. Goodbye!"); xmpp.send(from, "Alrighty, shutting down. Goodbye!");
debug(); debug();
process.exit(1); process.exit(1);
}} }
}
// Get users vCard (WIP) // Get users vCard (WIP)
else if (message === '!vcard'){ else if (message === '!vcard') {
if (from != user_jid){ if (from != user_jid) {
xmpp.send(from, pc); xmpp.send(from, pc);
} else { } else {
xmpp.getVCard(from, function (vcard) { xmpp.getVCard(from, function(vcard) {
xmpp.send(from, vcard); xmpp.send(from, vcard);
console.log(vcard) console.log(vcard)
}) })
}} }
}
// If the user sends something we don't have logic for, send them info about the project // If the user sends something we don't have logic for, send them info about the project
else { else {
xmpp.send(from, "This is an auto replying bot. Please see https://wiki.vanderwarker.family/doku.php?id=code:vxmppb:commands"); xmpp.send(from, "This is an auto replying bot. Please see https://wiki.vanderwarker.family/doku.php?id=code:vxmppb:commands");
debug(); debug();
} }
}); });
// Catch and log XMPP errors // Catch and log XMPP errors
xmpp.on('error', function(err) { xmpp.on('error', function(err) {
console.error(err); console.error(err);
}); });
// Set XMPP status // Set XMPP status
xmpp.setPresence('chat', 'Asterisk,Mastodon, and MQTT. Oh my!'); xmpp.setPresence('chat', 'Asterisk,Mastodon, and MQTT. Oh my!');
// Alas, we connect to XMPP! // Alas, we connect to XMPP!
xmpp.connect({ xmpp.connect({
jid: bot_jid, jid: bot_jid,
password: bot_password, password: bot_password,
host: bot_hostname, host: bot_hostname,
port: bot_port port: bot_port
}); });
// Checks the XMPP server for new 'friend' requests // Checks the XMPP server for new 'friend' requests
console.log(xmpp.getRoster()); console.log(xmpp.getRoster());
} catch(e) { } catch (e) {
console.log("Please create a config.ini"); console.log("Please create a config.ini");
console.log("See https://wiki.vanderwarker.family/doku.php?id=code:vxmppb:install:start"); console.log("See https://wiki.vanderwarker.family/doku.php?id=code:vxmppb:install:start");
console.log("and choose your prefered install method.") console.log("and choose your prefered install method.")
} }