Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- error_reporting(1);
- $servername = "localhost";
- $username = "###";
- $password = "###";
- $dbname = "###";
- $datatable = "###";
- // parameters
- $hubVerifyToken = '#####';
- $accessToken = "###############################################";
- // check token at setup
- if ($_REQUEST['hub_verify_token'] === $hubVerifyToken) {
- echo $_REQUEST['hub_challenge'];
- exit;
- }
- // handle bot's anwser
- $input = json_decode(file_get_contents('php://input'), true);
- $senderId = $input['entry'][0]['messaging'][0]['sender']['id'];
- $messageText = $input['entry'][0]['messaging'][0]['message']['text'];
- $response = null;
- $message = null; $msgat = null; $answer = null;
- //set Message
- if($messageText == "hi") {$answer = "Hello there! :)";}
- if($messageText == "Hi") {$answer = "Hello there! :)";}
- if($messageText == "hello") {$answer = "Hi there! :)";}
- if($messageText == "Hello") {$answer = "Hi there! :)";}
- //$rem = "$messengerText . $otherstring";
- // ($answer=)? echo $str[0]; gets first string in variable.
- // Create connection
- $conn = new mysqli($servername, $username, $password, $dbname);
- // Check connection
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- //checking and getting teh if first character in the message 'input' is an "@" symbol
- //making it divert to either printing the auto-reply, or accessing and printing the SQLdb reply.
- $msgat = substr($messageText,0,1);
- $message = substr($messageText,1);
- $mysqli = "SELECT verse FROM ".$datatable." WHERE book LIKE '%".$message."'";
- $result = $conn->query($mysqli);
- if ($result->num_rows > 0) {
- // output data of each row
- while($row = $result->fetch_assoc()) ;
- //$conn->close();
- }
- {
- if ($msgat == "@"){
- //$answer = "This works."; // <-- This Does Work. a simple $string.. live, within the use of the bot.
- $answer= " " . $row["verse"]; // <-- This does Not work, but it does 'locally' ??why not? :)
- }
- //echo $answer; // <-- used in localhost test script it works and echos either or of the above $answer showing correctly.
- else if($msgat[0] != "@"){
- $answer = $answer;}
- $response = [
- 'recipient' => [ 'id' => $senderId ],
- 'message' => [ 'text' => $answer ]
- ];
- }
- $conn->close();
- $ch = curl_init('https://graph.facebook.com/v2.6/me/messages?access_token='.$accessToken);
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($response));
- curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
- if(!empty($input)){
- $result = curl_exec($ch);
- }
- curl_close($ch);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement