prepare("SELECT id FROM conversations WHERE phone = ? LIMIT 1"); $stmt->execute([$from]); $conversationId = (int)($stmt->fetchColumn() ?: 0); if ($conversationId === 0) { $insert = $pdo->prepare("INSERT INTO conversations (contact_name, phone, channel, created_at) VALUES (?, ?, 'twilio', NOW())"); $insert->execute([$from, $from]); $conversationId = (int)$pdo->lastInsertId(); } $msgStmt = $pdo->prepare("INSERT INTO messages (conversation_id, direction, body, status, created_at) VALUES (?, 'inbound', ?, 'received', NOW())"); $msgStmt->execute([$conversationId, $body]); header('Content-Type: text/plain'); echo "OK";