Sending bulk SMS in Node.js using Twilio
SMS is a common method of sending short messages between cell phones, but these SMS can be sent to multiple users at a time using Twilio notify service. Sending bulk SMS to users at a time is possible using Twilio.
Introduction:
- It’s easy to get started and easy to use.
- It is widely used for sending bulk SMS.
- User can send SMS to desired mobile numbers fastly and efficiently.
Installation of Twilio module:
- You can visit the link to Install twilio module. You can install this package by using the following command.
npm install twilio
- After installing twilio you can check your twilio version in command prompt using the command.
npm version twilio
- After that, you can create a folder and add a file. For example
index.js. To run this file you need to run the following command.node index.js
- Requiring module: You need to include twilio module in your file by using these lines.
const client = require('twilio')(YOUR_ACCOUNT_SID, YOUR_AUTH_TOKEN); - Go to Twilio official website and create your account. After login, go to your dashboard and you can get your auth_token and account_sid.

- Now go to console/notify/services and create your own notify service.

- Now select a message service sid, if is it not showing then you have to create a message service sid and add a phone number to it and then you can see that message service sid in the drop-down as shown below.

- Copy your service_sid and click the save button.
- The project structure will look like this:

- Make sure you have installed twilio using the following commands:
npm install twilio
- Run index.js file using following command:
node index.js

- SMS send successfully to all numbers passed as parameter.

Steps to get your YOUR_ACCOUNT_SID, YOUR_AUTH_TOKEN and YOUR_SERVICE_SID:
Filename: index.js
ACCOUNT_SID = 'Your_Account_Sid'AUTH_TOKEN = 'Your_Auth_Token'SERVICE_SID = 'Your_Service_Sid'const client = require('twilio')(ACCOUNT_SID, AUTH_TOKEN); // User-defined function to send bulk SMS to desired// numbers bypassing numbers list as parameterfunction sendBulkMessages(messageBody, numberList){ var numbers = []; for(i = 0; i < numberList.length; i++) { numbers.push(JSON.stringify({ binding_type: 'sms', address: numberList[i]})) } const notificationOpts = { toBinding: numbers, body: messageBody, }; client.notify .services(SERVICE_SID) .notifications.create(notificationOpts) .then(notification => console.log(notification.sid)) .catch(error => console.log(error));} // Sending our custom message to all numbers// mentioned in array.sendBulkMessages('Greeting from geeksforgeeks', ['number1', 'number2']) // Example +919999999999 |
Steps to run the program:
Please Login to comment...