Skip to content

Commit

Permalink
feat(notification): add Streamlabs support (#1872)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomZanna committed Feb 9, 2021
1 parent 1009ce6 commit edb39f8
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 10 deletions.
13 changes: 13 additions & 0 deletions docs/reference/notification.md
Expand Up @@ -209,3 +209,16 @@ Instructions on how to set up tokens can be found at [d-fischer.github.io/twitch
| `TWITCH_ACCESS_TOKEN` | Twitch access token |
| `TWITCH_REFRESH_TOKEN` | Twitch refresh token |
| `TWITCH_CHANNEL` | Twitch channel |

## StreamLabs

Instructions on how to set up tokens can be found at [dev.streamlabs.com](https://dev.streamlabs.com/docs/register-your-application).
You don't need to submit your application for review, just whitelist yourself!

| Environment variable | Description |
|:---:|---|
| `STREAMLABS_ACCESS_TOKEN` | StreamLabs access token |
| `STREAMLABS_TYPE` | StreamLabs alert type |
| `STREAMLABS_IMAGE`| Custom image to display. Leave it blank for default |
| `STREAMLABS_SOUND` | Custom image to play. Leave it blank for default |
| `STREAMLABS_DURATION` | StreamLabs alert duration (in milliseconds) |
5 changes: 5 additions & 0 deletions dotenv-example
Expand Up @@ -116,4 +116,9 @@ TWITTER_ACCESS_TOKEN_SECRET=
TWITTER_CONSUMER_KEY=
TWITTER_CONSUMER_SECRET=
TWITTER_TWEET_TAGS=
STREAMLABS_ACCESS_TOKEN=
STREAMLABS_TYPE=
STREAMLABS_IMAGE=
STREAMLABS_SOUND=
STREAMLABS_DURATION=
WEB_PORT=
32 changes: 22 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions src/config.ts
Expand Up @@ -332,6 +332,13 @@ const notifications = {
consumerSecret: envOrString(process.env.TWITTER_CONSUMER_SECRET),
tweetTags: envOrString(process.env.TWITTER_TWEET_TAGS),
},
streamlabs: {
accessToken: envOrString(process.env.STREAMLABS_ACCESS_TOKEN),
type: envOrString(process.env.STREAMLABS_TYPE),
imageHref: envOrString(process.env.STREAMLABS_IMAGE),
soundHref: envOrString(process.env.STREAMLABS_SOUND),
duration: envOrNumber(process.env.STREAMLABS_DURATION),
},
};

const nvidia = {
Expand Down
2 changes: 2 additions & 0 deletions src/notification/notification.ts
Expand Up @@ -16,6 +16,7 @@ import {sendTwilioMessage} from './twilio';
import {sendTwitchMessage} from './twitch';
import {updateRedis} from './redis';
import {activateSmartthingsSwitch} from './smartthings';
import {sendStreamLabsAlert} from './streamlabs';

export function sendNotification(link: Link, store: Store) {
// Priority
Expand All @@ -37,4 +38,5 @@ export function sendNotification(link: Link, store: Store) {
sendTwilioMessage(link, store);
sendTwitchMessage(link, store);
updateRedis(link, store);
sendStreamLabsAlert(link, store);
}
42 changes: 42 additions & 0 deletions src/notification/streamlabs.ts
@@ -0,0 +1,42 @@
import {Link, Store} from '../store/model';
import {Print, logger} from '../logger';
import {config} from '../config';
import {URLSearchParams} from 'url';
import fetch from 'node-fetch';

const {streamlabs} = config.notifications;
let requestParams: URLSearchParams;

if (streamlabs.accessToken && streamlabs.type) {
requestParams = new URLSearchParams();
requestParams.append('access_token', streamlabs.accessToken);
requestParams.append('type', streamlabs.type);
requestParams.append('image_href', streamlabs.imageHref);
requestParams.append('sound_href', streamlabs.soundHref);
requestParams.append('duration', streamlabs.duration.toString());
}

export function sendStreamLabsAlert(link: Link, store: Store) {
if (requestParams) {
logger.debug('↗ sending StreamLabs alert');

(async () => {
const message = `${Print.inStock(link, store)}`;
requestParams.set('message', message);

try {
const response = await fetch('https://streamlabs.com/api/v1.0/alerts', {
method: 'POST',
body: requestParams,
});

const json = await response.json();
if (!json.success) throw Error(JSON.stringify(json));

logger.info('✔ StreamLabs alert sent');
} catch (error: unknown) {
logger.error("✖ couldn't send StreamLabs alert", error);
}
})();
}
}

0 comments on commit edb39f8

Please sign in to comment.