RSilva
RSilva

Reputation: 6943

WhatsApp Business Api - Can't create Message Template

I'm having trouble in creating a WhatsApp Message template, using the WhatsApp Business API.

I already can send messages, delete template, etc.

Creating the message template is the only API implementation where I'm having some dificulties. This is the code that I'm using to create a template:


public static function CreateTemplate($templateName, $templateCat, $language, $components){


    $businessaccountid = .....;
    $token = ....;

    $ch = curl_init();
    $url = 'https://graph.facebook.com/v16.0/' . $businessaccountid . '/message_templates?allow_category_change=true&name=' . $templateName . '&category=' . $templateCat . '&language=' . $language . '&components=' . $components;
    //echo($url);
    //curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    //curl_setopt($ch, CURLOPT_VERBOSE , TRUE);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, 1);
    $headers = array();
    
    $headers[] = 'Authorization: Bearer ' . $token ;
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    
    

    $result = curl_exec($ch);
    

    if ($result === false) {
        $result = curl_error($ch) . " - ".curl_errno($ch);
        
    }else{
        $resultDecode = json_decode($result);
        if($resultDecode!=null){
            $result = $resultDecode;
        }
        return($result);
    }
    curl_close($ch);
    
}

And this is the content of the $url variable that is being used:

"https://graph.facebook.com/v16.0/....../message_templates?allow_category_change=true&name=compra_terminada&category=MARKETING&language=pt_PT&components=[{"type":"BODY","text":"Obrigado por comprar na nossa loja. Assim que o objecto for expedido, irá receber no seu email o numero de rastreamento da encomenda."},{"type":"FOOTER","text":"A minha empresa"}]"

And this is error that it's returning from curl_error and curl_errno:

HTTP/2 stream 0 was not closed cleanly: Unknown error code (err 5) - 92

I've already tried to use CURL_HTTP_VERSION_1_1 but with no success. The curl_exec($ch) returns a generic html page from Facebook saying:

Sorry, something went wrong.
We're working on it and we'll get it fixed as soon as we can.

What do I need to change in my code to create a WhatsApp Template Message?

Upvotes: 0

Views: 1121

Answers (1)

RSilva
RSilva

Reputation: 6943

This issue was solved by sending the parameters as a json string, and setting the header to "application/json"

public static function CreateTemplate($templateName, $templateCat, $language, $components){


    $businessaccountid = .....;
    $token = ....;

    $ch = curl_init();
    $url = 'https://graph.facebook.com/v15.0/' . $businessaccountid . '/message_templates';

    $data = array(
        'allow_category_change' => true,
        'name' => $templateName,
        'category' => $templateCat,
        'language' => $language,
        'components' => $components
    );
    $headers = array();
    
    $headers[] = 'Authorization: Bearer ' . $token ;
    $headers[] = 'Content-Type:application/json' ;

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    
    $result = curl_exec($ch);
    

    if ($result === false) {
        $result = curl_error($ch) . " - ".curl_errno($ch);
        
    }else{
        $resultDecode = json_decode($result);
        if($resultDecode!=null){
            $result = $resultDecode;
        }
        return($result);
    }
    curl_close($ch);
    
}

Upvotes: 1

Related Questions