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