Merge pull request 'Close #10' (#19) from stephen/vxmppb:iss-10 into main

Reviewed-on: vxmppb/vxmppb#19
This commit is contained in:
Stephen Vanderwarker 2022-03-31 16:31:57 -04:00
commit 4666afd597

View File

@ -1,3 +1,4 @@
// initialize required packages
const fs = require('fs');
const ini = require('ini');
const mastodon = require('mastodon');
@ -6,10 +7,14 @@ const request = require('request');
const rgbcolor = require('rgb-color');
const xmpp = require('simple-xmpp');
// check a config.ini is in current directory
try {
fs.readFileSync('./config.ini', 'utf-8');
// read config.ini
const config = ini.parse(fs.readFileSync('./config.ini', 'utf-8'));
// check if user debugging is enabled
const debugging = config.debug;
// Asterisk config
@ -77,11 +82,16 @@ try {
const xmpp_muc_password = config.xmpp.muc_password;
const xmpp_muc_server = config.xmpp.muc_server;
// 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!';
// setup weather_url for !weather command
let weather_url = `https://api.openweathermap.org/data/2.5/weather?id=${weather_city_code}&appid=${weather_key}&units=${weather_units}`
// initialize asterisk
var ami = new require('asterisk-manager')(asterisk_port, asterisk_host, asterisk_user, asterisk_password, false);
// initialize mqtt
var client = mqtt.connect('tcp://' + mqtt_user + ':' + mqtt_password + '@' + mqtt_server);
// initialize mastodon
var M = new mastodon({
access_token: `${mastodon_token}`,
timeout_ms: 60 * 1000,
@ -89,23 +99,27 @@ try {
})
// thx: https://stackoverflow.com/questions/48750528/get-direction-from-degrees
// converts numerical degree direction from weather api to a direction name
function getDirection(angle) {
var directions = ['North', 'North-East', 'East', 'South-East', 'South', 'South-West', 'West', 'North-West'];
var index = Math.round(((angle %= 360) < 0 ? angle + 360 : angle) / 45) % 8;
return directions[index];
}
// Once we're online, we'll send a message alerting admins
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}`
});
});
// wait for incoming messages
xmpp.on('chat', function(from, message) {
// check for debugging
function debug() {
if (debugging === '1') {
console.log('-------------');
@ -114,7 +128,9 @@ try {
console.log('-------------');
};
}
// setup friends list
const friends = Object.keys(config.friends);
// 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
@ -138,7 +154,7 @@ try {
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)
@ -172,6 +188,7 @@ try {
if (from != user_jid) {
xmpp.send(from, pc);
} else {
// get current light status
request(tuya_status_url, (error, res, body) => {
if (error) {
return console.log(error)
@ -210,6 +227,7 @@ try {
if (from != user_jid) {
xmpp.send(from, pc);
} else {
// turn on light
request(tuya_toggle_url + "?stat=true&devname=" + tuya_name, (error, res, body) => {
if (error) {
return console.log(error)
@ -239,6 +257,7 @@ try {
if (from != user_jid) {
xmpp.send(from, pc);
} else {
// turn off light
request(tuya_toggle_url + "?stat=false&devname=" + tuya_name, (error, res, body) => {
if (error) {
return console.log(error)
@ -270,6 +289,7 @@ try {
} else {
var per = message.substring(message.indexOf("brightness") + 10);
var perc = (per * 10)
// get brightness of light
request(tuya_bright_url + "?brite=" + perc + "&devname=" + tuya_name, (error, res, body) => {
if (error) {
return console.log(error)
@ -300,6 +320,7 @@ try {
if (from != user_jid) {
xmpp.send(from, pc);
} else {
// 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);
@ -337,14 +358,19 @@ try {
}
// Ping a user to let them know you would like to communicate
else if (message === '!ping') {
// send xmpp ping message to message author
xmpp.send(from, "Pinging " + user_name + "! Please stand by.");
// send xmpp ping message to admin
xmpp.send(user_jid, from + " has pinged you!");
// 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) {
if (err) {
console.log('error:', err);
}
});
// call admin alerting of authors ping
ami.action({
'action': 'originate',
'channel': 'SIP/' + asterisk_callfrom,
@ -361,7 +387,7 @@ try {
if (from != user_jid) {
xmpp.send(from, pc);
} else {
xmpp.send(from, "Checking weather. Please hold...\r\n \r\n");
// check current weather
request(weather_url, function(err, response, body) {
if (err) {
console.log('error:', err);
@ -371,12 +397,15 @@ try {
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";
// 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
xmpp.send(from, wm);
// call admin with latest weather report
/*
ami.action({
'action':'originate',
@ -405,6 +434,7 @@ try {
if (from != user_jid) {
xmpp.send(from, pc);
} else {
// Create private XMPP room
const createRoom = {
method: 'POST',
url: xmpp_api_url + '/create_room_with_opts',
@ -434,13 +464,18 @@ try {
},
json: true
};
// 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)
setTimeout(() => {
// loop through friends, authorize users, and send invites
for (const key in config.friends) {
// send xmpp message to friends alerting of new room
xmpp.send(key, "Meeting started: " + xmpp_muc_name + "@" + xmpp_muc_server);
// authorize users
const addMember = {
method: 'POST',
url: xmpp_api_url + 'set_room_affiliation',
@ -456,6 +491,7 @@ try {
},
json: true
};
// invite users
const inviteMember = {
method: 'POST',
url: xmpp_api_url + '/send_direct_invitation',
@ -482,7 +518,6 @@ try {
});
}
}, 2000);
///////////////////////////////////////
debug();
}
}
@ -492,6 +527,7 @@ try {
if (from != user_jid) {
xmpp.send(from, pc);
} else {
// end meeting (delete private xmpp room)
const endMeeting = {
method: 'POST',
url: xmpp_api_url + '/destroy_room',
@ -505,11 +541,14 @@ try {
},
json: true
};
// send room delete request
request(endMeeting, function(error, response, body) {
if (error) throw new Error(error);
console.log(body);
});
// send meeting ended to user_jid
xmpp.send(from, "Meeting ended");
// send meeting ended to friends
for (const key in config.friends) {
xmpp.send(key, "Meeting ended");
}
@ -524,8 +563,10 @@ try {
if (from != user_jid) {
xmpp.send(from, pc);
} else {
// send shutdown message
xmpp.send(from, "Alrighty, shutting down. Goodbye!");
debug();
// kill bot
process.exit(1);
}
}
@ -535,6 +576,7 @@ try {
if (from != user_jid) {
xmpp.send(from, pc);
} else {
// get user_jid vcard (profile info)
xmpp.getVCard(from, function(vcard) {
xmpp.send(from, vcard);
console.log(vcard)