- Add Field Engineer Management feature, allowing creation, and assignment of engineers to service requests. - Add Service Contract/AMC Management feature, allowing creation of contracts and linking them to service requests. - Update admin panel to manage engineers and contracts. - Update service request form to include contract selection.
110 lines
4.1 KiB
PHP
110 lines
4.1 KiB
PHP
<?php
|
|
// Log all incoming requests to a temporary file for debugging
|
|
file_put_contents('whatsapp_log.txt', print_r($_REQUEST, true), FILE_APPEND);
|
|
|
|
require_once 'db/config.php';
|
|
|
|
// --- Placeholder for a function to send a reply via WhatsApp API ---
|
|
// You will need to replace this with the actual API call from your provider (e.g., Twilio)
|
|
function send_whatsapp_reply($to, $message) {
|
|
// In a real application, you would make an HTTP request to the WhatsApp API provider here.
|
|
// For now, we will just log the message that would be sent.
|
|
$log_message = "-----
|
|
" . "TO: $to
|
|
" . "MESSAGE: $message
|
|
" . "-----
|
|
";
|
|
file_put_contents('whatsapp_sent_messages.txt', $log_message, FILE_APPEND);
|
|
}
|
|
|
|
// Get the sender's phone number and the message they sent
|
|
// The field names might be different depending on your API provider (e.g., 'From', 'Body' for Twilio)
|
|
$from = $_REQUEST['From'] ?? null;
|
|
$body = $_REQUEST['Body'] ?? null;
|
|
|
|
// Basic validation
|
|
if (!$from || !$body) {
|
|
http_response_code(400);
|
|
echo 'Missing From or Body parameter';
|
|
exit;
|
|
}
|
|
|
|
// Sanitize the input
|
|
$from = htmlspecialchars($from);
|
|
$body = trim($body);
|
|
|
|
// --- Conversation Logic ---
|
|
|
|
// We'll use a simple session system based on files to keep track of the conversation state for each user.
|
|
$session_file = 'session_' . md5($from) . '.txt';
|
|
|
|
// Get the user's current state, or start a new conversation
|
|
$session_data = file_exists($session_file) ? json_decode(file_get_contents($session_file), true) : [];
|
|
$state = $session_data['state'] ?? 'new';
|
|
|
|
|
|
// --- State Machine for the conversation ---
|
|
switch ($state) {
|
|
case 'new':
|
|
// New conversation
|
|
$session_data['state'] = 'waiting_for_name';
|
|
file_put_contents($session_file, json_encode($session_data));
|
|
send_whatsapp_reply($from, "Welcome to our service booking bot! What is your full name?");
|
|
break;
|
|
|
|
case 'waiting_for_name':
|
|
// User sent their name
|
|
$session_data['name'] = $body;
|
|
$session_data['state'] = 'waiting_for_email';
|
|
file_put_contents($session_file, json_encode($session_data));
|
|
send_whatsapp_reply($from, "Thanks, {$body}! What is your email address?");
|
|
break;
|
|
|
|
case 'waiting_for_email':
|
|
// User sent their email
|
|
if (!filter_var($body, FILTER_VALIDATE_EMAIL)) {
|
|
send_whatsapp_reply($from, "That doesn't look like a valid email. Please provide a correct email address.");
|
|
break;
|
|
}
|
|
$session_data['email'] = $body;
|
|
$session_data['state'] = 'waiting_for_description';
|
|
file_put_contents($session_file, json_encode($session_data));
|
|
send_whatsapp_reply($from, "Got it. Please describe the service you need.");
|
|
break;
|
|
|
|
case 'waiting_for_description':
|
|
// User sent the service description
|
|
$session_data['description'] = $body;
|
|
|
|
// Save to database
|
|
try {
|
|
$stmt = db()->prepare("INSERT INTO service_requests (name, email, description, status) VALUES (?, ?, ?, 'New')");
|
|
$stmt->execute([
|
|
$session_data['name'],
|
|
$session_data['email'],
|
|
$session_data['description']
|
|
]);
|
|
send_whatsapp_reply($from, "Thank you! Your service request has been booked. We will contact you shortly.");
|
|
// End of conversation, so delete the session file
|
|
unlink($session_file);
|
|
|
|
} catch (PDOException $e) {
|
|
error_log("Database Error: " . $e->getMessage());
|
|
send_whatsapp_reply($from, "Sorry, there was a problem booking your service. Please try again later.");
|
|
}
|
|
|
|
// Reset state for the next conversation
|
|
$session_data = ['state' => 'new'];
|
|
file_put_contents($session_file, json_encode($session_data));
|
|
break;
|
|
|
|
default:
|
|
send_whatsapp_reply($from, "Sorry, I'm not sure how to respond. Let's start over. What is your full name?");
|
|
// Reset state
|
|
$session_data = ['state' => 'waiting_for_name'];
|
|
file_put_contents($session_file, json_encode($session_data));
|
|
break;
|
|
}
|
|
|
|
echo "OK";
|