react-chatbot-kit
repository·master·Indexed 18 days ago
https://github.com/fredrikoseberg/react-chatbot-kitA fully customizable toolkit for building chatbots within React applications. Version 2.2.0 provides foundational components and logic to manage chatbot state, user interactions, and conversation flows. It includes utilities for creating bot, user, and custom messages, a WidgetRegistry for extending functionality, and the useChatbot hook for programmatic interaction. The library allows deep UI/UX customization through IConfig, ICustomComponents, and ICustomStyles interfaces.
What's inside react-chatbot-kit
- react-chatbot-kit is a library designed to provide an easy way to build and integrate chatbots into React applications. It provides the foundational components and logic required to manage chatbot state, user interactions, and message flows.
Configure the Chatbot with IConfig
masterThe
IConfiginterface is the primary configuration object used to initialize the chatbot. It defines the bot's identity, initial conversation state, and allows for deep customization of components, styles, and behavior.Key properties include:
botName: A string representing the name of the bot.initialMessages: An array ofIMessageobjects that start the conversation.state: An object used to manage the chatbot's internal state.widgets: An array ofIWidgetobjects to extend chatbot functionality.customComponents,customStyles, andcustomMessages: Used for UI/UX overrides.
import { IConfig } from 'react-chatbot-kit'; const config: IConfig = { botName: 'My Bot', initialMessages: [{ text: 'Hello!', sender: 'bot' }], // ... other properties };Create a custom message type with createCustomMessage
masterUse
createCustomMessageto generate a message object with a specific, non-standard type. This is useful when you want to render specialized UI components based on the message type. It accepts the message string, the customtypestring, and an options object of typeIMessageOptions.import { createCustomMessage } from 'react-chatbot-kit'; const customMsg = createCustomMessage('Check this out!', 'image', { // additional IMessageOptions here });Register a custom widget with WidgetRegistry
masterTo add a new widget to the chatbot, use the
addWidgetmethod on an instance ofWidgetRegistry. You must provide a configuration object containing thewidgetName, thewidgetFunc(the component/function that renders the widget), and optionalmapStateToPropsandpropsobjects.widgetName: A unique string identifier for the widget.widgetFunc: The function or component that returns the widget UI.mapStateToProps: A function used to map specific parts of the chatbot state to the widget's props.props: Static props to be passed to the widget.
widgetRegistry.addWidget({ widgetName: 'myCustomWidget', widgetFunc: MyWidgetComponent, mapStateToProps: (state) => ({ user: state.user }), props: { color: 'blue' } });Retrieve a registered widget with getWidget
masterOnce a widget is registered, you can retrieve its rendered instance using
getWidget(widgetName, options). This method resolves the widget's props by merging several sources:parentProps: Props passed during registration.widgetObject.props: Static props provided during registration.mapStateToProps: Values derived from the providedoptions(acting as state).options: Theoptionsobject passed togetWidgetprovidesstate,payload,actions, andscrollIntoView.- Internal utilities:
setStateandactionProviderare automatically injected.
If the widget name does not exist in the registry, the method returns
undefined.const widget = widgetRegistry.getWidget('myCustomWidget', { state: { user: { name: 'John' } }, payload: { id: 123 }, actions: myActionProvider, scrollIntoView: true });Create a chatbot message with createChatBotMessage
masterUse
createChatBotMessageto generate a message object intended for the bot. This function automatically sets the messagetypeto'bot'and initializes theloadingstate totrue. It accepts a message string and an options object of typeIMessageOptions.import { createChatBotMessage } from 'react-chatbot-kit'; const botMsg = createChatBotMessage('Hello! How can I help you?', { // additional IMessageOptions here });Create a user message with createClientMessage
masterUse
createClientMessageto generate a message object representing a message sent by the user. This function automatically sets the messagetypeto'user'. It accepts a message string and an options object of typeIMessageOptions.import { createClientMessage } from 'react-chatbot-kit'; const userMsg = createClientMessage('Hi there!', { // additional IMessageOptions here });Identify message types with botMessage, userMessage, and customMessage
masterUse these helper functions to determine the type of a given
IMessageobject during rendering or logic processing:botMessage(message): Returnstrueif the message type is'bot'.userMessage(message): Returnstrueif the message type is'user'.customMessage(message, customMessages): Returnstrueif the message type exists as a key within the providedcustomMessagesobject.
import { botMessage, userMessage, customMessage } from 'react-chatbot-kit'; if (botMessage(msg)) { // Render bot UI } else if (userMessage(msg)) { // Render user UI } else if (customMessage(msg, { image: true })) { // Render custom image UI }Create chatbot messages with message utilities
masterThe package provides utility functions to create different types of messages for the chat interface:
createChatBotMessage: Creates a standard message from the bot.createClientMessage: Creates a message from the user (client).createCustomMessage: Creates a custom message type for specialized UI components.
import { createChatBotMessage, createClientMessage, createCustomMessage } from 'react-chatbot-kit'; const botMessage = createChatBotMessage('Hello!'); const clientMessage = createClientMessage('Hi there!');Use Custom Messages for Specific Message Types
masterThe
ICustomMessageinterface allows you to define custom React components for specific message types or indices. It uses a dictionary pattern where the key is a string (representing the message type or identifier) and the value is a function returning aReactElement.This is useful when you want certain messages to render differently based on their content or metadata.
import { ICustomMessage } from 'react-chatbot-kit'; const customMessages: ICustomMessage = { imageMessage: (props) => <img src={props.message.url} alt="chat-img" />, videoMessage: (props) => <video src={props.message.url} controls />, };Customize Chatbot UI Components
masterUse the
ICustomComponentsinterface to override the default React elements used for specific parts of the chatbot UI. Each property accepts a function that returns aReactElementand can receivepropsfor customization.Supported components:
header: The top header of the chat window.botAvatar: The avatar displayed for the bot.botChatMessage: The container/component for bot messages.userAvatar: The avatar displayed for the user.userChatMessage: The container/component for user messages.
import { ICustomComponents } from 'react-chatbot-kit'; const customComponents: ICustomComponents = { header: (props) => <div className="custom-header">Custom Header</div>, botAvatar: (props) => <img src="bot-icon.png" alt="bot" />, botChatMessage: (props) => <div className="custom-bot-msg">{props.message}</div>, userAvatar: (props) => <img src="user-icon.png" alt="user" />, userChatMessage: (props) => <div className="custom-user-msg">{props.message}</div>, };Customize Chatbot Styles
masterThe
ICustomStylesinterface allows you to override specific background colors for chatbot elements. Each style property expects an object containing abackgroundColorstring.Supported styles:
botMessageBox: Sets the background color for the bot's message bubbles.chatButton: Sets the background color for the chat toggle button.
import { ICustomStyles } from 'react-chatbot-kit'; const customStyles: ICustomStyles = { botMessageBox: { backgroundColor: '#f0f0f0' }, chatButton: { backgroundColor: '#007bff' }, };