Skip to content

Commit

Permalink
Add a new notifier type Ntfy
Browse files Browse the repository at this point in the history
  • Loading branch information
Nyrrell committed Jan 21, 2024
1 parent c898aee commit d436933
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 14 deletions.
27 changes: 19 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,17 @@ and `refreshToken` if you already have it).
- The priority level sent with the message (Default 5)
</details>
<details>
<summary><b>NTFY</b></summary>

- <b>type</b> : _string_ = `ntfy`
- <b>apiUrl</b> : _string_ = `https://ntfy.sh`
- <b>topic</b> : _string_ = `tgtg`
- <b>token</b>? : _string_ = `tk_AgQdq7mVBoFD37zQVN29RhuMzNIz2`
- Optional if your server don't use it. [How to create a token](https://docs.ntfy.sh/config/#access-tokens)
- <b>priority</b>? : _number_ = `5`
- The priority level sent with the message, range 1 - 5 (Default 3)
</details>
<details>
<summary><b>SIGNAL-CLI-REST-API</b></summary>

- <b>type</b> : _string_ = `signal`
Expand Down Expand Up @@ -120,23 +131,23 @@ After that `tgtg-notifier` start to monitor your favorite stores (once per minut
store's stock is add.

### Notification example
<details>
<summary><b>DISCORD</b></summary>
<details><summary><b>DISCORD</b></summary>

![notif](media/notifiers/discord.png)
</details>
<details>
<summary><b>GOTIFY</b></summary>
<details><summary><b>GOTIFY</b></summary>

![notif](media/notifiers/gotify.png)
</details>
<details>
<summary><b>SIGNAL-CLI-REST-API</b></summary>
<details><summary><b>NTFY</b></summary>

![notif](media/notifiers/ntfy.png)
</details>
<details><summary><b>SIGNAL-CLI-REST-API</b></summary>

![notif](media/notifiers/signal.png)
</details>
<details>
<summary><b>TELEGRAM</b></summary>
<details><summary><b>TELEGRAM</b></summary>

![notif](media/notifiers/telegram.png)
</details>
Binary file added media/notifiers/ntfy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "tgtg-notifier",
"description": "Monitors your favorite TGTG Store for newly available items",
"author": "Nyrrell",
"version": "1.1.9",
"version": "1.1.10",
"type": "module",
"main": "src/app.js",
"license": "MIT",
Expand Down
13 changes: 11 additions & 2 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { DiscordConfig, GotifyConfig, NotifierConfig, SignalConfig, TelegramConfig } from './notifiers/config/index.js';
import {
NotifierConfig,
TelegramConfig,
DiscordConfig,
GotifyConfig,
SignalConfig,
NtfyConfig,
} from './notifiers/config/index.js';
import { NotificationType, NotifierService, NotifierType } from './notifiers/notifierService.js';
import { Discord, Gotify, Signal, Telegram } from './notifiers/index.js';
import { Discord, Gotify, Ntfy, Signal, Telegram } from './notifiers/index.js';
import { sleep, TEST_ITEM } from './common/utils.js';
import { LOCALE, TIMEZONE } from './config.js';
import { logger } from './common/logger.js';
Expand Down Expand Up @@ -34,6 +41,8 @@ export class Client {
return new Signal(notifier as SignalConfig);
case NotifierType.TELEGRAM:
return new Telegram(notifier as TelegramConfig);
case NotifierType.NTFY:
return new Ntfy(notifier as NtfyConfig);
default:
throw new Error(`Unexpected notifier config: ${notifier.type}`);
}
Expand Down
1 change: 1 addition & 0 deletions src/notifiers/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './telegramConfig.js';
export * from './discordConfig.js';
export * from './gotifyConfig.js';
export * from './signalConfig.js';
export * from './ntfyConfig.js';
29 changes: 29 additions & 0 deletions src/notifiers/config/ntfyConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { IsInt, IsOptional, IsString, IsUrl, Max, Min } from 'class-validator';

import { NotifierType } from '../notifierService.js';
import { NotifierConfig } from './notifierConfig.js';

export class NtfyConfig extends NotifierConfig {
readonly icon = 'https://cdn.jsdelivr.net/gh/Nyrrell/tgtg-notifier@master/media/logo.png';
@IsUrl()
readonly apiUrl: string;
@IsOptional()
@IsString()
readonly token?: string;
@IsString()
readonly topic: string;
@IsInt()
@Min(1)
@Max(5)
@IsOptional()
readonly priority?: number;

constructor(config: NtfyConfig) {
super(NotifierType.NTFY);
this.apiUrl = config.apiUrl;
this.token = config?.token;
this.topic = config.topic;
this.priority = config?.priority;
this.validateConfig();
}
}
5 changes: 3 additions & 2 deletions src/notifiers/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './gotify.js';
export * from './telegram.js';
export * from './discord.js';
export * from './gotify.js';
export * from './signal.js';
export * from './telegram.js';
export * from './ntfy.js';
10 changes: 9 additions & 1 deletion src/notifiers/notifierService.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { NotifierConfig } from './config/index.js';
import { logger } from '../common/logger.js';

export abstract class NotifierService {
protected abstract readonly request: Request;
protected abstract readonly config: NotifierConfig;

sendNotification(type: NotificationType, payload: string | PARSE_TGTG_ITEM): Promise<void> {
public sendNotification(type: NotificationType, payload: string | PARSE_TGTG_ITEM): Promise<void> {
switch (type) {
case NotificationType.START:
return this.sendInfo(payload as string);
Expand All @@ -13,6 +14,12 @@ export abstract class NotifierService {
}
}

protected response = async (res: Response): Promise<void> => {
if (!res.ok) throw await res.text();
};

protected error = (reason: String): void => logger.warn(reason.trim());

protected abstract sendInfo(payload: string): Promise<void>;

protected abstract sendItem(payload: PARSE_TGTG_ITEM): Promise<void>;
Expand All @@ -25,6 +32,7 @@ export abstract class NotifierService {
export enum NotifierType {
DISCORD = 'discord',
GOTIFY = 'gotify',
NTFY = 'ntfy',
SIGNAL = 'signal',
TELEGRAM = 'telegram',
}
Expand Down
52 changes: 52 additions & 0 deletions src/notifiers/ntfy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { NotifierService } from './notifierService.js';
import { NtfyConfig } from './config/index.js';
import { PRICE, STOCK } from '../config.js';

export class Ntfy extends NotifierService {
protected readonly request: Request;
protected readonly config: NtfyConfig;

constructor(config: NtfyConfig) {
super();
this.config = new NtfyConfig(config);
this.request = new Request(`${this.config.apiUrl}`, {
method: 'POST',
headers: { 'Content-Type': 'application/json', authorization: `Bearer ${this.config.token}` },
});
}

private getDefaultPayload() {
return {
topic: this.config.topic,
icon: this.config.icon,
};
}

protected async sendInfo(message: string): Promise<void> {
await fetch(this.request, {
body: this.jsonPayload({
...this.getDefaultPayload(),
message: message,
}),
})
.then(this.response)
.catch(this.error);
}

protected async sendItem(item: PARSE_TGTG_ITEM): Promise<void> {
await fetch(this.request, {
body: this.jsonPayload({
...this.getDefaultPayload(),
title: item.name,
message:
`${STOCK} : ${item.available.padEnd(6)} ${PRICE} : ${item.price}\n` +
`📤 ${item.pickupDate} ${item.pickupTime}`,
...(this.config.priority && { priority: this.config.priority }),
click: `https://share.toogoodtogo.com/item/${item.id}`,
markdown: true,
}),
})
.then(this.response)
.catch(this.error);
}
}

0 comments on commit d436933

Please sign in to comment.