diff --git a/README.md b/README.md index 19b9280..75889e9 100644 --- a/README.md +++ b/README.md @@ -308,8 +308,8 @@ environment variables are **optional**._ | `EMAIL_PASSWORD` | Gmail password | See below if you have MFA | | `EMAIL_TO` | Destination Email | Defaults to username if not set. Can be comma separated | | `EMAIL_USERNAME` | Gmail address | E.g.: `jensen.robbed.us@gmail.com` | -| `PHONE_CARRIER` | [Supported carriers](#supported-carriers) for SMS | Email configuration required | -| `PHONE_NUMBER` | 10 digit phone number | E.g.: `1234567890`, email configuration required | +| `PHONE_CARRIER` | [Supported carriers](#supported-carriers) for SMS | E.g.: `att` or `att,verizon,google`, email configuration required. If multiple phone numbers are listed, enter a carrier for each phone number | +| `PHONE_NUMBER` | 10 digit phone number(s) | E.g.: `1234567890` or `1234567890,0987654321,11112223333`, email configuration required | | `SMTP_ADDRESS` | IP Address or fqdn of smtp server | | `SMTP_PORT` | TCP Port number on which the smtp server is listening for connections | Default: `25` | diff --git a/package.json b/package.json index 0556dd8..e3a46fe 100644 --- a/package.json +++ b/package.json @@ -73,7 +73,8 @@ "rules": { "sort-imports": "error", "sort-keys": "error", - "sort-vars": "error" + "sort-vars": "error", + "max-params": 0 } }, "husky": { diff --git a/src/config.ts b/src/config.ts index 9c46deb..a395b25 100644 --- a/src/config.ts +++ b/src/config.ts @@ -180,8 +180,8 @@ const notifications = { ['virgin', 'vmobl.com'], ['virgin-ca', 'vmobile.ca'] ]), - carrier: envOrString(process.env.PHONE_CARRIER), - number: envOrString(process.env.PHONE_NUMBER) + carrier: envOrArray(process.env.PHONE_CARRIER), + number: envOrArray(process.env.PHONE_NUMBER) }, playSound: envOrString(process.env.PLAY_SOUND), pushbullet: envOrString(process.env.PUSHBULLET), diff --git a/src/notification/sms.ts b/src/notification/sms.ts index 09b8599..02ccb2a 100644 --- a/src/notification/sms.ts +++ b/src/notification/sms.ts @@ -10,42 +10,55 @@ if (config.notifications.phone.number && !config.notifications.email.username) { const [email, phone] = [config.notifications.email, config.notifications.phone]; +if (phone.carrier.length !== phone.number.length) { + logger.warn('✖ the number of carriers must match the number of phone numbers'); +} + export function sendSms(link: Link, store: Store) { - if (phone.number) { - logger.debug('↗ sending sms'); - const carrier = phone.carrier; + for (let i = 0; i < Math.max(phone.number.length, phone.carrier.length); i++) { + const currentNumber = phone.number[i]; + const currentCarrier = phone.carrier[i]; - if (carrier && phone.availableCarriers.has(carrier)) { - const mailOptions: Mail.Options = { - attachments: link.screenshot ? [ - { - filename: link.screenshot, - path: `./${link.screenshot}` - } - ] : undefined, - from: email.username, - subject: Print.inStock(link, store, false, true), - text: link.cartUrl ? link.cartUrl : link.url, - to: generateAddress() - }; - - transporter.sendMail(mailOptions, error => { - if (error) { - logger.error('✖ couldn\'t send sms', error); - } else { - logger.info('✔ sms sent'); - } - }); + if (!currentNumber) { + logger.error(`✖ ${currentCarrier} is not associated with a number`); + continue; + } else if (!currentCarrier) { + logger.error(`✖ ${currentNumber} is not associated with a carrier`); + continue; } + + if (!phone.availableCarriers.has(currentCarrier)) { + logger.error(`✖ unknown carrier ${currentCarrier}`); + continue; + } + + logger.debug('↗ sending sms'); + + const mailOptions: Mail.Options = { + attachments: link.screenshot ? [ + { + filename: link.screenshot, + path: `./${link.screenshot}` + } + ] : undefined, + from: email.username, + subject: Print.inStock(link, store, false, true), + text: link.cartUrl ? link.cartUrl : link.url, + to: generateAddress(currentNumber, currentCarrier) + }; + + transporter.sendMail(mailOptions, error => { + if (error) { + logger.error(`✖ couldn't send sms to ${currentNumber} for carrier ${currentCarrier}`, error); + } else { + logger.info('✔ sms sent'); + } + }); } } -function generateAddress() { - const carrier = phone.carrier; - +function generateAddress(number: string, carrier: string) { if (carrier && phone.availableCarriers.has(carrier)) { - return [phone.number, phone.availableCarriers.get(carrier)].join('@'); + return [number, phone.availableCarriers.get(carrier)].join('@'); } - - logger.error('✖ unknown carrier', carrier); }