Skip to contentSkip to navigationSkip to topbar
Rate this page:

Twilio Notify



_68
// Description
_68
// Twilio Notify API to send bulk SMS with the same message (body) as one API request
_68
// Upload notifyList.txt as a private Twilio asset
_68
// notifyList.txt format (comma separated numbers w/wo spaces): +15105550100, +15105550101, +15105550102, +15105550103
_68
// Execute Syntax: https://x.x.x.x/<path>?queryParamPasscode=8675309
_68
// (change passcode below, replace this method with a secure auth method in production)
_68
// Make sure under Functions Global Config tab:
_68
// "Add my Twilio Credentials (ACCOUNT_SID) and (AUTH_TOKEN) to ENV" is CHECKED
_68
_68
const fs = require('fs');
_68
_68
// Add node-fetch 2.6.0 as a dependency under Settings, Dependencies
_68
const fetch = require('node-fetch');
_68
_68
//** START NECESSARY CONFIGURATION**
_68
// You must define your unique Twilio Notify SID - https://www.twilio.com/console/notify/services below
_68
// Notify will make use of a Twilio Messaging Service which you also need to define with a Twilio number(s)
_68
// https://www.twilio.com/console/sms/services
_68
const notifySid = 'IS076575.....';
_68
const passCode = '8675309'; // CHANGE THIS
_68
// Notify Bulk Message Body to Send
_68
const bulkMsgBody = '😸 Hello from Winston 😸';
_68
//** END NECESSARY CONFIGURATION**
_68
_68
exports.handler = function (context, event, callback) {
_68
const params = new URLSearchParams();
_68
const queryParamPasscode = event.queryParamPasscode;
_68
_68
const fileName = '/notifyList.txt';
_68
const file = Runtime.getAssets()[fileName].path;
_68
const numbers = fs.readFileSync(file, 'utf8').trim();
_68
_68
// Must pass a URL query parameter of queryParamPasscode with the value of passCode to execute
_68
if (queryParamPasscode != passCode) return callback('invalid operation'); // You can change the error message
_68
_68
params.append('Body', bulkMsgBody);
_68
_68
numbers.split(',').forEach((number) => {
_68
number.trim();
_68
params.append(
_68
`ToBinding`,
_68
`{ "binding_type": "sms", "address": "${number}" }`
_68
);
_68
});
_68
_68
const headers = {
_68
Authorization:
_68
'Basic ' +
_68
new Buffer.from(`${context.ACCOUNT_SID}:${context.AUTH_TOKEN}`).toString(
_68
'base64'
_68
),
_68
};
_68
_68
fetch(`https://notify.twilio.com/v1/Services/${notifySid}/Notifications`, {
_68
method: 'POST',
_68
headers,
_68
body: params,
_68
})
_68
.then((res) => res.json())
_68
.then((json) => {
_68
console.log(json);
_68
return callback(null, { result: 'success' });
_68
})
_68
.catch((error) => {
_68
console.error(error);
_68
return callback({ result: 'error' });
_68
});
_68
};


Rate this page: