HEX
Server: Apache
System: Linux sg2plzcpnl509433.prod.sin2.secureserver.net 4.18.0-553.54.1.lve.el8.x86_64 #1 SMP Wed Jun 4 13:01:13 UTC 2025 x86_64
User: qhl5pt3kkb1d (10888259)
PHP: 8.3.30
Disabled: NONE
Upload Files
File: /home/qhl5pt3kkb1d/public_html/tstp-old/process_message.php
<?php

$host = '127.0.0.1';  
$dbname = 'tstpihnp_tstpadmin';  
$user = 'tstpihnp_tstpadmin'; 
$password = '^$.=w3msj!G='; 

$apiKey = 'bGlLOFFiS1BhZ095bndYM0J3Tzh3MjFCS3dJa05NOWVGYmtIQTJ4X3lSUTo=';

// Create database connection
try {
    $conn = new PDO("mysql:host=$host;dbname=$dbname", $user, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
    http_response_code(500); // HTTP 500 Internal Server Error
    exit; // Stop further execution on connection error
}

// Function to send a response back to WhatsApp
function sendWhatsAppMessage($recipientId, $message, $apiKey) {
    $url = 'https://api.interakt.ai/v1/public/message/';

    $data = [
        'userId' => '',  
        'fullPhoneNumber' => $recipientId,  
        'callbackData' => 'some_callback_data',  
        'type' => 'Text',
        'data' => [
            'message' => $message,
        ],
    ];

    $headers = [
        'Authorization: Basic ' . $apiKey,
        'Content-Type: application/json'
    ];

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    
    $response = curl_exec($ch);
    curl_close($ch);
    
    file_put_contents('incoming_log.txt', $response . PHP_EOL, FILE_APPEND);
    
    return $response;
}

// Get incoming WhatsApp message
$input = file_get_contents("php://input");
$data = json_decode($input, true);

// Log the incoming message for debugging
file_put_contents('incoming_log.txt', print_r($data, true), FILE_APPEND);

// Check if the incoming message is valid
if (isset($data['message_type']) && $data['message_type'] == 'text') {
    $senderId = $data['from'];
    $userMessage = strtolower(trim($data['text']));

    // Example: User can type "product" to get a list of products
    if ($userMessage === 'product') {
        // Query the database to get product details
        $stmt = $conn->prepare("SELECT * FROM products");
        $stmt->execute();
        $products = $stmt->fetchAll(PDO::FETCH_ASSOC);
        
        // Log the fetched products for debugging
        file_put_contents('incoming_log.txt', print_r($products, true), FILE_APPEND);

        // Create a response message
        if (count($products) > 0) {
            $responseMessage = "Here are our available products:\n";
            foreach ($products as $product) {
                $responseMessage .= "Product: " . $product['product_name'] . 
                                    "\nDescription: " . $product['description'] . 
                                    "\nPrice: $" . $product['price'] . "\n\n";
            }
        } else {
            $responseMessage = "No products available at the moment.";
        }

        // Log the response message
        file_put_contents('incoming_log.txt', $responseMessage . PHP_EOL, FILE_APPEND);

        // Send the response back to the user
        sendWhatsAppMessage($senderId, $responseMessage, $apiKey);
        
        // Return a success response
        http_response_code(200); // HTTP 200 OK
        echo json_encode(['status' => 'success', 'products' => $products]);
        exit; // Ensure no further processing occurs
    } else {
        // Handle other messages
        $responseMessage = "I didn't understand that. Please type 'product' to see our products.";
        sendWhatsAppMessage($senderId, $responseMessage, $apiKey);

        // Return response for unrecognized command
        http_response_code(200); // HTTP 200 OK
        echo json_encode(['status' => 'error', 'message' => 'Unrecognized command.']);
        exit;
    }
} else {
    // If the incoming message is not valid
    http_response_code(400); // HTTP 400 Bad Request
    echo json_encode(['status' => 'error', 'message' => 'Invalid message format.']);
    exit;
}

// Send a success response back to the webhook provider
http_response_code(200); // HTTP 200 OK
?>