React ChatBotify Documentation
website·Indexed 32 days ago
https://react-chatbotify.comAn intuitive and versatile chatbot library for React designed to streamline the creation of conversational interfaces. Features include support for LLM conversations, real-time message streaming, voice (speech-to-text) and audio (text-to-speech) capabilities, custom theming, and advanced form validation.
What's inside React ChatBotify
- React ChatBotify is a chatbot library for React designed to handle a variety of use cases, ranging from simple FAQ bots to complex conversational interfaces and integrations with Large Language Models (LLMs). It provides a framework to customize conversational flows and expand chatbot functionality based on specific project requirements.
Overview of Conversation Block Attributes
v2Conversation blocks in react-chatbotify v2 use attributes to define bot behavior and UI. These are split into pre-processing (executed when entering a block) and post-processing (executed after user interaction). All attributes support either a static value or a function (sync or async) that optionally acceptsparamsto access dynamic data like user input.const flow = { start: { message: "Hello there! What is your name?", path: "ask_age", }, ask_age: { message: (params) => `Good to see you ${params.userInput}, how old are you?` } }Overview of React ChatBotify capabilities
v1React ChatBotify is a chatbot library for React designed to handle a variety of use cases, ranging from simple FAQ bots to complex conversational interfaces and integrations with Large Language Models (LLMs). It provides a balance between a streamlined development process and the flexibility to implement advanced, customized conversational flows.Extend chatbot functionality with plugins, hooks, and events
v2v2 introduces three new extensibility mechanisms:
pluginsprop: Used to import and load custom plugins.- Custom Hooks: Provided hooks allow external components to interact with the chatbot (e.g., toggling audio).
- Custom Events: The chatbot now emits events (e.g.,
RcbPreMessageInjectEvent) that developers can listen to and respond to.
Extend React ChatBotify functionality using the plugins prop
v2React ChatBotify v2 introduces apluginsprop that allows users to install and use custom plugins to add functionality to the chatbot. Plugins can be sourced from the official React ChatBotify Gallery or community contributors. Note that plugin maintenance is separate from the core library maintenance. Many plugins rely on specific chatbot settings (such as events) to function; always check the specific plugin's documentation for required configuration settings.Use Attribute parameters for chat context and control
v2Attributes in react-chatbotify provide access to aparamsobject containing contextual details and utility functions. Note that all utility functions inparamsareasync; you must useawaitto ensure predictable behavior and avoid issues like multiple messages being sent simultaneously.Replace Advance section with ChatBotProvider hooks
v2Theadvancesection in settings has been removed. Developers should now useChatBotProviderand its associated custom hooks to modify settings, styles, messages, paths, the text area, audio toggles, and toast popups from outside the chatbot.Implement response streaming
v2There are two ways to stream responses:
- Real-time streaming: Use this when streaming responses from a backend/LLM.
- Simulated streaming: Use this to simulate the appearance of streaming for static messages. Ensure you are on version 1.3.0 or higher for
simulateStream: trueto work.
Update Message attribute requirements for direct manipulation
v2TheMessageattribute has been expanded. If you are manipulating messages directly (now recommended via hooks), you must provide all five required fields:sender,content,id,type, andtimestamp.Manage chatbot messages externally using MessagesContext
v1To modify chatbot messages from outside the chatbot component using your own application logic, use the
MessagesContext.Provider. This allows you to maintain your own messages state and pass it into the chatbot.Important Constraints:
- You must enable
advance.useCustomMessages: truein the chatbot options. - When using
useCustomMessages, you are modifying the messages array directly. Consequently,audioandstreamfeatures will not function for these messages.
import React, { useState } from 'react'; import { ChatBot, MessagesContext } from 'react-chatbotify'; const MyChatBot = () => { // 1. Setup your own messages state const [messages, setMessages] = useState([]); // Example: Clear messages while preserving system messages const clearMessages = () => { setMessages(prev => prev.filter(msg => msg.sender === "system")); }; // Example: Manually insert a message const insertMessage = () => { setMessages(prev => [ ...prev, { content: "Hello I am new!", sender: "bot", type: "string" } ]); }; const options = { advance: { useCustomMessages: true // Required to enable custom message handling }, theme: { embedded: true }, chatHistory: { storageKey: "example_custom_messages" } }; const flow = { start: { message: "Hello there! I am a demo for custom messages!", chatDisabled: true } }; return ( <> <button onClick={clearMessages}>Clear Messages</button> <button onClick={insertMessage}>Add Message</button> {/* 2. Wrap ChatBot with MessagesContext.Provider */} <MessagesContext.Provider value={{ messages, setMessages }}> <ChatBot options={options} flow={flow} /> </MessagesContext.Provider> </> ); };- You must enable
Migrate HTML rendering from dangerouslySetInnerHtml to HTML Renderer Plugin
v2Inv2.0.0-beta.31, thestyles.botBubble.dangerouslySetInnerHtmlandstyles.userBubble.dangerouslySetInnerHtmlproperties were removed to improve security. To render HTML content within messages, you must now use the official HTML Renderer Plugin.npm install @rcb-plugins/html-renderer<ChatBot plugins={[ // Add the HTML Renderer Plugin here ]} />Understand the conversation block lifecycle
v2A conversation block represents the current state of the chat. It operates on a cycle of pre-processing and post-processing:
- Pre-processing: When a block is entered, pre-processing attributes (like
message,options,checkboxes,component,chatDisabled,isSensitive,transition) are executed immediately. The bot then waits for user input. - User Input: Triggered by sending a message, selecting an option, submitting checkboxes, or uploading a file.
- Post-processing: After input, post-processing attributes (like
function,file,path) are executed. If apathis defined, the flow moves to the specified next block, triggering its pre-processing phase.
Attributes are processed in the order they are declared, except for
path, which is always processed last during post-processing.- Pre-processing: When a block is entered, pre-processing attributes (like