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: Add NTFY.SH agent #3195

Merged
merged 5 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 Expand Up @@ -548,8 +555,8 @@
restartTime,
};

export function setConfig(newConfig: any) {

Check warning on line 558 in src/config.ts

View workflow job for this annotation

GitHub Actions / Build and lint

Unexpected any. Specify a different type
const writeConfig = config as any;

Check warning on line 559 in src/config.ts

View workflow job for this annotation

GitHub Actions / Build and lint

Unexpected any. Specify a different type
for (const key of Object.keys(newConfig)) {
writeConfig[key] = newConfig[key];
}
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);
}
})();
}
}
Loading