Skip to content

Instantly share code, notes, and snippets.

@arn-ob
Created January 16, 2019 16:09
Show Gist options
  • Select an option

  • Save arn-ob/985870d5eec72b849d574415029c94c1 to your computer and use it in GitHub Desktop.

Select an option

Save arn-ob/985870d5eec72b849d574415029c94c1 to your computer and use it in GitHub Desktop.
Chatbot Facebook using PHP
<?php
$token = '';
if(isset($_REQUEST['hub_challenge'])){
$challenge = $_REQUEST['hub_challenge'];
$token = $_REQUEST['hub_verify_token'];
}
if($token === "ar"){
echo $challenge;
}
// $input = file_get_contents('php://input');
// var_dump($input);
$input_f = (array)json_decode(file_get_contents('php://input'), true);
// echo $input_f;
// echo($input_f['entry']);
$userid = $input_f['entry'][0]['messaging'][0]['sender']['id'];
$message = $input_f['entry'][0]['messaging'][0]['message']['text'];
$echo = false;
if(isset($input_f['entry'][0]['messaging'][0]['message']['is_echo'])){
$echo = true;
}
$sends = '';
if($message == "hello"){
$sends = "Hello, How i help you!";
}
elseif ($message == "Need to buy"){
$sends = "What you wanted to buy!";
}
elseif ($message == "Game"){
$sends = "Okieee";
}
elseif ($message == "AAAA"){
$sends = "What you actully Mean!!! ";
}
elseif ($message == "funny"){
$sends = "I am not FB";
}
elseif ($message == "work"){
$sends = "Ya sure";
}
else {
$sends = 'Sorry Cant Understand you';
}
if(!$echo){
echo $userid . " and " . $message. "and send " . $sends;
$acc = "EAAgr7wgbbAQBANTjSx5mBLdBgIhmD24ZCSbaJCYZCF2QjQ6yUAxXmNeYOQVXLfcApQexal10dsxZCDHW5Hv0yljfoeUZBkHePIYOaZCCkJVlUC0F4XRWE36tZAw2YWGIVpaGhQZDZD";
$url = "https://graph.facebook.com/v2.6/me/messages?access_token=$acc";
$jsonss="{
'recipient':{'id': $userid },
'message':{'text': '". $sends."' }
}";
echo $jsonss;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonss);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
if(!empty($input_f['entry'][0]['messaging'][0]['message']) ){
curl_exec($ch);
}
} else {
echo 'Echo Found';
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment