Question about how configure a "webhook" with "tech provider"
19:57 28 Feb 2025

Actually I don't have a specific error, sorry about that. But I would like to ask a few questions about this topic, because I think the documentation a bit confusing.

I followed all the steps to use the Whatsapp API, including signing webhooks. I understood how everything works and tested my CRM, which sends and receives messages from customers, for a few weeks.

After that, I started the steps to become an independent tech provider. I also completed the steps for this, signed permissions and was fully verified. I started implementing "Embedded Signup", I had problems at first, but I read some forums and reread the documentation more times than I would like. I managed to implement it and I'm in the testing process.

Basically, I got the phone_number_id and the waba_id. And then I was able to exchange the token, as the documentation says. All the steps return true or finish, so it's working.

My question now is how to test or implement this in a practical way. I couldn't find in the documentation how exactly I send and receive messages on behalf of my clients. In other words, when sending the message, I send the message with my client's number, and he will receive a message if it is directed to this specific client. I read about how to manage client webhooks at "https://developers.facebook.com/docs/whatsapp/embedded-signup/webhooks", and about something similar to this in a question on this forum. Meta Cloud API is triggering every webhook for multiple Apps

My question is whether each client will have their own webhook and I need to code a webhook for each one. Or do all my clients use my webhook (which I signed and configured previously). And if they all use my webhook, how do I know who the user is sending a message to? Which company (my client) the message is sent to, please explain better.

Another question is whether my client needs to configure products in their application panel. If he needs to manually sign the WhatsApp API and the webhook and configure them manually. When I tested the "Embedded Signup" with another Facebook account and entered the apps panel, nothing signed appeared.

Sorry if I have some "dumb" questions, but Meta support is not working, and I can't find much material to study. I'm a beginner in this theme and would like some kind soul to help. And if you have the same questions as me, or other questions related to the WhatsApp API, I will be happy to answer them.

EDIT 1: The following file is the front end of "embedded signup". file "index.html"

    

  
    Soltec - Cadastro
    
    
    
  
  
     
    

    

    
    
    

Soltec

What I'm trying to do is replace the token (it's working, since it's returning true), sign the webhook and send a message. Signing the webhook and replacing the webhook url is working. I followed the doc https://developers.facebook.com/docs/whatsapp/embedded-signup/webhooks/override But sending a message is giving an error. It says "The account is not registered" file "bd_token.php":

 "Parâmetros ausentes"]);
    exit;
}

$CODE = $_POST['code'];
$WABA_ID = $_POST['waba_id'];
$BUSINESS_PHONE_NUMBER_ID = $_POST['phone_id'];

// Exemplo de log para depuração
error_log("WABA_ID: " . $WABA_ID);
error_log("CODE: " . $CODE);
error_log("BUSINESS_PHONE_NUMBER_ID: " . $BUSINESS_PHONE_NUMBER_ID);

// --------------------------------------------------------------------- RETORNA TOKEN
$url = "https://graph.facebook.com/{$API_VERSION}/oauth/access_token?client_id={$APP_ID}&client_secret={$APP_SECRET}&code={$CODE}";

$response = file_get_contents($url);
$result = json_decode($response, true);

