// initialize required packages const fs = require('fs'); const ini = require('ini'); const mastodon = require('mastodon'); const mqtt = require('mqtt'); const request = require('request'); const rgbcolor = require('rgb-color'); const xmpp = require('simple-xmpp'); // check a config.ini is in current directory try { fs.readFileSync('./config.ini', 'utf-8'); // read config.ini const config = ini.parse(fs.readFileSync('./config.ini', 'utf-8')); // check if user debugging is enabled const debugging = config.debug; // Asterisk config const asterisk_callfrom = config.asterisk.callfrom; const asterisk_callerID = config.asterisk.callerID; const asterisk_callto = config.asterisk.callerto; const asterisk_context = config.asterisk.context; const calls_enabled = config.asterisk.enabled; const asterisk_host = config.asterisk.host; const asterisk_password = config.asterisk.password; const asterisk_port = config.asterisk.port; const asterisk_user = config.asterisk.user; // Bot config const bot_hostname = config.bot.hostname; const bot_jid = config.bot.jid; const bot_password = config.bot.password; const bot_port = config.bot.port; // Gotify config const gotify_api_url = config.gotify.api_url; const gotify_enabled = config.gotify.enabled; const gotify_key = config.gotify.key; const gotify_message = config.gotify.message; const gotify_priority = config.gotify.priority; const gotify_title = config.gotify.title; // Mastodon config const mastodon_api_url = config.mastodon.api_url; const mastodon_enabled = config.mastodon.enabled; const mastodon_token = config.mastodon.token; const mastodon_toot = config.mastodon.toot; const mastodon_user = config.mastodon.user; // MQTT config const mqtt_enabled = config.mqtt.enabled; 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; // Tuya (Smart Lights) config const tuya_name = config.tuya.name; const tuya_bright_url = config.tuya.bright_url; const tuya_color_url = config.tuya.color_url; const tuya_status_url = config.tuya.status_url; const tuya_toggle_url = config.tuya.toggle_url; const tuya_reauth_url = config.tuya.reauth_url; // Unlocks API config const unlocks_api_url = config.unlocks.api_url; const unlocks_pass = config.unlocks.pass; const unlocks_user = config.unlocks.user; // User config (authorized user) const user_jid = config.user.jid //.split('/')[0]; const user_name = config.user.name; const user_server = config.user.server; // Weather config const weather_key = config.weather.key; const weather_city_code = config.weather.city_code; const weather_city_name = config.weather.city_name; const weather_units = config.weather.units; // 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_auth = config.xmpp.key; const xmpp_muc_name = config.xmpp.muc_name; const xmpp_muc_password = config.xmpp.muc_password; const xmpp_muc_server = config.xmpp.muc_server; const xmpp_server_type = config.xmpp.server_type; // 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!'; // 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}` // initialize asterisk 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); // initialize mastodon var M = new mastodon({ access_token: `${mastodon_token}`, timeout_ms: 60 * 1000, api_url: `${mastodon_api_url}`, }) // thx: https://stackoverflow.com/questions/48750528/get-direction-from-degrees // converts numerical degree direction from weather api to a direction name 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]; } // setup friends list const friends = Object.keys(config.friends); 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); //xmpp.send(user_jid, "[ONLINE]", false); // send mqtt online message if (mqtt_enabled === "1"){ client.publish(mqtt_topic, mqtt_message); } // toot online message if (mastodon_enabled === "1"){ M.post('statuses', { status: `${mastodon_toot}` }); } }); // wait for incoming messages xmpp.on('chat', function (from, message) { // check if user enabled asterisk function call(call_data) { if (calls_enabled === '1') { ami.action({ 'action': 'originate', 'channel': 'SIP/' + asterisk_callfrom, 'context': asterisk_context, 'callerID': asterisk_callerID, 'data': 'googletts.agi,' + call_data, 'application': 'agi' }); } } // check is user enabled debugging function debug() { if (debugging === '1') { console.log('[DEBUG]'); console.log(message); console.log(from); }; } // http request errors function resError(error, res) { 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'); } }; // setup gotify url let gotifyURL = `${gotify_api_url}/message?token=${gotify_key}&message=${from} ${gotify_message}&title=${gotify_title}&priority=${gotify_priority}`; let gotifyEUrl = encodeURI(gotifyURL); // !help reply if (message === '!help') { xmpp.send(from, 'https://wiki.vanderwarker.family/doku.php?id=code:vxmppb:commands'); debug(); } // !unlocks reply else if (message === '!unlocks') { if (from != user_jid) { xmpp.send(from, pc); } else { 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()]; // get latest unlocks request(unlocks_api_url + "?columns=" + n, (error, res, body) => { if (error) { return console.log(error) }; // check for valid api response if (!error && res.statusCode == 200) { var unlockBody = new RegExp(':(.*)}'); var unlockBodyMatch = body.match(unlockBody); // setup unlocks message const unlockFinalMessage = user_name + " has " + unlockBodyMatch[1] + " unlocks for " + n; xmpp.send(from, unlockFinalMessage); // call user with unlocks if asterisk enabled call(unlockFinalMessage); } // check for http errors resError(error, res); }); // output xmpp, and request errors if debugging is enabled. debug(); } } // Toot Unlocks else if (message === '!tootunlocks') { if (from != user_jid) { xmpp.send(from, pc); } else { 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()]; // get latest unlocks request(unlocks_api_url + "?columns=" + n, (error, res, body) => { if (error) { return console.log(error) }; if (!error && res.statusCode == 200) { var unlockBody = new RegExp(':(.*)}'); var unlockBodyMatch = body.match(unlockBody); const unlockFinalMessage = "@" + config.mastodon.user + " has " + unlockBodyMatch[1] + " unlocks for " + n; M.post('statuses', { status: unlockFinalMessage }); debug(); } resError(error, res); }); debug(); } } // Tuya light control else if (message === '!lights') { if (from != user_jid) { xmpp.send(from, pc); } else { // get current light status 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 from api call. Please reply ping to reach an admin."); console.log("Tuya API: Unknown reply!"); } } resError(error, res); }); debug(); } } else if (message === '!reauth') { if (from != user_jid) { xmpp.send(from, pc); } else { // refresh tokens for Tuya API request(tuya_reauth_url, (error, res, body) => { if (error) { return console.log(error) }; if (!error && res.statusCode == 200) { xmpp.send(from, 'Refreshing Tuya API auth keys'); } resError(error, res); }); debug(); } } else if (message === tuya_name + ' on') { if (from != user_jid) { xmpp.send(from, pc); } else { // turn on light 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); } resError(error, res); }); debug(); } } else if (message === tuya_name + ' off') { if (from != user_jid) { xmpp.send(from, pc); } else { // turn off light 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); } resError(error, res); }); debug(); } } else if (message.includes(tuya_name + ' brightness')) { if (from != user_jid) { xmpp.send(from, pc); } else { var per = message.substring(message.indexOf("brightness") + 10); var perc = (per * 10) // get brightness of light request(tuya_bright_url + "?brite=" + perc + "&devname=" + tuya_name, (error, res, body) => { if (error) { return console.log(error) }; if (!error && res.statusCode == 200) { xmpp.send(from, tuya_name + " set to " + per + "%"); } resError(error, res); }); if (config.debug === '1') { console.log('lights turned to ' + per + "%"); } } } else if (message.includes(tuya_name + ' color')) { if (from != user_jid) { xmpp.send(from, pc); } else { // change light color request(tuya_toggle_url + "?stat=true&devname=" + tuya_name, (error, res, body) => {}) var color = message.substring(message.indexOf("color") + 6); var colorRgb = rgbcolor(color); console.log(color) if (colorRgb.isValid()) { var obj = colorRgb.channels(); 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) => { if (error) { return console.log(error) }; if (!error && res.statusCode == 200) { xmpp.send(from, tuya_name + "set to " + color); console.log(tuya_name + "set to " + color); } resError(error, res); }); if (config.debug === '1') { console.log(tuya_name + "set to " + color); } } } // Ping a user to let them know you would like to communicate else if (message === '!ping') { // send xmpp ping message to message author xmpp.send(from, "Pinging " + user_name + "! Please stand by."); // send xmpp ping message to admin xmpp.send(user_jid, from + " has pinged you!"); // send mqtt ping message client.publish(mqtt_topic, from + ' has pinged you!'); // set gotify ping message to admin request.post(gotifyEUrl, function (err, response, body) { if (err) { console.log('error:', err); } }); // call admin alerting of authors ping call_data = "hello. " + from + " has pinged you!" call(call_data); debug(); } // Check the weather using weatherunderground API else if (message === '!weather') { if (from != user_jid) { xmpp.send(from, pc); } else { // check current weather request(weather_url, function (err, response, body) { if (err) { console.log('error:', err); } else { let w = JSON.parse(body); let wm = 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"; // send weather to mqtt client.publish('weather/temp', '' + w.main.temp + ''); client.publish('weather/conditions', '' + w.weather[0].main + ''); client.publish('weather/pressure', '' + w.main.pressure + ''); client.publish('weather/wind/direction', '' + getDirection(w.wind.deg) + ''); client.publish('weather/wind/speed', '' + w.wind.speed + ''); // send weather to xmpp admin xmpp.send(from, wm); // call admin with latest weather report call(wm); } }); debug(); } } // Sends a nice goodbye message else if (message === '!bye') { xmpp.send(from, "Bye, have a beautiful time!"); debug(); } // Share-A-Command which was #13, but kinda turned into a different command // This will generate a private channel, and add the users in the friends // secion from config.ini // Please note this section requires you to have an Ejabberd server. // I am welcome to pull requests for other servers (maybe set type in config.ini) else if (message === '!meet') { console.log(xmpp_server_type); if (from != user_jid) { xmpp.send(from, pc); } else { if (xmpp_server_type === "ejabberd"){ fs.readFileSync('./servers/ejabberd.js', 'utf-8'); console.log("server type is ejabberd"); debug(); xmpp.send(from, "Alright. Starting a new meeting: " + xmpp_muc_name + "@" + xmpp_muc_server + " and inviting friends"); } else if (xmpp_server_type === "prosidy"){ xmpp.send(from, "Ejabberd is the only supported server at the moment.") xmpp.send(from, "Please see https://git.vanderwarker.family/vxmppb/vxmppb/issues/20") } else if (xmpp_server_type === "openfire"){ // fs.readFileSync('./servers/openfire.js', 'utf-8'); xmpp.send(from, "Ejabberd is the only supported server at the moment.") xmpp.send(from, "Please see https://git.vanderwarker.family/vxmppb/vxmppb/issues/20") } } } // End meeting else if (message === '!endmeet') { if (from != user_jid) { xmpp.send(from, pc); } else { // end meeting (delete private xmpp room) const endMeeting = { method: 'POST', url: xmpp_api_url + '/destroy_room', headers: { 'Content-Type': 'application/json', Authorization: xmpp_auth }, body: { name: xmpp_muc_name, service: xmpp_muc_server }, json: true }; // send room delete request request(endMeeting, function (error, response, body) { if (error) throw new Error(error); console.log(body); }); for (const key in config.friends) { // alert friends meeting has ended xmpp.send(key, "Meeting ended"); } } debug(); } // Shutsdown bot if admin sends shutdown command (linux) else if (message === "sudo shutdown now -h") { if (from != user_jid) { xmpp.send(from, from + " is not in the sudoers file. This incident will be reported."); console.log(from + ' tried to shut me down!'); } else { // send shutdown message xmpp.send(from, "Alrighty, shutting down. Goodbye!"); debug(); // kill bot process.exit(1); } } // Get users vCard (WIP) else if (message === '!vcard') { if (from != user_jid) { xmpp.send(from, pc); } else { // get user_jid vcard (profile info) xmpp.getVCard(from, function (vcard) { xmpp.send(from, vcard); console.log(vcard) }) } } // If the user sends something we don't have logic for, send them info about the project else { xmpp.send(from, "This is an auto replying bot. Please see https://wiki.vanderwarker.family/doku.php?id=code:vxmppb:commands"); debug(); } }); // Catch and log XMPP errors xmpp.on('error', function (err) { console.error(err); }); // Set XMPP status xmpp.setPresence('chat', 'https://vxmppb.tk'); // Alas, we connect to XMPP! xmpp.connect({ jid: bot_jid, password: bot_password, host: bot_hostname, port: bot_port }); // subscribe to friends xmpp.on('subscribe', function (from) { if (from in config.friends == true) { console.log('accepting subsciption request from ' + from); // accept pending subscription requests xmpp.acceptSubscription(from); // check for incoming subscription requests xmpp.getRoster(); // subscribe to any incoming subscription requests console.log('requesting subscription to ' + from); xmpp.subscribe(from); } else { console.log('denied subscription request from: ' + from + " as they are not in the friends section of config.ini"); } }); process.on('SIGINT', function() { console.log("\r\n SigInt receieved. Shutdown eminent!") xmpp.send(user_jid, "SigInt receieved. Shutdown eminent!") process.exit(); }); } catch (e) { console.log("Please create a config.ini"); console.log("See https://wiki.vanderwarker.family/doku.php?id=code:vxmppb:install:start"); console.log("and choose your prefered install method."); }