Edgars Po Posted September 9, 2018 Report Share Posted September 9, 2018 Sveiki visi Interesejos par fb chatbotu darbibu tiesi PHP.Atradu visadus Python un savadakas iespejas ka izveidot sakarigu botu kas spej “macities” darbibas princips,ka teiksim bots Replica. Kas jauniegutu informaciju saglaba faila un failu izmanto jautajumu atpazisanai un atbildes piemeklesanai.Varbut kads var palidzet ar piemeru?Sobrid kods ir vienkars: // parameters $hubVerifyToken = 'Your token'; $accessToken = "Your acces token"; // check token at setup if ($_REQUEST['hub_verify_token'] === $hubVerifyToken) { echo $_REQUEST['hub_challenge']; exit; } // handle bot's anwser $input = json_decode(file_get_contents('php://input'), true); $senderId = $input['entry'][0]['messaging'][0]['sender']['id']; $messageText = $input['entry'][0]['messaging'][0]['message']['text']; $response = null; //set Message if($messageText == "hi") { $answer = "Hello"; } //send message to facebook bot $response = [ 'recipient' => [ 'id' => $senderId ], 'message' => [ 'text' => $answer ] ]; $ch = curl_init('https://graph.facebook.com/v2.6/me/messages?access_token='.$accessToken); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($response)); curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); if(!empty($input)){ $result = curl_exec($ch); } curl_close($ch); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.