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

Reviewed-on: vxmppb/vxmppb#14
This commit is contained in:
Stephen Vanderwarker 2022-03-28 08:54:13 -04:00
commit 3e7e033ae7
3 changed files with 138 additions and 101 deletions

View File

@ -1,38 +1,39 @@
api_pass = api_pass =
api_url = api_url =
api_user = api_user =
asterisk_callfrom = asterisk_callfrom =
asterisk_callto = asterisk_callto =
asterisk_callerID = asterisk_callerID =
asterisk_context = asterisk_context =
asterisk_host = asterisk_host =
asterisk_password = asterisk_password =
asterisk_port = asterisk_port =
asterisk_user = asterisk_user =
bot_hostname = bot_hostname =
bot_jid = bot_jid =
bot_password = bot_password =
city_code = city_code =
city_name = city_name =
gotify_api_key = debug =
gotify_message = gotify_api_key =
gotify_priority = gotify_message =
gotify_title = gotify_priority =
gotify_url = gotify_title =
mastodon_token = gotify_url =
mastodon_toot = mastodon_token =
mastodon_url = mastodon_toot =
mqtt_message = mastodon_url =
mqtt_password = mqtt_message =
mqtt_server = mqtt_password =
mqtt_topic = mqtt_server =
mqtt_user = mqtt_topic =
tuya_name = mqtt_user =
tuya_bright_url = tuya_name =
tuya_color_url = tuya_bright_url =
tuya_status_url = tuya_color_url =
tuya_toggle_url = tuya_status_url =
user_jid = tuya_toggle_url =
user_name = user_jid =
weather_api_key = user_name =
weather_units = weather_api_key =
weather_units =

158
index.js
View File

