Skip to content

Commit

Permalink
feat(notification): discord integration (#82)
Browse files Browse the repository at this point in the history
Co-authored-by: Jef LeCompte <jeffreylec@gmail.com>
  • Loading branch information
bmalaski and jef committed Sep 20, 2020
1 parent 5b91065 commit a3fc07d
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .env-example
Expand Up @@ -18,3 +18,5 @@ STORES="bestbuy,bandh,nvidia"
SCREENSHOT="true"
TELEGRAM_ACCESS_TOKEN=""
TELEGRAM_CHAT_ID="1234"
DISCORD_WEB_HOOK="https://discordapp.com/api/webhooks/23123123123/5dfsdfh3h2j5hdfhsdfsdhj55jf"
DISCORD_NOTIFY_GROUP="@here"
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -60,8 +60,11 @@ To customize `nvidia-snatcher`, make a copy of `.env-example` as `.env` and make

Here is a list of variables that you can use to customize your newly copied `.env` file:


| **Environment variable** | **Description** | **Notes** |
|:---:|---|---|
| `DISCORD_WEB_HOOK` | Discord Web Hook URL |
| `DISCORD_NOTIFY_GROUP` | Discord group you would like to notify; optional | E.g.: @here |
| `EMAIL_USERNAME` | Gmail address | E.g.: `jensen.robbed.us@gmail.com` |
| `EMAIL_PASSWORD` | Gmail password | See below if you have MFA |
| `HEADLESS` | Puppeteer to run headless or not | Debugging related, default: `true` |
Expand All @@ -82,6 +85,7 @@ Here is a list of variables that you can use to customize your newly copied `.en
| `SCREENSHOT` | Capture screenshot of page if a card is found | Default: `true` |
| `TELEGRAM_ACCESS_TOKEN` | Telegram access token |
| `TELEGRAM_CHAT_ID` | Telegram chat ID |

> :point_right: If you have multi-factor authentication (MFA), you will need to create an [app password](https://myaccount.google.com/apppasswords) and use this instead of your Gmail password.
#### Supported stores
Expand Down
23 changes: 23 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -42,6 +42,7 @@
"play-sound": "^1.1.3",
"rimraf": "^3.0.2",
"typescript": "^4.0.2",
"xo": "^0.33.1"
"xo": "^0.33.1",
"discord-webhook-node": "^1.1.8"
}
}
4 changes: 4 additions & 0 deletions src/config.ts
Expand Up @@ -42,6 +42,10 @@ const notifications = {
accessToken: process.env.TELEGRAM_ACCESS_TOKEN ?? '',
chatId: process.env.TELEGRAM_CHAT_ID ?? ''
},
discord: {
webHookUrl: process.env.DISCORD_WEB_HOOK ?? '',
notifyGroup: process.env.DISCORD_NOTIFY_GROUP ?? ''
},
test: process.env.NOTIFICATION_TEST === 'true'
};

Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Expand Up @@ -55,7 +55,7 @@ async function main() {
* Send test email.
*/
if (Config.notifications.test) {
sendNotification('test');
sendNotification('http://test.com/', {brand: 'THE BEST BRAND', model: 'VENTUS', oosLabels: [], url: '', cartUrl: ''});
}

/**
Expand Down
30 changes: 30 additions & 0 deletions src/notification/discord.ts
@@ -0,0 +1,30 @@
import {Webhook, MessageBuilder} from 'discord-webhook-node';
import {Config} from '../config';
import {Logger} from '../logger';
import {Link} from '../store/model';

const hook = new Webhook(Config.notifications.discord.webHookUrl);
const notifyGroup = Config.notifications.discord.notifyGroup;

export function sendDiscordMessage(cartUrl: string, link: Link) {
(async () => {
try {
const embed = new MessageBuilder();
embed.setTitle('Stock Notification');
embed.addField('URL', cartUrl, true);
embed.addField('Brand', link.brand, true);
embed.addField('Model', link.model, true);

if (notifyGroup !== '') {
embed.addField('Attention', notifyGroup, true);
}

embed.setColor(0x76B900);
embed.setTimestamp();
await hook.send(embed);
Logger.info(`✔ discord message sent: ${cartUrl}`);
} catch (error) {
Logger.error(error);
}
})();
}
8 changes: 7 additions & 1 deletion src/notification/notification.ts
Expand Up @@ -5,10 +5,12 @@ import {playSound} from './sound';
import {sendSlackMessage} from './slack';
import {sendPushoverNotification} from './pushover';
import {sendTelegramMessage} from './telegram';
import {sendDiscordMessage} from './discord';
import {Link} from '../store/model';

const notifications = Config.notifications;

export function sendNotification(cartUrl: string) {
export function sendNotification(cartUrl: string, link: Link) {
if (notifications.email.username && notifications.email.password) {
sendEmail(cartUrl);
}
Expand All @@ -21,6 +23,10 @@ export function sendNotification(cartUrl: string) {
sendTelegramMessage(cartUrl);
}

if (notifications.discord.webHookUrl) {
sendDiscordMessage(cartUrl, link);
}

if (notifications.phone.number) {
const carrier = notifications.phone.carrier.toLowerCase();
if (carrier && notifications.phone.availableCarriers.has(carrier)) {
Expand Down
2 changes: 1 addition & 1 deletion src/store/lookup.ts
Expand Up @@ -74,7 +74,7 @@ export async function lookup(browser: Browser, store: Store) {
await open(givenUrl);
}

sendNotification(givenUrl);
sendNotification(givenUrl, link);
}

await page.close();
Expand Down
2 changes: 1 addition & 1 deletion src/store/model/store.ts
@@ -1,4 +1,4 @@
interface Link {
export interface Link {
cartUrl?: string;
brand: string;
model: string;
Expand Down

0 comments on commit a3fc07d

Please sign in to comment.