Skip to content

Commit

Permalink
feat(notification): add gotify
Browse files Browse the repository at this point in the history
Resolves: #2541
Ref: #2536
  • Loading branch information
jef committed May 17, 2021
1 parent 1ab0af2 commit 8f2de5d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
11 changes: 9 additions & 2 deletions docs/reference/notification.md
Expand Up @@ -2,7 +2,7 @@

You can test your notification configuration by running `npm run test:notification`.

## Apple Push Notification Service
## Apple Push Notification Service

| Environment variable | Description |
|---|---|
Expand All @@ -13,7 +13,7 @@ You can test your notification configuration by running `npm run test:notificati
| `APNS_PRODUCTION` | true/false for production |
| `APNS_TEAMID` | Apple developer's team id |

Change your notification alert/payload/etc in apns.ts in the note object.
Change your notification alert/payload/etc in apns.ts in the note object.
Refer to https://github.com/node-apn/node-apn for config options.

## Desktop
Expand Down Expand Up @@ -94,6 +94,13 @@ Default provider is Gmail. If you use a different email provider, you must provi
| Virgin (CA) | `virgin-ca`|
| Visible | `visible`|

## Gotify

| Environment variable | Description |
|:---:|---|
| `GOTIFY_TOKEN` | Application token |
| `GOTIFY_URL` | Gotify's URL, e.g. `https://push.example.com` |

## MQTT

| Environment variable | Description |
Expand Down
4 changes: 4 additions & 0 deletions src/config.ts
Expand Up @@ -263,6 +263,10 @@ const notifications = {
),
username: envOrString(process.env.EMAIL_USERNAME),
},
gotify: {
token: envOrString(process.env.GOTIFY_TOKEN),
url: envOrString(process.env.GOTIFY_URL),
},
mqtt: {
broker: envOrString(process.env.MQTT_BROKER_ADDRESS),
clientId: envOrString(process.env.MQTT_CLIENT_ID),
Expand Down
25 changes: 25 additions & 0 deletions src/messaging/gotify.ts
@@ -0,0 +1,25 @@
import {Link, Store} from '../store/model';
import {Print, logger} from '../logger';
import {config} from '../config';
import fetch from 'node-fetch';
import {URLSearchParams} from 'url';

const {gotify} = config.notifications;

export function sendGotifyNotification(link: Link, store: Store) {
if (!gotify.token || !gotify.url) return;

logger.info(JSON.stringify(gotify));

(async () => {
const params = new URLSearchParams();
params.append('title', Print.inStock(link, store));
params.append('message', Print.productInStock(link));
const response = await fetch(`${gotify.url}/message?token=${gotify.token}`, {
method: 'POST',
body: params,
});

logger.info(JSON.stringify(await response.json()));
})();
}
4 changes: 3 additions & 1 deletion src/messaging/notification.ts
@@ -1,9 +1,11 @@
import {Link, Store} from '../store/model';
import {adjustPhilipsHueLights} from './philips-hue';
import {playSound} from './sound';
import {sendApns} from './apns';
import {sendDesktopNotification} from './desktop';
import {sendDiscordMessage, sendDMAsync as sendDiscordDM} from './discord';
import {sendEmail} from './email';
import {sendGotifyNotification} from './gotify';
import {sendMqttMessage} from './mqtt';
import {sendPagerDutyNotification} from './pagerduty';
import {sendPushbulletNotification} from './pushbullet';
Expand All @@ -18,7 +20,6 @@ import {updateRedis} from './redis';
import {activateSmartthingsSwitch} from './smartthings';
import {sendStreamLabsAlert} from './streamlabs';
import {sendFreeMobileAlert} from './freemobile';
import {sendApns} from './apns';
import {DMPayload} from '.';

export function sendNotification(link: Link, store: Store) {
Expand All @@ -32,6 +33,7 @@ export function sendNotification(link: Link, store: Store) {
// Non-priority
activateSmartthingsSwitch();
adjustPhilipsHueLights();
sendGotifyNotification(link, store);
sendMqttMessage(link, store);
sendPagerDutyNotification(link, store);
sendPushbulletNotification(link, store);
Expand Down

0 comments on commit 8f2de5d

Please sign in to comment.