Chatbot PHP - Facebook Messenger

Asked

Viewed 247 times

1

Hello, all right? I’m creating a chatbot but I’m having problems with the HTTP request (POST).

When I call a function to send the POST to Facebook Messenger it seems to me that it loops and starts sending all posts. Generating it as a result:

ChatBot

See the main code (index.php) that calls the function:

include 'Banco.php';
include 'Envio.php';
$banco = new Banco();
$envio = new Envio();

switch ($banco->getSecao($sender_id)) {
    case 0:
        //APRESENTACAO E PEDE CPF
        $envio->apresentacao($sender_id);
        break;
    case 1:
        //BOAS VINDAS E PEDE CPF
        $banco->setSecao($sender_id, 2);
        $envio->boasvindas($sender_id);
        break;
    case 2:
        //EXIBE RASTREIO            
        $banco->setSecao($sender_id, 3);
        $envio->pedidos($sender_id);
        break;
    case 3:
        //PEDE CODIGO DE RASTREIO
        $banco->setSecao($sender_id, 4);
        $envio->tracking($sender_id);
        break;
    case 4:
        //AGRADECE O USO PEDE CPF E COLOCA A SECAO EM 2
        $banco->setSecao($sender_id, 2);
        $envio->agradecimento($sender_id);
        break;
}

The Bot should only send the POST of the "section" that is returned from the Register. However, it will enter in all. I have tried leaving only one case. it keeps sending this case several times. See an example of the Upload class:

private function send($dados){            
        $url = $this->apiUrl . '?access_token=' . $this->access_token;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        $params = http_build_query($dados);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $params); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
        $server_output = curl_exec ($ch);
        curl_close ($ch);
        return $server_output;
    }

    public function apresentacao($idClient){
        $responseJSON = array(
            "recipient" => array(
                "id" => $idClient
            ),
            "message" => array(
                "text" => "Olá, eu sou o Onex, tudo bem?\nQue legal te ver por aqui! Posso te ajudar a rastrear algum pedido?"
            )
        );
        $response = $this->send($responseJSON);

        $responseJSON = array(
            "recipient" => array(
                "id" => $idClient
            ),
            "message" => array(
                "text" => "Pra começar, vou precisar de seu CPF ou CNPJ:"
            )
        );
        $response = $this->send($responseJSON);
    }
  • Does he really get into every case? Isn’t he simply performing the functions? Have you tried putting an echo in each case to see if you’re really getting into the cases? If you are entering home one, there is something wrong with the parameter passed inside the switch

  • @Andersonhenrique thank you for the reply. I removed the Switch and left only the presentation part, even so it sent the presentation several times I believe that error is in the Upload class, but I’m not able to identify.

1 answer

3


I come to share the solution.

I started to record in txt of every post that was sent, and noticed that facebook itself generates the loop, however as it is not my Bot that is sending the message field in JSON this blank, so I did a validation in this field:

$message = isset($input['entry'][0]['messaging'][0]['message']['text']) ? $input['entry'][0]['messaging'][0]['message']['text']: 'vazio' ;

And before activating the switch I validated:

if($message != 'vazio'){

And so it worked properly and ended with the loops.

Thank you very much and hugs!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.