Certainly! Below is a simple HTML layout that includes CSS and JavaScript to create a chat interface for interacting with the AI chatbot called "Xyris." The chat will send messages to the specified API, and responses will be displayed in the chat window.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Xyris Chatbot</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
margin: 0;
display: flex;
flex-direction: column;
height: 100vh;
}
#chat-container {
flex: 1;
overflow-y: auto;
padding: 10px;
background: white;
border-top: 2px solid #007bff;
border-bottom: 2px solid #007bff;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
#message-bar {
display: flex;
padding: 10px;
background-color: #fff;
border-top: 1px solid #ddd;
}
#message-input {
flex: 1;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
margin-right: 10px;
}
#send-button {
padding: 10px 15px;
border: none;
border-radius: 5px;
background-color: #007bff;
color: white;
cursor: pointer;
}
#send-button:hover {
background-color: #0056b3;
}
.message {
margin: 5px 0;
padding: 10px;
border-radius: 5px;
}
.user-message {
background-color: #e6f7ff;
align-self: flex-end;
}
.bot-message {
background-color: #f1f1f1;
align-self: flex-start;
}
</style>
</head>
<body>
<div id="chat-container"></div>
<div id="message-bar">
<input type="text" id="message-input" placeholder="Type your message here..." />
<button id="send-button">Send</button>
</div>
<script>
const apiUrl = "http://fi4.bot-hosting.net:22869/TestHubChatgptV4?msg=";
const chatContainer = document.getElementById('chat-container');
const messageInput = document.getElementById('message-input');
const sendButton = document.getElementById('send-button');
sendButton.addEventListener('click', sendMessage);
messageInput.addEventListener('keypress', function (e) {
if (e.key === 'Enter') {
sendMessage();
}
});
function sendMessage() {
const messageText = messageInput.value.trim();
if (messageText === '') return;
displayMessage(messageText, 'user');
messageInput.value = '';
fetch(apiUrl + encodeURIComponent(messageText))
.then(response => response.json())
.then(data => {
const botResponse = data.response || "Sorry, I didn't understand that.";
displayMessage(botResponse, 'bot');
})
.catch(error => {
console.error('Error:', error);
displayMessage("Error: " + error.message, 'bot');
});
}
function displayMessage(message, sender) {
const messageElement = document.createElement('div');
messageElement.classList.add('message');
messageElement.classList.add(sender === 'user' ? 'user-message' : 'bot-message');
messageElement.textContent = message;
chatContainer.appendChild(messageElement);
chatContainer.scrollTop = chatContainer.scrollHeight; // Scroll to bottom
}
</script>
</body>
</html>
Explanation:
-
HTML Structure:
- A container (
#chat-container) to display chat messages.
- An input area (
#message-bar) with a text input (#message-input) for user messages and a send button (#send-button).
-
CSS Styles:
- Basic styling to differentiate between user and bot messages, with hover effects on the send button.
- Flexbox is used to arrange the elements appropriately.
-
JavaScript Logic:
- Handles sending messages when the button is clicked or the enter key is pressed.
- Uses the Fetch API to send messages to the specified chatbot API.
- Displays the user's messages and bot's responses in the chat container, scrolling to the latest message.
How to Use:
- Copy and paste this code into an HTML file.
- Open the file in a web browser.
- You can type messages, and it will interact with the chatbot API. Make sure you have internet access as it fetches data from the API.
This implementation can be extended with more features (like loading indicators, better error handling, etc.) as needed!