@ -10,6 +10,8 @@ try {
fs.readFileSync('./config.ini', 'utf-8'); fs.readFileSync('./config.ini', 'utf-8');
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 api_pass = config.api_pass; const api_pass = config.api_pass;
const api_url = config.api_url; const api_url = config.api_url;
const api_user = config.api_user; 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_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 user_jid = config.user_jid; const user_jid = config.user_jid //.split('/')[0];
const user_name = config.user_name; const user_name = config.user_name;
const weather_api_key = config.weather_api_key; const weather_api_key = config.weather_api_key;
const weather_units = config.weather_units; 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 // thx: https://stackoverflow.com/questions/48750528/get-direction-from-degrees
function getDirection(angle) { function getDirection(angle) {
var directions = ['North', 'North-East', 'East', 'South-East', 'South', 'South-West', 'West', 'North-West']; var directions = ['North', 'North-East', 'East', 'South-East', 'South', 'South-West', 'West', 'North-West'];
@ -58,14 +71,6 @@ function getDirection(angle) {
return directions[index]; 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 // 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: ' + data.jid.user);
@ -75,14 +80,25 @@ xmpp.on('online', function(data, to) {
}); });
xmpp.on('chat', function(from, message) { 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 // HELP
if (message === 'help' || 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');
console.log('help'); debug();
} }
// UNLOCKS // 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 d = new Date();
var weekday = new Array(7); var weekday = new Array(7);
weekday[0] = "Sunday"; weekday[0] = "Sunday";
@ -104,7 +120,7 @@ xmpp.on('chat', function(from, message) {
var ule = body.match(ulb); var ule = body.match(ulb);
const ur = user_name + " has " + ule[1] + " unlocks for " + n; const ur = user_name + " has " + ule[1] + " unlocks for " + n;
xmpp.send(from, ur); xmpp.send(from, ur);
console.log(ur); debug();
} }
if (!error && res.statusCode == 401) { if (!error && res.statusCode == 401) {
console.log('Invalid credentials'); console.log('Invalid credentials');
@ -119,10 +135,13 @@ xmpp.on('chat', function(from, message) {
xmpp.send(from, 'Server error. Please ping admin'); xmpp.send(from, 'Server error. Please ping admin');
} }
}); });
console.log('unlocks'); debug();
} }}
// Tuya light control // 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) => { request(tuya_status_url,(error, res, body) => {
if (error) { if (error) {
return console.log(error) return console.log(error)
@ -155,9 +174,12 @@ xmpp.on('chat', function(from, message) {
xmpp.send(from, 'Server error. Please ping admin'); xmpp.send(from, 'Server error. Please ping admin');
} }
}); });
console.log('lights status'); debug();
} }}
else if (message === tuya_name + ' on') { 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) => { request(tuya_toggle_url + "?stat=true&devname=" + tuya_name,(error, res, body) => {
if (error) { if (error) {
return console.log(error) return console.log(error)
@ -181,9 +203,12 @@ xmpp.on('chat', function(from, message) {
xmpp.send(from, 'Server error. Please ping admin'); xmpp.send(from, 'Server error. Please ping admin');
} }
}); });
console.log('lights on'); debug();
} }}
else if (message === tuya_name + ' off') { 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) => { request(tuya_toggle_url + "?stat=false&devname=" + tuya_name,(error, res, body) => {
if (error) { if (error) {
return console.log(error) return console.log(error)
@ -207,10 +232,12 @@ xmpp.on('chat', function(from, message) {
xmpp.send(from, 'Server error. Please ping admin'); xmpp.send(from, 'Server error. Please ping admin');
} }
}); });
console.log('lights off'); debug();
} }}
else if (message.includes(tuya_name + ' brightness')) {
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 per=message.substring(message.indexOf("brightness") + 10);
var perc=(per*10) var perc=(per*10)
request(tuya_bright_url + "?brite=" + perc + "&devname=" + tuya_name,(error, res, body) => { 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'); xmpp.send(from, 'Server error. Please ping admin');
} }
}); });
console.log('lights turned to ' + per + "%"); if (config.debug === '1'){
} console.log('lights turned to ' + per + "%");}
}}
else if (message.includes(tuya_name + " color")) { 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) => {}) request(tuya_toggle_url + "?stat=true&devname=" + tuya_name,(error, res, body) => {})
var color=message.substring(message.indexOf("color") + 6); var color=message.substring(message.indexOf("color") + 6);
var colorRgb=rgbcolor(color); var colorRgb=rgbcolor(color);
@ -269,14 +299,19 @@ xmpp.on('chat', function(from, message) {
xmpp.send(from, 'Server error. Please ping admin'); 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 // 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(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) {
if(err){
console.log('error:', err);
}
});
ami.action({ ami.action({
'action':'originate', 'action':'originate',
'channel':'SIP/' + asterisk_callfrom, 'channel':'SIP/' + asterisk_callfrom,
@ -285,18 +320,14 @@ xmpp.on('chat', function(from, message) {
'data': 'googletts.agi, \"hello. ' + from + ' has pinged you!\"', 'data': 'googletts.agi, \"hello. ' + from + ' has pinged you!\"',
'application': 'agi' 'application': 'agi'
}); });
console.log('ping'); debug();
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);
}
});
} }
// Check the weather using weatherunderground API // 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"); xmpp.send(from, "Checking weather. Please hold...\r\n \r\n");
request(weather_url, function (err, response, body) { request(weather_url, function (err, response, body) {
if(err){ 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"; 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 = 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";
console.log(wm);
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');
client.publish('weather/pressure', 'w.main.pressure'); 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 // Sends a nice goodbye message
else if (message === 'bye' || message === "Bye"){ else if (message === '!bye'){
xmpp.send(from, "Bye, have a beautiful time!"); xmpp.send(from, "Bye, have a beautiful time!");
console.log('bye'); debug();
}
// 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);
} }
// 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) // Get users vCard (WIP)
else if (message === 'vcard' && from === user_jid){ else if (message === 'vcard' && from === user_jid){
if (from != user_jid){
xmpp.send(from, pc);
} else {
xmpp.getVCard(from, function (vcard) { xmpp.getVCard(from, function (vcard) {
xmpp.send(from, vcard); xmpp.send(from, vcard);
console.log(vcard) console.log(vcard)
}) })
} }}
// If the user sends something we don't have logic for, send them info about the project // If the user sends something we don't have logic for, send them info about the project
else { 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"); xmpp.send(from, "This is an auto replying bot. Please see https://wiki.vanderwarker.family/doku.php?id=code:vxmppb:commands");
console.log('autoreply'); debug();
} }
}); });
@ -375,7 +410,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");
} console.log("and choose your prefered install method.")
}

View File

@ -1,7 +1,7 @@
{ {
"name": "vxmppb", "name": "vxmppb",
"description": "vanderwarker.family XMPP Bot", "description": "vanderwarker.family XMPP Bot",
"version": "0.1.2", "version": "1.0.0",
"keywords": [ "keywords": [
"xmpp", "xmpp",
"bot", "bot",
@ -26,7 +26,7 @@
"mqtt": "4.2.6", "mqtt": "4.2.6",
"request": "2.88.0", "request": "2.88.0",
"rgb-color": "2.1.2", "rgb-color": "2.1.2",
"simple-xmpp": "2.3.1" "simple-xmpp": "1.3.0"
}, },
"bugs": { "bugs": {
"url": "https://git.vanderwarker.family/vxmppb/vxmppb/issues" "url": "https://git.vanderwarker.family/vxmppb/vxmppb/issues"