React Component Example
Chat message handling with TypeScript:
const handleSendMessage = async () => {
const userMessage: Message = {
id: Date.now().toString(),
content: inputValue.trim(),
sender: 'user',
timestamp: new Date()
};
setMessages(prev => [...prev, userMessage]);
setIsLoading(true);
const response = await fetch('/api/chatbot', {
method: 'POST',
body: JSON.stringify({ message: inputValue })
});
const data = await response.json();
// Handle response...
};Python Backend Example
Conversation storage with MySQL:
def store_conversation(session_id, message, response):
connection = get_db_connection()
cursor = connection.cursor()
query = """
INSERT INTO conversations
(session_id, message, response, sender)
VALUES (%s, %s, %s, %s)
"""
cursor.execute(query, (session_id, message, response, 'user'))
connection.commit()