skip to Main Content

We are using the following code to send WhatsApp messages using the WhatsApp Cloud API. The code is as follows:

<?php

$phoneNumberId = 'Phone number ID';
$accessToken = 'Access Token';

// Array of phone numbers
$recipientPhoneNumbers = [
    'number 1',
    'number 2',
    'number 3',
    'number 4',
];

$text = "
Hello,
Thank you for registering with Co Network. To activate your account and benefit from our services, please click on the following link:
Link
Thank you for choosing Co Network.";

$url = "https://graph.facebook.com/v20.0/$phoneNumberId/messages";

// Loop through each number in the array and send the message
foreach ($recipientPhoneNumbers as $recipientPhoneNumber) {
    $data = [
        "messaging_product" => "whatsapp",
        "to" => $recipientPhoneNumber,
        "type" => "text",
        "text" => [
            "body" => $text
        ]
    ];

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Content-Type: application/json',
        'Authorization: Bearer ' . $accessToken
    ]);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);

    $result = curl_exec($ch);

    if (curl_errno($ch)) {
        $error_msg = curl_error($ch);
        curl_close($ch);
        error_log('Error sending message to ' . $recipientPhoneNumber . ': ' . $error_msg);
        echo 'Error sending message to ' . $recipientPhoneNumber . ': ' . $error_msg . '<br>'; // Display error message directly on the screen
        continue; // Skip to the next number in case of an error
    }

    curl_close($ch);

    $response = json_decode($result, true);

    if (isset($response['error'])) {
        error_log('Error: ' . $response['error']['message']);
        echo 'Error: ' . $response['error']['message'] . ' for ' . $recipientPhoneNumber . '<br>'; // Display error message directly on the screen
        var_dump($response); // Display the full response for more accurate diagnosis
        continue; // Skip to the next number in case of an error
    }

    error_log('Message sent successfully to ' . $recipientPhoneNumber);
    echo 'Message sent successfully to ' . $recipientPhoneNumber . '<br>'; // Display success message directly on the screen
    var_dump($response); // Display the full response to confirm success
}

?>

The issue we are encountering is that messages are delivered to some numbers but not to others. In some cases, only one message out of many is delivered. We are certain that the numbers are correct and that they are registered on WhatsApp.

Please assist us in resolving this issue as quickly as possible.

Thank you.

The issue we are encountering is that messages are delivered to some numbers but not to others.

2

Answers


  1. The reason in your case might vary,

    1- you don’t have open sessions with the numbers you are trying to contact, if you don’t have open conversation with them you can only send a message using an approved template.

    2- your waba is restricted, check your business manager for any restriction messages.

    3- you didn’t connect a payment method to your Waba from business manager.

    4- if your are not receiving error message on your sent requests, probably you are getting response of 201 (Accepted) with a message ID, and to know what exactly the status of your sent message, you have to setup a webhook and listen for incoming webhooks from meta.
    you can’t directly get your sent messages status on request, you have to listen for incoming webhooks to know what is going on.

    I advice you to always register your WABA with a whatsapp partner, so you can get support from their side @Karzoun.

    Login or Signup to reply.
  2. Can probably help based on the error code… please provide the $error_msg for better context

    Check Cloud API Error Codes

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search