Skip to content

Commit

Permalink
added new PUSHOVER_ALARM_KEY
Browse files Browse the repository at this point in the history
with this pushover alarms can be disabled
also info level pushes (treatments) can be disabled
  • Loading branch information
jasoncalabrese committed Sep 5, 2015
1 parent abca6a0 commit 3429780
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 11 deletions.
5 changes: 3 additions & 2 deletions README.md
Expand Up @@ -266,8 +266,9 @@ To learn more about the Nightscout API, visit https://YOUR-SITE.com/api-docs.htm

* `ENABLE` - `pushover` should be added to the list of plugin, for example: `ENABLE="pushover"`.
* `PUSHOVER_API_TOKEN` - Used to enable pushover notifications, this token is specific to the application you create from in [Pushover](https://pushover.net/), ***[additional pushover information](#pushover)*** below.
* `PUSHOVER_USER_KEY` - Your Pushover user key, can be found in the top left of the [Pushover](https://pushover.net/) site, this can also be a pushover delivery group key to send to a group rather than just a single user. This also support a space delimited list of keys.
* `PUSHOVER_ANNOUNCEMENT_KEY` - An optional Pushover user/group key, will be used for system wide user generated announcements. If not defined this will fallback to `PUSHOVER_USER_KEY`. A possible use for this is sending important messages and alarms to a CWD that you don't want to send all notification too. This also support a space delimited list of keys.
* `PUSHOVER_USER_KEY` - Your Pushover user key, can be found in the top left of the [Pushover](https://pushover.net/) site, this can also be a pushover delivery group key to send to a group rather than just a single user. This also supports a space delimited list of keys. To disable `INFO` level pushes set this to `off`.
* `PUSHOVER_ALARMS_KEY` - An optional Pushover user/group key, will be used for system wide alarms (level > `WARN`). If not defined this will fallback to `PUSHOVER_USER_KEY`. A possible use for this is sending important messages and alarms to a CWD that you don't want to send all notification too. This also support a space delimited list of keys. To disable Alarm pushes set this to `off`.
* `PUSHOVER_ANNOUNCEMENT_KEY` - An optional Pushover user/group key, will be used for system wide user generated announcements. If not defined this will fallback to `PUSHOVER_USER_KEY` or `PUSHOVER_ANNOUNCEMENT_KEY`. A possible use for this is sending important messages and alarms to a CWD that you don't want to send all notification too. This also support a space delimited list of keys. To disable Announcement pushes set this to `off`.
* `BASE_URL` - Used for pushover callbacks, usually the URL of your Nightscout site, use https when possible.
* `API_SECRET` - Used for signing the pushover callback request for acknowledgments.

Expand Down
53 changes: 44 additions & 9 deletions lib/plugins/pushover.js
Expand Up @@ -14,8 +14,29 @@ function init (env) {

var pushoverAPI = setupPushover(env);

pushover.send = function wrapSend(notify, callback) {
var selectedKeys = notify.isAnnouncement ? pushoverAPI.announcementKeys : pushoverAPI.userKeys;
function selectKeys (notify) {
var keys = null;

if (notify.isAnnouncement) {
keys = pushoverAPI.announcementKeys;
} else if (levels.isAlarm(notify.level)) {
keys = pushoverAPI.alarmKeys;
} else {
keys = pushoverAPI.userKeys;
}

return keys;
}

pushover.send = function wrapSend (notify, callback) {
var selectedKeys = selectKeys(notify);

if (selectedKeys.length == 0) {
console.info('No Pushover Keys defined for', notify);
if (callback) {
return callback();
}
}

var msg = {
expire: times.mins(15).secs
Expand Down Expand Up @@ -76,24 +97,38 @@ function init (env) {
function setupPushover (env) {
var apiToken = env.extendedSettings && env.extendedSettings.pushover && env.extendedSettings.pushover.apiToken;

var userKeys = (env.extendedSettings && env.extendedSettings.pushover &&
env.extendedSettings.pushover.userKey && env.extendedSettings.pushover.userKey.split(' ')) || [];
function keysByType (type, fallback) {
fallback = fallback || [];

var key = env.extendedSettings && env.extendedSettings.pushover && env.extendedSettings.pushover[type];

if (key === 'off') {
return []; //don't consider fallback, this type has been disabled
} else if (key && key.split) {
return key.split(' ') || fallback;
} else {
return fallback;
}
}

var userKeys = keysByType('userKey', []);

if (userKeys.length === 0) {
userKeys = (env.extendedSettings && env.extendedSettings.pushover &&
env.extendedSettings.pushover.groupKey && env.extendedSettings.pushover.groupKey.split(' ')) || [];
userKeys = keysByType('groupKey') || [];
}

var announcementKeys = (env.extendedSettings && env.extendedSettings.pushover &&
env.extendedSettings.pushover.announcementKey && env.extendedSettings.pushover.announcementKey.split(' ')) || userKeys;
var alarmKeys = keysByType('alarmKey', userKeys);

var announcementKeys = keysByType('announcementKey', userKeys || alarmKeys);

if (apiToken && (userKeys.length > 0 || announcementKeys.length > 0)) {
if (apiToken && (userKeys.length > 0 || alarmKeys.length > 0 || announcementKeys.length > 0)) {
var pushoverAPI = new Pushover({
token: apiToken
});

pushoverAPI.apiToken = apiToken;
pushoverAPI.userKeys = userKeys;
pushoverAPI.alarmKeys = alarmKeys;
pushoverAPI.announcementKeys = announcementKeys;

return pushoverAPI;
Expand Down

0 comments on commit 3429780

Please sign in to comment.