This Node.js script reads a CSV file, sends a WhatsApp message to each phone number listed in the file using Twilio Programmable Messaging (using a pre-registered Content from the Content API/Editor), and then registers the message using Twilio Sync.
- Content API access
- Twilio CLI installed
- Twilio Serverless Toolkit installed
- Node.js installed. (Minimum Version: Nodejs v12)
- Clone this repository
- In the root folder, copy the Environment Variables file sample into a production file
cp .env.example .env
- Install dependencies
npm install
- Setup
.env
with your Twilio Project Service SIDs and WhatsApp Phone Number- API Key SID and Secret
- Sync Service SID
- Create a new Sync Service Map, navigate back to your Sync Service and copy the SID
- WhatsApp Phone Number
- Navigate to
contact-helper
cd contact-helper
- Copy the Serverless Application Environment Variables file sample into a production file
cp .env.example .env
- Setup
.env
using the same info from the script Environment File, except for the WhatsApp Phone Number - Deploy the Twilio Functions
twilio serverless:deploy
- In the root folder, run
node load-contact-list.js [contacts list CSV path]
- The CSV file must contain the following columns (in any order):
- PHONE
- TEMPLATE
- Template variables with numbers (1,2,3, etc)
The get-contact
function expects two parameters:
- phone: The sender phone number (OBS: Twilio processes WhatsApp phone numbers as
whatsapp:<phone number>
. Don't worry about parsing the number. The function already takes care of this) - originalMessageSid: The Original Message SID. In Studio, it'll be part of the trigger message, in
{{trigger.message.OriginalRepliedMessageSid}}
To create new content you can go to the Content Editor or use the Content API directly. Keep in mind that WhatsApp must approve content to be used as outbound messages
Beware that this solutions is meant only as a quickstart for bulk sending WhatsApp messages. Keep in mind thar Twilio APIs are rate limited, which means you will need to implement some sort of backoff or pacing logic to avoid 429 errors
.