Open In App

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:



  1. It’s easy to get started and easy to use.
  2. It is widely used for sending bulk SMS.
  3. User can send SMS to desired mobile numbers fastly and efficiently.

Installation of Twilio module:

  1. You can visit the link to Install twilio module. You can install this package by using the following command.
    npm install twilio
  2. After installing twilio you can check your twilio version in command prompt using the command.
    npm version twilio
  3. 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
  4. 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);
  5. Steps to get your YOUR_ACCOUNT_SID, YOUR_AUTH_TOKEN and YOUR_SERVICE_SID:



    1. 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.
    2. Now go to console/notify/services and create your own notify service.
    3. 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.
    4. Copy your service_sid and click the save button.

    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 parameter
    function 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:

    1. The project structure will look like this:
    2. Make sure you have installed twilio using the following commands:
      npm install twilio
    3. Run index.js file using following command:
      node index.js

    4. SMS send successfully to all numbers passed as parameter.

Article Tags :