feat(notification): add Streamlabs support (#1872)

This commit is contained in:
TomZanna
2021-02-09 14:31:21 +01:00
committed by GitHub
parent 1009ce6636
commit edb39f8f05
6 changed files with 91 additions and 10 deletions
+2
View File
@@ -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
@@ -37,4 +38,5 @@ export function sendNotification(link: Link, store: Store) {
sendTwilioMessage(link, store);
sendTwitchMessage(link, store);
updateRedis(link, store);
sendStreamLabsAlert(link, store);
}
+42
View File
@@ -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);
}
})();
}
}