Jump to content
php.lv forumi
Edgars Po

FB PHP chatbot

Recommended Posts

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);

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×