From 768f405099b917f1a56e7ab1455255c9276371c4 Mon Sep 17 00:00:00 2001 From: stephen Date: Sat, 26 Mar 2022 13:40:26 -0400 Subject: [PATCH 1/3] Adding debugging --- config.ini.dist | 77 +++++++++++++++++++++++++------------------------ 1 file changed, 39 insertions(+), 38 deletions(-) diff --git a/config.ini.dist b/config.ini.dist index 7d7a7c5..9299023 100644 --- a/config.ini.dist +++ b/config.ini.dist @@ -1,38 +1,39 @@ -api_pass = -api_url = -api_user = -asterisk_callfrom = -asterisk_callto = -asterisk_callerID = -asterisk_context = -asterisk_host = -asterisk_password = -asterisk_port = -asterisk_user = -bot_hostname = -bot_jid = -bot_password = -city_code = -city_name = -gotify_api_key = -gotify_message = -gotify_priority = -gotify_title = -gotify_url = -mastodon_token = -mastodon_toot = -mastodon_url = -mqtt_message = -mqtt_password = -mqtt_server = -mqtt_topic = -mqtt_user = -tuya_name = -tuya_bright_url = -tuya_color_url = -tuya_status_url = -tuya_toggle_url = -user_jid = -user_name = -weather_api_key = -weather_units = +api_pass = +api_url = +api_user = +asterisk_callfrom = +asterisk_callto = +asterisk_callerID = +asterisk_context = +asterisk_host = +asterisk_password = +asterisk_port = +asterisk_user = +bot_hostname = +bot_jid = +bot_password = +city_code = +city_name = +debug = +gotify_api_key = +gotify_message = +gotify_priority = +gotify_title = +gotify_url = +mastodon_token = +mastodon_toot = +mastodon_url = +mqtt_message = +mqtt_password = +mqtt_server = +mqtt_topic = +mqtt_user = +tuya_name = +tuya_bright_url = +tuya_color_url = +tuya_status_url = +tuya_toggle_url = +user_jid = +user_name = +weather_api_key = +weather_units = From ee857f658ccabae7d0e35dc43565d98eda14c6fd Mon Sep 17 00:00:00 2001 From: stephen Date: Sun, 27 Mar 2022 08:38:33 -0400 Subject: [PATCH 2/3] Version bump 1.0.0 as I will be using semantic versioning, and the command styles changed. Also, downgrading simple-xmpp, until issue #13 is fixed. --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 0631846..860ab73 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "vxmppb", "description": "vanderwarker.family XMPP Bot", - "version": "0.1.2", + "version": "1.0.0", "keywords": [ "xmpp", "bot", @@ -26,7 +26,7 @@ "mqtt": "4.2.6", "request": "2.88.0", "rgb-color": "2.1.2", - "simple-xmpp": "2.3.1" + "simple-xmpp": "1.3.0" }, "bugs": { "url": "https://git.vanderwarker.family/vxmppb/vxmppb/issues" From 15ac5eed4e4e9ad55b161744c96a5551eaacd461 Mon Sep 17 00:00:00 2001 From: stephen Date: Sun, 27 Mar 2022 19:24:35 -0400 Subject: [PATCH 3/3] 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.") + }