34516-vm/api/chat.php
2025-09-30 13:07:05 +00:00

59 lines
3.0 KiB
PHP

<?php
header('Content-Type: application/json');
require_once __DIR__ . '/../db/config.php';
// Basic validation
if (!isset($_POST['message']) || empty(trim($_POST['message']))) {
echo json_encode(['error' => 'Message is required.']);
http_response_code(400);
exit;
}
$userMessage = trim($_POST['message']);
$botReply = '';
// Simple rule-based responses
$lowerMessage = strtolower($userMessage);
if (str_contains($lowerMessage, 'hello') || str_contains($lowerMessage, 'hi')) {
$botReply = 'Hi there! I am the EvolveX guide. Ask me about our categories like AI, coding, or wellness.';
} elseif (str_contains($lowerMessage, 'money')) {
$botReply = 'Money management is key to financial freedom. Our tutorials cover budgeting, investing, and saving strategies.';
} elseif (str_contains($lowerMessage, 'ai') || str_contains($lowerMessage, 'artificial intelligence')) {
$botReply = 'Artificial Intelligence is transforming the world. We have tutorials on machine learning, neural networks, and AI ethics.';
} elseif (str_contains($lowerMessage, 'code') || str_contains($lowerMessage, 'coding')) {
$botReply = 'Our coding tutorials focus on future-relevant skills, including Python, JavaScript, and smart contract development.';
} elseif (str_contains($lowerMessage, 'productivity')) {
$botReply = 'Boost your efficiency! Our productivity section covers tools, techniques, and workflows to help you achieve more.';
} elseif (str_contains($lowerMessage, 'marketing')) {
$botReply = 'Digital marketing is always evolving. Learn about SEO, content marketing, and social media strategies with us.';
} elseif (str_contains($lowerMessage, 'cybersecurity')) {
$botReply = 'Protecting digital assets is crucial. Our cybersecurity path covers network security, ethical hacking, and data protection.';
} elseif (str_contains($lowerMessage, 'wellness') || str_contains($lowerMessage, 'skills')) {
$botReply = 'Invest in yourself. We cover everything from mindfulness and mental health to learning new practical skills for personal growth.';
} elseif (str_contains($lowerMessage, 'entrepreneurship')) {
$botReply = 'Ready to build something new? Our entrepreneurship guides cover business planning, fundraising, and scaling your venture.';
} else {
$botReply = 'That is a great question. While I am still in training, EvolveX offers a wide range of tutorials on AI, coding, wellness, and more. Which category interests you most?';
}
try {
$pdo = db_connect();
// Save user message
$stmt = $pdo->prepare("INSERT INTO messages (role, content) VALUES (?, ?)");
$stmt->execute(['user', $userMessage]);
// Save bot reply
$stmt = $pdo->prepare("INSERT INTO messages (role, content) VALUES (?, ?)");
$stmt->execute(['bot', $botReply]);
} catch (PDOException $e) {
// In a real app, you would log this error. For this stub, we'll ignore it so the chat still works.
// http_response_code(500);
// echo json_encode(['error' => 'Database error.']);
// exit;
}
echo json_encode(['reply' => $botReply]);