Advertisement
Guest User

Untitled

a guest
Jun 2nd, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 20.44 KB | None | 0 0
  1. <?php
  2.  
  3. // Sends a message back to the user and leaves response open text
  4. function sendText($ch, $body, $to, $chatId)
  5. {
  6.     // == Setup credentials ==
  7.     curl_setopt($ch, CURLOPT_URL,            "https://api.kik.com/v1/message");
  8.     curl_setopt($ch, CURLOPT_USERPWD,        "satisfitest:ef194887-c4fa-40a9-8680-d738395a6c79");
  9.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
  10.     curl_setopt($ch, CURLOPT_POST,           1 );
  11.  
  12.     // == Organize data ==
  13.     $stuffToSend = array(
  14.         'messages' => array(
  15.             array(
  16.                 'body' => $body,
  17.                 "to" => $to,
  18.                 'type' => 'text',
  19.                 'chatId' => $chatId
  20.             )
  21.         )
  22.     );
  23.  
  24.        
  25.     // == Encode data into JSON ==
  26.     $body = json_encode($stuffToSend);
  27.  
  28.     // == Add json to payload ==
  29.     curl_setopt($ch, CURLOPT_POSTFIELDS,     $body );
  30.     curl_setopt($ch, CURLOPT_HTTPHEADER,     array('Content-Type: application/json'));
  31.  
  32.     // == Send message ==
  33.     $result = curl_exec($ch);
  34. }
  35.  
  36. //sends a mesage back to user and provides one button option
  37. function sendButton1($ch, $body, $to, $chatId, $button1)
  38. {
  39.     // == Setup credentials ==
  40.     curl_setopt($ch, CURLOPT_URL,            "https://api.kik.com/v1/message");
  41.     curl_setopt($ch, CURLOPT_USERPWD,        "satisfitest:ef194887-c4fa-40a9-8680-d738395a6c79");
  42.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
  43.     curl_setopt($ch, CURLOPT_POST,           1 );
  44.  
  45.     // == Organize data ==
  46.     $stuffToSend = array(
  47.         'messages' => array(
  48.             array(
  49.                 'body' => $body,
  50.                 "to" => $to,
  51.                 'type' => 'text',
  52.                 'chatId' => $chatId,
  53.                     "keyboards" => array(
  54.                         array(
  55.                             "type" => "suggested",
  56.                             "responses" => array(
  57.                                 array(
  58.                                     "type" => "text",
  59.                                     "body" => $button1
  60.                                 )
  61.                             )
  62.                         )
  63.                     )
  64.             )
  65.         )
  66.     );
  67.  
  68.     // == Encode data into JSON ==
  69.     $body = json_encode($stuffToSend);
  70.  
  71.     // == Add json to payload ==
  72.     curl_setopt($ch, CURLOPT_POSTFIELDS,     $body );
  73.     curl_setopt($ch, CURLOPT_HTTPHEADER,     array('Content-Type: application/json'));
  74.  
  75.     // == Send message ==
  76.     $result = curl_exec($ch);
  77. }
  78.  
  79. //sends a message back to the user and provides two button opions
  80. function sendButton2($ch, $body, $to, $chatId, $button1, $button2)
  81. {
  82.     // == Setup credentials ==
  83.     curl_setopt($ch, CURLOPT_URL,            "https://api.kik.com/v1/message");
  84.     curl_setopt($ch, CURLOPT_USERPWD,        "satisfitest:ef194887-c4fa-40a9-8680-d738395a6c79");
  85.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
  86.     curl_setopt($ch, CURLOPT_POST,           1 );
  87.  
  88.     // == Organize data ==
  89.     $stuffToSend = array(
  90.         'messages' => array(
  91.             array(
  92.                 'body' => $body,
  93.                 "to" => $to,
  94.                 'type' => 'text',
  95.                 'chatId' => $chatId,
  96.                     "keyboards" => array(
  97.                         array(
  98.                             "type" => "suggested",
  99.                             "responses" => array(
  100.                                 array(
  101.                                     "type" => "text",
  102.                                     "body" => $button1
  103.                                 ),
  104.                                 array(
  105.                                     "type" => "text",
  106.                                     "body" => $button2
  107.                                 )
  108.                             )
  109.                         )
  110.                     )
  111.             )
  112.         )
  113.     );
  114.  
  115.     // == Encode data into JSON ==
  116.     $body = json_encode($stuffToSend);
  117.  
  118.     // == Add json to payload ==
  119.     curl_setopt($ch, CURLOPT_POSTFIELDS,     $body );
  120.     curl_setopt($ch, CURLOPT_HTTPHEADER,     array('Content-Type: application/json'));
  121.  
  122.     // == Send message ==
  123.     $result = curl_exec($ch);
  124. }
  125.  
  126. //sends a message back to the user and provides 3 button options
  127. function sendButton3($ch, $body, $to, $chatId, $button1, $button2, $button3)
  128. {
  129.     // == Setup credentials ==
  130.     curl_setopt($ch, CURLOPT_URL,            "https://api.kik.com/v1/message");
  131.     curl_setopt($ch, CURLOPT_USERPWD,        "satisfitest:ef194887-c4fa-40a9-8680-d738395a6c79");
  132.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
  133.     curl_setopt($ch, CURLOPT_POST,           1 );
  134.  
  135.     // == Organize data ==
  136.     $stuffToSend = array(
  137.         'messages' => array(
  138.             array(
  139.                 'body' => $body,
  140.                 "to" => $to,
  141.                 'type' => 'text',
  142.                 'chatId' => $chatId,
  143.                     "keyboards" => array(
  144.                         array(
  145.                             "type" => "suggested",
  146.                             "responses" => array(
  147.                                 array(
  148.                                     "type" => "text",
  149.                                     "body" => $button1
  150.                                 ),
  151.                                 array(
  152.                                     "type" => "text",
  153.                                     "body" => $button2
  154.                                 ),
  155.                                 array(
  156.                                     "type" => "text",
  157.                                     "body" => $button3
  158.                                 )
  159.                             )
  160.                         )
  161.                     )
  162.             )
  163.         )
  164.     );
  165.  
  166.     // == Encode data into JSON ==
  167.     $body = json_encode($stuffToSend);
  168.  
  169.     // == Add json to payload ==
  170.     curl_setopt($ch, CURLOPT_POSTFIELDS,     $body );
  171.     curl_setopt($ch, CURLOPT_HTTPHEADER,     array('Content-Type: application/json'));
  172.  
  173.     // == Send message ==
  174.     $result = curl_exec($ch);
  175. }
  176.  
  177. function sendButton4($ch, $body, $to, $chatId, $button1, $button2, $button3, $button4)
  178. {
  179.     // == Setup credentials ==
  180.     curl_setopt($ch, CURLOPT_URL,            "https://api.kik.com/v1/message");
  181.     curl_setopt($ch, CURLOPT_USERPWD,        "satisfitest:ef194887-c4fa-40a9-8680-d738395a6c79");
  182.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
  183.     curl_setopt($ch, CURLOPT_POST,           1 );
  184.  
  185.     // == Organize data ==
  186.     $stuffToSend = array(
  187.         'messages' => array(
  188.             array(
  189.                 'body' => $body,
  190.                 "to" => $to,
  191.                 'type' => 'text',
  192.                 'chatId' => $chatId,
  193.                     "keyboards" => array(
  194.                         array(
  195.                             "type" => "suggested",
  196.                             "responses" => array(
  197.                                 array(
  198.                                     "type" => "text",
  199.                                     "body" => $button1
  200.                                 ),
  201.                                 array(
  202.                                     "type" => "text",
  203.                                     "body" => $button2
  204.                                 ),
  205.                                 array(
  206.                                     "type" => "text",
  207.                                     "body" => $button3
  208.                                 ),
  209.                                 array(
  210.                                     "type" => "text",
  211.                                     "body" => $button4
  212.                                 )
  213.                             )
  214.                         )
  215.                     )
  216.             )
  217.         )
  218.     );
  219.  
  220.     // == Encode data into JSON ==
  221.     $body = json_encode($stuffToSend);
  222.  
  223.     // == Add json to payload ==
  224.     curl_setopt($ch, CURLOPT_POSTFIELDS,     $body );
  225.     curl_setopt($ch, CURLOPT_HTTPHEADER,     array('Content-Type: application/json'));
  226.  
  227.     // == Send message ==
  228.     $result = curl_exec($ch);
  229. }
  230.  
  231. function sendPicture($ch, $to, $chatId, $url, $button1, $button2)
  232. {
  233.     // == Setup credentials ==
  234.     curl_setopt($ch, CURLOPT_URL,            "https://api.kik.com/v1/message");
  235.     curl_setopt($ch, CURLOPT_USERPWD,        "satisfitest:ef194887-c4fa-40a9-8680-d738395a6c79");
  236.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
  237.     curl_setopt($ch, CURLOPT_POST,           1 );
  238.  
  239.     // == Organize data ==
  240.     $stuffToSend = array(
  241.         'messages' => array(
  242.             array(
  243.                 "to" => $to,
  244.                 'type' => 'picture',
  245.                 'chatId' => $chatId,
  246.                     "picUrl" => $url,
  247.                     //"text" => $body,
  248.                     "keyboards" => array(
  249.                         array(
  250.                             "type" => "suggested",
  251.                             "responses" => array(
  252.                                 array(
  253.                                     "type" => "text",
  254.                                     "body" => $button1
  255.                                 ),
  256.                                 array(
  257.                                     "type" => "text",
  258.                                     "body" => $button2
  259.                                 )
  260.                             )
  261.                         )
  262.                     )
  263.             )
  264.         )
  265.     );
  266.  
  267.        
  268.     // == Encode data into JSON ==
  269.     $body = json_encode($stuffToSend);
  270.  
  271.     // == Add json to payload ==
  272.     curl_setopt($ch, CURLOPT_POSTFIELDS,     $body );
  273.     curl_setopt($ch, CURLOPT_HTTPHEADER,     array('Content-Type: application/json'));
  274.  
  275.     // == Send message ==
  276.     $result = curl_exec($ch);
  277. }
  278.  
  279. //sends a message back to the user with a linked picture and text
  280. function sendPictureLink($ch, $body, $to, $chatId, $url, $pic, $button1, $button2)
  281. {
  282.     // == Setup credentials ==
  283.     curl_setopt($ch, CURLOPT_URL,            "https://api.kik.com/v1/message");
  284.     curl_setopt($ch, CURLOPT_USERPWD,        "satisfitest:ef194887-c4fa-40a9-8680-d738395a6c79");
  285.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
  286.     curl_setopt($ch, CURLOPT_POST,           1 );
  287.  
  288.     // == Organize data ==
  289.     $stuffToSend = array(
  290.         'messages' => array(
  291.             array(
  292.                 'text' => $body,
  293.                 "to" => $to,
  294.                 'type' => 'link',
  295.                 'chatId' => $chatId,
  296.                     "url" => $url,
  297.                     "picUrl" => $pic,
  298.                     "keyboards" => array(
  299.                             array(
  300.                                 "type" => "suggested",
  301.                                 "responses" => array(
  302.                                     array(
  303.                                         "type" => "text",
  304.                                         "body" => $button1
  305.                                     ),
  306.                                     array(
  307.                                         "type" => "text",
  308.                                         "body" => $button2
  309.                                     )
  310.                                 )
  311.                         )
  312.                     )
  313.             )
  314.         )
  315.     );
  316.  
  317.        
  318.     // == Encode data into JSON ==
  319.     $body = json_encode($stuffToSend);
  320.  
  321.     // == Add json to payload ==
  322.     curl_setopt($ch, CURLOPT_POSTFIELDS,     $body );
  323.     curl_setopt($ch, CURLOPT_HTTPHEADER,     array('Content-Type: application/json'));
  324.  
  325.     // == Send message ==
  326.     $result = curl_exec($ch);
  327. }
  328.  
  329. //pass with initialized curl and username, returns "firstname lastname"
  330. function getUserData($ch, $from){
  331.     //$name = "not found";
  332.    
  333.     //get the user data using curl get request thing
  334.     $url = "https://api.kik.com/v1/user/" . $from;
  335.     curl_setopt($ch, CURLOPT_URL,            $url);
  336.     curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
  337.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
  338.     curl_setopt($ch, CURLOPT_USERPWD,        "satisfitest:ef194887-c4fa-40a9-8680-d738395a6c79");
  339.     $data = curl_exec($ch);
  340.     $user = json_decode($data);
  341.    
  342.     //$name = $user->firstName . " " . $user->lastName;
  343.     //echo $name;
  344.    
  345.     return $user;
  346. }
  347.  
  348. //deals with the execution of bot logic. pass sql connection and property array
  349. function execBot($conn, $userRow, $ch, $type, $body, $from, $chatId, $firstName, $lastName){
  350.     // == Finds the user location within the bot path ==
  351.     $sql = "SELECT * FROM tbl_bot_kik WHERE prop1='{$userRow["prop1"]}' AND prop2='{$userRow["prop2"]}' AND prop3='{$userRow["prop3"]}' AND prop4='{$userRow["prop4"]}' AND botid={$userRow["botid"]}";
  352.     $query = mysqli_query($conn, $sql);
  353.     $bot = mysqli_fetch_assoc($query);
  354.    
  355.    
  356.     // == Act on this data ==
  357.    
  358.     if($type == "start-chatting"){          //Greet new user
  359.         sendText($ch, "Hello, $firstName $lastName.  We hope you enjoy the experience.", $from, $chatId);
  360.         sleep(2);
  361.        
  362.         sendText($ch, $bot["response1"], $from, $chatId);
  363.         sleep(3);
  364.         sendButton2($ch, $bot["response2"], $from, $chatId, $bot["but1"], $bot["but2"]);
  365.         return;
  366.     }
  367.    
  368.  
  369.     switch($body){                          //Act on database decision tree
  370.         case "Start Over" :
  371.             $curId = $userRow["botid"];
  372.             $sql = "UPDATE tbl_user_bot_loc SET prop1='NONE', prop2='NONE', prop3='NONE', prop4='NONE', botid=$curId WHERE chat_id='$chatId'";
  373.             mysqli_query($conn, $sql);
  374.             $userRow = array(
  375.                 "chatId" => $chatId,
  376.                 "prop1" =>  "NONE",
  377.                 "prop2" =>  "NONE",
  378.                 "prop3" =>  "NONE",
  379.                 "prop4" =>  "NONE",
  380.                 "botid" => $curId);
  381.            
  382.             $sql = "SELECT * FROM tbl_bot_kik WHERE prop1='{$userRow["prop1"]}' AND prop2='{$userRow["prop2"]}' AND prop3='{$userRow["prop3"]}' AND prop4='{$userRow["prop4"]}' AND botid={$userRow["botid"]}";
  383.             $query = mysqli_query($conn, $sql);
  384.             $newBot = mysqli_fetch_assoc($query);
  385.            
  386.             goto message;
  387.            
  388.             sendText($ch, $newBot["response1"], $from, $chatId);
  389.             sleep(2);
  390.             sendButton3($ch, $newBot["response2"], $from, $chatId, $newBot["but1"], $newBot["but2"], $newBot["but3"]);
  391.             return;
  392.             break;
  393.         case "Exit" :
  394.             $sql = "UPDATE tbl_user_bot_loc SET prop1='NONE', prop2='NONE', prop3='NONE', prop4='NONE', botid=0 WHERE chat_id='$chatId'";
  395.             mysqli_query($conn, $sql);
  396.             $userRow = array(
  397.                 "chatId" => $chatId,
  398.                 "prop1" =>  "NONE",
  399.                 "prop2" =>  "NONE",
  400.                 "prop3" =>  "NONE",
  401.                 "prop4" =>  "NONE",
  402.                 'botid' => 0);
  403.            
  404.             //sendText($ch, $userRow["botid"], $from, $chatId);
  405.            
  406.             $sql = "SELECT * FROM tbl_bot_kik WHERE prop1='{$userRow["prop1"]}' AND prop2='{$userRow["prop2"]}' AND prop3='{$userRow["prop3"]}' AND prop4='{$userRow["prop4"]}' AND botid={$userRow["botid"]}";
  407.             $query = mysqli_query($conn, $sql);
  408.             $newBot = mysqli_fetch_assoc($query);
  409.            
  410.             goto message;
  411.  
  412.             sendText($ch, $newBot["response1"], $from, $chatId);
  413.             sleep(2);
  414.             sendButton2($ch, $newBot["response2"], $from, $chatId, $newBot["but1"], $newBot["but2"]);
  415.             return;
  416.             break;
  417.         case $bot["but1"] :
  418.             $action = substr($bot["but_opt1"], 4, 5);
  419.             $prop = substr($bot["but_opt1"], 10);
  420.             break;
  421.         case $bot["but2"] :
  422.             $action = substr($bot["but_opt2"], 4, 5);
  423.             $prop = substr($bot["but_opt2"], 10);
  424.             break;
  425.         case $bot["but3"] :
  426.             $action = substr($bot["but_opt3"], 4, 5);
  427.             $prop = substr($bot["but_opt3"], 10);
  428.             break;
  429.     }
  430.    
  431.     if(strlen($prop) == 1){
  432.         $prop = intval($prop);
  433.     }
  434.  
  435.     $sql = "UPDATE tbl_user_bot_loc SET $action='$prop' WHERE chat_id='$chatId'";
  436.     if(mysqli_query($conn, $sql)){
  437.         //success
  438.     } else {
  439.         //fail -> PANIC!
  440.     }
  441.    
  442.     $userRow[$action] = $prop;
  443.     $sql = "SELECT * FROM tbl_bot_kik WHERE prop1='{$userRow["prop1"]}' AND prop2='{$userRow["prop2"]}' AND prop3='{$userRow["prop3"]}' AND prop4='{$userRow["prop4"]}' AND botid={$userRow["botid"]}";
  444.     $query = mysqli_query($conn, $sql);
  445.     $newBot = mysqli_fetch_assoc($query);
  446.    
  447.     message:
  448.        
  449.     sendText($ch, $newBot["response1"], $from, $chatId);
  450.     sleep(1);
  451.    
  452.     // == Determine what type of message to send back to the user ==
  453.     if($newBot["image"] == "X" && $newBot["link"] == "X"){ //buttons
  454.         if($newBot["but2"] == "X" && $newBot["but3"] == "X"){
  455.             sendButton1($ch, $newBot["response2"], $from, $chatId, $newBot["but1"]);
  456.         } else if ($newBot["but3"] == "X") {
  457.             sendButton2($ch, $newBot["response2"], $from, $chatId, $newBot["but1"], $newBot["but2"]);
  458.         } else {
  459.             sendButton3($ch, $newBot["response2"], $from, $chatId, $newBot["but1"], $newBot["but2"], $newBot["but3"]);
  460.         }
  461.     } else if ($newBot["link"] == "X"){ //image
  462.         sendPicture($ch, $from, $chatId, $newBot["image"], $newBot["but1"], $newBot["but2"]);
  463.     } else { //image with link
  464.         sendPictureLink($ch, $newBot["response2"], $from, $chatId, $newBot["link"], $newBot["image"], $newBot["but1"], $newBot["but2"]);
  465.     }
  466.    
  467.    
  468.    
  469. }
  470.  
  471. // == Get data that we were sent and decode it ==
  472. $entityBody = file_get_contents('php://input');
  473.  
  474. $decoded = json_decode($entityBody);
  475. $messages = $decoded->messages;
  476.  
  477. // == Initialize CURL (only do this once) ==
  478. $ch = curl_init();
  479.  
  480. // == Connect to the MySQL Database ==
  481. $servername = "localhost";
  482. $username = "satisfi_bot";
  483. $password = "BotsAreFun21";
  484. $dbname = "satisfi_bot";
  485.  
  486. // Create connection
  487. $conn = mysqli_connect($servername, $username, $password, $dbname);
  488.  
  489. // Check connection
  490. if (!$conn) {
  491.     die("Connection failed: " . mysqli_connect_error());
  492. }
  493. echo "Connected successfully";
  494.  
  495. // == Iterate over all messages (a single request can theoretically have multiple) ==
  496. foreach ($messages as $message)
  497. {
  498.     // == Obtain information that we are currently trying to process ==
  499.     $body = $message->body;
  500.     $from = $message->from;
  501.     $chatId = $message->chatId;
  502.         $type = $message->type;
  503.        
  504.         // ==  Check if the chat is new.  If so, add them to the data base. ==
  505.         // ==  Otherwise, pull their information from the MySQL server...   ==
  506.        
  507.         $sql = "SELECT * FROM tbl_user_data WHERE chat_id='$chatId'";
  508.         $query = mysqli_query($conn, $sql);
  509.         if(mysqli_num_rows($query) >= 1){
  510.             //sendText($ch, "Welcome Back", $from, $chatId);
  511.             // == This a user who we have on record ==
  512.             $row = mysqli_fetch_assoc($query);
  513.             $firstName = $row["first_name"];
  514.             $lastName = $row["last_name"];
  515.            
  516.         } else {
  517.             // == This is a new user ==
  518.             $user = getUserData($ch, $from);
  519.             $firstName = $user->firstName;
  520.             $lastName = $user->lastName;
  521.             $sql = "INSERT INTO tbl_user_data (chat_id, user_name, first_name, last_name) VALUES ('$chatId' ,'$from', '$firstName', '$lastName')";
  522.             if(mysqli_query($conn, $sql) == TRUE){
  523.                 //sendText($ch, "New record created successfully", $from, $chatId);
  524.                 // == We successfully added the new record ==
  525.                 //TODO: Do something
  526.                
  527.             } else {
  528.                 // == We failed at adding the new record ==
  529.             }
  530.         }
  531.        
  532.         // == Find the user's location in the bot path, decide what to send ==
  533.        
  534.         $sql = "SELECT * FROM tbl_user_bot_loc WHERE chat_id='$chatId'";
  535.         $query = mysqli_query($conn, $sql);
  536.         if(mysqli_num_rows($query) >= 1){
  537.             // == This a user who has used the bot before ==
  538.             $userRow = mysqli_fetch_assoc($query);      //gets user's location in the bot
  539.            
  540.             execBot($conn, $userRow, $ch, $type, $body, $from, $chatId, $firstName, $lastName);
  541.         } else {
  542.             // == This user has never used the bot ==
  543.             $sql = "INSERT INTO tbl_user_bot_loc (chat_id, prop1, prop2, prop3, prop4, botid) VALUES ('$chatId', 'NONE' ,'NONE', 'NONE', 'NONE', 0)";
  544.             if(mysqli_query($conn, $sql) == TRUE){
  545.                 // == We successfully added the user to the bot, now start them off ==
  546.                 $userRow = array(
  547.                     "chatId" => $chatId,
  548.                     "prop1" =>  "NONE",
  549.                     "prop2" =>  "NONE",
  550.                     "prop3" =>  "NONE",
  551.                     "prop4" =>  "NONE",
  552.                     "botid" => 0);
  553.                
  554.                
  555.                 execBot($conn, $userRow, $ch, $type, $body, $from, $chatId, $firstName, $lastName);
  556.             } else {
  557.                 // == We failed at adding the new record ==
  558.                 //TODO: PANIC!
  559.             }
  560.         }
  561. }
  562.  
  563. mysqli_close($conn);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement