Merge pull request 'Merge stephen/vxmppb' (#25) from stephen/vxmppb:main into main

Reviewed-on: vxmppb/vxmppb#25
I need to update the docker image! Sorry (Coming soon? :P)
This commit is contained in:
Stephen Vanderwarker 2022-06-02 20:52:31 -04:00
commit b38c528575
4 changed files with 204 additions and 225 deletions

View File

@ -8,6 +8,8 @@ Vanderwarker Family XMPP Bot.
[![Known Vulnerabilities](https://snyk.io/test/npm/vxmppb/badge.svg)](https://snyk.io/test/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) ![npm](https://img.shields.io/npm/dt/vxmppb)
### :book: How To Install ### :book: How To Install

View File

@ -5,6 +5,7 @@ callfrom =
callto = callto =
callerID = callerID =
context = context =
enabled =
host = host =
password = password =
port = port =
@ -17,19 +18,22 @@ password =
port = port =
[gotify] [gotify]
api_url =
enabled =
key = key =
message = message =
priority = priority =
title = title =
api_url =
[mastodon] [mastodon]
token = token =
toot = toot =
api_url = api_url =
user = user =
enabled =
[mqtt] [mqtt]
enabled =
message = message =
password = password =
server = server =
@ -42,6 +46,7 @@ bright_url =
color_url = color_url =
status_url = status_url =
toggle_url = toggle_url =
reauth_url
[unlocks] [unlocks]
api_url = api_url =
@ -65,5 +70,6 @@ key =
muc_name = muc_name =
muc_password = muc_password =
muc_server = muc_server =
server_type =
[friends] [friends]

207
index.js
View File

@ -22,6 +22,7 @@ try {
const asterisk_callerID = config.asterisk.callerID; const asterisk_callerID = config.asterisk.callerID;
const asterisk_callto = config.asterisk.callerto; const asterisk_callto = config.asterisk.callerto;
const asterisk_context = config.asterisk.context; const asterisk_context = config.asterisk.context;
const calls_enabled = config.asterisk.enabled;
const asterisk_host = config.asterisk.host; const asterisk_host = config.asterisk.host;
const asterisk_password = config.asterisk.password; const asterisk_password = config.asterisk.password;
const asterisk_port = config.asterisk.port; const asterisk_port = config.asterisk.port;
@ -35,6 +36,7 @@ try {
// Gotify config // Gotify config
const gotify_api_url = config.gotify.api_url; const gotify_api_url = config.gotify.api_url;
const gotify_enabled = config.gotify.enabled;
const gotify_key = config.gotify.key; const gotify_key = config.gotify.key;
const gotify_message = config.gotify.message; const gotify_message = config.gotify.message;
const gotify_priority = config.gotify.priority; const gotify_priority = config.gotify.priority;
@ -42,11 +44,13 @@ try {
// Mastodon config // Mastodon config
const mastodon_api_url = config.mastodon.api_url; const mastodon_api_url = config.mastodon.api_url;
const mastodon_enabled = config.mastodon.enabled;
const mastodon_token = config.mastodon.token; const mastodon_token = config.mastodon.token;
const mastodon_toot = config.mastodon.toot; const mastodon_toot = config.mastodon.toot;
const mastodon_user = config.mastodon.user; const mastodon_user = config.mastodon.user;
// MQTT config // MQTT config
const mqtt_enabled = config.mqtt.enabled;
const mqtt_message = config.mqtt.message; const mqtt_message = config.mqtt.message;
const mqtt_password = config.mqtt.password; const mqtt_password = config.mqtt.password;
const mqtt_server = config.mqtt.server; const mqtt_server = config.mqtt.server;
@ -59,6 +63,7 @@ try {
const tuya_color_url = config.tuya.color_url; const tuya_color_url = config.tuya.color_url;
const tuya_status_url = config.tuya.status_url; const tuya_status_url = config.tuya.status_url;
const tuya_toggle_url = config.tuya.toggle_url; const tuya_toggle_url = config.tuya.toggle_url;
const tuya_reauth_url = config.tuya.reauth_url;
// Unlocks API config // Unlocks API config
const unlocks_api_url = config.unlocks.api_url; const unlocks_api_url = config.unlocks.api_url;
@ -82,6 +87,7 @@ try {
const xmpp_muc_name = config.xmpp.muc_name; const xmpp_muc_name = config.xmpp.muc_name;
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;
const xmpp_server_type = config.xmpp.server_type;
// setup private command message // 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!';
@ -113,38 +119,66 @@ try {
xmpp.on('online', function (data, to) { xmpp.on('online', function (data, to) {
// once we're online, we'll send a message alerting xmpp admins // 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 // send mqtt online message
if (mqtt_enabled === "1"){
client.publish(mqtt_topic, mqtt_message); client.publish(mqtt_topic, mqtt_message);
}
// toot online message // toot online message
/* if (mastodon_enabled === "1"){
M.post('statuses', { M.post('statuses', {
status: `${mastodon_toot}` status: `${mastodon_toot}`
}); });
*/ }
}); });
// wait for incoming messages // wait for incoming messages
xmpp.on('chat', function (from, message) { xmpp.on('chat', function (from, message) {
// check for debugging // 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() { function debug() {
if (debugging === '1') { if (debugging === '1') {
console.log('-------------'); console.log('[DEBUG]');
console.log(message); console.log(message);
console.log(from); console.log(from);
console.log('-------------');
}; };
} }
// 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 // 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 reply
if (message === '!help') { if (message === '!help') {
xmpp.send(from, 'https://wiki.vanderwarker.family/doku.php?id=code:vxmppb:commands'); xmpp.send(from, 'https://wiki.vanderwarker.family/doku.php?id=code:vxmppb:commands');
debug(); debug();
} }
// UNLOCKS // !unlocks reply
else if (message === '!unlocks') { else if (message === '!unlocks') {
if (from != user_jid) { if (from != user_jid) {
xmpp.send(from, pc); xmpp.send(from, pc);
@ -164,27 +198,20 @@ try {
if (error) { if (error) {
return console.log(error) return console.log(error)
}; };
// check for valid api response
if (!error && res.statusCode == 200) { if (!error && res.statusCode == 200) {
var unlockBody = new RegExp(':(.*)}'); var unlockBody = new RegExp(':(.*)}');
var unlockBodyMatch = body.match(unlockBody); var unlockBodyMatch = body.match(unlockBody);
// setup unlocks message
const unlockFinalMessage = user_name + " has " + unlockBodyMatch[1] + " unlocks for " + n; const unlockFinalMessage = user_name + " has " + unlockBodyMatch[1] + " unlocks for " + n;
xmpp.send(from, unlockFinalMessage); xmpp.send(from, unlockFinalMessage);
debug(); // call user with unlocks if asterisk enabled
} call(unlockFinalMessage);
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');
} }
// check for http errors
resError(error, res);
}); });
// output xmpp, and request errors if debugging is enabled.
debug(); debug();
} }
} }
@ -218,18 +245,7 @@ try {
}); });
debug(); debug();
} }
if (!error && res.statusCode == 401) { resError(error, res);
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');
}
}); });
debug(); debug();
} }
@ -260,18 +276,24 @@ try {
console.log("Tuya API: Unknown reply!"); console.log("Tuya API: Unknown reply!");
} }
} }
if (!error && res.statusCode == 401) { resError(error, res);
console.log('Invalid credentials'); });
xmpp.send(from, 'Invalid credentials'); debug();
} }
if (!error && res.statusCode == 404) { } else if (message === '!lightsauth') {
console.log('Record not found'); if (from != user_jid) {
xmpp.send(from, 'Record not found'); xmpp.send(from, pc);
} } else {
if (!error && res.statusCode == 500) { // refresh tokens for Tuya API
console.log('Server error.'); request(tuya_reauth_url, (error, res, body) => {
xmpp.send(from, 'Server error. Please !ping'); if (error) {
return console.log(error)
};
if (!error && res.statusCode == 200) {
xmpp.send(from, 'Refreshing Tuya API auth keys');
} }
resError(error, res);
}); });
debug(); debug();
} }
@ -290,18 +312,7 @@ try {
xmpp.send(from, stat); xmpp.send(from, stat);
console.log(stat); console.log(stat);
} }
if (!error && res.statusCode == 401) { resError(error, res);
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');
}
}); });
debug(); debug();
} }
@ -314,24 +325,12 @@ try {
if (error) { if (error) {
return console.log(error) return console.log(error)
}; };
if (!error && res.statusCode == 200) { if (!error && res.statusCode == 200) {
const stat = tuya_name + " has been turned off"; const stat = tuya_name + " has been turned off";
xmpp.send(from, stat); xmpp.send(from, stat);
console.log(stat); console.log(stat);
} }
if (!error && res.statusCode == 401) { resError(error, res);
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');
}
}); });
debug(); debug();
} }
@ -349,20 +348,8 @@ try {
if (!error && res.statusCode == 200) { if (!error && res.statusCode == 200) {
xmpp.send(from, tuya_name + " set to " + per + "%"); 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') { if (config.debug === '1') {
console.log('lights turned to ' + per + "%"); console.log('lights turned to ' + per + "%");
@ -390,18 +377,7 @@ try {
xmpp.send(from, tuya_name + "set to " + color); xmpp.send(from, tuya_name + "set to " + color);
console.log(tuya_name + "set to " + color); console.log(tuya_name + "set to " + color);
} }
if (!error && res.statusCode == 401) { resError(error, res);
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');
}
}); });
if (config.debug === '1') { if (config.debug === '1') {
console.log(tuya_name + "set to " + color); console.log(tuya_name + "set to " + color);
@ -423,14 +399,8 @@ try {
} }
}); });
// call admin alerting of authors ping // call admin alerting of authors ping
ami.action({ call_data = "hello. " + from + " has pinged you!"
'action': 'originate', call(call_data);
'channel': 'SIP/' + asterisk_callfrom,
'context': asterisk_context,
'callerID': asterisk_callerID,
'data': 'googletts.agi, \"hello. ' + from + ' has pinged you!\"',
'application': 'agi'
});
debug(); debug();
} }
@ -445,9 +415,7 @@ try {
console.log('error:', err); console.log('error:', err);
} else { } else {
let w = JSON.parse(body); 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: " + let wm = weather_city_name + " Weather: Tempature: " + w.main.temp + " degrees. Conditions: " + w.weather[0].main + ". 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: " +
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 // send weather to mqtt
client.publish('weather/temp', '' + w.main.temp + ''); client.publish('weather/temp', '' + w.main.temp + '');
@ -458,16 +426,7 @@ try {
// send weather to xmpp admin // send weather to xmpp admin
xmpp.send(from, wm); xmpp.send(from, wm);
// call admin with latest weather report // call admin with latest weather report
/* call(wm);
ami.action({
'action':'originate',
'channel':'SIP/' + asterisk_callfrom,
'context': asterisk_context,
'callerID' : asterisk_callerID,
'data': 'googletts.agi,' + wmc,
'application': 'agi'
});
*/
} }
}); });
debug(); debug();
@ -484,13 +443,13 @@ try {
// Share-A-Command which was #13, but kinda turned into a different command // 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 // This will generate a private channel, and add the users in the friends
// secion from config.ini // 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) // I am welcome to pull requests for other servers (maybe set type in config.ini)
else if (message === '!meet') { else if (message === '!meet') {
if (from != user_jid) { if (from != user_jid) {
xmpp.send(from, pc); xmpp.send(from, pc);
} else { } else {
if (xmpp_server_type === "ejabberd"){
// Create private XMPP room // Create private XMPP room
const createRoom = { const createRoom = {
method: 'POST', method: 'POST',
@ -576,7 +535,11 @@ try {
} }
}, 2000); }, 2000);
debug(); 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")
}}
} }
// End meeting // End meeting
@ -676,6 +639,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) { } catch (e) {
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");

View File

@ -1,7 +1,7 @@
{ {
"name": "vxmppb", "name": "vxmppb",
"description": "vanderwarker.family XMPP Bot", "description": "vanderwarker.family XMPP Bot",
"version": "1.0.0", "version": "1.1.0",
"keywords": [ "keywords": [
"xmpp", "xmpp",
"bot", "bot",
@ -35,9 +35,11 @@
"type": "git", "type": "git",
"url": "https://git.vanderwarker.family/vxmppb/vxmppb.git" "url": "https://git.vanderwarker.family/vxmppb/vxmppb.git"
}, },
"license": { "licenses": [
"type": "MIT" {
}, "type": "The MIT License",
"url": "http://www.opensource.org/licenses/mit-license.php"
}],
"homepage": "https://wiki.vanderwarker.family/doku.php?id=code:vxmppb:start", "homepage": "https://wiki.vanderwarker.family/doku.php?id=code:vxmppb:start",
"scripts": { "scripts": {
"test": "echo \"No tests available. Please consider helping with a pull request!\" && exit 0" "test": "echo \"No tests available. Please consider helping with a pull request!\" && exit 0"