const codeengine = require('codeengine');
async function sendTwilioSms(to, from, body) {
const account = await codeengine
.getAccount('Twilio')
.then((a) => a.properties);
const url = `https://api.twilio.com/2010-04-01/Accounts/${account.SID}/Messages.json`;
const data = new URLSearchParams();
data.append('To', to);
data.append('From', from);
data.append('Body', body);
const requestOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
Authorization:
'Basic ' +
Buffer.from(`${account.SID}:${account.TOKEN}`).toString('base64'),
},
data,
};
try {
const response = await codeengine.axios(url, requestOptions);
const jsonResponse = await response.data;
if (response.ok) {
console.log('SMS sent successfully:', jsonResponse);
return jsonResponse;
} else {
console.error('Error sending SMS:', jsonResponse);
throw new Error(jsonResponse.message);
}
} catch (error) {
console.error('Error:', error);
throw error;
}
}