refactor: env, cartUrl optional, other consistencies

Signed-off-by: Jef LeCompte <jeffreylec@gmail.com>
This commit is contained in:
Jef LeCompte
2020-09-19 17:35:46 -04:00
parent 393d5f6898
commit 8c5d7d0c49
19 changed files with 80 additions and 83 deletions
+1 -1
View File
@@ -3,8 +3,8 @@ import Mail from 'nodemailer/lib/mailer';
import {Config} from '../config';
import {Logger} from '../logger';
const subject = 'NVIDIA - BUY NOW';
const email = Config.notifications.email;
const subject = 'NVIDIA - BUY NOW';
const transporter = nodemailer.createTransport({
service: 'gmail',
+11 -9
View File
@@ -3,34 +3,36 @@ import {sendEmail} from './email';
import {sendSMS} from './sms';
import {playSound} from './sound';
import {sendSlackMessage} from './slack';
import sendPushoverNotification from './pushover';
import {sendPushoverNotification} from './pushover';
import {sendTelegramMessage} from './telegram';
const notifications = Config.notifications;
export function sendNotification(cartUrl: string) {
if (Config.notifications.email.username && Config.notifications.email.password) {
if (notifications.email.username && notifications.email.password) {
sendEmail(cartUrl);
}
if (Config.notifications.slack.channel && Config.notifications.slack.token) {
if (notifications.slack.channel && notifications.slack.token) {
sendSlackMessage(cartUrl);
}
if (Config.notifications.telegram.botToken && Config.notifications.telegram.chatId) {
if (notifications.telegram.accessToken && notifications.telegram.chatId) {
sendTelegramMessage(cartUrl);
}
if (Config.notifications.phone.number) {
const carrier = Config.notifications.phone.carrier.toLowerCase();
if (carrier && Config.notifications.phone.availableCarriers.has(carrier)) {
if (notifications.phone.number) {
const carrier = notifications.phone.carrier.toLowerCase();
if (carrier && notifications.phone.availableCarriers.has(carrier)) {
sendSMS(cartUrl);
}
}
if (Config.notifications.pushover.token && Config.notifications.pushover.user) {
if (notifications.pushover.token && notifications.pushover.user) {
sendPushoverNotification(cartUrl);
}
if (Config.notifications.playSound !== 'false') {
if (notifications.playSound === 'true') {
playSound();
}
}
+7 -6
View File
@@ -1,18 +1,19 @@
import Push from 'pushover-notifications';
import {Config} from '../config';
import {Logger} from '../logger';
import Push = require('pushover-notifications');
const p = new Push({
user: Config.notifications.pushover.user,
token: Config.notifications.pushover.token
const pushover = Config.notifications.pushover;
const push = new Push({
user: pushover.user,
token: pushover.token
});
export default function sendPushoverNotification(text: string) {
export function sendPushoverNotification(text: string) {
const message = {
message: text
};
p.send(message, (err: Error, result: string) => {
push.send(message, (err: Error, result: string) => {
if (err) {
Logger.error(err);
} else {
+4 -5
View File
@@ -2,18 +2,17 @@ import {Config} from '../config';
import {Logger} from '../logger';
import {TelegramClient} from 'messaging-api-telegram';
const chatId = Config.notifications.telegram.chatId;
const accessToken = Config.notifications.telegram.botToken;
const telegram = Config.notifications.telegram;
const client = new TelegramClient({
accessToken
accessToken: telegram.accessToken
});
export function sendTelegramMessage(text: string) {
(async () => {
try {
await client.sendMessage(chatId, text);
Logger.info(`✔ telegram message sent to '${chatId}': ${text}`);
await client.sendMessage(telegram.chatId, text);
Logger.info(`✔ telegram message sent to '${telegram.chatId}': ${text}`);
} catch (error) {
Logger.error(error);
}