I want to change the body of sms in evey message using Twilio Nofity?

Viewed 48

I am trying to send the bulk sms I had uploaded the number list in database and fetch that here. This code is working fine for different numbers with same sms body.

$recipients = array();
foreach($phone_nos as $phone_no) {
            array_push($recipients, $phone_no['phone_no']);
}

$binding = array();
foreach ($recipients as $recipient) { 
    $binding[] = '{"binding_type":"sms", "address":"'.$recipient.'"}'; 
}

$notification = $twilio->notify->v1->services($serviceSid)
                ->notifications->create([
                "toBinding" => $binding,
                "body" => Hi First Name, How are you welcome to panel, 
                 // I want to make this dynamic, Every time first name will change
                "sms" => [
                    "status_callback" => AURL .'GroupSms/bulk_sms_status_callback'
                ],
]);

Now, I want the body of each sms dynamic. Like "Hi (First Name) welcome on the panel". Each phone number will have his First Name. How I can achieve it. Neither I found the solution on search engine nor on documentation.

1 Answers

The Twilio Notify API only allows you to send the same message to each binding.

As Alan has said in the comments, to send different message to each person in a list means you will need to make individual calls to the Messages Resource.

$recipients = array();
foreach($phone_nos as $phone_no) {
    array_push($recipients, $phone_no['phone_no']);
}

foreach ($recipients as $recipient) {
    $twilio->messages
        ->create($recipient, // to
                 ["from" => $myTwilioNumber, "body" => $personalisedBody]
        );
}

Note that this does require you to make an API request for each number in your list. If the list is long, this can take a long time, which would be a bad experience if this was in response to an HTTP request. You might want to consider sending the messages in background job off of the main thread.

Related