Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Sends a message back to the user and leaves response open text
- function sendText($ch, $body, $to, $chatId)
- {
- // == Setup credentials ==
- curl_setopt($ch, CURLOPT_URL, "https://api.kik.com/v1/message");
- curl_setopt($ch, CURLOPT_USERPWD, "satisfitest:ef194887-c4fa-40a9-8680-d738395a6c79");
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
- curl_setopt($ch, CURLOPT_POST, 1 );
- // == Organize data ==
- $stuffToSend = array(
- 'messages' => array(
- array(
- 'body' => $body,
- "to" => $to,
- 'type' => 'text',
- 'chatId' => $chatId
- )
- )
- );
- // == 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);
- }
- //sends a mesage back to user and provides one button option
- function sendButton1($ch, $body, $to, $chatId, $button1)
- {
- // == Setup credentials ==
- curl_setopt($ch, CURLOPT_URL, "https://api.kik.com/v1/message");
- curl_setopt($ch, CURLOPT_USERPWD, "satisfitest:ef194887-c4fa-40a9-8680-d738395a6c79");
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
- curl_setopt($ch, CURLOPT_POST, 1 );
- // == Organize data ==
- $stuffToSend = array(
- 'messages' => array(
- array(
- 'body' => $body,
- "to" => $to,
- 'type' => 'text',
- 'chatId' => $chatId,
- "keyboards" => array(
- array(
- "type" => "suggested",
- "responses" => array(
- array(
- "type" => "text",
- "body" => $button1
- )
- )
- )
- )
- )
- )
- );
- // == 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);
- }
- //sends a message back to the user and provides two button opions
- function sendButton2($ch, $body, $to, $chatId, $button1, $button2)
- {
- // == Setup credentials ==
- curl_setopt($ch, CURLOPT_URL, "https://api.kik.com/v1/message");
- curl_setopt($ch, CURLOPT_USERPWD, "satisfitest:ef194887-c4fa-40a9-8680-d738395a6c79");
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
- curl_setopt($ch, CURLOPT_POST, 1 );
- // == Organize data ==
- $stuffToSend = array(
- 'messages' => array(
- array(
- 'body' => $body,
- "to" => $to,
- 'type' => 'text',
- 'chatId' => $chatId,
- "keyboards" => array(
- array(
- "type" => "suggested",
- "responses" => array(
- array(
- "type" => "text",
- "body" => $button1
- ),
- array(
- "type" => "text",
- "body" => $button2
- )
- )
- )
- )
- )
- )
- );
- // == 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);
- }
- //sends a message back to the user and provides 3 button options
- function sendButton3($ch, $body, $to, $chatId, $button1, $button2, $button3)
- {
- // == Setup credentials ==
- curl_setopt($ch, CURLOPT_URL, "https://api.kik.com/v1/message");
- curl_setopt($ch, CURLOPT_USERPWD, "satisfitest:ef194887-c4fa-40a9-8680-d738395a6c79");
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
- curl_setopt($ch, CURLOPT_POST, 1 );
- // == Organize data ==
- $stuffToSend = array(
- 'messages' => array(
- array(
- 'body' => $body,
- "to" => $to,
- 'type' => 'text',
- 'chatId' => $chatId,
- "keyboards" => array(
- array(
- "type" => "suggested",
- "responses" => array(
- array(
- "type" => "text",
- "body" => $button1
- ),
- array(
- "type" => "text",
- "body" => $button2
- ),
- array(
- "type" => "text",
- "body" => $button3
- )
- )
- )
- )
- )
- )
- );
- // == 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);
- }
- function sendButton4($ch, $body, $to, $chatId, $button1, $button2, $button3, $button4)
- {
- // == Setup credentials ==
- curl_setopt($ch, CURLOPT_URL, "https://api.kik.com/v1/message");
- curl_setopt($ch, CURLOPT_USERPWD, "satisfitest:ef194887-c4fa-40a9-8680-d738395a6c79");
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
- curl_setopt($ch, CURLOPT_POST, 1 );
- // == Organize data ==
- $stuffToSend = array(
- 'messages' => array(
- array(
- 'body' => $body,
- "to" => $to,
- 'type' => 'text',
- 'chatId' => $chatId,
- "keyboards" => array(
- array(
- "type" => "suggested",
- "responses" => array(
- array(
- "type" => "text",
- "body" => $button1
- ),
- array(
- "type" => "text",
- "body" => $button2
- ),
- array(
- "type" => "text",
- "body" => $button3
- ),
- array(
- "type" => "text",
- "body" => $button4
- )
- )
- )
- )
- )
- )
- );
- // == 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);
- }
- function sendPicture($ch, $to, $chatId, $url, $button1, $button2)
- {
- // == Setup credentials ==
- curl_setopt($ch, CURLOPT_URL, "https://api.kik.com/v1/message");
- curl_setopt($ch, CURLOPT_USERPWD, "satisfitest:ef194887-c4fa-40a9-8680-d738395a6c79");
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
- curl_setopt($ch, CURLOPT_POST, 1 );
- // == Organize data ==
- $stuffToSend = array(
- 'messages' => array(
- array(
- "to" => $to,
- 'type' => 'picture',
- 'chatId' => $chatId,
- "picUrl" => $url,
- //"text" => $body,
- "keyboards" => array(
- array(
- "type" => "suggested",
- "responses" => array(
- array(
- "type" => "text",
- "body" => $button1
- ),
- array(
- "type" => "text",
- "body" => $button2
- )
- )
- )
- )
- )
- )
- );
- // == 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);
- }
- //sends a message back to the user with a linked picture and text
- function sendPictureLink($ch, $body, $to, $chatId, $url, $pic, $button1, $button2)
- {
- // == Setup credentials ==
- curl_setopt($ch, CURLOPT_URL, "https://api.kik.com/v1/message");
- curl_setopt($ch, CURLOPT_USERPWD, "satisfitest:ef194887-c4fa-40a9-8680-d738395a6c79");
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
- curl_setopt($ch, CURLOPT_POST, 1 );
- // == Organize data ==
- $stuffToSend = array(
- 'messages' => array(
- array(
- 'text' => $body,
- "to" => $to,
- 'type' => 'link',
- 'chatId' => $chatId,
- "url" => $url,
- "picUrl" => $pic,
- "keyboards" => array(
- array(
- "type" => "suggested",
- "responses" => array(
- array(
- "type" => "text",
- "body" => $button1
- ),
- array(
- "type" => "text",
- "body" => $button2
- )
- )
- )
- )
- )
- )
- );
- // == 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);
- }
- //pass with initialized curl and username, returns "firstname lastname"
- function getUserData($ch, $from){
- //$name = "not found";
- //get the user data using curl get request thing
- $url = "https://api.kik.com/v1/user/" . $from;
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
- curl_setopt($ch, CURLOPT_USERPWD, "satisfitest:ef194887-c4fa-40a9-8680-d738395a6c79");
- $data = curl_exec($ch);
- $user = json_decode($data);
- //$name = $user->firstName . " " . $user->lastName;
- //echo $name;
- return $user;
- }
- //deals with the execution of bot logic. pass sql connection and property array
- function execBot($conn, $userRow, $ch, $type, $body, $from, $chatId, $firstName, $lastName){
- // == Finds the user location within the bot path ==
- $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"]}";
- $query = mysqli_query($conn, $sql);
- $bot = mysqli_fetch_assoc($query);
- // == Act on this data ==
- if($type == "start-chatting"){ //Greet new user
- sendText($ch, "Hello, $firstName $lastName. We hope you enjoy the experience.", $from, $chatId);
- sleep(2);
- sendText($ch, $bot["response1"], $from, $chatId);
- sleep(3);
- sendButton2($ch, $bot["response2"], $from, $chatId, $bot["but1"], $bot["but2"]);
- return;
- }
- switch($body){ //Act on database decision tree
- case "Start Over" :
- $curId = $userRow["botid"];
- $sql = "UPDATE tbl_user_bot_loc SET prop1='NONE', prop2='NONE', prop3='NONE', prop4='NONE', botid=$curId WHERE chat_id='$chatId'";
- mysqli_query($conn, $sql);
- $userRow = array(
- "chatId" => $chatId,
- "prop1" => "NONE",
- "prop2" => "NONE",
- "prop3" => "NONE",
- "prop4" => "NONE",
- "botid" => $curId);
- $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"]}";
- $query = mysqli_query($conn, $sql);
- $newBot = mysqli_fetch_assoc($query);
- goto message;
- sendText($ch, $newBot["response1"], $from, $chatId);
- sleep(2);
- sendButton3($ch, $newBot["response2"], $from, $chatId, $newBot["but1"], $newBot["but2"], $newBot["but3"]);
- return;
- break;
- case "Exit" :
- $sql = "UPDATE tbl_user_bot_loc SET prop1='NONE', prop2='NONE', prop3='NONE', prop4='NONE', botid=0 WHERE chat_id='$chatId'";
- mysqli_query($conn, $sql);
- $userRow = array(
- "chatId" => $chatId,
- "prop1" => "NONE",
- "prop2" => "NONE",
- "prop3" => "NONE",
- "prop4" => "NONE",
- 'botid' => 0);
- //sendText($ch, $userRow["botid"], $from, $chatId);
- $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"]}";
- $query = mysqli_query($conn, $sql);
- $newBot = mysqli_fetch_assoc($query);
- goto message;
- sendText($ch, $newBot["response1"], $from, $chatId);
- sleep(2);
- sendButton2($ch, $newBot["response2"], $from, $chatId, $newBot["but1"], $newBot["but2"]);
- return;
- break;
- case $bot["but1"] :
- $action = substr($bot["but_opt1"], 4, 5);
- $prop = substr($bot["but_opt1"], 10);
- break;
- case $bot["but2"] :
- $action = substr($bot["but_opt2"], 4, 5);
- $prop = substr($bot["but_opt2"], 10);
- break;
- case $bot["but3"] :
- $action = substr($bot["but_opt3"], 4, 5);
- $prop = substr($bot["but_opt3"], 10);
- break;
- }
- if(strlen($prop) == 1){
- $prop = intval($prop);
- }
- $sql = "UPDATE tbl_user_bot_loc SET $action='$prop' WHERE chat_id='$chatId'";
- if(mysqli_query($conn, $sql)){
- //success
- } else {
- //fail -> PANIC!
- }
- $userRow[$action] = $prop;
- $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"]}";
- $query = mysqli_query($conn, $sql);
- $newBot = mysqli_fetch_assoc($query);
- message:
- sendText($ch, $newBot["response1"], $from, $chatId);
- sleep(1);
- // == Determine what type of message to send back to the user ==
- if($newBot["image"] == "X" && $newBot["link"] == "X"){ //buttons
- if($newBot["but2"] == "X" && $newBot["but3"] == "X"){
- sendButton1($ch, $newBot["response2"], $from, $chatId, $newBot["but1"]);
- } else if ($newBot["but3"] == "X") {
- sendButton2($ch, $newBot["response2"], $from, $chatId, $newBot["but1"], $newBot["but2"]);
- } else {
- sendButton3($ch, $newBot["response2"], $from, $chatId, $newBot["but1"], $newBot["but2"], $newBot["but3"]);
- }
- } else if ($newBot["link"] == "X"){ //image
- sendPicture($ch, $from, $chatId, $newBot["image"], $newBot["but1"], $newBot["but2"]);
- } else { //image with link
- sendPictureLink($ch, $newBot["response2"], $from, $chatId, $newBot["link"], $newBot["image"], $newBot["but1"], $newBot["but2"]);
- }
- }
- // == Get data that we were sent and decode it ==
- $entityBody = file_get_contents('php://input');
- $decoded = json_decode($entityBody);
- $messages = $decoded->messages;
- // == Initialize CURL (only do this once) ==
- $ch = curl_init();
- // == 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) {
- die("Connection failed: " . mysqli_connect_error());
- }
- echo "Connected successfully";
- // == Iterate over all messages (a single request can theoretically have multiple) ==
- foreach ($messages as $message)
- {
- // == Obtain information that we are currently trying to process ==
- $body = $message->body;
- $from = $message->from;
- $chatId = $message->chatId;
- $type = $message->type;
- // == Check if the chat is new. If so, add them to the data base. ==
- // == Otherwise, pull their information from the MySQL server... ==
- $sql = "SELECT * FROM tbl_user_data WHERE chat_id='$chatId'";
- $query = mysqli_query($conn, $sql);
- if(mysqli_num_rows($query) >= 1){
- //sendText($ch, "Welcome Back", $from, $chatId);
- // == This a user who we have on record ==
- $row = mysqli_fetch_assoc($query);
- $firstName = $row["first_name"];
- $lastName = $row["last_name"];
- } else {
- // == This is a new user ==
- $user = getUserData($ch, $from);
- $firstName = $user->firstName;
- $lastName = $user->lastName;
- $sql = "INSERT INTO tbl_user_data (chat_id, user_name, first_name, last_name) VALUES ('$chatId' ,'$from', '$firstName', '$lastName')";
- if(mysqli_query($conn, $sql) == TRUE){
- //sendText($ch, "New record created successfully", $from, $chatId);
- // == We successfully added the new record ==
- //TODO: Do something
- } else {
- // == We failed at adding the new record ==
- }
- }
- // == Find the user's location in the bot path, decide what to send ==
- $sql = "SELECT * FROM tbl_user_bot_loc WHERE chat_id='$chatId'";
- $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);
- } else {
- // == This user has never used the bot ==
- $sql = "INSERT INTO tbl_user_bot_loc (chat_id, prop1, prop2, prop3, prop4, botid) VALUES ('$chatId', '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" => $chatId,
- "prop1" => "NONE",
- "prop2" => "NONE",
- "prop3" => "NONE",
- "prop4" => "NONE",
- "botid" => 0);
- execBot($conn, $userRow, $ch, $type, $body, $from, $chatId, $firstName, $lastName);
- } else {
- // == We failed at adding the new record ==
- //TODO: PANIC!
- }
- }
- }
- mysqli_close($conn);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement