Certainly! Below is a simple implementation of an HTML page that integrates with the provided API, styled with CSS and includes JavaScript to handle chat interactions. The chat interface is placed at the bottom of the page.
<!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;
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
height: 100vh;
}
#chat-container {
flex: 1;
padding: 20px;
overflow-y: auto;
background-color: #f9f9f9;
border-top: 1px solid #ddd;
}
#chat-box {
display: flex;
padding: 10px;
align-items: center;
border-top: 1px solid #ddd;
background-color: #fff;
}
#user-input {
flex: 1;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
margin-right: 10px;
}
#send-button {
padding: 10px 15px;
background-color: #007bff;
color: #fff;
border: none;
border-radius: 5px;
cursor: pointer;
}
#send-button:hover {
background-color: #0056b3;
}
.message {
margin: 10px 0;
}
.user-message {
text-align: right;
}
.bot-message {
text-align: left;
}
</style>
</head>
<body>
<div id="chat-container"></div>
<div id="chat-box">
<input type="text" id="user-input" placeholder="Type your message..." />
<button id="send-button">Send</button>
</div>
<script>
const chatContainer = document.getElementById('chat-container');
const userInput = document.getElementById('user-input');
const sendButton = document.getElementById('send-button');
sendButton.addEventListener('click', sendMessage);
userInput.addEventListener('keypress', function(event) {
if (event.key === 'Enter') {
sendMessage();
}
});
function sendMessage() {
const message = userInput.value.trim();
if (message) {
displayMessage(message, 'user');
userInput.value = '';
fetchResponse(message);
}
}
function displayMessage(message, sender) {
const messageDiv = document.createElement('div');
messageDiv.className = `message ${sender}-message`;
messageDiv.textContent = message;
chatContainer.appendChild(messageDiv);
chatContainer.scrollTop = chatContainer.scrollHeight; // Scroll to the latest message
}
function fetchResponse(message) {
const encodedMessage = encodeURIComponent(message);
const apiUrl = `http://fi4.bot-hosting.net:22869/TestHubChatgptV4?msg=${encodedMessage}`;
fetch(apiUrl)
.then(response => response.json())
.then(data => {
const botMessage = data.reply || "Sorry, I didn't understand that.";
displayMessage(botMessage, 'bot');
})
.catch(error => {
console.error('Error:', error);
displayMessage("Error connecting to the server.", 'bot');
});
}
</script>
</body>
</html>
Features:
- Chat Container: Displays messages from both the user and the chatbot.
- Input Field: For typing messages, with functionality to send by clicking the "Send" button or pressing the "Enter" key.
- CSS Styling: Simple, clean design with styling for user and bot messages.
- API Integration: Fetches responses from the API based on user input.
Instructions:
- Save the code to an
.html file.
- Open the file in a web browser to interact with the AI chatbot "Xyris".
- Make sure you have internet connectivity, as the code makes requests to the online API.
Note: Ensure that the API is functional and allows access from your location, as it may have restrictions.