Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(notification): add Streamlabs support #1872

Merged
merged 6 commits into from Feb 9, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 |
TomZanna marked this conversation as resolved.
Show resolved Hide resolved
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),
},
jef marked this conversation as resolved.
Show resolved Hide resolved
};

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) {
TomZanna marked this conversation as resolved.
Show resolved Hide resolved
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());
jef marked this conversation as resolved.
Show resolved Hide resolved
}

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);
}
})();
}
}