Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function sendText($ch, $body, $to, $token){
- curl_setopt($ch, CURLOPT_URL, "https://graph.facebook.com/v2.6/me/messages?access_token=$token");
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0 );
- curl_setopt($ch, CURLOPT_POST, 1 );
- // == Organize data ==
- $stuffToSend = array(
- 'recipient' => array(
- "id" => $to
- ),
- 'message' => array(
- "text" => $body
- )
- );
- // == Encode data into JSON ==
- $data = json_encode($stuffToSend);
- // == Add json to payload ==
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data );
- curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
- // == Send message ==
- $result = curl_exec($ch);
- return $result;
- }
- function sendImage($ch, $image, $to, $token){
- curl_setopt($ch, CURLOPT_URL, "https://graph.facebook.com/v2.6/me/messages?access_token=$token");
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0 );
- curl_setopt($ch, CURLOPT_POST, 1 );
- // == Organize data ==
- $stuffToSend = array(
- 'recipient' => array(
- "id" => $to
- ),
- 'message' => array(
- "attachment" => array(
- "type" => "image",
- "payload" => array(
- "url" => $image
- )
- )
- )
- );
- // == Encode data into JSON ==
- $body = json_encode($stuffToSend);
- // == Add json to payload ==
- curl_setopt($ch, CURLOPT_POSTFIELDS, $body );
- curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
- // == Send message ==
- $result = curl_exec($ch);
- return $result;
- }
- function sendButton1($ch, $body, $to, $token, $b1, $bo1){
- curl_setopt($ch, CURLOPT_URL, "https://graph.facebook.com/v2.6/me/messages?access_token=$token");
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0 );
- curl_setopt($ch, CURLOPT_POST, 1 );
- // == Organize data ==
- $stuffToSend = array(
- 'recipient' => array(
- "id" => $to
- ),
- 'message' => array(
- "attachment" => array(
- "type" => "template",
- "payload" => array(
- "template_type" => "button",
- "text" => $body,
- "buttons" => array(
- array(
- "type" => "postback",
- "title" => $b1,
- "payload" => $bo1
- )
- )
- )
- )
- )
- );
- // == Encode data into JSON ==
- $body = json_encode($stuffToSend);
- // == Add json to payload ==
- curl_setopt($ch, CURLOPT_POSTFIELDS, $body );
- curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
- // == Send message ==
- $result = curl_exec($ch);
- return $result;
- }
- function sendButton2($ch, $body, $to, $token, $b1, $bo1, $b2, $bo2){
- curl_setopt($ch, CURLOPT_URL, "https://graph.facebook.com/v2.6/me/messages?access_token=$token");
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0 );
- curl_setopt($ch, CURLOPT_POST, 1 );
- // == Organize data ==
- $stuffToSend = array(
- 'recipient' => array(
- "id" => $to
- ),
- 'message' => array(
- "attachment" => array(
- "type" => "template",
- "payload" => array(
- "template_type" => "button",
- "text" => $body,
- "buttons" => array(
- array(
- "type" => "postback",
- "title" => $b1,
- "payload" => $bo1
- ),
- array(
- "type" => "postback",
- "title" => $b2,
- "payload" => $bo2
- )
- )
- )
- )
- )
- );
- // == Encode data into JSON ==
- $body = json_encode($stuffToSend);
- // == Add json to payload ==
- curl_setopt($ch, CURLOPT_POSTFIELDS, $body );
- curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
- // == Send message ==
- $result = curl_exec($ch);
- return $result;
- }
- function sendButton3($ch, $body, $to, $token, $b1, $bo1, $b2, $bo2, $b3, $bo3){
- curl_setopt($ch, CURLOPT_URL, "https://graph.facebook.com/v2.6/me/messages?access_token=$token");
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0 );
- curl_setopt($ch, CURLOPT_POST, 1 );
- // == Organize data ==
- $stuffToSend = array(
- 'recipient' => array(
- "id" => $to
- ),
- 'message' => array(
- "attachment" => array(
- "type" => "template",
- "payload" => array(
- "template_type" => "button",
- "text" => $body,
- "buttons" => array(
- array(
- "type" => "postback",
- "title" => $b1,
- "payload" => $bo1
- ),
- array(
- "type" => "postback",
- "title" => $b2,
- "payload" => $bo2
- ),
- array(
- "type" => "postback",
- "title" => $b3,
- "payload" => $bo3
- )
- )
- )
- )
- )
- );
- // == Encode data into JSON ==
- $body = json_encode($stuffToSend);
- // == Add json to payload ==
- curl_setopt($ch, CURLOPT_POSTFIELDS, $body );
- curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
- // == Send message ==
- $result = curl_exec($ch);
- return $result;
- }
- function sendButtonLink3($ch, $body, $to, $token, $b1, $bo1, $b2, $bo2, $b3, $bo3){
- curl_setopt($ch, CURLOPT_URL, "https://graph.facebook.com/v2.6/me/messages?access_token=$token");
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0 );
- curl_setopt($ch, CURLOPT_POST, 1 );
- // == Organize data ==
- $stuffToSend = array(
- 'recipient' => array(
- "id" => $to
- ),
- 'message' => array(
- "attachment" => array(
- "type" => "template",
- "payload" => array(
- "template_type" => "button",
- "text" => $body,
- "buttons" => array(
- array(
- "type" => "web_url",
- "url" => $bo3,
- "title" => $b3,
- ),
- array(
- "type" => "postback",
- "title" => $b1,
- "payload" => $bo1
- ),
- array(
- "type" => "postback",
- "title" => $b2,
- "payload" => $bo2
- ),
- )
- )
- )
- )
- );
- // == Encode data into JSON ==
- $body = json_encode($stuffToSend);
- // == Add json to payload ==
- curl_setopt($ch, CURLOPT_POSTFIELDS, $body );
- curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
- // == Send message ==
- $result = curl_exec($ch);
- return $result;
- }
- /* Sends buttons. Building the $bodyArr must be precise or sending will fail
- *
- * Require: $bodyArr is (arrayof (arrayof "type" => (oneof "postback", "web_url"),
- * "title" => 'text',
- * (oneof "payload", "url") => 'text'))
- */
- function sendGenButtons($ch, $body, $bodyArr, $to, $token){
- curl_setopt($ch, CURLOPT_URL, "https://graph.facebook.com/v2.6/me/messages?access_token=$token");
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0 );
- curl_setopt($ch, CURLOPT_POST, 1 );
- // == Organize data ==
- $stuffToSend = array(
- 'recipient' => array(
- "id" => $to
- ),
- 'message' => array(
- "attachment" => array(
- "type" => "template",
- "payload" => array(
- "template_type" => "button",
- "text" => $body,
- "buttons" => $bodyArr
- )
- )
- )
- );
- // == Encode data into JSON ==
- $body = json_encode($stuffToSend);
- // == Add json to payload ==
- curl_setopt($ch, CURLOPT_POSTFIELDS, $body );
- curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
- // == Send message ==
- $result = curl_exec($ch);
- return $result;
- }
- function getUser($ch, $id, $token){
- curl_setopt($ch, CURLOPT_URL, "https://graph.facebook.com/v2.6/$id?fields=first_name,last_name,profile_pic,locale,timezone,gender&access_token=$token");
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
- $data = curl_exec($ch);
- return json_decode($data);
- }
- function execBot($conn, $ch, $user, $userLoc, $userData, $type, $rec, $token){
- // == Finds the user location within the bot path ==
- switch ($type){
- case "free":
- sendText($ch, "Sorry, we don't recoginze that response. We'll send that again to retry.", $user, $token);
- goto free;
- break;
- case "struct":
- switch ($rec){
- case "restart":
- $curId = $userLoc["botid"];
- $sql = "UPDATE tbl_fb_bot_loc SET prop1='NONE', prop2='NONE', prop3='NONE', prop4='NONE', botid=$curId WHERE user_id='$user'";
- mysqli_query($conn, $sql);
- $userLoc = array(
- "user_id" => $user,
- "prop1" => "NONE",
- "prop2" => "NONE",
- "prop3" => "NONE",
- "prop4" => "NONE",
- "botid" => $curId);
- $sql = "SELECT * FROM tbl_bot_fb WHERE prop1='{$userLoc["prop1"]}' AND prop2='{$userLoc["prop2"]}' AND prop3='{$userLoc["prop3"]}' AND prop4='{$userLoc["prop4"]}' AND botid={$userLoc["botid"]}";
- $query = mysqli_query($conn, $sql);
- $newBot = mysqli_fetch_assoc($query);
- goto message;
- break;
- case "Set botid=0":
- $sql = "UPDATE tbl_fb_bot_loc SET prop1='NONE', prop2='NONE', prop3='NONE', prop4='NONE', botid=0 WHERE user_id='$user'";
- mysqli_query($conn, $sql);
- $userLoc = array(
- "user_id" => $user,
- "prop1" => "NONE",
- "prop2" => "NONE",
- "prop3" => "NONE",
- "prop4" => "NONE",
- "botid" => 0);
- $sql = "SELECT * FROM tbl_bot_fb WHERE prop1='{$userLoc["prop1"]}' AND prop2='{$userLoc["prop2"]}' AND prop3='{$userLoc["prop3"]}' AND prop4='{$userLoc["prop4"]}' AND botid={$userLoc["botid"]}";
- $query = mysqli_query($conn, $sql);
- $newBot = mysqli_fetch_assoc($query);
- goto message;
- break;
- default:
- $action = substr($rec, 4, 5);
- $prop = substr($rec, 10);
- break;
- }
- break;
- }
- if(strlen($prop) == 1){
- $prop = intval($prop);
- }
- $sql = "UPDATE tbl_fb_bot_loc SET $action='$prop' WHERE user_id='$user'";
- if(mysqli_query($conn, $sql)){
- //success
- } else {
- //fail -> PANIC!
- }
- $userLoc[$action] = $prop;
- free:
- $sql = "SELECT * FROM tbl_bot_fb WHERE prop1='{$userLoc["prop1"]}' AND prop2='{$userLoc["prop2"]}' AND prop3='{$userLoc["prop3"]}' AND prop4='{$userLoc["prop4"]}' AND botid={$userLoc["botid"]}";
- $query = mysqli_query($conn, $sql);
- $newBot = mysqli_fetch_assoc($query);
- message:
- sendText($ch, $newBot["response1"], $user, $token);
- sleep(1);
- if($newBot["image"] != "X"){
- sendImage($ch, $newBot["image"], $user, $token);
- sleep(1);
- }
- if($newBot["link"] != "X"){
- sendButtonLink3($ch, $newBot["response2"], $user, $token, $newBot["but1"], $newBot["but_opt1"], $newBot["but2"], $newBot["but_opt2"], $newBot["but3"], $newBot["link"]);
- } else if($newBot["but2"] == "X" && $newBot["but3"] == "X"){
- sendButton1($ch, $newBot["response2"], $user, $token, $newBot["but1"], $newBot["but_opt1"]);
- } else if($newBot["but3"] == "X"){
- sendButton2($ch, $newBot["response2"], $user, $token, $newBot["but1"], $newBot["but_opt1"], $newBot["but2"], $newBot["but_opt2"]);
- } else {
- sendButton3($ch, $newBot["response2"], $user, $token, $newBot["but1"], $newBot["but_opt1"], $newBot["but2"], $newBot["but_opt2"], $newBot["but3"], $newBot["but_opt3"]);
- }
- }
- // == First time settup ==
- $entityBody = file_get_contents('php://input');
- $decoded = json_decode($entityBody, true);
- $ch = curl_init();
- $token = "EAADZB54bKfIgBAHrrbZBUEsdRghd7W2JmkcZCuCcZCZBKRQufPjJf8CGbs1x8NUkiPLjbaKtO2oqz3CudNklnIX3szQa6OoVaezXRtlCczTBsS6W0LyQJZBijPoq64twHA2vE1m0rQJPZALVu6HsEJGj49Aeje96JxZCnMCx81XhwgZDZD";
- // == Connect to the MySQL Database ==
- $servername = "localhost";
- $username = "satisfi_bot";
- $password = "BotsAreFun21";
- $dbname = "satisfi_bot";
- // Create connection
- $conn = mysqli_connect($servername, $username, $password, $dbname);
- // Check connection
- if (!$conn) {
- \file_put_contents("output.txt", "Connection failed: " . mysqli_connect_error(). "\n", FILE_APPEND);
- }
- // == Pull data from convoluted JSON payload ==
- $body = $decoded["entry"][0]["messaging"][0]["message"]["text"]; //these will be empty strings if they are not the correct message type
- $payload = $decoded["entry"][0]["messaging"][0]["postback"]["payload"]; // ^^ ditto
- $from = $decoded["entry"][0]["messaging"][0]["sender"]["id"];
- \file_put_contents("output.txt", "test\n", FILE_APPEND);
- //sendText($ch, "YOU PRESSED A BUTTON! CONGRATULATIONS!", $from, $token);
- $sql = "SELECT * FROM tbl_fb_user_data WHERE user_id='$from'";
- $query = mysqli_query($conn, $sql);
- if(mysqli_num_rows($query) >= 1){
- // == We already have this user on record ==
- $userData = mysqli_fetch_assoc($query);
- } else {
- // == We have no record of this user, add them to the database ==
- $userData = getUser($ch, $from, $token);
- $firstName = $userData->first_name;
- $lastName = $userData->last_name;
- $gender = $userData->gender;
- $picture = $userData->profile_pic;
- $locale = $userData->locale;
- $timeZone = $userData->timezone;
- $sql = "INSERT INTO tbl_fb_user_data (user_id, first_name, last_name, gender, profile_pic, locale, timezone) VALUES ('$from', '$firstName', '$lastName', '$gender', '$picture', '$locale', '$timeZone')";
- mysqli_query($conn, $sql);
- }
- // == Find the user's location in the bot path, decide what to send ==
- $sql = "SELECT * FROM tbl_fb_bot_loc WHERE user_id='$from'";
- $query = mysqli_query($conn, $sql);
- if(mysqli_num_rows($query) >= 1){
- // == This a user who has used the bot before ==
- $userRow = mysqli_fetch_assoc($query); //gets user's location in the bot
- //execBot($conn, $userRow, $ch, $type, $body, $from, $chatId, $firstName, $lastName);
- //$result = LogDebugMsg("&Note=ExecutedBot1&Body=".$body."&From=".$from."&chatID=".$chatId."&Type=".$type);
- } else {
- // == This user has never used the bot ==
- $sql = "INSERT INTO tbl_fb_bot_loc (user_id, prop1, prop2, prop3, prop4, botid) VALUES ('$from', 'NONE' ,'NONE', 'NONE', 'NONE', 0)";
- if(mysqli_query($conn, $sql) == TRUE){
- // == We successfully added the user to the bot, now start them off ==
- $userRow = array(
- "chatId" => $from,
- "prop1" => "NONE",
- "prop2" => "NONE",
- "prop3" => "NONE",
- "prop4" => "NONE",
- "botid" => 0);
- } else {
- // == We failed at adding the new record ==
- //TODO: PANIC!
- }
- }
- // == Workaround for the infinite message glitch ==
- if($body != ""){
- execBot($conn, $ch, $from, $userRow, $userData, "free", $body, $token);
- //free text was sent
- // if($body == "Who am I?"){
- // // == Proof that we can grab user profile data ==
- // $userData = getUser($ch, $from, $token);
- // // == This is all the data we can grab for a user ==
- // $firstName = $userData->first_name;
- // $lastName = $userData->last_name;
- // $gender = $userData->gender;
- // $picture = $userData->profile_pic;
- // $locale = $userData->locale;
- // $timeZone = $userData->timezone;
- // // == ensure we read the data correctly by sending some of it as a message
- // sendText($ch, "You are $firstName $lastName. I believe you are $gender.", $from, $token);
- // } else if ($body == "What do I look like?"){
- // $image = $row["profile_pic"];
- // sendImage($ch, $image, $from, $token);
- // } else {
- // sendButtons($ch, "These are some buttons", $from, $token);
- // }
- } else if ($payload != "") {
- // // a button was pressed
- execBot($conn, $ch, $from, $userRow, $userData, "struct", $payload, $token);
- // sendText($ch, "YOU PRESSED A BUTTON! CONGRATULATIONS!", $from, $token);
- //
- }else {
- //someother message type occured, discard it
- exit();
- }
- // == Store data in text file for debug purposes ==
- \file_put_contents("output.txt", "$from : $body\n", FILE_APPEND);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement