Javier Auspont Cerda
Javier Auspont Cerda

Reputation: 11

WhatsApp Business API custom text message with Laravel won't work

I'm trying to use the API to send to myself a custom message, I can already send a hello world template, but I been unable to send myself a custom only text message.

//API token given by Meta
        $token = '*private*';

        //Reciever number of msg
        $telefono = '*private*';

        //URL of msg given by Meta
        $url = '*I think is private too*';

        $mensaje = '{"messaging_product": "whatsapp", "to": "'.$telefono.'", "type": "text", "text": {"preview_url": false, "body": "MESSAGE_CONTENT" }}';
//header of msg
$header = array("Authorization: Bearer " . $token, "Content-Type: application/json");

so that's what I use to make the Curl to send the message, which I do like this

//curl init
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $mensaje);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

        //get response from information sent
        $response = json_decode(curl_exec($curl),true);

        //print response
        print_r($response);

        //get curl response code
        $status_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);

        print_r($status_code);
        //close curl
        curl_close($curl);

I think is kind of working, because it shows on screen this result

Array ( [messaging_product] => whatsapp [contacts] => Array ( [0] => Array ( [input] => phone [wa_id] => 56994134989 ) ) [messages] => Array ( [0] => Array ( [id] => i think is private too ) ) ) 200

In the end I just need to test if its possible to send myself custom text messages, the thing is that this code isn't working as I want it to, as you should see, on the print of the result and the status_code, the code is 200 what makes me think that the request is going through at some point but maybe it need something else to finally be sent to my phone.

Upvotes: 0

Views: 601

Answers (1)

Javier Auspont Cerda
Javier Auspont Cerda

Reputation: 11

Testing a little bit more and researching came to the solution for anyone that has this similar problem.

When using Whatsapp Bussiness API you will be given a test number, at first glance you can only send Hello World templates messages to your phone, the way to "enable" sending custom messages is by sending yourself a message to the test number, that way the next time you run the code it sends the message you wanted.

Upvotes: 1

Related Questions