This commit is contained in:
Stephen Vanderwarker 2022-03-31 16:29:06 -04:00
parent 3abe2454c6
commit 08b5727c31
Signed by: stephen
GPG Key ID: EF429EF847868C14
1 changed files with 48 additions and 6 deletions

View File

@ -1,3 +1,4 @@
// initialize required packages
const fs = require('fs'); const fs = require('fs');
const ini = require('ini'); const ini = require('ini');
const mastodon = require('mastodon'); const mastodon = require('mastodon');
@ -6,10 +7,14 @@ const request = require('request');
const rgbcolor = require('rgb-color'); const rgbcolor = require('rgb-color');
const xmpp = require('simple-xmpp'); const xmpp = require('simple-xmpp');
// check a config.ini is in current directory
try { try {
fs.readFileSync('./config.ini', 'utf-8'); fs.readFileSync('./config.ini', 'utf-8');
// read config.ini
const config = ini.parse(fs.readFileSync('./config.ini', 'utf-8')); const config = ini.parse(fs.readFileSync('./config.ini', 'utf-8'));
// check if user debugging is enabled
const debugging = config.debug; const debugging = config.debug;
// Asterisk config // Asterisk config
@ -77,11 +82,16 @@ try {
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;
// setup private command message
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!';
// setup weather_url for !weather command
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}`
// initialize asterisk
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);
// initialize mqtt
var client = mqtt.connect('tcp://' + mqtt_user + ':' + mqtt_password + '@' + mqtt_server); var client = mqtt.connect('tcp://' + mqtt_user + ':' + mqtt_password + '@' + mqtt_server);
// initialize mastodon
var M = new mastodon({ var M = new mastodon({
access_token: `${mastodon_token}`, access_token: `${mastodon_token}`,
timeout_ms: 60 * 1000, timeout_ms: 60 * 1000,
@ -89,23 +99,27 @@ try {
}) })
// thx: https://stackoverflow.com/questions/48750528/get-direction-from-degrees // thx: https://stackoverflow.com/questions/48750528/get-direction-from-degrees
// converts numerical degree direction from weather api to a direction name
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
xmpp.on('online', function(data, to) { xmpp.on('online', function(data, to) {
// once we're online, we'll send a message alerting xmpp admins
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);
// send mqtt online message
client.publish(mqtt_topic, mqtt_message); client.publish(mqtt_topic, mqtt_message);
// toot online message
M.post('statuses', { M.post('statuses', {
status: `${mastodon_toot}` status: `${mastodon_toot}`
}); });
}); });
// wait for incoming messages
xmpp.on('chat', function(from, message) { xmpp.on('chat', function(from, message) {
// check for debugging
function debug() { function debug() {
if (debugging === '1') { if (debugging === '1') {
console.log('-------------'); console.log('-------------');
@ -114,7 +128,9 @@ try {
console.log('-------------'); console.log('-------------');
}; };
} }
// setup friends list
const friends = Object.keys(config.friends); const friends = Object.keys(config.friends);
// setup gotify url
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
@ -138,7 +154,7 @@ try {
weekday[5] = "Friday"; weekday[5] = "Friday";
weekday[6] = "Saturday"; weekday[6] = "Saturday";
var n = weekday[d.getDay()]; var n = weekday[d.getDay()];
// get latest unlocks
request(unlocks_api_url + "?columns=" + n, (error, res, body) => { request(unlocks_api_url + "?columns=" + n, (error, res, body) => {
if (error) { if (error) {
return console.log(error) return console.log(error)
@ -172,6 +188,7 @@ try {
if (from != user_jid) { if (from != user_jid) {
xmpp.send(from, pc); xmpp.send(from, pc);
} else { } else {
// get current light status
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)
@ -210,6 +227,7 @@ try {
if (from != user_jid) { if (from != user_jid) {
xmpp.send(from, pc); xmpp.send(from, pc);
} else { } else {
// turn on light
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)
@ -239,6 +257,7 @@ try {
if (from != user_jid) { if (from != user_jid) {
xmpp.send(from, pc); xmpp.send(from, pc);
} else { } else {
// turn off light
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)
@ -270,6 +289,7 @@ try {
} 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)
// get brightness of light
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)
@ -300,6 +320,7 @@ try {
if (from != user_jid) { if (from != user_jid) {
xmpp.send(from, pc); xmpp.send(from, pc);
} else { } else {
// change light color
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);
@ -337,14 +358,19 @@ try {
} }
// 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') {
// send xmpp ping message to message author
xmpp.send(from, "Pinging " + user_name + "! Please stand by."); xmpp.send(from, "Pinging " + user_name + "! Please stand by.");
// send xmpp ping message to admin
xmpp.send(user_jid, from + " has pinged you!"); xmpp.send(user_jid, from + " has pinged you!");
// send mqtt ping message
client.publish(mqtt_topic, from + ' has pinged you!'); client.publish(mqtt_topic, from + ' has pinged you!');
// set gotify ping message to admin
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);
} }
}); });
// call admin alerting of authors ping
ami.action({ ami.action({
'action': 'originate', 'action': 'originate',
'channel': 'SIP/' + asterisk_callfrom, 'channel': 'SIP/' + asterisk_callfrom,
@ -361,7 +387,7 @@ try {
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"); // check current weather
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);
@ -371,12 +397,15 @@ try {
w.main.pressure + " \r\n Wind Direction: " + getDirection(w.wind.deg) + "\r\n Wind Speed: " + w.wind.speed + " m.p.h. \r\n"; w.main.pressure + " \r\n Wind Direction: " + getDirection(w.wind.deg) + "\r\n Wind Speed: " + w.wind.speed + " m.p.h. \r\n";
let wmc = weather_city_name + " Weather: Tempature: " + w.main.temp + " degrees. Conditions: " + w.weather[0].main + ". Pressure: " + let wmc = weather_city_name + " Weather: Tempature: " + w.main.temp + " degrees. Conditions: " + w.weather[0].main + ". Pressure: " +
w.main.pressure + ". Wind Direction: " + getDirection(w.wind.deg) + ". Wind Speed: " + w.wind.speed + " miles per hour \r\n"; w.main.pressure + ". Wind Direction: " + getDirection(w.wind.deg) + ". Wind Speed: " + w.wind.speed + " miles per hour \r\n";
// send weather to mqtt
client.publish('weather/temp', 'w.main.temp'); client.publish('weather/temp', 'w.main.temp');
client.publish('weather/conditions', 'w.weather[0].main'); client.publish('weather/conditions', 'w.weather[0].main');
client.publish('weather/pressure', 'w.main.pressure'); client.publish('weather/pressure', 'w.main.pressure');
client.publish('weather/wind/direction', 'getDirection(w.wind.deg)'); client.publish('weather/wind/direction', 'getDirection(w.wind.deg)');
client.publish('weather/wind/speed', 'w.wind.speed'); client.publish('weather/wind/speed', 'w.wind.speed');
// send weather to xmpp admin
xmpp.send(from, wm); xmpp.send(from, wm);
// call admin with latest weather report
/* /*
ami.action({ ami.action({
'action':'originate', 'action':'originate',
@ -405,6 +434,7 @@ try {
if (from != user_jid) { if (from != user_jid) {
xmpp.send(from, pc); xmpp.send(from, pc);
} else { } else {
// Create private XMPP room
const createRoom = { const createRoom = {
method: 'POST', method: 'POST',
url: xmpp_api_url + '/create_room_with_opts', url: xmpp_api_url + '/create_room_with_opts',
@ -434,13 +464,18 @@ try {
}, },
json: true json: true
}; };
// send request to create room
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);
}); });
// wait 2 seconds until room has been created (http issues)
setTimeout(() => { setTimeout(() => {
// loop through friends, authorize users, and send invites
for (const key in config.friends) { for (const key in config.friends) {
// send xmpp message to friends alerting of new room
xmpp.send(key, "Meeting started: " + xmpp_muc_name + "@" + xmpp_muc_server); xmpp.send(key, "Meeting started: " + xmpp_muc_name + "@" + xmpp_muc_server);
// authorize users
const addMember = { const addMember = {
method: 'POST', method: 'POST',
url: xmpp_api_url + 'set_room_affiliation', url: xmpp_api_url + 'set_room_affiliation',
@ -456,6 +491,7 @@ try {
}, },
json: true json: true
}; };
// invite users
const inviteMember = { const inviteMember = {
method: 'POST', method: 'POST',
url: xmpp_api_url + '/send_direct_invitation', url: xmpp_api_url + '/send_direct_invitation',
@ -482,7 +518,6 @@ try {
}); });
} }
}, 2000); }, 2000);
///////////////////////////////////////
debug(); debug();
} }
} }
@ -492,6 +527,7 @@ try {
if (from != user_jid) { if (from != user_jid) {
xmpp.send(from, pc); xmpp.send(from, pc);
} else { } else {
// end meeting (delete private xmpp room)
const endMeeting = { const endMeeting = {
method: 'POST', method: 'POST',
url: xmpp_api_url + '/destroy_room', url: xmpp_api_url + '/destroy_room',
@ -505,11 +541,14 @@ try {
}, },
json: true json: true
}; };
// send room delete request
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);
}); });
// send meeting ended to user_jid
xmpp.send(from, "Meeting ended"); xmpp.send(from, "Meeting ended");
// send meeting ended to friends
for (const key in config.friends) { for (const key in config.friends) {
xmpp.send(key, "Meeting ended"); xmpp.send(key, "Meeting ended");
} }
@ -524,8 +563,10 @@ try {
if (from != user_jid) { if (from != user_jid) {
xmpp.send(from, pc); xmpp.send(from, pc);
} else { } else {
// send shutdown message
xmpp.send(from, "Alrighty, shutting down. Goodbye!"); xmpp.send(from, "Alrighty, shutting down. Goodbye!");
debug(); debug();
// kill bot
process.exit(1); process.exit(1);
} }
} }
@ -535,6 +576,7 @@ try {
if (from != user_jid) { if (from != user_jid) {
xmpp.send(from, pc); xmpp.send(from, pc);
} else { } else {
// get user_jid vcard (profile info)
xmpp.getVCard(from, function(vcard) { xmpp.getVCard(from, function(vcard) {
xmpp.send(from, vcard); xmpp.send(from, vcard);
console.log(vcard) console.log(vcard)
@ -573,4 +615,4 @@ try {
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.")
} }