Path : /var/www/html/main/
File Upload :
Current File : /var/www/html/main/fb-webhooks.php

<?php
/*!
 * Copyright Anucha Puangpaka
 * Contacts anucha.ppk@icloud.com 062-479-9836
 */

error_reporting(E_ALL);
ini_set("display_errors", 0);

include('config.php');

$access_token = "EAAISDPHZBda8BAEpIz7qPf7IwZB5Vqtwpud7KMYJsUslls2PwuVc4TKaq8Nt8rIoZBwTt8ZAOJT0xSZBJTbCmUUzwEoSQCcre1cy0NKSgziMOEdR3AviH3lsJ8isiwyssCQNYRRGkDvZC6fkDUXvwTh47BX1R9fSvqssMwbQ1JBsbodyn59TLy";
$verify_token = "$token";
$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;
 return;
}

$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 = '';

if($message=="สวัสดี" || $message=="สวัสดีครับ" || $message=="สวัสดีค่ะ"){
  $message_to_reply = "สวัสดีครับ";
}elseif($message=="ขอบคุณ" || $message=="ขอบคุณครับ" || $message=="ขอบคุณค่ะ"){
  $message_to_reply = "ยินดีครับ";
}else{

  $sql = "select * from ap_question_answer where question like '%$message%'";
  $query = mysqli_query($conn,$sql);
  $num_rows = mysqli_num_rows($query);
  $rows = mysqli_fetch_array($query);

  if($num_rows>0){
    $message_to_reply = "$rows[answer]";
  }else{
    $message_to_reply = "เราไม่พบข้อมูลนี้ค่ะ";
  }

}

$jsonData = '{
    "recipient":{
      "id":"'.$sender.'"
    },
    "message":{
      "text":"'.$message_to_reply.'"
    }
}';

$jsonDataEncoded = $jsonData;

$url = 'https://graph.facebook.com/v2.6/me/messages?access_token='.$access_token;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

if(!empty($input['entry'][0]['messaging'][0]['message'])){
  $result = curl_exec($ch);
}

include('logs.php');

mysqli_close($conn);
?>