0

I want to send a message via whatsapp using Twilio API for WhatsApp in PHP. my code doesn't give an error but the message isn't being received in the target mobile phone..

here's my code:

// For live API
$this->sid = 'MY_TWILIO_SID';
$this->token = 'MY_TOKEN';
$this->from_number = '+178678xxx';

$client = new Client($this->sid, $this->token);

$to = '+628123456789';
$msg = 'Some greeting message';

// Use the client to do fun stuff like send text messages!
$response = $client->messages
                    ->create(
                        // the number you'd like to send the message to
                        "whatsapp:".$to,
                        array(
                            // A Twilio phone number you purchased at twilio.com/console
                            'from' => "whatsapp:".$this->from_number,
                            // the body of the text message you'd like to send
                            'body' => $msg
                        )
                    );

echo '<pre>'; 
print_r($response); 
echo '</pre>';

the result was a Twilio MessageInstance Object like this:

Twilio\Rest\Api\V2010\Account\MessageInstance Object
(
    [_media:protected] => 
    [_feedback:protected] => 
    [version:protected] => Twilio\Rest\Api\V2010 Object
...
    [solution:protected] => Array
        (
            [accountSid] => xxxxx
            [sid] => xxxxx
        )

)

note: I've contacted the Twilio cust.service and 4 days ago she informed me that my sender number (+1786...) has been registered and can send and receive messages via whatsapp.

dapidmini
  • 1,490
  • 2
  • 23
  • 46
  • In my case, I had to accept the terms and conditions to be able to received messages. More info here: https://stackoverflow.com/a/74402256/2990234 – Anfuca Nov 11 '22 at 12:09

1 Answers1

0

This is how the WhatsApp API works. Whenever you make a right syntax call, Twilio will return a message SID at runtime and forward the message to WhatsApp/Meta.

Then Meta might decide that you message won't be delivered, e.g. because you're outside of a messaging session or the message doesn't fit a pre-approved template.

Are you able to see your messages in the "messaging monitor" in the console? You should be able to see the status with a more detailed description in there.

enter image description here

IObert
  • 2,118
  • 1
  • 10
  • 17