3
votes

I am using Twilio PHP Library, and i want to send SMS messages to a big list of people using Twilio PHP REST API. I have a text file, that contains the list of unique phone numbers (approx 1000 members).

Currently my plan was to create a cron job, which parses the text file, and makes a REST API call for each of the phone numbers in the file.

Ex:

<?php
require_once 'vendor/autoload.php';
use Twilio\Rest\Client;
$sid = "ACXXXXXXXXXXXXXXXXXXXXXXXX";
$token = "XXXXXXXXXXXXXXXXXXXXXXXXXX";
$client = new Client($sid, $token);

$sms_sent = $client->messages->create(
    '+1xxxxxxxxxx',
    array(
        'from' => '+1xxxxxxxxxx',
        'body' => "XXXXXX XXXXXX",
        'statusCallback' => "http://myapplication_callback_url"
    )
);

But i wanted to know, whether Twilio can do the job for me (like i will upload the text file to Twilio, and Twilio will process the file and send SMS to each number in it).

Is there any option like this with Twilio? I have read about the BULK SMS option here, but that seems like it sends out a static message to all the users. I need to send unique message to each user. How is this possible with Twilio? Please let me know.

EDIT: 27-09-2017: I have heard about Twilio COPILOT, but not sure how to use that service.

3
Why not ask support.twilio.com/hc/en-us/requests/new ? It's not quite a programming question.Alex Blex
Where is that unique message to each user coming from if what you say is you want to upload a text file with numbers to Twilio?Alex Baban
@AlexBaban I can also specify a web service url which outputs a unique message for each call, which Twilio can probably use to send message to users, while parsing the text file.shasi kanth

3 Answers

2
votes

Twilio developer evangelist here.

Just to distill your problem, you need to send about 1000 unique messages to users, right?

If that's the case, then this is what you need to do.

Since the message is unique to each number, you need to make an API call to the Twilio REST API to send each message individually. Twilio caps the message sending at 1 message per number per second. If you are already over this limit, then Twilio will queue those messages up for you. It should not take 5 seconds per API call.

This is the case if you use one number, like in your original example. For 1000 messages it will take almost 17 minutes to send them all.

However, you can use a messaging service to speed this up. A messaging service is a pool of numbers and other services provided by Copilot (including geo matching numbers, alphanumeric sender fallback and other such things). For sending a lot of messages, like in this case, the number pool is the important part because each Twilio number you are sending from can send 1 message per second. So if you add 2 numbers to the messaging service you can send 2 messages per second, if you add 10 numbers to the pool you can send 10 a second.

To send messages using a messaging service and number pool, like this, you need to create a messaging service in your Twilio console. Add or buy numbers for the pool. Then, get the messaging service SID:

Find the messaging service SID in your Twilio console where you created the service.

You can then use the messaging service SID in place of the from number in your call to the API.

$sms_sent = $client->messages->create(
    '+1xxxxxxxxxx',
    array(
        'from' => 'MESSAGING SERVICE SID',
        'body' => "XXXXXX XXXXXX",
        'statusCallback' => "http://myapplication_callback_url"
    )
);

Then your messages will be fanned out by the messaging service.

Notably, if you need to send 1000 messages in a day, we recommend you spread that out over at least 4 numbers as US carriers start to block long code numbers that are used more than that. Check out more in our guidelines for sending SMS messages to the US.

Let me know if that helps at all.

0
votes

Very simple. First configure your number properly for notification and then use my code:

$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
]);
-1
votes

do you have the ability to create an SQL connection? 1 column to house the number to call, one column for the message content?

you can then loop through the rows in the table with your script above to fire off your messages one at a time.

//spaghetti code, will not work
//1. Create MySQL Connection
//2. loop through table
foreach($row as $number=>$message) {
$sms_sent = $client->messages->create(
    $number,
    array(
        'from' => '+1xxxxxxxxxx',
        'body' => $message,
        'statusCallback' => "http://myapplication_callback_url"
        )
    );
}