Skip to content

Commit

Permalink
feat(slack): add Slack integration
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Config format changed.

	To migrate the config follow the example below:

	Before:
	{
	  "appToken": "",
	  "deliveryToken": ""
	}

	After:
	{
	  pushover: {
	    "enabled": true,
	    "appToken": "",
	    "deliveryToken": ""
      }
	}

	This is due to the addition of Slack support, which requires
	it's own configuration, therefore each service has an object
	inside the configuration file.
  • Loading branch information
Chr12t0pher committed Mar 7, 2016
1 parent 5abc5fe commit d77957e
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 84 deletions.
41 changes: 26 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,36 @@ playing a sound and triggering a vibration every 30 seconds until it is acknowle

## Installation
- Install to `nodecg/bundles/lfg-omega13`.
- [Sign up for a Pushover account](https://pushover.net/login).
- Install the Pushover app on your phone.
- [Create a new Pushover application](https://pushover.net/apps/build). Make note of your application's token.
- (Optional) If you want more than one person to receive notifications,
create a delivery group for your application and have others sign up to receive notifications.
- Create `nodecg/cfg/lfg-omega13.json`, following the format specified in the Configuration section.
- To send Pushover alerts:
- [Sign up for a Pushover account](https://pushover.net/login).
- Install the Pushover app on your phone.
- [Create a new Pushover application](https://pushover.net/apps/build). Make note of your application's token.
- (Optional) If you want more than one person to receive notifications,
create a delivery group for your application and have others sign up to receive notifications.
- Create `nodecg/cfg/lfg-omega13.json`, following the format specified in the Configuration section.
- To send Slack alerts:
- Create a [new incoming Webhook](https://my.slack.com/services/new/incoming-webhook/) for Slack, don't worry about
the channel it's posting to, you'll set that in the bundle config. Take note of the 'Webhook URL' it gives you.

## Configuration
lfg-omega13 depends on a JSON config file being available at `nodecg/cfg/lfg-omega13.json`. It cannot function without
proper configuration. The config file has the following properties:
- `appToken`: The token of your Pushover application.
- `deliveryToken`: The token of either the Pushover group or individual user that notifications should be sent to.
- `priority`: _Optional_ (Default: **2**) [See the relevant Pushover API docs](https://pushover.net/api#priority).
- `retry`: _Optional_ (Default: **30**)
[See the description of Emergency priority in the Pushover API docs](https://pushover.net/api#priority).
Only applies when `priority` is `2`.
- `expire`: _Optional_ (Default: **3600**)
[See the description of Emergency priority in the Pushover API docs](https://pushover.net/api#priority).
Only applies when `priority` is `2`.
- `pushover`
- `enabled`: Whether to send Pushover alerts or not.
- `appToken`: The token of your Pushover application.
- `deliveryToken`: The token of either the Pushover group or individual user that notifications should be sent to.
- `priority`: _Optional_ (Default: **2**) [See the relevant Pushover API docs](https://pushover.net/api#priority).
- `retry`: _Optional_ (Default: **30**)
[See the description of Emergency priority in the Pushover API docs](https://pushover.net/api#priority).
Only applies when `priority` is `2`.
- `expire`: _Optional_ (Default: **3600**)
[See the description of Emergency priority in the Pushover API docs](https://pushover.net/api#priority).
Only applies when `priority` is `2`.
- `slack`
- `enabled`: Whether to send Slack alerts or not.
- `user`: The username the alert should come from, for example 'NodeCG Alerts'.
- `destination`: Who should the message go to, can be a #channel or a @user.
- `url`: The Webhook URL provided by Slack when creating a new incoming Webhook.

## Usage
- Open the dashboard and click "REQUEST HELP" in the top toolbar.
Expand Down
102 changes: 70 additions & 32 deletions configschema.json
Original file line number Diff line number Diff line change
@@ -1,38 +1,76 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"appToken": {
"type": "string",
"description": "The token of your Pushover application."
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"pushover": {
"enabled": {
"type": "boolean",
"description": "Whether pushover notifications are enabled or not.",
"default": false
},
"appToken": {
"type": "string",
"description": "The token of your Pushover application."
},
"deliveryToken": {
"type": "string",
"description": "The token of either the Pushover group or individual user that notifications should be sent to."
},
"priority": {
"type": "integer",
"description": "Affects how the receipient of the notification sees it. See https://pushover.net/api#priority",
"default": 2,
"minimum": -2,
"maximum": 2
},
"retry": {
"type": "integer",
"description": "How often to re-display the notification until the user acknowledges it. Only applies when `priority` is `2`. See https://pushover.net/api#priority",
"default": 30,
"minimum": 30
},
"expire": {
"type": "integer",
"description": "How long to conintue re-displaying a notification before giving up. Only applies when `priority` is `2`. See https://pushover.net/api#priority",
"default": 3600,
"minimum": 60,
"maximum": 86400
},
"requred": [
"appToken",
"deliveryToken"
]
},
"slack": {
"type": "object",
"properties": {
"enabled": {
"type": "boolean",
"description": "Whether Slack notifications are enabled or not.",
"default": false
},
"deliveryToken": {
"type": "string",
"description": "The token of either the Pushover group or individual user that notifications should be sent to."
"user": {
"type": "string",
"description": "The username the alert should come from, for example 'NodeCG Alerts'."
},
"priority": {
"type": "integer",
"description": "Affects how the receipient of the notification sees it. See https://pushover.net/api#priority",
"default": 2,
"minimum": -2,
"maximum": 2
"destination": {
"type": "string",
"description": "Who should the message go to, can be a #channel or a @user."
},
"retry": {
"type": "integer",
"description": "How often to re-display the notification until the user acknowledges it. Only applies when `priority` is `2`. See https://pushover.net/api#priority",
"default": 30,
"minimum": 30
"url": {
"type": "string",
"description": "The Webhook URL provided by Slack."
},
"expire": {
"type": "integer",
"description": "How long to conintue re-displaying a notification before giving up. Only applies when `priority` is `2`. See https://pushover.net/api#priority",
"default": 3600,
"minimum": 60,
"maximum": 86400
},
"requred": [
"appToken",
"deliveryToken"
"required": [
"user",
"destination",
"url"
]
}
}
}
},
"required": [
"pushover",
"slack"
]
}
}
Binary file modified dashboard/dialogs/confirm_help.html
Binary file not shown.
109 changes: 73 additions & 36 deletions extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,85 @@ var request = require('request');
var querystring = require('querystring');

module.exports = function (nodecg) {
if (!nodecg.bundleConfig) {
throw new Error('cfg/lfg-omega13.json not found, this config file is required!');
}

nodecg.listenFor('help', function (message, cb) {
request.post({
url: 'https://api.pushover.net/1/messages.json',
body: querystring.stringify({
token: nodecg.bundleConfig.appToken,
user: nodecg.bundleConfig.deliveryToken,
title: nodecg.config.host + ' requires assistance',
message: message || 'No message provided.',
priority: nodecg.bundleConfig.priority || 2,
retry: nodecg.bundleConfig.retry || 30,
expire: nodecg.bundleConfig.expire || 3600
})
}, function (err, res, body) {
if (err) {
nodecg.log.err(err.stack);
cb(false);
return;
}
if (nodecg.bundleConfig.pushover && nodecg.bundleConfig.pushover.enabled) {
request.post({
url: 'https://api.pushover.net/1/messages.json',
body: querystring.stringify({
token: nodecg.bundleConfig.appToken,
user: nodecg.bundleConfig.deliveryToken,
title: nodecg.config.host + ' requires assistance',
message: message || 'No message provided.',
priority: nodecg.bundleConfig.priority || 2,
retry: nodecg.bundleConfig.retry || 30,
expire: nodecg.bundleConfig.expire || 3600
})
}, function (err, res, body) {
if (err) {
nodecg.log.error(err.stack);
cb(false);
return;
}

var json;
try {
json = JSON.parse(body);
} catch (e) {
nodecg.log.error('Could not parse response to JSON:', e.stack);
cb(false);
return;
}
var json;
try {
json = JSON.parse(body);
} catch (e) {
nodecg.log.error('Could not parse response to JSON:', e.stack);
cb(false);
return;
}

/* enslint-disable no-lonley-if */
if (json.status === 1) {
cb(true);
} else {
if (json.errors) {
var resErrString = json.errors.join('. ');
nodecg.log.error(resErrString);
/* enslint-disable no-lonley-if */
if (json.status === 1) {
cb(true);
} else {
if (json.errors) {
var resErrString = json.errors.join('. ');
nodecg.log.error(resErrString);
cb(false);
} else {
nodecg.log.error('Unknown error:', json);
cb(false);
}
}
/* enslint-enable no-lonley-if */
});
}
if (nodecg.bundleConfig.slack && nodecg.bundleConfig.slack.enabled) {
request.post({
headers: {'content-type': 'application/json'},
url: nodecg.bundleConfig.slack.url,
body: JSON.stringify({
username: nodecg.bundleConfig.slack.user,
channel: nodecg.bundleConfig.slack.destination,
attachments: [
{
"fallback": nodecg.config.host + ' requires assistance! ' + message,
"color": 'danger',
"title": 'Help requested by ' + nodecg.config.host,
"text": message || 'No message provided.'
}
]
})
}, function (err, res, body) {
if (err) {
nodecg.log.error(err.stack);
cb(false);
return;
}

if (body === 'ok') {
cb(true);
} else {
nodecg.log.error('Unknown error:', json);
nodecg.log.error('Unknown error:', body);
cb(false);
}
}
/* enslint-enable no-lonley-if */
});
});
}
});
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "lfg-omega13",
"version": "0.1.1",
"description": "A big red button that sends a very aggressive phone notification.",
"description": "A big red button that sends a very aggressive phone and/or Slack notification.",
"repository": {
"type": "git",
"url": "git+https://lange@github.com/SupportClass/lfg-omega13.git"
Expand Down

0 comments on commit d77957e

Please sign in to comment.