Merge pull request 'Friend commands!' (#17) from stephen/vxmppb:iss-12 into main

Reviewed-on: vxmppb/vxmppb#17
This commit is contained in:
Stephen Vanderwarker 2022-03-31 14:01:58 -04:00
commit 5330154584
2 changed files with 266 additions and 94 deletions

View File

@ -1,39 +1,68 @@
api_pass = debug =
[asterisk]
callfrom =
callto =
callerID =
context =
host =
password =
port =
user =
[bot]
hostname =
jid =
password =
port =
[gotify]
key =
message =
priority =
title =
api_url = api_url =
api_user =
asterisk_callfrom = [mastodon]
asterisk_callto = token =
asterisk_callerID = toot =
asterisk_context = api_url =
asterisk_host =
asterisk_password = [mqtt]
asterisk_port = message =
asterisk_user = password =
bot_hostname = server =
bot_jid = topic =
bot_password = user =
[tuya]
name =
bright_url =
color_url =
status_url =
toggle_url =
[unlocks]
api_url =
pass =
user =
[user]
jid =
name =
server =
[weather]
city_code = city_code =
city_name = city_name =
debug = key =
gotify_api_key = units =
gotify_message =
gotify_priority = [xmpp]
gotify_title = api_url =
gotify_url = key =
mastodon_token = muc_name =
mastodon_toot = muc_password =
mastodon_url = muc_server =
mqtt_message =
mqtt_password = [friends]
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 =

259
index.js
View File

@ -12,56 +12,80 @@ try {
const config = ini.parse(fs.readFileSync('./config.ini', 'utf-8')); const config = ini.parse(fs.readFileSync('./config.ini', 'utf-8'));
const debugging = config.debug; const debugging = config.debug;
const api_pass = config.api_pass; // Asterisk config
const api_url = config.api_url; const asterisk_callfrom = config.asterisk.callfrom;
const api_user = config.api_user; const asterisk_callerID = config.asterisk.callerID;
const app_port = config.app_port; const asterisk_callto = config.asterisk.callerto;
const asterisk_callfrom = config.asterisk_callfrom; const asterisk_context = config.asterisk.context;
const asterisk_callto = config.asterisk_callerto; const asterisk_host = config.asterisk.host;
const asterisk_callerID = config.asterisk_callerID; const asterisk_password = config.asterisk.password;
const asterisk_context = config.asterisk_context; const asterisk_port = config.asterisk.port;
const asterisk_host = config.asterisk_host; const asterisk_user = config.asterisk.user;
const asterisk_password = config.asterisk_password;
const asterisk_port = config.asterisk_port; // Bot config
const asterisk_user = config.asterisk_user; const bot_hostname = config.bot.hostname;
const bot_hostname = config.bot_hostname; const bot_jid = config.bot.jid;
const bot_jid = config.bot_jid; const bot_password = config.bot.password;
const bot_password = config.bot_password; const bot_port = config.bot.port;
const bot_port = config.bot_port;
const city_code = config.city_code; // Gotify config
const city_name = config.city_name; const gotify_api_url = config.gotify.api_url;
const gotify_api_key = config.gotify_api_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;
const gotify_title = config.gotify_title; const gotify_title = config.gotify.title;
const gotify_url = config.gotify_url;
const mastodon_token = config.mastodon_token; // Mastodon config
const mastodon_toot = config.mastodon_toot; const mastodon_api_url = config.mastodon.api_url;
const mastodon_url = config.mastodon_url; const mastodon_token = config.mastodon.token;
const mqtt_message = config.mqtt_message; const mastodon_toot = config.mastodon.toot;
const mqtt_password = config.mqtt_password;
const mqtt_server = config.mqtt_server; // MQTT config
const mqtt_topic = config.mqtt_topic; const mqtt_message = config.mqtt.message;
const mqtt_user = config.mqtt_user; const mqtt_password = config.mqtt.password;
const tuya_name = config.tuya_name; const mqtt_server = config.mqtt.server;
const tuya_bright_url = config.tuya_bright_url; const mqtt_topic = config.mqtt.topic;
const tuya_color_url = config.tuya_color_url; const mqtt_user = config.mqtt.user;
const tuya_status_url = config.tuya_status_url;
const tuya_toggle_url = config.tuya_toggle_url; // Tuya (Smart Lights) config
const user_jid = config.user_jid //.split('/')[0]; const tuya_name = config.tuya.name;
const user_name = config.user_name; const tuya_bright_url = config.tuya.bright_url;
const weather_api_key = config.weather_api_key; const tuya_color_url = config.tuya.color_url;
const weather_units = config.weather_units; const tuya_status_url = config.tuya.status_url;
const tuya_toggle_url = config.tuya.toggle_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;
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!';
let weather_url = `https://api.openweathermap.org/data/2.5/weather?id=${city_code}&appid=${weather_api_key}&units=${weather_units}` let weather_url = `https://api.openweathermap.org/data/2.5/weather?id=${weather_city_code}&appid=${weather_key}&units=${weather_units}`
var ami = new require('asterisk-manager')(asterisk_port, asterisk_host, asterisk_user, asterisk_password, false); 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 client = mqtt.connect('tcp://' + mqtt_user + ':' + mqtt_password + '@' + mqtt_server);
var M = new mastodon({ var M = new mastodon({
access_token: `${mastodon_token}`, access_token: `${mastodon_token}`,
timeout_ms: 60*1000, timeout_ms: 60*1000,
api_url: `${mastodon_url}`, api_url: `${mastodon_api_url}`,
}) })
// thx: https://stackoverflow.com/questions/48750528/get-direction-from-degrees // thx: https://stackoverflow.com/questions/48750528/get-direction-from-degrees
@ -73,7 +97,7 @@ function getDirection(angle) {
// Once we're online, we'll send a message alerting admins // Once we're online, we'll send a message alerting admins
xmpp.on('online', function(data, to) { xmpp.on('online', function(data, to) {
console.log('Connected with JID: ' + data.jid.user); console.log('Connected with JID: ' + bot_jid);
xmpp.send(user_jid, "[ONLINE]", false); xmpp.send(user_jid, "[ONLINE]", false);
client.publish(mqtt_topic, mqtt_message); client.publish(mqtt_topic, mqtt_message);
M.post('statuses', { status: `${mastodon_toot}` }); M.post('statuses', { status: `${mastodon_toot}` });
@ -82,12 +106,15 @@ xmpp.on('online', function(data, to) {
xmpp.on('chat', function(from, message) { xmpp.on('chat', function(from, message) {
function debug(){ function debug(){
if (debugging === '1'){ if (debugging === '1'){
console.log('-------------');
console.log(message); console.log(message);
}; console.log(from);
console.log('-------------');
};
} }
let gurl = `${gotify_url}/message?token=${gotify_api_key}&message=${from} ${gotify_message}&title=${gotify_title}&priority=${gotify_priority}`; const friends = Object.keys(config.friends);
let gurle = encodeURI(gurl); 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
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');
@ -110,16 +137,16 @@ let gurle = encodeURI(gurl);
weekday[6] = "Saturday"; weekday[6] = "Saturday";
var n = weekday[d.getDay()]; var n = weekday[d.getDay()];
request(api_url + "?columns=" + n, (error, res, body) => { request(unlocks_api_url + "?columns=" + n, (error, res, body) => {
if (error) { if (error) {
return console.log(error) return console.log(error)
}; };
if (!error && res.statusCode == 200) { if (!error && res.statusCode == 200) {
var ulb = new RegExp(':(.*)}'); var unlockBody = new RegExp(':(.*)}');
var ule = body.match(ulb); var unlockBodyMatch = body.match(unlockBody);
const ur = user_name + " has " + ule[1] + " unlocks for " + n; const unlockFinalMessage = user_name + " has " + unlockBodyMatch[1] + " unlocks for " + n;
xmpp.send(from, ur); xmpp.send(from, unlockFinalMessage);
debug(); debug();
} }
if (!error && res.statusCode == 401) { if (!error && res.statusCode == 401) {
@ -157,7 +184,7 @@ let gurle = encodeURI(gurl);
xmpp.send(from, stat); xmpp.send(from, stat);
console.log(stat); console.log(stat);
} else { } else {
xmpp.send(from, "Unknown reply. Please reply ping to reach an admin."); xmpp.send(from, "Unknown reply from api call. Please reply ping to reach an admin.");
console.log("Tuya API: Unknown reply!"); console.log("Tuya API: Unknown reply!");
} }
} }
@ -307,7 +334,7 @@ let gurle = encodeURI(gurl);
xmpp.send(from, "Pinging " + user_name + "! Please stand by."); xmpp.send(from, "Pinging " + user_name + "! Please stand by.");
xmpp.send(user_jid, from + " has pinged you!"); xmpp.send(user_jid, from + " has pinged you!");
client.publish(mqtt_topic, from + ' has pinged you!'); client.publish(mqtt_topic, from + ' has pinged you!');
request.post(gurle, function (err, response, body) { request.post(gotifyEUrl, function (err, response, body) {
if(err){ if(err){
console.log('error:', err); console.log('error:', err);
} }
@ -334,9 +361,9 @@ let gurle = encodeURI(gurl);
console.log('error:', err); console.log('error:', err);
} else { } else {
let w = JSON.parse(body); let w = JSON.parse(body);
let wm = 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: \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"; 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: " + 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";
client.publish('weather/temp', 'w.main.temp'); client.publish('weather/temp', 'w.main.temp');
client.publish('weather/conditions', 'w.weather[0].main'); client.publish('weather/conditions', 'w.weather[0].main');
@ -364,7 +391,121 @@ let gurle = encodeURI(gurl);
xmpp.send(from, "Bye, have a beautiful time!"); xmpp.send(from, "Bye, have a beautiful time!");
debug(); debug();
} }
// Shutsdown bot if admin messages "goodbye"
// Share-A-Command (#13?)
else if (message === '!meet'){
if (from != user_jid){
xmpp.send(from, pc);
} else {
const createRoom = {
method: 'POST',
url: xmpp_api_url + '/create_room_with_opts',
headers: {
'Content-Type': 'application/json',
Authorization: xmpp_auth
},
body: {
name: xmpp_muc_name,
service: xmpp_muc_server,
host: bot_hostname,
options: {
allow_change_subj: 'false',
allow_private_messages: 'false',
allow_private_messages_from_visitors: 'false',
allow_visitor_nickchange: 'false',
allow_visitor_status: 'false',
lang: 'en',
mam: 'true',
members_only: 'true',
persistent: 'true',
public: 'false',
public_list: 'false',
title: 'Secure channel generated by https://vxmppb.tk',
allow_subscription: 'true'
}
},
json: true
};
request(createRoom, function (error, response, body) {
if (error) throw new Error(error);
console.log(body);
});
setTimeout(() => {
for (const key in config.friends) {
xmpp.send(key, "Meeting started: " + xmpp_muc_name + "@" + xmpp_muc_server);
const addMember = {
method: 'POST',
url: xmpp_api_url + 'set_room_affiliation',
headers: {
'Content-Type': 'application/json',
Authorization: xmpp_auth
},
body: {
name: xmpp_muc_name,
service: xmpp_muc_server,
jid: key,
affiliation: 'member'
},
json: true
};
const inviteMember = {
method: 'POST',
url: xmpp_api_url + '/send_direct_invitation',
headers: {
'Content-Type': 'application/json',
Authorization: xmpp_auth
},
body: {
name: xmpp_muc_name,
service: xmpp_muc_server,
password: '',
reason: 'vXMPPb Meeting Invite',
users: key
},
json: true
};
request(addMember, function (error, response, body) {
if (error) throw new Error(error);
console.log(body);
});
request(inviteMember, function (error, response, body) {
if (error) throw new Error(error);
console.log(body);
});
}}, 2000);
///////////////////////////////////////
debug();
}}
// End meeting
else if (message === '!endmeet'){
if (from != user_jid){
xmpp.send(from, pc);
} else {
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
};
request(endMeeting, function (error, response, body) {
if (error) throw new Error(error);
console.log(body);
});
xmpp.send(from, "Meeting ended");
for (const key in config.friends) {
xmpp.send(key, "Meeting ended");
}
}debug();}
// Shutsdown bot if admin sends shutdown command (linux)
else if (message === "sudo shutdown now -h"){ else if (message === "sudo shutdown now -h"){
if (from != user_jid){ if (from != user_jid){
xmpp.send(from, pc); xmpp.send(from, pc);
@ -375,7 +516,7 @@ let gurle = encodeURI(gurl);
}} }}
// Get users vCard (WIP) // Get users vCard (WIP)
else if (message === 'vcard' && from === user_jid){ else if (message === '!vcard'){
if (from != user_jid){ if (from != user_jid){
xmpp.send(from, pc); xmpp.send(from, pc);
} else { } else {
@ -410,6 +551,8 @@ xmpp.connect({
// Checks the XMPP server for new 'friend' requests // Checks the XMPP server for new 'friend' requests
console.log(xmpp.getRoster()); console.log(xmpp.getRoster());
} 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");