mirror of
https://github.com/opelly27/streetmerchant.git
synced 2026-05-20 14:37:41 +00:00
feat(notification): add Twillio notification provider (#344)
This commit is contained in:
@@ -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),
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
})();
|
||||
}
|
||||
Reference in New Issue
Block a user