feat: sms notification for usa carriers (#40)

Co-authored-by: Jef LeCompte <jeffreylec@gmail.com>
This commit is contained in:
Alexandru Gutu
2020-09-18 19:54:59 -04:00
committed by GitHub
parent ec2108f4b9
commit 5a3636bcb6
5 changed files with 67 additions and 2 deletions
+4 -2
View File
@@ -1,8 +1,10 @@
EMAIL_USERNAME="youremail@gmail.com" EMAIL_USERNAME="youremail@gmail.com"
EMAIL_PASSWORD="secretpassword" EMAIL_PASSWORD="secretpassword"
NOTIFICATION_TEST="false" NOTIFICATION_TEST="false"
PAGE_TIMEOUT=30000 PAGE_TIMEOUT="30000"
RATE_LIMIT_TIMEOUT=5000 RATE_LIMIT_TIMEOUT="5000"
SLACK_CHANNEL="SlackChannelName" SLACK_CHANNEL="SlackChannelName"
SLACK_TOKEN="slack-token" SLACK_TOKEN="slack-token"
STORES="bestbuy,bandh,nvidia" STORES="bestbuy,bandh,nvidia"
PHONE_NUMBER="1234567890"
CARRIER="tmobile"
+2
View File
@@ -68,6 +68,8 @@ First, you're going to need to copy the `.env.example` to `.env`. The current op
| `EMAIL_PASSWORD` | Gmail password; see below if you have MFA; optional | | `EMAIL_PASSWORD` | Gmail password; see below if you have MFA; optional |
| `NOTIFICATION_TEST` | Test all the notifications configured; optional, default: `false` | | `NOTIFICATION_TEST` | Test all the notifications configured; optional, default: `false` |
| `PAGE_TIMEOUT` | Navigation Timeout in milliseconds (`0` for infinite); optional, default: `30000` | | `PAGE_TIMEOUT` | Navigation Timeout in milliseconds (`0` for infinite); optional, default: `30000` |
| `PHONE_NUMBER` | 10 digit phone number, only USA, SMS may apply (e.g., `1234567890`); optional, email configuration required |
| `PHONE_CARRIER` | Service provider for SMS, supports `["sprint", "tmobile", "att", "verizon"]`; optional, email configuration required |
| `RATE_LIMIT_TIMEOUT` | Rate limit timeout for each full store cycle; optional, default: `5000` | | `RATE_LIMIT_TIMEOUT` | Rate limit timeout for each full store cycle; optional, default: `5000` |
| `SLACK_CHANNEL` | Slack channel for posting (e.g., `update`); optional | | `SLACK_CHANNEL` | Slack channel for posting (e.g., `update`); optional |
| `SLACK_TOKEN` | Slack API token; optional | `SLACK_TOKEN` | Slack API token; optional
+5
View File
@@ -8,6 +8,11 @@ const notifications = {
username: process.env.EMAIL_USERNAME ?? '', username: process.env.EMAIL_USERNAME ?? '',
password: process.env.EMAIL_PASSWORD ?? '' password: process.env.EMAIL_PASSWORD ?? ''
}, },
phone: {
availableCarriers: ['sprint', 'verizon', 'tmobile', 'att'],
carrier: process.env.PHONE_CARRIER,
number: process.env.PHONE_NUMBER
},
slack: { slack: {
channel: process.env.SLACK_CHANNEL ?? '', channel: process.env.SLACK_CHANNEL ?? '',
token: process.env.SLACK_TOKEN ?? '' token: process.env.SLACK_TOKEN ?? ''
+7
View File
@@ -1,6 +1,7 @@
import {Config} from '../config'; import {Config} from '../config';
import sendEmail from './email'; import sendEmail from './email';
import sendSlaskMessage from './slack'; import sendSlaskMessage from './slack';
import sendSMS from './sms';
export default function sendNotification(cartUrl: string) { export default function sendNotification(cartUrl: string) {
if (Config.notifications.email.username && Config.notifications.email.password) { if (Config.notifications.email.username && Config.notifications.email.password) {
@@ -10,4 +11,10 @@ export default function sendNotification(cartUrl: string) {
if (Config.notifications.slack.channel && Config.notifications.slack.token) { if (Config.notifications.slack.channel && Config.notifications.slack.token) {
sendSlaskMessage(cartUrl); sendSlaskMessage(cartUrl);
} }
if (Config.notifications.phone.number && Config.notifications.phone.carrier) {
if (Config.notifications.availableCarriers.includes(Config.notifications.phone.carrier.toLowerCase())) {
sendSMS(cartUrl);
}
}
} }
+49
View File
@@ -0,0 +1,49 @@
import nodemailer from 'nodemailer';
import Mail from 'nodemailer/lib/mailer';
import {Config} from '../config';
import {Logger} from '../logger';
const subject = 'NVIDIA - BUY NOW';
enum carrierAddress {
sprint = 'messaging.sprintpcs.com',
verizon = 'vtext.com',
tmobile = 'tmomail.net',
att = 'txt.att.net'
}
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: Config.email.username,
pass: Config.email.password
}
});
const mailOptions: Mail.Options = {
from: Config.email.username,
to: generateAddress(),
subject
};
export default function sendSMS(text: string) {
mailOptions.text = text;
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}`);
}
});
}
function generateAddress() {
for (const carrier of Object.keys(carrierAddress)) {
if (Config.phone.carrier && carrier === Config.phone.carrier.toLowerCase()) {
// @ts-expect-error
return [Config.phone.number, carrierAddress[carrier]].join('@');
}
}
}