From 2a1f15041e5e95a55f3869dce2053994ca2438ed Mon Sep 17 00:00:00 2001 From: Jef LeCompte Date: Sun, 20 Sep 2020 13:18:32 -0400 Subject: [PATCH] chore: move notification test, other refactoring (#111) --- .env-example | 5 ++--- README.md | 6 +++--- package.json | 3 ++- src/__test__/notification-test.ts | 16 ++++++++++++++++ src/config.ts | 8 ++++---- src/index.ts | 8 -------- src/notification/discord.ts | 2 +- src/notification/email.ts | 6 +++--- src/notification/pushover.ts | 6 +++--- src/notification/slack.ts | 6 +++--- src/notification/sms.ts | 2 +- src/notification/sound.ts | 2 +- src/notification/telegram.ts | 2 +- 13 files changed, 40 insertions(+), 32 deletions(-) create mode 100644 src/__test__/notification-test.ts diff --git a/.env-example b/.env-example index 991a917..79b8cc4 100644 --- a/.env-example +++ b/.env-example @@ -1,8 +1,9 @@ +DISCORD_NOTIFY_GROUP="@here" +DISCORD_WEB_HOOK="https://discordapp.com/api/webhooks/23123123123/5dfsdfh3h2j5hdfhsdfsdhj55jf" EMAIL_USERNAME="youremail@gmail.com" EMAIL_PASSWORD="secretpassword" HEADLESS="true" LOG_LEVEL="info" -NOTIFICATION_TEST="false" OPEN_BROWSER="true" PAGE_TIMEOUT="30000" PHONE_NUMBER="1234567890" @@ -18,5 +19,3 @@ 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" diff --git a/README.md b/README.md index e957fa4..eb23aff 100644 --- a/README.md +++ b/README.md @@ -60,16 +60,14 @@ 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 | +| `DISCORD_WEB_HOOK` | Discord Web Hook URL | | `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` | | `LOG_LEVEL` | [Logging levels](https://github.com/winstonjs/winston#logging-levels) | Debugging related, default: `info` | -| `NOTIFICATION_TEST` | Test all the notifications configured | Default: `false` | | `OPEN_BROWSER` | Toggle for whether or not the browser should open when item is found | Default: `true` | | `PAGE_TIMEOUT` | Navigation Timeout in milliseconds | `0` for infinite, default: `30000` | | `PHONE_NUMBER` | 10 digit phone number | E.g.: `1234567890`, email configuration required | @@ -88,6 +86,8 @@ Here is a list of variables that you can use to customize your newly copied `.en > :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. +> :point_right: You can test your notification configuration by running `npm run test:notification`. + #### Supported stores | **Stores** | **Environment variable** | diff --git a/package.json b/package.json index 7e234b6..281439c 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,8 @@ "build": "rimraf ./build && tsc", "lint": "xo", "lint:fix": "xo --fix", - "start": "npm run build && node build/index.js" + "start": "npm run build && node build/index.js", + "test:notification": "npm run build && node build/__test__/*.js" }, "repository": { "type": "git", diff --git a/src/__test__/notification-test.ts b/src/__test__/notification-test.ts new file mode 100644 index 0000000..0effcfe --- /dev/null +++ b/src/__test__/notification-test.ts @@ -0,0 +1,16 @@ +import {Link} from '../store/model'; +import {sendNotification} from '../notification'; + +const link: Link = { + brand: 'brand', + cartUrl: 'http://example.com/', + captchaLabels: ['captcha'], + model: 'model', + oosLabels: ['out of stock'], + url: 'http://example.com/' +}; + +/** + * Send test email. + */ +sendNotification(link.cartUrl ?? link.url, link); diff --git a/src/config.ts b/src/config.ts index df6a238..cbac645 100644 --- a/src/config.ts +++ b/src/config.ts @@ -12,6 +12,10 @@ const browser = { const logLevel = process.env.LOG_LEVEL ?? 'info'; const notifications = { + discord: { + notifyGroup: process.env.DISCORD_NOTIFY_GROUP ?? '', + webHookUrl: process.env.DISCORD_WEB_HOOK ?? '' + }, email: { username: process.env.EMAIL_USERNAME ?? '', password: process.env.EMAIL_PASSWORD ?? '' @@ -42,10 +46,6 @@ 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' }; diff --git a/src/index.ts b/src/index.ts index f2a4efd..5431347 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,7 +4,6 @@ import adblockerPlugin from 'puppeteer-extra-plugin-adblocker'; import {Config} from './config'; import {Store, Stores} from './store/model'; import {Logger} from './logger'; -import {sendNotification} from './notification'; import {lookup} from './store'; import async from 'async'; @@ -51,13 +50,6 @@ async function main() { await browser.close(); } -/** - * Send test email. - */ -if (Config.notifications.test) { - sendNotification('http://test.com/', {brand: 'THE BEST BRAND', model: 'VENTUS', oosLabels: [], url: '', cartUrl: ''}); -} - /** * Will continually run until user interferes. */ diff --git a/src/notification/discord.ts b/src/notification/discord.ts index 8d6e8d8..13d27c0 100644 --- a/src/notification/discord.ts +++ b/src/notification/discord.ts @@ -22,7 +22,7 @@ export function sendDiscordMessage(cartUrl: string, link: Link) { embed.setColor(0x76B900); embed.setTimestamp(); await hook.send(embed); - Logger.info(`✔ discord message sent: ${cartUrl}`); + Logger.info(`↗ discord message sent: ${cartUrl}`); } catch (error) { Logger.error(error); } diff --git a/src/notification/email.ts b/src/notification/email.ts index ef108d3..7275473 100644 --- a/src/notification/email.ts +++ b/src/notification/email.ts @@ -20,15 +20,15 @@ const mailOptions: Mail.Options = { subject }; -export function sendEmail(text: string) { - mailOptions.text = text; +export function sendEmail(cartUrl: string) { + mailOptions.text = cartUrl; transporter.sendMail(mailOptions, (error, info) => { if (error) { Logger.error(error); } else { // eslint-disable-next-line @typescript-eslint/restrict-template-expressions - Logger.info(`✔ email sent: ${info.response}`); + Logger.info(`↗ email sent: ${info.response}`); } }); } diff --git a/src/notification/pushover.ts b/src/notification/pushover.ts index 50e9c0c..aa2b299 100644 --- a/src/notification/pushover.ts +++ b/src/notification/pushover.ts @@ -8,16 +8,16 @@ const push = new Push({ token: pushover.token }); -export function sendPushoverNotification(text: string) { +export function sendPushoverNotification(cartUrl: string) { const message = { - message: text + message: cartUrl }; push.send(message, (err: Error, result: string) => { if (err) { Logger.error(err); } else { - Logger.info(`✔ Pushover notification sent: ${result}`); + Logger.info(`↗ pushover notification sent: ${result}`); } }); } diff --git a/src/notification/slack.ts b/src/notification/slack.ts index 582eab5..9bd13b6 100644 --- a/src/notification/slack.ts +++ b/src/notification/slack.ts @@ -6,16 +6,16 @@ const channel = Config.notifications.slack.channel; const token = Config.notifications.slack.token; const web = new WebClient(token); -export function sendSlackMessage(text: string) { +export function sendSlackMessage(cartUrl: string) { (async () => { try { - const result = await web.chat.postMessage({text, channel}); + const result = await web.chat.postMessage({text: cartUrl, channel}); if (!result.ok) { Logger.error(result.error); return; } - Logger.info(`✔ slack message sent to '${channel}': ${text}`); + Logger.info(`↗ slack message sent to '${channel}': ${cartUrl}`); } catch (error) { Logger.error(error); } diff --git a/src/notification/sms.ts b/src/notification/sms.ts index 3c16a3c..b31afac 100644 --- a/src/notification/sms.ts +++ b/src/notification/sms.ts @@ -28,7 +28,7 @@ export function sendSMS(text: string) { Logger.error(error); } else { // eslint-disable-next-line @typescript-eslint/restrict-template-expressions - Logger.info(`✔ sms sent: ${info.response}`); + Logger.info(`↗ sms sent: ${info.response}`); } }); } diff --git a/src/notification/sound.ts b/src/notification/sound.ts index 75b52c8..66226f7 100644 --- a/src/notification/sound.ts +++ b/src/notification/sound.ts @@ -15,7 +15,7 @@ export function playSound() { } player.play(notificationSound, (err: string) => { - Logger.info('✔ playing sound'); + Logger.info('↗ playing sound'); if (err) { Logger.error(`error playing sound: ${err}`); diff --git a/src/notification/telegram.ts b/src/notification/telegram.ts index 6b8025a..249945b 100644 --- a/src/notification/telegram.ts +++ b/src/notification/telegram.ts @@ -12,7 +12,7 @@ export function sendTelegramMessage(text: string) { (async () => { try { await client.sendMessage(telegram.chatId, text); - Logger.info(`✔ telegram message sent to '${telegram.chatId}': ${text}`); + Logger.info(`↗ telegram message sent to '${telegram.chatId}': ${text}`); } catch (error) { Logger.error(error); }