-
-
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.
- Loading branch information
1 parent
f59d3fc
commit e18f231
Showing
5 changed files
with
82 additions
and
0 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
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,55 @@ | ||
import {Link, Store} from '../store/model'; | ||
import {Print, logger} from '../logger'; | ||
import {config} from '../config'; | ||
import fetch from 'node-fetch'; | ||
|
||
const {ntfy} = config.notifications; | ||
|
||
export function sendNtfyAlert(link: Link, store: Store) { | ||
if (ntfy.topic) { | ||
logger.debug('↗ sending ntfy alert'); | ||
|
||
(async () => { | ||
const message = `${Print.inStock(link, store)}`; | ||
const headers: Record<string, string> = {}; | ||
|
||
if (ntfy.priority) headers['Priority'] = ntfy.priority; | ||
headers[ | ||
'Tags' | ||
] = `${store.name},${link.model},${link.series},${link.brand}`; | ||
if (ntfy.title) headers['Title'] = ntfy.title; | ||
if (ntfy.accessToken) | ||
headers['Authorization'] = `Bearer ${ntfy.accessToken}`; | ||
|
||
const body = { | ||
topic: ntfy.topic, | ||
message, | ||
actions: [ | ||
{ | ||
action: 'view', | ||
label: 'Add to cart', | ||
url: link.cartUrl ?? link.url, | ||
}, | ||
], | ||
}; | ||
|
||
try { | ||
const response = await fetch(ntfy.url, { | ||
method: 'POST', | ||
body: JSON.stringify(body), | ||
headers: { | ||
...headers, | ||
'Content-Type': 'application/json', | ||
}, | ||
}); | ||
|
||
if (!response.ok) | ||
throw new Error(`Failed to send ntfy alert: ${response.statusText}`); | ||
|
||
logger.info('✔ ntfy alert sent'); | ||
} catch (error: unknown) { | ||
logger.error("✖ couldn't send ntfy alert", error); | ||
} | ||
})(); | ||
} | ||
} |