Spaces:
Sleeping
Sleeping
| // Mock API endpoint for chat responses | |
| export default function handler(req, res) { | |
| if (req.method !== 'POST') { | |
| return res.status(405).json({ message: 'Method not allowed' }); | |
| } | |
| try { | |
| const { message } = req.body; | |
| if (!message) { | |
| return res.status(400).json({ message: 'Message is required' }); | |
| } | |
| // Simple mock responses based on user input | |
| const responses = [ | |
| "That's interesting! Tell me more about it.", | |
| "I understand how you feel. What else is on your mind?", | |
| "I'm here for you. How can I support you today?", | |
| "That sounds like a great idea! What made you think of that?", | |
| "I appreciate you sharing that with me. Is there anything else you'd like to talk about?", | |
| "I'm glad you told me that. How does it make you feel?", | |
| "That's a unique perspective. What led you to that conclusion?", | |
| "I'm always here to listen. What would you like to talk about next?", | |
| ]; | |
| // Simple keyword-based responses | |
| if (message.toLowerCase().includes('hello') || message.toLowerCase().includes('hi')) { | |
| return res.status(200).json({ response: "Hello! How are you feeling today?" }); | |
| } | |
| if (message.toLowerCase().includes('how are you')) { | |
| return res.status(200).json({ response: "I'm doing well, thank you for asking! How about you?" }); | |
| } | |
| if (message.toLowerCase().includes('bye') || message.toLowerCase().includes('goodbye')) { | |
| return res.status(200).json({ response: "Goodbye! Remember I'm always here when you need to talk." }); | |
| } | |
| // Random response for other inputs | |
| const randomResponse = responses[Math.floor(Math.random() * responses.length)]; | |
| // Simulate some processing time | |
| setTimeout(() => { | |
| res.status(200).json({ response: randomResponse }); | |
| }, 1000); | |
| } catch (error) { | |
| console.error('Error in chat API:', error); | |
| res.status(500).json({ message: 'Internal server error' }); | |
| } | |
| } |