-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(notification): add Streamlabs support (#1872)
- Loading branch information
Showing
6 changed files
with
91 additions
and
10 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -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); | ||
} | ||
})(); | ||
} | ||
} |