From 2aafdd6c42c088001387ad357991e81d23848ac2 Mon Sep 17 00:00:00 2001 From: stephen <1-stephen@users.noreply.gitlab.vanderwarker.dev> Date: Mon, 25 Apr 2022 20:36:36 -0400 Subject: [PATCH 01/21] Adding tuya reauth, will become crontab in documentation. Adding function for request error responses (see #24) --- config.ini.dist | 1 + index.js | 112 ++++++++++++++++-------------------------------- 2 files changed, 39 insertions(+), 74 deletions(-) diff --git a/config.ini.dist b/config.ini.dist index d71d41e..c24bad5 100644 --- a/config.ini.dist +++ b/config.ini.dist @@ -42,6 +42,7 @@ bright_url = color_url = status_url = toggle_url = +reauth_url [unlocks] api_url = diff --git a/index.js b/index.js index 6e2669b..5fd9b3b 100755 --- a/index.js +++ b/index.js @@ -59,6 +59,7 @@ try { 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; @@ -134,7 +135,21 @@ try { console.log(from); console.log('-------------'); }; - } + } + 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); @@ -170,20 +185,8 @@ try { var unlockBodyMatch = body.match(unlockBody); const unlockFinalMessage = user_name + " has " + unlockBodyMatch[1] + " unlocks for " + n; xmpp.send(from, unlockFinalMessage); - debug(); - } - 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'); } + resError(error,res); }); debug(); } @@ -260,18 +263,24 @@ try { console.log("Tuya API: Unknown reply!"); } } - 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'); + resError(error,res); + }); + debug(); + } + } else if (message === '!lightsauth') { + 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(); } @@ -290,18 +299,7 @@ try { xmpp.send(from, stat); console.log(stat); } - 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'); - } + resError(error,res); }); debug(); } @@ -314,24 +312,12 @@ try { 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); } - 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'); - } + resError(error,res); }); debug(); } @@ -351,18 +337,7 @@ try { xmpp.send(from, tuya_name + "set to " + per + "%"); console.log(tuya_name + "set to " + per + "%"); } - 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'); - } + resError(error,res); }); if (config.debug === '1') { console.log('lights turned to ' + per + "%"); @@ -390,18 +365,7 @@ try { xmpp.send(from, tuya_name + "set to " + color); console.log(tuya_name + "set to " + color); } - 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'); - } + resError(error,res); }); if (config.debug === '1') { console.log(tuya_name + "set to " + color); From 6f2e29c95ef5e27ef242d6781bce3a5725e324fe Mon Sep 17 00:00:00 2001 From: stephen Date: Mon, 25 Apr 2022 20:42:23 -0400 Subject: [PATCH 02/21] spacing. --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 5fd9b3b..333a9c2 100755 --- a/index.js +++ b/index.js @@ -334,8 +334,8 @@ try { }; if (!error && res.statusCode == 200) { - xmpp.send(from, tuya_name + "set to " + per + "%"); - console.log(tuya_name + "set to " + per + "%"); + xmpp.send(from, tuya_name + " set to " + per + "%"); + console.log(tuya_name + " set to " + per + "%"); } resError(error,res); }); From 2f4f1fc4007c2edd31820f895b8b9215f32e149b Mon Sep 17 00:00:00 2001 From: stephen Date: Tue, 26 Apr 2022 11:54:14 -0400 Subject: [PATCH 03/21] Changing debug message style, See #24, for !tootunlocks --- index.js | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/index.js b/index.js index 333a9c2..09c91e8 100755 --- a/index.js +++ b/index.js @@ -130,10 +130,9 @@ try { // check for debugging function debug() { if (debugging === '1') { - console.log('-------------'); + console.log('[DEBUG]'); console.log(message); console.log(from); - console.log('-------------'); }; } function resError(error,res){ @@ -221,18 +220,7 @@ try { }); debug(); } - 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'); - } + resError(error,res); }); debug(); } @@ -335,7 +323,6 @@ try { if (!error && res.statusCode == 200) { xmpp.send(from, tuya_name + " set to " + per + "%"); - console.log(tuya_name + " set to " + per + "%"); } resError(error,res); }); From f2a5bc5415665bb55194d169fad3ed475a8bee1d Mon Sep 17 00:00:00 2001 From: stephen Date: Tue, 26 Apr 2022 14:03:15 -0400 Subject: [PATCH 04/21] spelling --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 333a9c2..181ed8b 100755 --- a/index.js +++ b/index.js @@ -448,7 +448,7 @@ try { // 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 not this section requires you to have an Ejabberd server. + // 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') { From daaebbd507107209a6400f9cb37a7b7b31cc8926 Mon Sep 17 00:00:00 2001 From: stephen Date: Wed, 27 Apr 2022 15:18:46 -0400 Subject: [PATCH 05/21] Fixing license --- package.json | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 145499b..cce3ba8 100644 --- a/package.json +++ b/package.json @@ -35,9 +35,11 @@ "type": "git", "url": "https://git.vanderwarker.family/vxmppb/vxmppb.git" }, - "license": { - "type": "MIT" - }, + "licenses": [ + { + "type": "The MIT License", + "url": "http://www.opensource.org/licenses/mit-license.php" + }], "homepage": "https://wiki.vanderwarker.family/doku.php?id=code:vxmppb:start", "scripts": { "test": "echo \"No tests available. Please consider helping with a pull request!\" && exit 0" From 1af04358d23ac08149e9c15b38ea8d2b6c3fdf9b Mon Sep 17 00:00:00 2001 From: stephen Date: Wed, 27 Apr 2022 15:22:52 -0400 Subject: [PATCH 06/21] Re-adding docker badge --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 0ab7233..777a585 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,8 @@ Vanderwarker Family XMPP Bot. [![NPM](https://nodei.co/npm/vxmppb.png?compact=true)](https://nodei.co/npm/vxmppb/) [![Known Vulnerabilities](https://snyk.io/test/npm/vxmppb/badge.svg)](https://snyk.io/test/npm/vxmppb) + +![docker](https://img.shields.io/docker/pulls/vxmppb/vxmppb.svg) ![npm](https://img.shields.io/npm/dt/vxmppb) From 1259729c91f38007fb77b3b239102967d6c7bec2 Mon Sep 17 00:00:00 2001 From: stephen Date: Wed, 27 Apr 2022 16:35:53 -0400 Subject: [PATCH 07/21] Removing wmc, renamed wmc in asterisk call to wm. Re-enabled call asterisk for weather report. --- index.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 20788af..9a19adb 100755 --- a/index.js +++ b/index.js @@ -396,9 +396,7 @@ try { console.log('error:', err); } else { let w = JSON.parse(body); - let wm = weather_city_name + " Weather: \r\n Tempature: " + w.main.temp + " degrees. \r\n Conditions: " + w.weather[0].main + "\r\n Pressure: " + - 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 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 + ''); @@ -409,7 +407,6 @@ try { // send weather to xmpp admin xmpp.send(from, wm); // call admin with latest weather report - /* ami.action({ 'action':'originate', 'channel':'SIP/' + asterisk_callfrom, @@ -418,7 +415,7 @@ try { 'data': 'googletts.agi,' + wmc, 'application': 'agi' }); - */ + } }); debug(); From fae2c435ad77d8e4b1f3702bfe7806214176bc27 Mon Sep 17 00:00:00 2001 From: stephen Date: Wed, 27 Apr 2022 16:38:16 -0400 Subject: [PATCH 08/21] !! --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 9a19adb..6bda016 100755 --- a/index.js +++ b/index.js @@ -412,7 +412,7 @@ try { 'channel':'SIP/' + asterisk_callfrom, 'context': asterisk_context, 'callerID' : asterisk_callerID, - 'data': 'googletts.agi,' + wmc, + 'data': 'googletts.agi,' + wm, 'application': 'agi' }); From ac0338d220b66ea5000bddaa02cf76a6e7a7f07f Mon Sep 17 00:00:00 2001 From: stephen Date: Fri, 29 Apr 2022 17:22:57 -0400 Subject: [PATCH 09/21] Adding config to enable/disable calls. --- index.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/index.js b/index.js index 6bda016..72985a1 100755 --- a/index.js +++ b/index.js @@ -16,6 +16,8 @@ try { const config = ini.parse(fs.readFileSync('./config.ini', 'utf-8')); // check if user debugging is enabled const debugging = config.debug; + // check if user wants phone calls with commands are run + const callme = config.calls; // Asterisk config const asterisk_callfrom = config.asterisk.callfrom; From 4f8d74c7e358ff6a8b8e12a710a8f9a1e965be26 Mon Sep 17 00:00:00 2001 From: stephen Date: Fri, 29 Apr 2022 17:23:21 -0400 Subject: [PATCH 10/21] Adding enable/disable calls to config.ini.dist --- config.ini.dist | 1 + 1 file changed, 1 insertion(+) diff --git a/config.ini.dist b/config.ini.dist index c24bad5..139fdd3 100644 --- a/config.ini.dist +++ b/config.ini.dist @@ -1,4 +1,5 @@ debug = +calls = [asterisk] callfrom = From 2462dce4f8f241cff4d26e1ae55a4e5620b942e7 Mon Sep 17 00:00:00 2001 From: stephen Date: Fri, 29 Apr 2022 20:20:34 -0400 Subject: [PATCH 11/21] changed constant for config.calls function for calls (see #24) re-beautified code --- index.js | 274 +++++++++++++++++++++++++++---------------------------- 1 file changed, 137 insertions(+), 137 deletions(-) diff --git a/index.js b/index.js index 72985a1..c5aa263 100755 --- a/index.js +++ b/index.js @@ -17,7 +17,7 @@ try { // check if user debugging is enabled const debugging = config.debug; // check if user wants phone calls with commands are run - const callme = config.calls; + const calls_enabled = config.calls; // Asterisk config const asterisk_callfrom = config.asterisk.callfrom; @@ -113,45 +113,59 @@ try { // setup friends list const friends = Object.keys(config.friends); - 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); xmpp.send(user_jid, "[ONLINE]", false); // send mqtt online message client.publish(mqtt_topic, mqtt_message); // toot online message -/* - M.post('statuses', { - status: `${mastodon_toot}` - }); -*/ + /* + M.post('statuses', { + status: `${mastodon_toot}` + }); + */ }); // wait for incoming messages - xmpp.on('chat', function(from, message) { + xmpp.on('chat', function (from, message) { // check for debugging + 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' + }); + } + } + function debug() { if (debugging === '1') { console.log('[DEBUG]'); console.log(message); console.log(from); }; - } - 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'); - } + } + + 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 + // 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 @@ -175,7 +189,7 @@ try { weekday[5] = "Friday"; weekday[6] = "Saturday"; var n = weekday[d.getDay()]; - // get latest unlocks + // get latest unlocks request(unlocks_api_url + "?columns=" + n, (error, res, body) => { if (error) { return console.log(error) @@ -186,54 +200,55 @@ try { var unlockBodyMatch = body.match(unlockBody); const unlockFinalMessage = user_name + " has " + unlockBodyMatch[1] + " unlocks for " + n; xmpp.send(from, unlockFinalMessage); + call(unlockFinalMessage); } - resError(error,res); + resError(error, res); }); 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()]; + // 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); + 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 + // get current light status request(tuya_status_url, (error, res, body) => { if (error) { return console.log(error) @@ -253,7 +268,7 @@ try { console.log("Tuya API: Unknown reply!"); } } - resError(error,res); + resError(error, res); }); debug(); } @@ -261,7 +276,7 @@ try { if (from != user_jid) { xmpp.send(from, pc); } else { - // refresh tokens for Tuya API + // refresh tokens for Tuya API request(tuya_reauth_url, (error, res, body) => { if (error) { return console.log(error) @@ -270,7 +285,7 @@ try { if (!error && res.statusCode == 200) { xmpp.send(from, 'Refreshing Tuya API auth keys'); } - resError(error,res); + resError(error, res); }); debug(); } @@ -278,7 +293,7 @@ try { if (from != user_jid) { xmpp.send(from, pc); } else { - // turn on light + // turn on light request(tuya_toggle_url + "?stat=true&devname=" + tuya_name, (error, res, body) => { if (error) { return console.log(error) @@ -289,7 +304,7 @@ try { xmpp.send(from, stat); console.log(stat); } - resError(error,res); + resError(error, res); }); debug(); } @@ -297,7 +312,7 @@ try { if (from != user_jid) { xmpp.send(from, pc); } else { - // turn off light + // turn off light request(tuya_toggle_url + "?stat=false&devname=" + tuya_name, (error, res, body) => { if (error) { return console.log(error) @@ -307,7 +322,7 @@ try { xmpp.send(from, stat); console.log(stat); } - resError(error,res); + resError(error, res); }); debug(); } @@ -317,7 +332,7 @@ try { } else { var per = message.substring(message.indexOf("brightness") + 10); var perc = (per * 10) - // get brightness of light + // get brightness of light request(tuya_bright_url + "?brite=" + perc + "&devname=" + tuya_name, (error, res, body) => { if (error) { return console.log(error) @@ -326,7 +341,7 @@ try { if (!error && res.statusCode == 200) { xmpp.send(from, tuya_name + " set to " + per + "%"); } - resError(error,res); + resError(error, res); }); if (config.debug === '1') { console.log('lights turned to ' + per + "%"); @@ -336,7 +351,7 @@ try { if (from != user_jid) { xmpp.send(from, pc); } else { - // change light color + // 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); @@ -354,7 +369,7 @@ try { xmpp.send(from, tuya_name + "set to " + color); console.log(tuya_name + "set to " + color); } - resError(error,res); + resError(error, res); }); if (config.debug === '1') { console.log(tuya_name + "set to " + color); @@ -363,27 +378,21 @@ try { } // Ping a user to let them know you would like to communicate else if (message === '!ping') { - // send xmpp ping message to message author + // send xmpp ping message to message author xmpp.send(from, "Pinging " + user_name + "! Please stand by."); - // send xmpp ping message to admin + // send xmpp ping message to admin xmpp.send(user_jid, from + " has pinged you!"); - // send mqtt ping message + // 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) { + // 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 - ami.action({ - 'action': 'originate', - 'channel': 'SIP/' + asterisk_callfrom, - 'context': asterisk_context, - 'callerID': asterisk_callerID, - 'data': 'googletts.agi, \"hello. ' + from + ' has pinged you!\"', - 'application': 'agi' - }); + // call admin alerting of authors ping + call_data = "hello. " + from + " has pinged you!" + call(call_data); debug(); } @@ -392,32 +401,24 @@ try { if (from != user_jid) { xmpp.send(from, pc); } else { - // check current weather - request(weather_url, function(err, response, body) { + // 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 + // 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 + // send weather to xmpp admin xmpp.send(from, wm); - // call admin with latest weather report - ami.action({ - 'action':'originate', - 'channel':'SIP/' + asterisk_callfrom, - 'context': asterisk_context, - 'callerID' : asterisk_callerID, - 'data': 'googletts.agi,' + wm, - 'application': 'agi' - }); - + // call admin with latest weather report + call(wm); } }); debug(); @@ -431,17 +432,16 @@ try { } - // 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 + // 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') { if (from != user_jid) { xmpp.send(from, pc); } else { - // Create private XMPP room + // Create private XMPP room const createRoom = { method: 'POST', url: xmpp_api_url + '/create_room_with_opts', @@ -471,18 +471,18 @@ try { }, json: true }; - // send request to create room - request(createRoom, function(error, response, body) { + // send request to create room + request(createRoom, function (error, response, body) { if (error) throw new Error(error); console.log(body); }); - // wait 2 seconds until room has been created (http issues) + // wait 2 seconds until room has been created (http issues) setTimeout(() => { - // loop through friends, authorize users, and send invites + // loop through friends, authorize users, and send invites for (const key in config.friends) { - // send xmpp message to friends alerting of new room + // send xmpp message to friends alerting of new room xmpp.send(key, "Meeting started"); - // authorize users + // authorize users const addMember = { method: 'POST', url: xmpp_api_url + 'set_room_affiliation', @@ -498,7 +498,7 @@ try { }, json: true }; - // invite users + // invite users const inviteMember = { method: 'POST', url: xmpp_api_url + '/send_direct_invitation', @@ -515,11 +515,11 @@ try { }, json: true }; - request(addMember, function(error, response, body) { + request(addMember, function (error, response, body) { if (error) throw new Error(error); console.log(body); }); - request(inviteMember, function(error, response, body) { + request(inviteMember, function (error, response, body) { if (error) throw new Error(error); console.log(body); }); @@ -534,7 +534,7 @@ try { if (from != user_jid) { xmpp.send(from, pc); } else { - // end meeting (delete private xmpp room) + // end meeting (delete private xmpp room) const endMeeting = { method: 'POST', url: xmpp_api_url + '/destroy_room', @@ -548,13 +548,13 @@ try { }, json: true }; - // send room delete request - request(endMeeting, function(error, response, body) { + // 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 + // alert friends meeting has ended xmpp.send(key, "Meeting ended"); } } @@ -565,12 +565,12 @@ try { 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!'); + console.log(from + ' tried to shut me down!'); } else { - // send shutdown message + // send shutdown message xmpp.send(from, "Alrighty, shutting down. Goodbye!"); debug(); - // kill bot + // kill bot process.exit(1); } } @@ -580,8 +580,8 @@ try { if (from != user_jid) { xmpp.send(from, pc); } else { - // get user_jid vcard (profile info) - xmpp.getVCard(from, function(vcard) { + // get user_jid vcard (profile info) + xmpp.getVCard(from, function (vcard) { xmpp.send(from, vcard); console.log(vcard) }) @@ -596,7 +596,7 @@ try { }); // Catch and log XMPP errors - xmpp.on('error', function(err) { + xmpp.on('error', function (err) { console.error(err); }); @@ -610,24 +610,24 @@ try { 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"); - } - }); + // 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"); + } + }); } 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."); -} +} \ No newline at end of file From d62efc18bfee2d5d8bcaae0b8ab79e6265ae67fa Mon Sep 17 00:00:00 2001 From: stephen Date: Mon, 2 May 2022 17:16:45 -0400 Subject: [PATCH 12/21] Get messages from MQTT catching ctrl+c --- index.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index c5aa263..35fcd3c 100755 --- a/index.js +++ b/index.js @@ -112,6 +112,16 @@ try { // setup friends list const friends = Object.keys(config.friends); + client.subscribe('phones/pixel2/media', function (err) { + if (!err) { + debug(); + } + }) + client.on('message', function (topic, payload) { + var nowPlaying = payload.toString() + console.log(nowPlaying) + xmpp.send(user_jid, nowPlaying) + }) xmpp.on('online', function (data, to) { // once we're online, we'll send a message alerting xmpp admins @@ -482,7 +492,7 @@ try { for (const key in config.friends) { // send xmpp message to friends alerting of new room xmpp.send(key, "Meeting started"); - // authorize users + // authorize users const addMember = { method: 'POST', url: xmpp_api_url + 'set_room_affiliation', @@ -526,6 +536,7 @@ try { } }, 2000); debug(); + xmpp.send(from, "Alright. Starting a new meeting: " + xmpp_muc_name + "@" + xmpp_muc_server + " and inviting friends"); } } @@ -626,6 +637,12 @@ try { } }); + 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"); From 92ecfeed1a45cb1d371020a56a193f6e01028d40 Mon Sep 17 00:00:00 2001 From: stephen Date: Sat, 7 May 2022 20:59:41 -0400 Subject: [PATCH 13/21] changing formatting for asterisk calls config config.ini.dist changes asterisk, adds mastodon, mqtt, and gotify enable flags check if mastodon send toots enabled for on.connect --- index.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 35fcd3c..be27973 100755 --- a/index.js +++ b/index.js @@ -16,14 +16,13 @@ try { const config = ini.parse(fs.readFileSync('./config.ini', 'utf-8')); // check if user debugging is enabled const debugging = config.debug; - // check if user wants phone calls with commands are run - const calls_enabled = config.calls; // 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; @@ -37,6 +36,7 @@ try { // 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; @@ -44,11 +44,13 @@ try { // 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; @@ -130,12 +132,11 @@ try { // send mqtt online message client.publish(mqtt_topic, mqtt_message); // toot online message - /* - M.post('statuses', { - status: `${mastodon_toot}` - }); - */ - + if (mastodon_toots_enabled === "1"){ + M.post('statuses', { + status: `${mastodon_toot}` + }); + } }); // wait for incoming messages xmpp.on('chat', function (from, message) { From 4045b29b721251b8e4535c58bf86478c13b89883 Mon Sep 17 00:00:00 2001 From: stephen Date: Sat, 7 May 2022 21:01:35 -0400 Subject: [PATCH 14/21] moving calls to asterisk.enabled abc, enable flag gotify enable flag mastodon enable flag mqtt --- config.ini.dist | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/config.ini.dist b/config.ini.dist index 139fdd3..3369889 100644 --- a/config.ini.dist +++ b/config.ini.dist @@ -1,11 +1,11 @@ debug = -calls = [asterisk] callfrom = callto = callerID = context = +enabled = host = password = port = @@ -18,19 +18,22 @@ password = port = [gotify] +api_url = +enabled = key = message = priority = title = -api_url = [mastodon] token = toot = api_url = user = +enabled = [mqtt] +enabled = message = password = server = From b2444eb609c0215738edc5785e4752165ee8b8f6 Mon Sep 17 00:00:00 2001 From: stephen Date: Thu, 26 May 2022 19:06:33 -0400 Subject: [PATCH 15/21] Removing MQTT message inbound. --- index.js | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/index.js b/index.js index be27973..b39b590 100755 --- a/index.js +++ b/index.js @@ -114,25 +114,15 @@ try { // setup friends list const friends = Object.keys(config.friends); - client.subscribe('phones/pixel2/media', function (err) { - if (!err) { - debug(); - } - }) - client.on('message', function (topic, payload) { - var nowPlaying = payload.toString() - console.log(nowPlaying) - xmpp.send(user_jid, nowPlaying) - }) - + 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); + //xmpp.send(user_jid, "[ONLINE]", false); // send mqtt online message client.publish(mqtt_topic, mqtt_message); // toot online message - if (mastodon_toots_enabled === "1"){ + if (mastodon_enabled === "1"){ M.post('statuses', { status: `${mastodon_toot}` }); From d5dc804f60b106d2df064002258ddd696bd54bb2 Mon Sep 17 00:00:00 2001 From: stephen Date: Thu, 26 May 2022 19:14:36 -0400 Subject: [PATCH 16/21] checking for mqtt_enabled Adding comments --- index.js | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index b39b590..618f5be 100755 --- a/index.js +++ b/index.js @@ -120,7 +120,9 @@ try { console.log('Connected with JID: ' + bot_jid); //xmpp.send(user_jid, "[ONLINE]", false); // send mqtt online message - client.publish(mqtt_topic, mqtt_message); + if (mqtt_enabled === "1"){ + client.publish(mqtt_topic, mqtt_message); + } // toot online message if (mastodon_enabled === "1"){ M.post('statuses', { @@ -130,7 +132,7 @@ try { }); // wait for incoming messages xmpp.on('chat', function (from, message) { - // check for debugging + // check if user enabled asterisk function call(call_data) { if (calls_enabled === '1') { ami.action({ @@ -143,7 +145,7 @@ try { }); } } - + // check is user enabled debugging function debug() { if (debugging === '1') { console.log('[DEBUG]'); @@ -151,7 +153,7 @@ try { console.log(from); }; } - + // http request errors function resError(error, res) { if (!error && res.statusCode == 401) { console.log('Invalid credentials'); @@ -169,13 +171,13 @@ try { // 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 + // !help reply if (message === '!help') { xmpp.send(from, 'https://wiki.vanderwarker.family/doku.php?id=code:vxmppb:commands'); debug(); } - // UNLOCKS + // !unlocks reply else if (message === '!unlocks') { if (from != user_jid) { xmpp.send(from, pc); @@ -195,16 +197,20 @@ try { 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(); } } From 95835fa8626c68e65043efc86823472fce993f30 Mon Sep 17 00:00:00 2001 From: stephen Date: Thu, 26 May 2022 19:18:40 -0400 Subject: [PATCH 17/21] Adding server_type for #20 --- config.ini.dist | 1 + 1 file changed, 1 insertion(+) diff --git a/config.ini.dist b/config.ini.dist index 3369889..3c8d9bd 100644 --- a/config.ini.dist +++ b/config.ini.dist @@ -70,5 +70,6 @@ key = muc_name = muc_password = muc_server = +server_type = [friends] From 805535cfc3aabbcd688e2c655a58ee71f0b26536 Mon Sep 17 00:00:00 2001 From: stephen Date: Thu, 26 May 2022 19:18:58 -0400 Subject: [PATCH 18/21] Beginning work on #20 --- index.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/index.js b/index.js index 618f5be..c99dc56 100755 --- a/index.js +++ b/index.js @@ -87,6 +87,7 @@ try { 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!'; @@ -460,6 +461,7 @@ try { name: xmpp_muc_name, service: xmpp_muc_server, host: bot_hostname, +// options: xmpp_server_type; options: { allow_change_subj: 'false', allow_private_messages: 'false', From 6ad84aeef48633226b810b37d506e8b3a989fc06 Mon Sep 17 00:00:00 2001 From: stephen Date: Thu, 26 May 2022 19:37:42 -0400 Subject: [PATCH 19/21] check server type hardcoding :'( needs to loop, seperate config, or section? --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index c99dc56..a5f8810 100755 --- a/index.js +++ b/index.js @@ -449,6 +449,7 @@ try { if (from != user_jid) { xmpp.send(from, pc); } else { + if (xmpp_server_type === "ejabberd"){ // Create private XMPP room const createRoom = { method: 'POST', @@ -461,7 +462,6 @@ try { name: xmpp_muc_name, service: xmpp_muc_server, host: bot_hostname, -// options: xmpp_server_type; options: { allow_change_subj: 'false', allow_private_messages: 'false', @@ -536,7 +536,7 @@ try { }, 2000); debug(); xmpp.send(from, "Alright. Starting a new meeting: " + xmpp_muc_name + "@" + xmpp_muc_server + " and inviting friends"); - } + }} } // End meeting From f21b00c169e268b08f222187283651c8e625ef22 Mon Sep 17 00:00:00 2001 From: stephen Date: Thu, 26 May 2022 19:39:19 -0400 Subject: [PATCH 20/21] [NPM] minor version bump 1.1.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index cce3ba8..aaae7a2 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "vxmppb", "description": "vanderwarker.family XMPP Bot", - "version": "1.0.0", + "version": "1.1.0", "keywords": [ "xmpp", "bot", From ca65ed3c7575da2fdbf1c5492f2a22819577673e Mon Sep 17 00:00:00 2001 From: stephen Date: Thu, 2 Jun 2022 20:43:36 -0400 Subject: [PATCH 21/21] Adding support message for server type #20 --- index.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/index.js b/index.js index a5f8810..4ff83cb 100755 --- a/index.js +++ b/index.js @@ -536,6 +536,9 @@ try { }, 2000); debug(); xmpp.send(from, "Alright. Starting a new meeting: " + xmpp_muc_name + "@" + xmpp_muc_server + " and inviting friends"); + } else { + 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") }} }