1

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.

NomanJaved
  • 1,324
  • 17
  • 32
  • @philnash #philnash or Twilio Geek Please assist me about the question. . – NomanJaved Jul 19 '22 at 23:12
  • You can’t use Twilio Notify for that use case. You must use the /Messages resource, https://www.twilio.com/docs/sms/api/message-resource. – Alan Jul 19 '22 at 23:38

1 Answers1

0

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.

philnash
  • 70,667
  • 10
  • 60
  • 88