// Verificar se houve erro
if (isset($result['error'])) {
    echo json_encode(["error" => $result['error']['message']]);
    exit;
}
// Retornar o token obtido
//echo json_encode(["business_token" => $result['access_token']]);
if($result["access_token"]){
    $BUSINESS_TOKEN = $result["access_token"];

    // ----------------------------------------------------------- ASSINAR OS WEBHOOKS
    $url_webhook = "https://graph.facebook.com/{$API_VERSION}/{$WABA_ID}/subscribed_apps";
    // Configurar headers e envio via cURL
    $headers = [
        "Authorization: Bearer {$BUSINESS_TOKEN}",
        "Content-Type: application/json"
    ];
    //$data = json_encode(["subscribed_fields" => ["messages"]]);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url_webhook);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_POST, true);
    //curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $response_webhook = curl_exec($ch);
    curl_close($ch);

    $result_webhook = json_decode($response_webhook, true);

    if(isset($result_webhook['success'])){
        // --------------------------------------------------- URL WEBHOOK ALTERNATIVO
        $alternate_callback_url = "https://soltecinfo.com.br/crm/webhook_alternativo/";
        $verify_token = "123"; // 🔑 Defina um token de verificação

        $url_alternate_callback = "https://graph.facebook.com/{$API_VERSION}/{$WABA_ID}/subscribed_apps";

        $data_alternate_callback = json_encode([
            "override_callback_uri" => $alternate_callback_url,
            "verify_token" => $verify_token
        ]);

        $ch_alt = curl_init();
        curl_setopt($ch_alt, CURLOPT_URL, $url_alternate_callback);
        curl_setopt($ch_alt, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch_alt, CURLOPT_POST, true);
        curl_setopt($ch_alt, CURLOPT_POSTFIELDS, $data_alternate_callback);
        curl_setopt($ch_alt, CURLOPT_RETURNTRANSFER, true);

        $response_alt_callback = curl_exec($ch_alt);
        curl_close($ch_alt);

        $result_alt_callback = json_decode($response_alt_callback, true);

        // Retornar o resultado da configuração do callback alternativo

        // Agora, verificamos se o webhook está correto usando o BUSINESS_PHONE_NUMBER_ID
        $url_check_webhook = "https://graph.facebook.com/{$API_VERSION}/{$BUSINESS_PHONE_NUMBER_ID}?fields=webhook_configuration";

        $ch_check = curl_init();
        curl_setopt($ch_check, CURLOPT_URL, $url_check_webhook);
        curl_setopt($ch_check, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch_check, CURLOPT_RETURNTRANSFER, true);

        $response_check_webhook = curl_exec($ch_check);
        curl_close($ch_check);

        $result_check_webhook = json_decode($response_check_webhook, true);

        // ------------------------------------------------------------ ENVIA MENSAGEM
        //$BUSINESS_PHONE_NUMBER_ID = "ID PHONE OF MY CLIENT"; // Substitua com o ID correto
        $WHATSAPP_USER_NUMBER = "My number phone for test"; // Substitua com o número do cliente
        $bodyText = "Olá, esta é uma mensagem de teste.";

        // Definir os dados para enviar a mensagem
        $messageData = [
            'messaging_product' => 'whatsapp',
            'recipient_type' => 'individual',
            'to' => $WHATSAPP_USER_NUMBER,
            'type' => 'text',
            'text' => [
                'body' => $bodyText
            ]
        ];

        // URL do endpoint de envio de mensagem
        $messageUrl = "https://graph.facebook.com/{$API_VERSION}/{$BUSINESS_PHONE_NUMBER_ID}/messages";

        // Enviar a mensagem via cURL
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $messageUrl);
        curl_setopt($ch, CURLOPT_HTTPHEADER, [
            "Authorization: Bearer {$BUSINESS_TOKEN}",
            "Content-Type: application/json"
        ]);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($messageData));

        $messageResponse = curl_exec($ch);
        curl_close($ch);

        $messageResult = json_decode($messageResponse, true);
        
    }

    echo json_encode([
        "token_response" => $result,
        "webhook_response" => $result_webhook,
        "alternate_callback_response" => $result_alt_callback,
        "webhook_verification_response" => $result_check_webhook,
        "message_response" => $messageResult,
        "business_token" => $BUSINESS_TOKEN,
        "BUSINESS_PHONE_NUMBER_ID" => $BUSINESS_PHONE_NUMBER_ID,
        "override_callback_uri" => $alternate_callback_url,
        "verify_token" => $verify_token
        
    ]);

}else{
    echo json_encode([
        "error" => "result access_token - token nn existe",
    ]);
}
?>

Error console log

webhooks whatsapp