Advertisement
x1705

bot2

Feb 20th, 2019
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. <?php
  2. error_reporting(1);
  3. $servername = "localhost";
  4. $username = "###";
  5. $password = "###";
  6. $dbname = "###";
  7. $datatable = "###";
  8.  
  9. // parameters
  10. $hubVerifyToken = '#####';
  11. $accessToken = "###############################################";
  12.  
  13. // check token at setup
  14. if ($_REQUEST['hub_verify_token'] === $hubVerifyToken) {
  15. echo $_REQUEST['hub_challenge'];
  16. exit;
  17. }
  18.  
  19. // handle bot's anwser
  20. $input = json_decode(file_get_contents('php://input'), true);
  21.  
  22. $senderId = $input['entry'][0]['messaging'][0]['sender']['id'];
  23. $messageText = $input['entry'][0]['messaging'][0]['message']['text'];
  24. $response = null;
  25. $message = null; $msgat = null; $answer = null;
  26.  
  27. //set Message
  28.  
  29. if($messageText == "hi") {$answer = "Hello there! :)";}
  30. if($messageText == "Hi") {$answer = "Hello there! :)";}
  31.  
  32. if($messageText == "hello") {$answer = "Hi there! :)";}
  33. if($messageText == "Hello") {$answer = "Hi there! :)";}
  34.  
  35.  
  36. //$rem = "$messengerText . $otherstring";
  37. // ($answer=)? echo $str[0]; gets first string in variable.
  38.  
  39. // Create connection
  40. $conn = new mysqli($servername, $username, $password, $dbname);
  41. // Check connection
  42. if ($conn->connect_error) {
  43. die("Connection failed: " . $conn->connect_error);
  44. }
  45.  
  46. //checking and getting teh if first character in the message 'input' is an "@" symbol
  47. //making it divert to either printing the auto-reply, or accessing and printing the SQLdb reply.
  48. $msgat = substr($messageText,0,1);
  49. $message = substr($messageText,1);
  50.  
  51. $mysqli = "SELECT verse FROM ".$datatable." WHERE book LIKE '%".$message."'";
  52.  
  53. $result = $conn->query($mysqli);
  54.  
  55. if ($result->num_rows > 0) {
  56. // output data of each row
  57. while($row = $result->fetch_assoc()) ;
  58. //$conn->close();
  59. }
  60. {
  61.  
  62. if ($msgat == "@"){
  63. //$answer = "This works."; // <-- This Does Work. a simple $string.. live, within the use of the bot.
  64. $answer= " " . $row["verse"]; // <-- This does Not work, but it does 'locally' ??why not? :)
  65. }
  66. //echo $answer; // <-- used in localhost test script it works and echos either or of the above $answer showing correctly.
  67.  
  68. else if($msgat[0] != "@"){
  69. $answer = $answer;}
  70.  
  71.  
  72. $response = [
  73. 'recipient' => [ 'id' => $senderId ],
  74. 'message' => [ 'text' => $answer ]
  75. ];
  76. }
  77. $conn->close();
  78. $ch = curl_init('https://graph.facebook.com/v2.6/me/messages?access_token='.$accessToken);
  79. curl_setopt($ch, CURLOPT_POST, 1);
  80. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($response));
  81. curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
  82. if(!empty($input)){
  83. $result = curl_exec($ch);
  84. }
  85.  
  86. curl_close($ch);
  87. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement