Chatbot Component
Desktop Light | Mobile Light |
---|---|
Desktop Dark | Mobile Dark |
---|---|
The Chatbot
component provides a dynamic and interactive chat interface for users to communicate with an AI chatbot. It renders chat messages between a user and a chatbot, including features like typing simulation for the chatbot’s responses and automatic scrolling to the latest message.
Pre-requisite
Ensure you have the @neo4j-ndl
library installed in your project to use this Chatbot
component.
Usage
To integrate the Chatbot
component into your application, you will first need to import it:
import Chatbot from './path/to/Chatbot';
Next, render the Chatbot
component in your application’s component tree, providing the necessary props:
<Chatbot messages={listMessages} />
listMessages
should be an array of message objects that you want to display initially.
Component Props
The Chatbot
component accepts the following props:
Name | Description | Required |
---|---|---|
|
An array of message objects that the chatbot will render. Each message object should contain |
Yes |
Message Object Structure
Each message object in the messages
array prop should follow this structure:
{
id: number; // Unique identifier for the message
user: string; // "user" for user messages, "chatbot" for chatbot messages
message: string; // The message text
datetime: string; // Timestamp of the message
isTyping?: boolean; // Optional, simulates typing effect for chatbot messages
}
Key components
Handling State and Effects
The component uses React’s useState
hook to manage:
-
listMessages
: The current list of messages to be displayed. -
inputMessage
: The current text input by the user.
It also uses the useEffect
hook to automatically scroll to the bottom of the message list whenever a new message is added thansk to the messagesEndRef
reference.
Example
Here is a basic example of using the Chatbot
component with initial messages:
const listMessages = [
{
id: 1,
user: 'user',
message: 'Hello, chatbot!',
datetime: '01/01/2024 00:00:00',
},
{
id: 2,
user: 'chatbot',
message: 'Hello! How can I assist you today?',
datetime: '01/01/2024 00:00:00',
},
];
<Chatbot messages={listMessages} />
This will render a chat interface with two initial messages, one from the user and one from the chatbot.
Component Integration
Integrating the Chatbot
component into an existing application is straightforward. Make sure to provide it with the necessary messages
prop to initialize the chat history. The component handles user inputs and chatbot responses internally, offering a complete chat interface experience out of the box.
Here is an example if you already have a backend application taking care of generating the chatbot’s responses and you want to integrate it with this Chatbot
component:
First, we will set a new state gettingResponse
that will indicate us if we are currently fetching a response from the backend:
const [gettingResponse, setGettingResponse] = useState(false);
Then, we will define a new function fetchResponseFromAPI
that will be responsible for fetching the chatbot’s response from the backend based on the user’s message:
const fetchResponseFromAPI = async () => {
setGettingResponse(true);
const requestBody = {
message: inputMessage
};
try {
const response = await fetch(`<URI_TO_YOUR_BACKEND_API>`, {
method: 'POST',
headers: {
'accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(requestBody),
});
const data = await response.json();
setGettingResponse(false);
return data.content;
} catch (error) {
console.error("API call failed:", error);
return "Sorry, something went wrong.";
} finally {
setGettingResponse(false);
}
};
Ideally you will want to consider using a framework to manage the states, caching and hooks like tanstack/react-query for example as well as adding an authentication and authorization to your backend API calls
|
Then all we need to do is to call this function when the user submits a message, retrieve the response, and simulate the typing effect:
In our handleSubmit
function:
const chatbotReply = await fetchResponseFromAPI();
simulateTypingEffect(chatbotReply);