Witam ogarniam sobie z tym poradnikiem bota https://devcorner.pl/zbudowac-chatbota-dla-messengera-php-…/ i mam problem. Bot odpisuje tylko jak piszę z konta z którego go stworzyłem. Znajomy jak coś pisze to mu nie odpisuje a wiadomość leci do skrzynki odbiorczej :( Co może być nie tak tutaj może ten kod jest zły. Dzięki z góry za rady :)
<?php
$verify_token = "WixBot";
$hub_verify_token = null;
if(isset($_REQUEST['hub_challenge'])) {
$challenge = $_REQUEST['hub_challenge'];
$hub_verify_token = $_REQUEST['hub_verify_token'];
}
if ($hub_verify_token === $verify_token) {
echo $challenge;
}
$access_token = "xsxsxsxsxs";
$input = json_decode(file_get_contents('php://input'), true);
$sender = $input['entry'][0]['messaging'][0]['sender']['id'];
$message = $input['entry'][0]['messaging'][0]['message']['text'];
$message_to_reply = '';
$user_info_url = "https://graph.facebook.com/v2.6/".$sender."?fields=first_name,last_name,profile_pic,locale,timezone,gender&access_token=".$access_token;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $user_info_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close ($ch);
$user = json_decode($result);
if(preg_match('[cześć|dzień dobry|witam|hej]', strtolower($message))) {
$array = ['Dzień dobry', 'Witam', 'Cześć', 'Hej'];
$message_to_reply = $array[array_rand($array)]." ".$user->first_name;
$jsonData = '{
"recipient":{
"id":"'.$sender.'"
},
"message":{
"text":"'.$message_to_reply.'"
}
}';
}
else {
$message_to_reply = 'Nie rozumiem. Co masz na myśli?';
$jsonData = '{
"recipient":{
"id":"'.$sender.'"
},
"message":{
"text":"'.$message_to_reply.'"
}
}';
}
$url = 'https://graph.facebook.com/v2.6/me/messages?access_token='.$access_token;
$ch = curl_init($url);
// informujemy cURL, że chcemy przesłać żądanie POST.
curl_setopt($ch, CURLOPT_POST, 1);
// dodajemy zakodowany JSON string do POST.
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
// ustawiamy content type na application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
// wykonaj żądanie
if(!empty($input['entry'][0]['messaging'][0]['message'])){
$result = curl_exec($ch);
}
?>