Compare commits

...

71 Commits
0.1.1 ... main

Author SHA1 Message Date
Stephen Vanderwarker b38c528575 Merge pull request 'Merge stephen/vxmppb' (#25) from stephen/vxmppb:main into main
Reviewed-on: #25
I need to update the docker image! Sorry (Coming soon? :P)
2022-06-02 20:52:31 -04:00
Stephen Vanderwarker ca65ed3c75
Adding support message for server type
#20
2022-06-02 20:43:36 -04:00
Stephen Vanderwarker f21b00c169
[NPM] minor version bump
1.1.0
2022-05-26 19:39:19 -04:00
Stephen Vanderwarker 6ad84aeef4
check server type
hardcoding :'(
needs to loop, seperate config, or section?
2022-05-26 19:37:42 -04:00
Stephen Vanderwarker 805535cfc3
Beginning work on #20 2022-05-26 19:18:58 -04:00
Stephen Vanderwarker 95835fa862
Adding server_type for #20 2022-05-26 19:18:40 -04:00
Stephen Vanderwarker d5dc804f60
checking for mqtt_enabled
Adding comments
2022-05-26 19:14:36 -04:00
Stephen Vanderwarker b2444eb609
Removing MQTT message inbound. 2022-05-26 19:06:33 -04:00
Stephen Vanderwarker 4045b29b72
moving calls to asterisk.enabled
abc, enable flag gotify
enable flag mastodon
enable flag mqtt
2022-05-07 21:01:35 -04:00
Stephen Vanderwarker 92ecfeed1a
changing formatting for asterisk calls config
config.ini.dist changes asterisk, adds mastodon, mqtt, and gotify enable flags
check if mastodon send toots enabled for on.connect
2022-05-07 20:59:41 -04:00
Stephen Vanderwarker d62efc18bf
Get messages from MQTT
catching ctrl+c
2022-05-02 17:16:45 -04:00
Stephen Vanderwarker 2462dce4f8
changed constant for config.calls
function for calls (see #24)
re-beautified code
2022-04-29 20:20:34 -04:00
Stephen Vanderwarker 4f8d74c7e3
Adding enable/disable calls to config.ini.dist 2022-04-29 17:23:21 -04:00
Stephen Vanderwarker ac0338d220
Adding config to enable/disable calls. 2022-04-29 17:22:57 -04:00
Stephen Vanderwarker fae2c435ad
!! 2022-04-27 16:38:16 -04:00
Stephen Vanderwarker 1259729c91
Removing wmc, renamed wmc in asterisk call to wm.
Re-enabled call asterisk for weather report.
2022-04-27 16:35:53 -04:00
Stephen Vanderwarker 1af04358d2
Re-adding docker badge 2022-04-27 15:22:52 -04:00
Stephen Vanderwarker daaebbd507
Fixing license 2022-04-27 15:18:46 -04:00
Stephen Vanderwarker 1573f91271
Merge branch 'main' of https://git.vanderwarker.family/stephen/vxmppb 2022-04-26 14:03:47 -04:00
Stephen Vanderwarker f2a5bc5415
spelling 2022-04-26 14:03:15 -04:00
Stephen Vanderwarker 2f4f1fc400 Changing debug message style,
See #24, for !tootunlocks
2022-04-26 11:54:14 -04:00
Stephen Vanderwarker 6f2e29c95e spacing. 2022-04-25 20:42:23 -04:00
stephen 2aafdd6c42 Adding tuya reauth, will become crontab in documentation.
Adding function for request error responses (see #24)
2022-04-25 20:36:36 -04:00
Stephen Vanderwarker eb4d9d6b7c Merge pull request 'Removing Jenkinsfile' (#23) from dev into main
Reviewed-on: #23
2022-04-24 08:17:16 -04:00
Stephen Vanderwarker 62c97f9f78 Removing Jenkinsfile 2022-04-24 08:15:22 -04:00
Stephen Vanderwarker daa7c076f0 Merge pull request 'adding mastodon user to config.ini.dist' (#22) from stephen/vxmppb:main into main
Reviewed-on: #22
2022-04-24 08:09:40 -04:00
Stephen Vanderwarker 2e1b02f955 Merge branch 'main' into main 2022-04-24 08:09:33 -04:00
Stephen Vanderwarker e2842443c4 adding mastodon user to config.ini.dist 2022-04-24 08:08:33 -04:00
Stephen Vanderwarker b5258b7e30 Merge pull request 'main' (#21) from stephen/vxmppb:main into main
Reviewed-on: #21
2022-04-24 08:06:20 -04:00
Stephen Vanderwarker d61d768215 Adding mastodon user, commented out posting toot. 2022-04-24 08:02:20 -04:00
Stephen Vanderwarker 4bf12d42ab Merge branch 'main' of https://git.vanderwarker.family/stephen/vxmppb 2022-04-23 23:31:11 -04:00
Stephen Vanderwarker 71f9d6eb9a Version bump mqtt 2022-04-23 23:30:47 -04:00
Stephen Vanderwarker b48e039783 Fixing weather. Seems like an odd an incorrect way to fix, but alas...it works. 2022-04-23 23:27:14 -04:00
Stephen Vanderwarker 30fd59b4f6 Changing issue template. 2022-04-23 22:50:59 -04:00
Stephen Vanderwarker 9b41de2e51
Adding subscription back as it will only accept ones from [friends] in config.ini 2022-04-04 20:48:51 -04:00
Stephen Vanderwarker 7f6cbd36a0
Readding invite code. Send meeting messages to friends, as admin starts it.
Removing second message for failed admin shutdown.
Changing status message.
2022-04-01 21:32:45 -04:00
Stephen Vanderwarker 9695b125a2
Removing invite. Just adding user to channel as member.
removing white space.
2022-04-01 09:40:37 -04:00
Stephen Vanderwarker 4666afd597 Merge pull request 'Close #10' (#19) from stephen/vxmppb:iss-10 into main
Reviewed-on: #19
2022-03-31 16:31:57 -04:00
Stephen Vanderwarker 4985fd378c Merge branch 'main' into iss-10 2022-03-31 16:31:35 -04:00
Stephen Vanderwarker 08b5727c31
Close #10 2022-03-31 16:29:06 -04:00
Stephen Vanderwarker 3598bd5082 Merge pull request 'Close #9' (#18) from stephen/vxmppb:iss-9 into main
Reviewed-on: #18
2022-03-31 14:19:00 -04:00
Stephen Vanderwarker 3abe2454c6
Close #9 2022-03-31 14:15:12 -04:00
Stephen Vanderwarker 5330154584 Merge pull request 'Friend commands!' (#17) from stephen/vxmppb:iss-12 into main
Reviewed-on: #17
2022-03-31 14:01:58 -04:00
Stephen Vanderwarker 34c1a56d01 Merge branch 'main' into iss-12 2022-03-31 14:01:13 -04:00
Stephen Vanderwarker b300195584
Refactoring configs. Adding friends commands.
See #10 #12 and #15
2022-03-31 13:53:16 -04:00
Stephen Vanderwarker 13472e570f
Rearranging, and renaming. 2022-03-31 13:39:09 -04:00
Stephen Vanderwarker dbc952b780
Starting restructure for config.ini.dist
See #15
2022-03-29 10:26:03 -04:00
Stephen Vanderwarker 8c06219e23
Adding ejabberd api url for new !friend command. Adding friends section
See #12
2022-03-29 03:35:27 -04:00
Stephen Vanderwarker 3e7e033ae7 Merge pull request '1.0.0' (#14) from stephen/vxmppb:main into main
Reviewed-on: #14
2022-03-28 08:54:13 -04:00
Stephen Vanderwarker 15ac5eed4e
Added debugging. Added private commands.
npm version bump 1.0.0
2022-03-27 19:27:13 -04:00
Stephen Vanderwarker ee857f658c
Version bump 1.0.0 as I will be using semantic versioning, and the command styles changed.
Also, downgrading simple-xmpp, until issue #13 is fixed.
2022-03-27 08:38:33 -04:00
Stephen Vanderwarker 768f405099
Adding debugging 2022-03-26 13:40:26 -04:00
Stephen Vanderwarker e6ade36780
Adding docker support
Signed-off-by: stephen <stephen@vanderwarker.family>
2022-02-27 11:24:25 -05:00
Stephen Vanderwarker ce90a74e68 Merge pull request 'Adding color control' (#8) from stephen/vxmppb:tuya into main
Reviewed-on: #8
2022-01-30 14:01:27 -05:00
Stephen Vanderwarker de35cedb1f
Adding Tuya color control 2022-01-30 13:55:13 -05:00
Stephen Vanderwarker 54912d6376
Adding rgb-color as dependency
See #5
2022-01-30 13:54:18 -05:00
Stephen Vanderwarker ca69387d93
Adding tuya_color_url
See #5
2022-01-30 13:53:11 -05:00
Stephen Vanderwarker fa38276c04 Merge pull request 'Adding brightness control' (#7) from stephen/vxmppb:tuya into main
Reviewed-on: #7
2022-01-29 22:53:24 -05:00
Stephen Vanderwarker c34b89e5ef Merge branch 'main' of https://git.vanderwarker.family/vxmppb/vxmppb into tuya 2022-01-29 22:50:54 -05:00
Stephen Vanderwarker 2531e35682
Adding brightness control
See #5
2022-01-29 22:40:21 -05:00
Stephen Vanderwarker f2235b0a8a Merge pull request 'Adding Tuya for lights (Uses Tuya API)' (#6) from stephen/vxmppb:tuya into main
Reviewed-on: #6
2022-01-27 20:33:27 -05:00
Stephen Vanderwarker 530b6bdeae
Removing RSS, and VFC dependencies, and code. Adding XMPP presence 2022-01-27 20:27:43 -05:00
Stephen Vanderwarker 5ec19de979
Adding Tuya for lights (Uses Tuya API)
Removed app_port from config, as it's depreciated.
2022-01-27 10:48:21 -05:00
Stephen Vanderwarker 2056c5a7f2 Adding wiki link for installation methods 2021-12-12 09:17:22 -05:00
Stephen Vanderwarker 6bcd1c1e38 Error catching when user doesn't have config.ini 2021-12-12 09:15:09 -05:00
Stephen Vanderwarker 27678bdbf8 Changing install links. 2021-12-10 07:35:11 -05:00
Stephen Vanderwarker b99c4c3556 0.1.2 2021-12-07 16:44:49 -05:00
Stephen Vanderwarker 65627d43a8 0.1.2 ready! Removed chalk, d2d, and express 2021-12-07 16:43:46 -05:00
Stephen Vanderwarker fdb01808bf Ignore visualstudio files 2021-12-07 16:19:24 -05:00
Stephen Vanderwarker fd306dd889 !! 2021-12-07 16:18:41 -05:00
Stephen Vanderwarker 08851d3a6b Prepping for 0.1.2 2021-12-07 16:17:31 -05:00
9 changed files with 730 additions and 308 deletions

8
.dockerignore Normal file
View File

@ -0,0 +1,8 @@
node_modules
package-lock.json
config.ini
build/
npm-debug.log
.vs/
Dockerfile
Jenkinsfile

View File

@ -11,6 +11,6 @@
## Specifications
- Version:
- Node JS Version:
- Platform:
- Subsystem:
- vXMPPb Version:

1
.gitignore vendored
View File

@ -4,3 +4,4 @@ config.ini
build/
.npmignore
npm-debug.log
.vs/

6
Dockerfile Normal file
View File

@ -0,0 +1,6 @@
FROM node:16-alpine
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
CMD ["node", "index.js"]

21
Jenkinsfile vendored
View File

@ -1,21 +0,0 @@
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building..'
}
}
stage('Test') {
steps {
echo 'Testing..'
}
}
stage('Deploy') {
steps {
echo 'Deploying....'
}
}
}
}

View File

@ -4,19 +4,19 @@ Vanderwarker Family XMPP Bot.
----
[![NPM](https://nodei.co/npm/vxmppb.png?compact=true)](https://nodei.co/npm/vxmppb/)
[![Known Vulnerabilities](https://snyk.io/test/npm/vxmppb/badge.svg)](https://snyk.io/test/npm/vxmppb)
![npm](https://img.shields.io/npm/dt/vxmppb)
[![Build Status](https://build.vanderwarker.dev/buildStatus/icon?job=vxmppb)](https://build.vanderwarker.dev/job/vxmppb/)
![docker](https://img.shields.io/docker/pulls/vxmppb/vxmppb.svg)
![npm](https://img.shields.io/npm/dt/vxmppb)
### :book: How To Install
You can install using:
- [npm](https://npmjs.com/package/vxmppb)
- [Wiki](https://wiki.vanderwarker.family/doku.php?id=code:vxmppb:install)
- [npm](https://wiki.vanderwarker.family/doku.php?id=code:vxmppb:install:npm)
- [Git](https://wiki.vanderwarker.family/doku.php?id=code:vxmppb:install:git)
- [Docker](https://wiki.vanderwarker.family/doku.php?id=code:vxmppb:install:docker)
### :1234: Commands
[See here](https://wiki.vanderwarker.family/doku.php?id=code:vxmppb:commands)

View File

@ -1,34 +1,75 @@
api_pass =
api_url =
api_user =
app_port =
asterisk_callfrom =
asterisk_callerID =
asterisk_context =
asterisk_host =
asterisk_password =
asterisk_port =
asterisk_user =
bot_hostname =
bot_jid =
bot_password =
debug =
[asterisk]
callfrom =
callto =
callerID =
context =
enabled =
host =
password =
port =
user =
[bot]
hostname =
jid =
password =
port =
[gotify]
api_url =
enabled =
key =
message =
priority =
title =
[mastodon]
token =
toot =
api_url =
user =
enabled =
[mqtt]
enabled =
message =
password =
server =
topic =
user =
[tuya]
name =
bright_url =
color_url =
status_url =
toggle_url =
reauth_url
[unlocks]
api_url =
pass =
user =
[user]
jid =
name =
server =
[weather]
city_code =
city_name =
gotify_api_key =
gotify_message =
gotify_priority =
gotify_title =
gotify_url =
mastodon_key =
mastodon_toot =
mastodon_url =
mqtt_message =
mqtt_password =
mqtt_server =
mqtt_topic =
mqtt_user =
rss_url =
user_jid =
user_name =
weather_api_key =
weather_units =
key =
units =
[xmpp]
api_url =
key =
muc_name =
muc_password =
muc_server =
server_type =
[friends]

861
index.js Normal file → Executable file
View File

@ -1,263 +1,652 @@
const chalk = require('chalk');
const d2d = require('degrees-to-direction');
const express = require('express')
const app = express();
// initialize required packages
const fs = require('fs');
const ini = require('ini');
const mastodon = require('mastodon');
const mqtt = require('mqtt');
const request = require('request');
let Parser = require('rss-parser');
let parser = new Parser();
const rgbcolor = require('rgb-color');
const xmpp = require('simple-xmpp');
const config = ini.parse(fs.readFileSync('./config.ini', 'utf-8'));
const api_pass = config.api_pass;
const api_url = config.api_url;
const api_user = config.api_user;
const app_port = config.app_port;
const asterisk_callfrom = config.asterisk_callfrom;
const asterisk_callerID = config.asterisk_callerID;
const asterisk_context = config.asterisk_context;
const asterisk_host = config.asterisk_host;
const asterisk_password = config.asterisk_password;
const asterisk_port = config.asterisk_port;
const asterisk_user = config.asterisk_user;
const bot_hostname = config.bot_hostname;
const bot_jid = config.bot_jid;
const bot_password = config.bot_password;
const city_code = config.city_code;
const city_name = config.city_name;
const gotify_api_key = config.gotify_api_key;
const gotify_message = config.gotify_message;
const gotify_priority = config.gotify_priority;
const gotify_title = config.gotify_title;
const gotify_url = config.gotify_url;
const mastodon_token = config.mastodon_token;
const mastodon_toot = config.mastodon_toot;
const mastodon_url = config.mastodon_url;
const mqtt_message = config.mqtt_message;
const mqtt_password = config.mqtt_password;
const mqtt_server = config.mqtt_server;
const mqtt_topic = config.mqtt_topic;
const mqtt_user = config.mqtt_user;
const rpc_url = config.rpc_url;
const user_jid = config.user_jid;
const user_name = config.user_name;
const vfc_url = config.vfc_url;
const weather_api_key = config.weather_api_key;
const weather_units = config.weather_units;
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
xmpp.on('online', function(data, to) {
console.log('Connected with JID: ' + data.jid.user);
xmpp.send(user_jid, "[ONLINE]", false);
client.publish(mqtt_topic, mqtt_message);
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}!`));
});
// check a config.ini is in current directory
try {
fs.readFileSync('./config.ini', 'utf-8');
xmpp.on('chat', function(from, message) {
// read config.ini
const config = ini.parse(fs.readFileSync('./config.ini', 'utf-8'));
// check if user debugging is enabled
const debugging = config.debug;
// HELP
if (message === 'help' || message === 'Help') {
xmpp.send(from, 'https://vanderwarker.family/help/xmpp#chatbot');
console.log(chalk.blue('help'));
}
// Asterisk config
const asterisk_callfrom = config.asterisk.callfrom;
const asterisk_callerID = config.asterisk.callerID;
const asterisk_callto = config.asterisk.callerto;
const asterisk_context = config.asterisk.context;
const calls_enabled = config.asterisk.enabled;
const asterisk_host = config.asterisk.host;
const asterisk_password = config.asterisk.password;
const asterisk_port = config.asterisk.port;
const asterisk_user = config.asterisk.user;
// UNLOCKS
else if (message === 'unlocks' || message === 'Unlocks') {
var d = new Date();
var weekday = new Array(7);
weekday[0] = "Sunday";
weekday[1] = "Monday";
weekday[2] = "Tuesday";
weekday[3] = "Wednesday";
weekday[4] = "Thursday";
weekday[5] = "Friday";
weekday[6] = "Saturday";
var n = weekday[d.getDay()];
// Bot config
const bot_hostname = config.bot.hostname;
const bot_jid = config.bot.jid;
const bot_password = config.bot.password;
const bot_port = config.bot.port;
request(api_url + "?columns=" + n, (error, res, body) => {
if (error) {
return chalk.red(console.log(error))
};
// Gotify config
const gotify_api_url = config.gotify.api_url;
const gotify_enabled = config.gotify.enabled;
const gotify_key = config.gotify.key;
const gotify_message = config.gotify.message;
const gotify_priority = config.gotify.priority;
const gotify_title = config.gotify.title;
if (!error && res.statusCode == 200) {
var ulb = new RegExp(':(.*)}');
var ule = body.match(ulb);
const ur = user_name + " has " + ule[1] + " unlocks for " + n;
xmpp.send(from, ur);
console.log(chalk.yellow(ur));
}
if (!error && res.statusCode == 401) {
console.log(chalk.red('Invalid credentials'));
xmpp.send(from, 'Invalid credentials');
}
if (!error && res.statusCode == 404) {
console.log(chalk.red('Record not found'));
xmpp.send(from, 'Record not found');
}
if (!error && res.statusCode == 500) {
console.log(chalk.red('Server error.'));
xmpp.send(from, 'Server error. Please ping admin');
}
});
console.log(chalk.blue('unlocks'));
}
// Mastodon config
const mastodon_api_url = config.mastodon.api_url;
const mastodon_enabled = config.mastodon.enabled;
const mastodon_token = config.mastodon.token;
const mastodon_toot = config.mastodon.toot;
const mastodon_user = config.mastodon.user;
// Send VFC from coinbase to Pixel2 (Metamask)
else if (message === "vfc" || message === "Vfc") {
ami.action({
'action':'originate',
'channel':'SIP/' + asterisk_callfrom,
'context': asterisk_context,
'callerID' : asterisk_callerID,
'exten' : '832',
'priority' : '1',
});
request(vfc_url, (error, res, body) => {
if (error) {
return chalk.red(console.log(error))
};
// MQTT config
const mqtt_enabled = config.mqtt.enabled;
const mqtt_message = config.mqtt.message;
const mqtt_password = config.mqtt.password;
const mqtt_server = config.mqtt.server;
const mqtt_topic = config.mqtt.topic;
const mqtt_user = config.mqtt.user;
if (!error && res.statusCode == 200) {
const balance = user_name + " has " + res.body + " VFC";
xmpp.send(from, balance);
console.log(chalk.yellow(balance));
}
});
console.log(chalk.blue('vfc'));
}
// Tuya (Smart Lights) config
const tuya_name = config.tuya.name;
const tuya_bright_url = config.tuya.bright_url;
const tuya_color_url = config.tuya.color_url;
const tuya_status_url = config.tuya.status_url;
const tuya_toggle_url = config.tuya.toggle_url;
const tuya_reauth_url = config.tuya.reauth_url;
// Get latest toots (RSS)
else if (message === "rss" || message === "Rss") {
(async () => {
let feed = await parser.parseURL(rss_url);
// console.log(feed.title);
feed.items.forEach(item => {
console.log(item.title + ':' + item.link)
xmpp.send(from, item.title)
});
})();
}
// Unlocks API config
const unlocks_api_url = config.unlocks.api_url;
const unlocks_pass = config.unlocks.pass;
const unlocks_user = config.unlocks.user;
// PING
else if (message === "ping" || message === 'Ping') {
xmpp.send(from, "Pinging " + user_name + "! Please stand by.");
xmpp.send(user_jid, from + " has pinged you!");
client.publish(mqtt_topic, from + ' has pinged you!');
ami.action({
'action':'originate',
'channel':'SIP/' + asterisk_callfrom,
'context': asterisk_context,
'callerID' : asterisk_callerID,
'data': 'googletts.agi, \"hello. ' + from + ' has pinged you!\"',
'application': 'agi'
});
console.log(chalk.blue('ping'));
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(chalk.red('error:', err));
}
});
}
// 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
else if (message === "weather" || message === 'Weather') {
xmpp.send(from, "Checking weather. Please hold...\r\n \r\n");
request(weather_url, function (err, response, body) {
if(err){
console.log(chalk.red('error:', err));
} else {
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: " +
w.main.pressure + " \r\n Wind Direction: " + d2d(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: " +
w.main.pressure + ". Wind Direction: " + d2d(w.wind.deg) + ". Wind Speed: " + w.wind.speed + " miles per hour \r\n";
console.log(chalk.yellow(wm));
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', 'd2d(w.wind.deg)');
client.publish('weather/wind/speed', 'w.wind.speed');
xmpp.send(from, wm);
/*
ami.action({
'action':'originate',
'channel':'SIP/' + asterisk_callfrom,
'context': asterisk_context,
'callerID' : asterisk_callerID,
'data': 'googletts.agi,' + wmc,
'application': 'agi'
});
*/
}
});
console.log(chalk.blue('weather'));
}
// 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;
// BYE
else if (message === 'bye' || message === "Bye" && from != user_jid) {
xmpp.send(from, "Bye, have a beautiful time!");
if (from === user_jid || message === "!bye") {
xmpp.send(from, "Alrighty, shutting down. Goodbye!");
console.log(chalk.red('Admin shutdown.'));
process.exit(1)
}
console.log(chalk.blue('bye'));
}
// 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;
const xmpp_server_type = config.xmpp.server_type;
// VCARD (WIP)
else if (message === 'vcard' && from === user_jid){
xmpp.getVCard(from, function (vcard) {
xmpp.send(from, vcard);
console.log(chalk.yellow(vcard))
})
}
// 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}`
// AUTO REPLY (ALL OTHER MESSAGES)
else {
xmpp.send(from, "This is an auto replying bot. Reply \"help\" for more info.");
console.log(chalk.blue('autoreply'));
}
});
// 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,
api_url: `${mastodon_api_url}`,
})
// CATCH XMPP ERRORS
xmpp.on('error', function(err) {
console.error(err);
});
// 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];
}
// setup friends list
const friends = Object.keys(config.friends);
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
if (mqtt_enabled === "1"){
client.publish(mqtt_topic, mqtt_message);
}
// toot online message
if (mastodon_enabled === "1"){
M.post('statuses', {
status: `${mastodon_toot}`
});
}
});
// wait for incoming messages
xmpp.on('chat', function (from, message) {
// 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() {
if (debugging === '1') {
console.log('[DEBUG]');
console.log(message);
console.log(from);
};
}
// 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
let gotifyURL = `${gotify_api_url}/message?token=${gotify_key}&message=${from} ${gotify_message}&title=${gotify_title}&priority=${gotify_priority}`;
let gotifyEUrl = encodeURI(gotifyURL);
// !help reply
if (message === '!help') {
xmpp.send(from, 'https://wiki.vanderwarker.family/doku.php?id=code:vxmppb:commands');
debug();
}
// !unlocks reply
else if (message === '!unlocks') {
if (from != user_jid) {
xmpp.send(from, pc);
} else {
var d = new Date();
var weekday = new Array(7);
weekday[0] = "Sunday";
weekday[1] = "Monday";
weekday[2] = "Tuesday";
weekday[3] = "Wednesday";
weekday[4] = "Thursday";
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)
};
// check for valid api response
if (!error && res.statusCode == 200) {
var unlockBody = new RegExp(':(.*)}');
var unlockBodyMatch = body.match(unlockBody);
// setup unlocks message
const unlockFinalMessage = user_name + " has " + unlockBodyMatch[1] + " unlocks for " + n;
xmpp.send(from, unlockFinalMessage);
// call user with unlocks if asterisk enabled
call(unlockFinalMessage);
}
// check for http errors
resError(error, res);
});
// output xmpp, and request errors if debugging is enabled.
debug();
}
}
// Toot Unlocks
else if (message === '!tootunlocks') {
if (from != user_jid) {
xmpp.send(from, pc);
} else {
var d = new Date();
var weekday = new Array(7);
weekday[0] = "Sunday";
weekday[1] = "Monday";
weekday[2] = "Tuesday";
weekday[3] = "Wednesday";
weekday[4] = "Thursday";
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)
};
if (!error && res.statusCode == 200) {
var unlockBody = new RegExp(':(.*)}');
var unlockBodyMatch = body.match(unlockBody);
const unlockFinalMessage = "@" + config.mastodon.user + " has " + unlockBodyMatch[1] + " unlocks for " + n;
M.post('statuses', {
status: unlockFinalMessage
});
debug();
}
resError(error, res);
});
debug();
}
}
// Tuya light control
else if (message === '!lights') {
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)
};
if (!error && res.statusCode == 200) {
if (res.body === "true") {
const stat = tuya_name + ": On";
xmpp.send(from, stat);
console.log(stat);
} else if (res.body === "false") {
const stat = tuya_name + ": Off";
xmpp.send(from, stat);
console.log(stat);
} else {
xmpp.send(from, "Unknown reply from api call. Please reply ping to reach an admin.");
console.log("Tuya API: Unknown reply!");
}
}
resError(error, res);
});
debug();
}
} else if (message === '!lightsauth') {
if (from != user_jid) {
xmpp.send(from, pc);
} else {
// refresh tokens for Tuya API
request(tuya_reauth_url, (error, res, body) => {
if (error) {
return console.log(error)
};
if (!error && res.statusCode == 200) {
xmpp.send(from, 'Refreshing Tuya API auth keys');
}
resError(error, res);
});
debug();
}
} else if (message === tuya_name + ' on') {
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)
};
if (!error && res.statusCode == 200) {
const stat = tuya_name + " has been turned on";
xmpp.send(from, stat);
console.log(stat);
}
resError(error, res);
});
debug();
}
} else if (message === tuya_name + ' off') {
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)
};
if (!error && res.statusCode == 200) {
const stat = tuya_name + " has been turned off";
xmpp.send(from, stat);
console.log(stat);
}
resError(error, res);
});
debug();
}
} 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 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)
};
if (!error && res.statusCode == 200) {
xmpp.send(from, tuya_name + " set to " + per + "%");
}
resError(error, res);
});
if (config.debug === '1') {
console.log('lights turned to ' + per + "%");
}
}
} else if (message.includes(tuya_name + ' color')) {
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);
console.log(color)
if (colorRgb.isValid()) {
var obj = colorRgb.channels();
console.log(obj.r + ', ' + obj.g + ', ' + obj.b);
}
request(tuya_color_url + "?r=" + colorRgb.r + "&g=" + colorRgb.g + "&b=" + colorRgb.b + "&devname=" + tuya_name, (error, res, body) => {
if (error) {
return console.log(error)
};
if (!error && res.statusCode == 200) {
xmpp.send(from, tuya_name + "set to " + color);
console.log(tuya_name + "set to " + color);
}
resError(error, res);
});
if (config.debug === '1') {
console.log(tuya_name + "set to " + color);
}
}
}
// 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
call_data = "hello. " + from + " has pinged you!"
call(call_data);
debug();
}
// Check the weather using weatherunderground API
else if (message === '!weather') {
if (from != user_jid) {
xmpp.send(from, pc);
} else {
// check current weather
request(weather_url, function (err, response, body) {
if (err) {
console.log('error:', err);
} else {
let w = JSON.parse(body);
let wm = 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
call(wm);
}
});
debug();
}
}
// Sends a nice goodbye message
else if (message === '!bye') {
xmpp.send(from, "Bye, have a beautiful time!");
debug();
}
// EXTENDING XEPS (WIP)
/*
xmpp.on('chatstate', function(from, state) {
console.log(chalk.yellow(state));
});
// 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
// secion from config.ini
// 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)
else if (message === '!meet') {
if (from != user_jid) {
xmpp.send(from, pc);
} else {
if (xmpp_server_type === "ejabberd"){
// Create private XMPP room
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: 'false',
public: 'false',
public_list: 'false',
title: 'Secure channel generated by https://vxmppb.tk',
allow_subscription: 'true'
}
},
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");
// authorize users
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
};
// invite users
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();
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")
}}
}
xmpp.on('buddy', function(jid, state, statusText, resource) {
console.log(chalk.yellow(state + statusText + resource));
});
*/
// End meeting
else if (message === '!endmeet') {
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',
headers: {
'Content-Type': 'application/json',
Authorization: xmpp_auth
},
body: {
name: xmpp_muc_name,
service: xmpp_muc_server
},
json: true
};
// send room delete request
request(endMeeting, function (error, response, body) {
if (error) throw new Error(error);
console.log(body);
});
for (const key in config.friends) {
// alert friends meeting has ended
xmpp.send(key, "Meeting ended");
}
}
debug();
}
// CONNECT TO XMPP!
xmpp.connect({
jid: bot_jid,
password: bot_password,
host: bot_hostname,
port: 5222
});
// Shutsdown bot if admin sends shutdown command (linux)
else if (message === "sudo shutdown now -h") {
if (from != user_jid) {
xmpp.send(from, from + " is not in the sudoers file. This incident will be reported.");
console.log(from + ' tried to shut me down!');
} else {
// send shutdown message
xmpp.send(from, "Alrighty, shutting down. Goodbye!");
debug();
// kill bot
process.exit(1);
}
}
// CHECK FOR NEW INCOMING REQUESTS
console.log(chalk.yellow(xmpp.getRoster()));
// Get users vCard (WIP)
else if (message === '!vcard') {
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)
})
}
}
// 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. Please see https://wiki.vanderwarker.family/doku.php?id=code:vxmppb:commands");
debug();
}
});
// Catch and log XMPP errors
xmpp.on('error', function (err) {
console.error(err);
});
// Set XMPP status
xmpp.setPresence('chat', 'https://vxmppb.tk');
// Alas, we connect to XMPP!
xmpp.connect({
jid: bot_jid,
password: bot_password,
host: bot_hostname,
port: bot_port
});
// subscribe to friends
xmpp.on('subscribe', function (from) {
if (from in config.friends == true) {
console.log('accepting subsciption request from ' + from);
// accept pending subscription requests
xmpp.acceptSubscription(from);
// check for incoming subscription requests
xmpp.getRoster();
// subscribe to any incoming subscription requests
console.log('requesting subscription to ' + from);
xmpp.subscribe(from);
} else {
console.log('denied subscription request from: ' + from + " as they are not in the friends section of config.ini");
}
});
process.on('SIGINT', function() {
console.log("\r\n SigInt receieved. Shutdown eminent!")
xmpp.send(user_jid, "SigInt receieved. Shutdown eminent!")
process.exit();
});
} catch (e) {
console.log("Please create a config.ini");
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,14 +1,13 @@
{
"name": "vxmppb",
"description": "vanderwarker.family XMPP Bot",
"version": "0.1.1",
"version": "1.1.0",
"keywords": [
"xmpp",
"bot",
"sms",
"autoreply",
"asterisk",
"rss",
"mqtt",
"gotify",
"push notifications"
@ -22,15 +21,12 @@
],
"dependencies": {
"asterisk-manager": "0.1.16",
"chalk": "2.4.2",
"degrees-to-direction": "1.1.1",
"express": "4.17.1",
"ini": "2.0.0",
"mastodon": "1.2.2",
"mqtt": "4.2.6",
"mqtt": "4.3.7",
"request": "2.88.0",
"rss-parser": "3.12.0",
"simple-xmpp": "2.3.1"
"rgb-color": "2.1.2",
"simple-xmpp": "1.3.0"
},
"bugs": {
"url": "https://git.vanderwarker.family/vxmppb/vxmppb/issues"
@ -39,9 +35,11 @@
"type": "git",
"url": "https://git.vanderwarker.family/vxmppb/vxmppb.git"
},
"license": {
"type": "MIT"
},
"licenses": [
{
"type": "The MIT License",
"url": "http://www.opensource.org/licenses/mit-license.php"
}],
"homepage": "https://wiki.vanderwarker.family/doku.php?id=code:vxmppb:start",
"scripts": {
"test": "echo \"No tests available. Please consider helping with a pull request!\" && exit 0"