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:
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
– Anderson Henrique
@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.
– Pedro Daher