64 lines
2.1 KiB
TypeScript
64 lines
2.1 KiB
TypeScript
import { fetchCall } from '@seed/helpers/FetchHelper';
|
|
import { interpolate, promiseAll } from '@seed/helpers/Utils';
|
|
import EmailSettingsModel from '@services/module-cms/functions/emails-settings/emails-settings.model';
|
|
import { AvailableTranslation } from '@src/__components/components';
|
|
|
|
export const sendSmsHelper = async (
|
|
emailSettings: EmailSettingsModel,
|
|
finalLanguage: AvailableTranslation,
|
|
numbers: string | string[],
|
|
data: any,
|
|
): Promise<boolean> => {
|
|
const smsSettings = emailSettings.smsSettings;
|
|
if (!smsSettings || !smsSettings.text[finalLanguage]) {
|
|
console.error('No sms settings found for this language', finalLanguage);
|
|
return false;
|
|
}
|
|
|
|
//const toNumbers: any[] = [];
|
|
|
|
if (!Array.isArray(numbers)) numbers = [numbers];
|
|
/* for (let index = 0; index < numbers.length; index++) {
|
|
toNumbers.push({ Email: numbers[index] });
|
|
} */
|
|
|
|
const body = interpolate(smsSettings.text[finalLanguage] || '', data);
|
|
|
|
const promises: Promise<any>[] = [];
|
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
const twilio = require('twilio');
|
|
|
|
const client = new twilio(process.env.TWILIO_ACCOUNT_SID, process.env.TWILIO_AUTH_TOKEN);
|
|
|
|
numbers.forEach((element) => {
|
|
const dataToSend = {
|
|
body: body,
|
|
from: process.env.TWILIO_FROM_NUMBER || '',
|
|
to: element,
|
|
};
|
|
|
|
console.log('SMS - Sending to', dataToSend);
|
|
|
|
promises.push(
|
|
/* fetchCall({
|
|
url: `https://api.twilio.com/2010-04-01/Accounts/${process.env.TWILIO_ACCOUNT_SID}/Messages.json`,
|
|
method: 'POST',
|
|
query: dataToSend,
|
|
headers: {
|
|
Authorization: 'Basic ' + Buffer.from(`${process.env.TWILIO_ACCOUNT_SID}:${process.env.TWILIO_AUTH_TOKEN}`).toString('base64'),
|
|
"Content-Type": 'x-www-form-urlencoded',
|
|
},
|
|
}), */
|
|
|
|
client.messages.create({
|
|
...dataToSend,
|
|
}),
|
|
);
|
|
});
|
|
|
|
const promiseResults = await promiseAll(promises);
|
|
console.log(promiseResults);
|
|
|
|
return true;
|
|
};
|