feat(notification): add Twillio notification provider (#344)

This commit is contained in:
Wills Bithrey
2020-09-27 20:12:56 +01:00
committed by GitHub
parent 3b90bbbe5d
commit f2f8d81498
7 changed files with 295 additions and 0 deletions
+6
View File
@@ -104,6 +104,12 @@ const notifications = {
accessToken: envOrString(process.env.TELEGRAM_ACCESS_TOKEN),
chatId: envOrString(process.env.TELEGRAM_CHAT_ID)
},
twilio: {
accountSid: envOrString(process.env.TWILIO_ACCOUNT_SID),
authToken: envOrString(process.env.TWILIO_AUTH_TOKEN),
from: envOrString(process.env.TWILIO_FROM_NUMBER),
to: envOrString(process.env.TWILIO_TO_NUMBER)
},
twitter: {
accessTokenKey: envOrString(process.env.TWITTER_ACCESS_TOKEN_KEY),
accessTokenSecret: envOrString(process.env.TWITTER_ACCESS_TOKEN_SECRET),
+6
View File
@@ -11,6 +11,7 @@ import {sendSMS} from './sms';
import {sendSlackMessage} from './slack';
import {sendTelegramMessage} from './telegram';
import {sendTweet} from './twitter';
import {sendTwilioMessage} from './twilio';
const notifications = Config.notifications;
@@ -53,6 +54,11 @@ export function sendNotification(link: Link, store: Store) {
sendTelegramMessage(link, store);
}
if (notifications.twilio.accountSid && notifications.twilio.authToken) {
Logger.debug('↗ sending twilio message');
sendTwilioMessage(link, store);
}
if (notifications.pushBulletApiKey) {
Logger.debug('↗ sending pushbullet message');
sendPushBulletNotification(link, store);
+26
View File
@@ -0,0 +1,26 @@
import {Link, Store} from '../store/model';
import {Logger, Print} from '../logger';
import {Config} from '../config';
import twilio from 'twilio';
const config = Config.notifications.twilio;
const client = twilio(config.accountSid, config.authToken);
export function sendTwilioMessage(link: Link, store: Store) {
(async () => {
const givenUrl = link.cartUrl ? link.cartUrl : link.url;
const message = `${Print.inStock(link, store)}\n${givenUrl}`;
try {
await client.messages.create({
body: message,
from: config.from,
to: config.to
});
Logger.info('✔ twilio message sent');
} catch (error) {
Logger.error('✖ couldn\'t send twilio message', error);
}
})();
}