From 15ac5eed4e4e9ad55b161744c96a5551eaacd461 Mon Sep 17 00:00:00 2001 From: stephen Date: Sun, 27 Mar 2022 19:24:35 -0400 Subject: [PATCH] Added debugging. Added private commands. npm version bump 1.0.0 --- index.js | 158 ++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 97 insertions(+), 61 deletions(-) diff --git a/index.js b/index.js index 5dc6d0c..1ac337a 100755 --- a/index.js +++ b/index.js @@ -10,6 +10,8 @@ try { fs.readFileSync('./config.ini', 'utf-8'); const config = ini.parse(fs.readFileSync('./config.ini', 'utf-8')); +const debugging = config.debug; + const api_pass = config.api_pass; const api_url = config.api_url; const api_user = config.api_user; @@ -46,11 +48,22 @@ 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 user_jid = config.user_jid; +const user_jid = config.user_jid //.split('/')[0]; const user_name = config.user_name; const weather_api_key = config.weather_api_key; const weather_units = config.weather_units; +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=${city_code}&appid=${weather_api_key}&units=${weather_units}` + +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 M = new mastodon({ + access_token: `${mastodon_token}`, + timeout_ms: 60*1000, + api_url: `${mastodon_url}`, +}) + // 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']; @@ -58,14 +71,6 @@ function getDirection(angle) { return directions[index]; } -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); @@ -75,14 +80,25 @@ xmpp.on('online', function(data, to) { }); xmpp.on('chat', function(from, message) { + function debug(){ + if (debugging === '1'){ + console.log(message); + }; + } +let gurl = `${gotify_url}/message?token=${gotify_api_key}&message=${from} ${gotify_message}&title=${gotify_title}&priority=${gotify_priority}`; +let gurle = encodeURI(gurl); + // HELP - if (message === 'help' || message === 'Help') { + if (message === '!help') { xmpp.send(from, 'https://wiki.vanderwarker.family/doku.php?id=code:vxmppb:commands'); - console.log('help'); + debug(); } // UNLOCKS - else if (message === 'unlocks' || message === 'Unlocks') { + 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"; @@ -104,7 +120,7 @@ xmpp.on('chat', function(from, message) { var ule = body.match(ulb); const ur = user_name + " has " + ule[1] + " unlocks for " + n; xmpp.send(from, ur); - console.log(ur); + debug(); } if (!error && res.statusCode == 401) { console.log('Invalid credentials'); @@ -119,10 +135,13 @@ xmpp.on('chat', function(from, message) { xmpp.send(from, 'Server error. Please ping admin'); } }); - console.log('unlocks'); - } + debug(); +}} // Tuya light control - else if (message === 'lights' || message === 'Lights') { + else if (message === '!lights') { + if (from != user_jid){ + xmpp.send(from, pc); + } else { request(tuya_status_url,(error, res, body) => { if (error) { return console.log(error) @@ -155,9 +174,12 @@ xmpp.on('chat', function(from, message) { xmpp.send(from, 'Server error. Please ping admin'); } }); - console.log('lights status'); - } + debug(); +}} else if (message === tuya_name + ' on') { + if (from != user_jid){ + xmpp.send(from, pc); + } else { request(tuya_toggle_url + "?stat=true&devname=" + tuya_name,(error, res, body) => { if (error) { return console.log(error) @@ -181,9 +203,12 @@ xmpp.on('chat', function(from, message) { xmpp.send(from, 'Server error. Please ping admin'); } }); - console.log('lights on'); - } + debug(); +}} else if (message === tuya_name + ' off') { + if (from != user_jid){ + xmpp.send(from, pc); + } else { request(tuya_toggle_url + "?stat=false&devname=" + tuya_name,(error, res, body) => { if (error) { return console.log(error) @@ -207,10 +232,12 @@ xmpp.on('chat', function(from, message) { xmpp.send(from, 'Server error. Please ping admin'); } }); - console.log('lights off'); - } - - else if (message.includes(tuya_name + " brightness")) { + 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) request(tuya_bright_url + "?brite=" + perc + "&devname=" + tuya_name,(error, res, body) => { @@ -235,10 +262,13 @@ xmpp.on('chat', function(from, message) { xmpp.send(from, 'Server error. Please ping admin'); } }); - console.log('lights turned to ' + per + "%"); - } - - else if (message.includes(tuya_name + " color")) { + 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 { request(tuya_toggle_url + "?stat=true&devname=" + tuya_name,(error, res, body) => {}) var color=message.substring(message.indexOf("color") + 6); var colorRgb=rgbcolor(color); @@ -269,14 +299,19 @@ xmpp.on('chat', function(from, message) { xmpp.send(from, 'Server error. Please ping admin'); } }); - console.log(tuya_name + "set to " + color); - } - + 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" || message === 'Ping') { + else if (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!'); + request.post(gurle, function (err, response, body) { + if(err){ + console.log('error:', err); + } + }); ami.action({ 'action':'originate', 'channel':'SIP/' + asterisk_callfrom, @@ -285,18 +320,14 @@ xmpp.on('chat', function(from, message) { 'data': 'googletts.agi, \"hello. ' + from + ' has pinged you!\"', 'application': 'agi' }); - console.log('ping'); - 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){ - console.log('error:', err); - } - }); + debug(); } // Check the weather using weatherunderground API - else if (message === "weather" || message === 'Weather') { + else if (message === '!weather') { + if (from != user_jid){ + xmpp.send(from, pc); + } else { xmpp.send(from, "Checking weather. Please hold...\r\n \r\n"); request(weather_url, function (err, response, body) { if(err){ @@ -307,7 +338,6 @@ xmpp.on('chat', function(from, message) { 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 = 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"; - console.log(wm); client.publish('weather/temp', 'w.main.temp'); client.publish('weather/conditions', 'w.weather[0].main'); client.publish('weather/pressure', 'w.main.pressure'); @@ -326,34 +356,39 @@ xmpp.on('chat', function(from, message) { */ } }); - console.log('weather'); - } + debug(); + }} // Sends a nice goodbye message - else if (message === 'bye' || message === "Bye"){ + else if (message === '!bye'){ xmpp.send(from, "Bye, have a beautiful time!"); - console.log('bye'); - - } - // Shutsdown bot if admin messages "goodbye" - else if (message === "goodbye" || from === user_jid){ - xmpp.send(from, "Alrighty, shutting down. Goodbye!"); - console.log('Admin shutdown.'); - process.exit(1); + debug(); } + // Shutsdown bot if admin messages "goodbye" + else if (message === "sudo shutdown now -h"){ + if (from != user_jid){ + xmpp.send(from, pc); + } else { + xmpp.send(from, "Alrighty, shutting down. Goodbye!"); + debug(); + process.exit(1); + }} // Get users vCard (WIP) else if (message === 'vcard' && from === user_jid){ + if (from != user_jid){ + xmpp.send(from, pc); + } else { 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. Reply \"help\" for more info, or https://wiki.vanderwarker.family/doku.php?id=code:vxmppb:start"); - console.log('autoreply'); + xmpp.send(from, "This is an auto replying bot. Please see https://wiki.vanderwarker.family/doku.php?id=code:vxmppb:commands"); + debug(); } }); @@ -375,7 +410,8 @@ xmpp.connect({ // Checks the XMPP server for new 'friend' requests console.log(xmpp.getRoster()); -} catch(e) { - console.log("Please create a config.ini"); - console.log("See https://wiki.vanderwarker.family/doku.php?id=code:vxmppb:install:start"); -} + } 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.") + }