Skip to content

Commit

Permalink
feat: Weather Alerts
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian Moffitt authored and Ian Moffitt committed Mar 1, 2021
1 parent f240b84 commit 18bbce1
Show file tree
Hide file tree
Showing 4 changed files with 10,674 additions and 308 deletions.
43 changes: 36 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ cronrunner.on('message', function (task) {
cooldown();
});

break;
case 'alerts':

weather.getAlerts(config.config.weather.lat, config.config.weather.lon, false).then(function (alerts) {
say(alerts, task.groups);
cooldown();
});

break;
default:
break;
Expand Down Expand Up @@ -91,6 +99,10 @@ bot.on('start', function () {
hal.addMass(data);
});

weather.prefillAlerts(config.config.weather.lat, config.config.weather.lon).then(function (weather) {
cooldown();
});

debug('ping? pong!');

});
Expand All @@ -107,7 +119,25 @@ bot.on('message', function (data) {

if (meetsCriteria('data', data)) {

}

if (meetsCriteria('weather', data)) {

if (canDo()) {

warmUp();

let channel = data.channel;

weather.getAlerts(config.config.weather.lat, config.config.weather.lon, true).then(function (weather) {
say(weather, channel);
cooldown();
}, function (error) {
say(error, channel);
cooldown();
});

}

}

Expand Down Expand Up @@ -151,7 +181,7 @@ bot.on('message', function (data) {

let response = [
'I am catbot! You can talk to me and I might reply. I like hearing my name.',
'Other commands: `pokemon`, `gh`, `bukkit`, `!doggo`'
'Other commands: `pokemon`, `gh`, `bukkit`, `!doggo`, `!weather`'
];

say(response, channel);
Expand Down Expand Up @@ -391,13 +421,12 @@ function meetsCriteria(tool, data) {
validRoom = true;
}

}

if (typeof toolConfig['regex'] !== 'undefined') {
let regex = new RegExp(toolConfig['regex'], 'i');
if (typeof toolConfig['regex'] !== 'undefined') {
let regex = new RegExp(toolConfig['regex'], 'i');

if (message.match(regex) !== null) {
validMatch = true;
if (message.match(regex) !== null) {
validMatch = true;
}
}
}

Expand Down
114 changes: 112 additions & 2 deletions modules/weather.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const util = require('util');
const request = require('request');
const config = require('../bot.json');
const breiutil = require('brei-util');

module.exports = {

Expand All @@ -27,6 +28,9 @@ module.exports = {
"50n": ":fog:",
},

alerts: [],
sentAlerts: [],

get: function (lat, lon) {

return new Promise(function (resolve, reject) {
Expand All @@ -43,6 +47,78 @@ module.exports = {
response.push(daily);
response.push(hours);

if (this.alerts.length > 0) {
response.push('*' + this.alerts.length + '* Active Weather Alert(s). *!weather* to see.');
}

resolve(response);

}.bind(this), function (error) {

response.push(error);

return reject(response);

}.bind(this));

}.bind(this));

},

getAlerts: function (lat, lon, allAlerts) {

if (typeof allAlerts === 'undefined' || allAlerts === null) {
allAlerts = false;
}

return new Promise(function (resolve, reject) {

let response = [];

this.apiGet(lat, lon).then(function (weather) {

if (allAlerts) {
let daily = this.formatDay(weather);

let hours = this.formatHours(weather);

response.push(daily);
response.push(hours);
}

let alerts = this.alerts.filter(x => {
let currDate = new Date();
let alertDate = new Date(x.start * 1000);

let diff = (currDate.getTime() - alertDate.getTime()) / 1000 / 60; // Minutes

let recent = (diff > 10);

const key = breiutil.slugify(x['start'] + '-' + x['end'] + '-' + x['event']);

let spentAlert = this.sentAlerts.filter(y => y === key);

return (recent && spentAlert.length === 0) || allAlerts; // More than 10 minute difference
});

if (alerts.length > 0) {

for (var i in alerts) {
if (alerts.hasOwnProperty(i)) {
response.push('*' + alerts[i]['sender_name'] + '*: ' + alerts[i]['description']);
}
}

this.sentAlerts = [];

for (var j in this.alerts) {
if (this.alerts.hasOwnProperty(j)) {
this.sentAlerts.push(breiutil.slugify(this.alerts[j]['start'] + '-' + this.alerts[j]['end'] + '-' + this.alerts[j]['event']))
}
}

}

resolve(response);

}.bind(this), function (error) {
Expand All @@ -57,8 +133,32 @@ module.exports = {

},

prefillAlerts: function (lat, lon) {

return new Promise(function (resolve, reject) {

let response = false;

this.apiGet(lat, lon).then(function (weather) {

response = true;

resolve(response);

}.bind(this), function (error) {

return reject(response);

}.bind(this));

}.bind(this));

},

apiGet: function (lat, lon) {

let _this = this;

let apikey = config.config.weather.apikey;
let endp = this.endpoint;
let weather = {};
Expand All @@ -71,8 +171,6 @@ module.exports = {

request(endp, function (error, response, body) {

// console.log(response);

if (error) {
return reject(new Error(error));
}
Expand All @@ -83,6 +181,18 @@ module.exports = {

weather = JSON.parse(body);

if (typeof weather.alerts !== 'undefined') {
_this.alerts = weather.alerts;

_this.sentAlerts = [];

for (var j in _this.alerts) {
if (_this.alerts.hasOwnProperty(j)) {
_this.sentAlerts.push(breiutil.slugify(_this.alerts[j]['start'] + '-' + _this.alerts[j]['end'] + '-' + _this.alerts[j]['event']))
}
}
}

resolve(weather);

});
Expand Down
Loading

0 comments on commit 18bbce1

Please sign in to comment.