mirror of
https://github.com/opelly27/streetmerchant.git
synced 2026-05-20 05:17:35 +00:00
34 lines
850 B
TypeScript
34 lines
850 B
TypeScript
import {Link, Store} from '../store/model';
|
|
import {Print, logger} from '../logger';
|
|
import {Twilio} from 'twilio';
|
|
import {config} from '../config';
|
|
|
|
const twilio = config.notifications.twilio;
|
|
let client: Twilio;
|
|
|
|
if (twilio.accountSid && twilio.authToken) {
|
|
client = new Twilio(twilio.accountSid, twilio.authToken);
|
|
}
|
|
|
|
export function sendTwilioMessage(link: Link, store: Store) {
|
|
if (client) {
|
|
logger.debug('↗ sending twilio message');
|
|
|
|
(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: twilio.from,
|
|
to: twilio.to
|
|
});
|
|
logger.info('✔ twilio message sent');
|
|
} catch (error) {
|
|
logger.error('✖ couldn\'t send twilio message', error);
|
|
}
|
|
})();
|
|
}
|
|
}
|