0.1.2 ready! Removed chalk, d2d, and express

This commit is contained in:
Stephen Vanderwarker 2021-12-07 16:43:46 -05:00
parent fdb01808bf
commit 65627d43a8
2 changed files with 39 additions and 39 deletions

View File

@ -1,7 +1,3 @@
const chalk = require('chalk');
const d2d = require('degrees-to-direction');
const express = require('express')
const app = express();
const fs = require('fs'); const fs = require('fs');
const ini = require('ini'); const ini = require('ini');
const mastodon = require('mastodon'); const mastodon = require('mastodon');
@ -51,6 +47,13 @@ const vfc_url = config.vfc_url;
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;
// thx: https://stackoverflow.com/questions/48750528/get-direction-from-degrees
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];
}
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);
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=${city_code}&appid=${weather_api_key}&units=${weather_units}`
var client = mqtt.connect('tcp://' + mqtt_user + ':' + mqtt_password + '@' + mqtt_server); var client = mqtt.connect('tcp://' + mqtt_user + ':' + mqtt_password + '@' + mqtt_server);
@ -65,8 +68,6 @@ xmpp.on('online', function(data, to) {
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}` });
app.get('/', (req, res) => res.send("<!DOCTYPE html><html><head><title>Bot Online!</title><meta name='viewport' content='width=device-width, initial-scale=1.0'/></head><body><p>Bot Online!</p></body></html>"));
app.listen(app_port, () => console.log(`Listening on ${app_port}!`));
}); });
xmpp.on('chat', function(from, message) { xmpp.on('chat', function(from, message) {
@ -74,7 +75,7 @@ xmpp.on('chat', function(from, message) {
// HELP // HELP
if (message === 'help' || message === 'Help') { if (message === 'help' || 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(chalk.blue('help')); console.log('help');
} }
// UNLOCKS // UNLOCKS
@ -92,7 +93,7 @@ xmpp.on('chat', function(from, message) {
request(api_url + "?columns=" + n, (error, res, body) => { request(api_url + "?columns=" + n, (error, res, body) => {
if (error) { if (error) {
return chalk.red(console.log(error)) return console.log(error)
}; };
if (!error && res.statusCode == 200) { if (!error && res.statusCode == 200) {
@ -100,22 +101,22 @@ 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(chalk.yellow(ur)); console.log(ur);
} }
if (!error && res.statusCode == 401) { if (!error && res.statusCode == 401) {
console.log(chalk.red('Invalid credentials')); console.log('Invalid credentials');
xmpp.send(from, 'Invalid credentials'); xmpp.send(from, 'Invalid credentials');
} }
if (!error && res.statusCode == 404) { if (!error && res.statusCode == 404) {
console.log(chalk.red('Record not found')); console.log('Record not found');
xmpp.send(from, 'Record not found'); xmpp.send(from, 'Record not found');
} }
if (!error && res.statusCode == 500) { if (!error && res.statusCode == 500) {
console.log(chalk.red('Server error.')); console.log('Server error.');
xmpp.send(from, 'Server error. Please ping admin'); xmpp.send(from, 'Server error. Please ping admin');
} }
}); });
console.log(chalk.blue('unlocks')); console.log('unlocks');
} }
// Check VFC balance // Check VFC balance
@ -130,16 +131,16 @@ xmpp.on('chat', function(from, message) {
}); });
request(vfc_url, (error, res, body) => { request(vfc_url, (error, res, body) => {
if (error) { if (error) {
return chalk.red(console.log(error)) return console.log(error)
}; };
if (!error && res.statusCode == 200) { if (!error && res.statusCode == 200) {
const balance = user_name + " has " + res.body + " VFC"; const balance = user_name + " has " + res.body + " VFC";
xmpp.send(from, balance); xmpp.send(from, balance);
console.log(chalk.yellow(balance)); console.log(balance);
} }
}); });
console.log(chalk.blue('vfc')); console.log('vfc');
} }
// Get latest RSS item from feed // Get latest RSS item from feed
@ -168,12 +169,12 @@ 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(chalk.blue('ping')); console.log('ping');
let guf = `${gotify_url}/message?token=${gotify_api_key}&message=${from} ${gotify_message}&title=${gotify_title}&priority=${gotify_priority}`; let guf = `${gotify_url}/message?token=${gotify_api_key}&message=${from} ${gotify_message}&title=${gotify_title}&priority=${gotify_priority}`;
let gufe = encodeURI(guf); let gufe = encodeURI(guf);
request.post(gufe, function (err, response, body) { request.post(gufe, function (err, response, body) {
if(err){ if(err){
console.log(chalk.red('error:', err)); console.log('error:', err);
} }
}); });
} }
@ -183,18 +184,18 @@ xmpp.on('chat', function(from, message) {
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){
console.log(chalk.red('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 = 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: " + d2d(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: " + d2d(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(chalk.yellow(wm)); 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');
client.publish('weather/wind/direction', 'd2d(w.wind.deg)'); client.publish('weather/wind/direction', 'getDirection(w.wind.deg)');
client.publish('weather/wind/speed', 'w.wind.speed'); client.publish('weather/wind/speed', 'w.wind.speed');
xmpp.send(from, wm); xmpp.send(from, wm);
/* /*
@ -209,31 +210,33 @@ xmpp.on('chat', function(from, message) {
*/ */
} }
}); });
console.log(chalk.blue('weather')); console.log('weather');
} }
// BYE // Sends a nice goodbye message
else if (message === 'bye' || message === "Bye" && from != user_jid) { else if (message === 'bye' || message === "Bye"){
xmpp.send(from, "Bye, have a beautiful time!"); xmpp.send(from, "Bye, have a beautiful time!");
if (from === user_jid || message === "!bye") { console.log('bye');
}
// Shutsdown bot if admin messages "goodbye"
else if (message === "goodbye" || from === user_jid){
xmpp.send(from, "Alrighty, shutting down. Goodbye!"); xmpp.send(from, "Alrighty, shutting down. Goodbye!");
console.log(chalk.red('Admin shutdown.')); console.log('Admin shutdown.');
process.exit(1) process.exit(1);
}
console.log(chalk.blue('bye'));
} }
// Get users vCard (WIP) // Get users vCard (WIP)
else if (message === 'vcard' && from === user_jid){ else if (message === 'vcard' && from === user_jid){
xmpp.getVCard(from, function (vcard) { xmpp.getVCard(from, function (vcard) {
xmpp.send(from, vcard); xmpp.send(from, vcard);
console.log(chalk.yellow(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 { 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"); 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");
console.log(chalk.blue('autoreply')); console.log('autoreply');
} }
}); });
@ -245,11 +248,11 @@ console.error(err);
// EXTENDING XEPS (WIP) // EXTENDING XEPS (WIP)
/* /*
xmpp.on('chatstate', function(from, state) { xmpp.on('chatstate', function(from, state) {
console.log(chalk.yellow(state)); console.log(state);
}); });
xmpp.on('buddy', function(jid, state, statusText, resource) { xmpp.on('buddy', function(jid, state, statusText, resource) {
console.log(chalk.yellow(state + statusText + resource)); console.log(state + statusText + resource);
}); });
*/ */
@ -262,4 +265,4 @@ xmpp.connect({
}); });
// Checks the XMPP server for new 'friend' requests // Checks the XMPP server for new 'friend' requests
console.log(chalk.yellow(xmpp.getRoster())); console.log(xmpp.getRoster());

View File

@ -22,9 +22,6 @@
], ],
"dependencies": { "dependencies": {
"asterisk-manager": "0.1.16", "asterisk-manager": "0.1.16",
"chalk": "2.4.2",
"degrees-to-direction": "1.1.1",
"express": "4.17.1",
"ini": "2.0.0", "ini": "2.0.0",
"mastodon": "1.2.2", "mastodon": "1.2.2",
"mqtt": "4.2.6", "mqtt": "4.2.6",