Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send bulk sms in twilio api without for loop

I'm trying to send bulk sms through twilio Api. Is there any method to pass the array of all phone numbers in a single API request.

like image 454
Hassan Ejaz Avatar asked Oct 31 '17 07:10

Hassan Ejaz


People also ask

How many messages can Twilio send at once?

Requesting to increase sender pool limit for a Messaging Service. If you need more than 400 message senders in a single Messaging Service, please contact Twilio Support about a Messaging Service sender pool limit increase.


3 Answers

Twilio developer evangelist here.

Yes there is now! It's known as the passthrough API (as it allows you to pass through many different messaging systems and send bulk messages. It's part of the Notify API and you can use it to send bulk SMS messages. You need to set up a messaging service and a notify service in your console, then you can use the following code:

<?php
// NOTE: This example uses the next generation Twilio helper library - for more
// information on how to download and install this version, visit
// https://www.twilio.com/docs/libraries/php
require_once '/path/to/vendor/autoload.php';

use Twilio\Rest\Client;

// Your Account SID and Auth Token from https://www.twilio.com/console
$accountSid = "your_account_sid";
$authToken = "your_auth_token";

// your notify service sid
$serviceSid = "ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";

// Initialize the client
$client = new Client($accountSid, $authToken);

// Create a notification
$notification = $client
    ->notify->services($serviceSid)
    ->notifications->create([
        "toBinding" => [
            '{"binding_type":"sms", "address":"+15555555555"}',
            '{"binding_type":"sms", "address":"+12345678912"}'
        ],
        "body" => "Hello Bob"
    ]);

Checkout the documentation on sending multiple messages with the Notify passthrough API for all the details.

like image 104
philnash Avatar answered Oct 22 '22 02:10

philnash


In case someone else had troubles with preparing the toBinding parameter from a PHP array() as I had, here is an example for that:

<?php
require_once '/path/to/vendor/autoload.php';

use Twilio\Rest\Client;

$accountSid = "your_account_sid";
$authToken = "your_auth_token";
$serviceSid = "ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";

$client = new Client($accountSid, $authToken);

$recipients = array($num1, $num2, ...); // Your array of phone numbers

$binding = array();
foreach ($recipients as $recipient) { 
    $binding[] = '{"binding_type":"sms", "address":"+1'.$recipient.'"}'; // +1 is used for US country code. You should use your own country code.
}

$notification = $client
->notify->services($service_sid)
->notifications->create([
    "toBinding" => $binding,
    "body" => $text
]);
?>
like image 28
Envayo Avatar answered Oct 22 '22 01:10

Envayo


First of all, you need to configure your twilio number properly for notification. Then your to use below code to send bulk SMS.

$message = 'Any text message';
$to = array();
foreach ($users as $user) { 
    $to[] = '{"binding_type":"sms", "address":"'.$user->phone_number.'"}';
}

$sid    = 'TWILIO_ACCOUNT_SID';
$token  = 'TWILIO_AUTH_TOKEN';
$services_id = 'TWILIO_SERVICE_ID';
$twilio = new Client($sid, $token);


$notification = $twilio
->notify->services($services_id)
->notifications->create([
    "toBinding" => $to,
    "body" => $message
]);
like image 1
MD. Shafayatul Haque Avatar answered Oct 22 '22 01:10

MD. Shafayatul Haque