forked from nightscout/cgm-remote-monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow notification to be created manually support for routing these to special pushover and maker keys support for multiple pushover and maker key
- Loading branch information
1 parent
c68a330
commit add721b
Showing
15 changed files
with
459 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,91 @@ | ||
'use strict'; | ||
|
||
var _ = require('lodash'); | ||
var Pushover = require('pushover-notifications'); | ||
var request = require('request'); | ||
var levels = require('../levels'); | ||
|
||
var TIME_2_MINS_S = 120 | ||
, TIME_15_MINS_S = 15 * 60 | ||
; | ||
|
||
function init (env) { | ||
var pushover = null; | ||
var pushover = { }; | ||
var pushoverAPI = null; | ||
|
||
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(' ')) || []; | ||
|
||
if (env.pushover_api_token && env.pushover_user_key) { | ||
pushover = new Pushover({ | ||
token: env.pushover_api_token, | ||
user: env.pushover_user_key | ||
var announcementKeys = (env.extendedSettings && env.extendedSettings.pushover && | ||
env.extendedSettings.pushover.announcementKey && env.extendedSettings.pushover.announcementKey.split(' ')) || userKeys; | ||
|
||
if (apiToken && (userKeys.length > 0 || announcementKeys.length > 0)) { | ||
pushoverAPI = new Pushover({ | ||
token: apiToken | ||
}); | ||
} | ||
|
||
pushover.PRIORITY_NORMAL = 0; | ||
pushover.PRIORITY_EMERGENCY = 2; | ||
|
||
pushover.cancelWithReceipt = function cancelWithReceipt (receipt, callback) { | ||
request | ||
.get('https://api.pushover.net/1/receipts/' + receipt + '/cancel.json?token=' + env.pushover_api_token) | ||
.on('response', function(response) { | ||
callback(null, response); | ||
}) | ||
.on('error', function(err) { | ||
callback(err); | ||
}); | ||
pushover.PRIORITY_NORMAL = 0; | ||
pushover.PRIORITY_EMERGENCY = 2; | ||
|
||
pushover.send = function wrapSend (notify, callback) { | ||
|
||
var selectedKeys = notify.announcement ? announcementKeys : userKeys; | ||
|
||
var msg = { | ||
expire: TIME_15_MINS_S | ||
, title: notify.title | ||
, message: notify.message | ||
, sound: notify.pushoverSound || 'gamelan' | ||
, timestamp: new Date() | ||
//USE PUSHOVER_EMERGENCY for WARN and URGENT so we get the acks | ||
, priority: notify.level >= levels.WARN ? pushover.PRIORITY_EMERGENCY : pushover.PRIORITY_NORMAL | ||
}; | ||
} | ||
|
||
return pushover; | ||
if (levels.isAlarm(notify.level)) { | ||
//ADJUST RETRY TIME based on WARN or URGENT | ||
msg.retry = notify.level === levels.URGENT ? TIME_2_MINS_S : TIME_15_MINS_S; | ||
if (env.baseUrl) { | ||
msg.callback = env.baseUrl + '/api/v1/notifications/pushovercallback'; | ||
} | ||
} | ||
|
||
_.forEach(selectedKeys, function eachKey(key) { | ||
msg.user = key; | ||
pushover.sendAPIRequest(msg, callback); | ||
}); | ||
|
||
}; | ||
|
||
pushover.sendAPIRequest = function sendAPIRequest (msg, callback) { | ||
pushoverAPI.send(msg, function (err, result) { | ||
if (err) { | ||
console.error('unable to send pushover notification', err); | ||
} else { | ||
console.info('sent pushover notification: ', msg, 'result: ', result); | ||
} | ||
callback(err, result); | ||
}); | ||
}; | ||
|
||
pushover.cancelWithReceipt = function cancelWithReceipt (receipt, callback) { | ||
request | ||
.get('https://api.pushover.net/1/receipts/' + receipt + '/cancel.json?token=' + apiToken) | ||
.on('response', function(response) { | ||
callback(null, response); | ||
}) | ||
.on('error', function(err) { | ||
callback(err); | ||
}); | ||
}; | ||
|
||
if (pushoverAPI) { | ||
return pushover; | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
module.exports = init; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.