Skip to content

Commit

Permalink
feat: add NTFY.SH agent (#3195)
Browse files Browse the repository at this point in the history
  • Loading branch information
GewoonJaap authored Jan 29, 2025
1 parent f59d3fc commit e18f231
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 0 deletions.
13 changes: 13 additions & 0 deletions docs/reference/notification.md
Original file line number Diff line number Diff line change
Expand Up @@ -262,3 +262,16 @@ Copy the API key generated with the service activation.
| `FREEMOBILE_API_KEY` | API key generated with your notification option activation |

Note: here you do not need to give neither your password nor phone number.

## NTFY.sh

You can send notifications using NTFY.sh, which supports various features like priority, tags, and action buttons.
Use the free service at [ntfy.sh](https://ntfy.sh) or host your own instance.

| Environment variable | Description |
|:---:|---|
| `NTFY_URL` | ntfy server URL, e.g. `https://ntfy.sh` |
| `NTFY_TOPIC` | Topic to publish alerts to |
| `NTFY_PRIORITY` | Message priority, e.g. max/high/default/low/min, I recommend to use the numbers instead of the string values for the priority. https://docs.ntfy.sh/publish/?h=priority#message-priority |
| `NTFY_TITLE` | Title of the message |
| `NTFY_ACCESS_TOKEN` | Access token for authentication. https://docs.ntfy.sh/config/#access-tokens |
5 changes: 5 additions & 0 deletions dotenv-example
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,9 @@ STREAMLABS_SOUND=
STREAMLABS_DURATION=
FREEMOBILE_ID=
FREEMOBILE_API_KEY=
NTFY_TOPIC=
NTFY_PRIORITY=
NTFY_TITLE=
NTFY_ACCESS_TOKEN=
NTFY_URL=
WEB_PORT=
7 changes: 7 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,13 @@ const notifications = {
id: envOrString(process.env.FREEMOBILE_ID),
apiKey: envOrString(process.env.FREEMOBILE_API_KEY),
},
ntfy: {
url: envOrString(process.env.NTFY_URL, 'https://ntfy.sh'),
topic: envOrString(process.env.NTFY_TOPIC),
priority: envOrString(process.env.NTFY_PRIORITY),
title: envOrString(process.env.NTFY_TITLE),
accessToken: envOrString(process.env.NTFY_ACCESS_TOKEN),
},
};

const nvidia = {
Expand Down
2 changes: 2 additions & 0 deletions src/messaging/notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ import {updateRedis} from './redis';
import {sendStreamLabsAlert} from './streamlabs';
import {sendFreeMobileAlert} from './freemobile';
import {DMPayload} from '.';
import {sendNtfyAlert} from './ntfy';

export function sendNotification(link: Link, store: Store) {
// Priority
playSound();
sendNtfyAlert(link, store);
sendDiscordMessage(link, store);
sendDesktopNotification(link, store);
sendEmail(link, store);
Expand Down
55 changes: 55 additions & 0 deletions src/messaging/ntfy.ts
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);
}
})();
}
}

0 comments on commit e18f231

Please sign in to comment.