React ChatBotify Documentation

website·Indexed 32 days ago

https://react-chatbotify.com

An 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.

Tokens
44.4K
Snippets
202
Records
269
Agent score
62%

What's inside React ChatBotify

  1. Overview of React ChatBotify capabilities

    v2
    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.
  2. Overview of Conversation Block Attributes

    v2
    Conversation 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 accepts params to 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?`
      }
    }
  3. Overview of React ChatBotify capabilities

    v1
    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 balance between a streamlined development process and the flexibility to implement advanced, customized conversational flows.
  4. Extend chatbot functionality with plugins, hooks, and events

    v2

    v2 introduces three new extensibility mechanisms:

    • plugins prop: 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.
  5. Extend React ChatBotify functionality using the plugins prop

    v2
    React ChatBotify v2 introduces a plugins prop 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.
  6. Use Attribute parameters for chat context and control

    v2
    Attributes in react-chatbotify provide access to a params object containing contextual details and utility functions. Note that all utility functions in params are async; you must use await to ensure predictable behavior and avoid issues like multiple messages being sent simultaneously.
  7. Replace Advance section with ChatBotProvider hooks

    v2
    The advance section in settings has been removed. Developers should now use ChatBotProvider and its associated custom hooks to modify settings, styles, messages, paths, the text area, audio toggles, and toast popups from outside the chatbot.
  8. Implement response streaming

    v2

    There are two ways to stream responses:

    1. Real-time streaming: Use this when streaming responses from a backend/LLM.
    2. 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: true to work.
  9. Update Message attribute requirements for direct manipulation

    v2
    The Message attribute has been expanded. If you are manipulating messages directly (now recommended via hooks), you must provide all five required fields: sender, content, id, type, and timestamp.
  10. Manage chatbot messages externally using MessagesContext

    v1

    To 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: true in the chatbot options.
    • When using useCustomMessages, you are modifying the messages array directly. Consequently, audio and stream features 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>
        </>
      );
    };
  11. Migrate HTML rendering from dangerouslySetInnerHtml to HTML Renderer Plugin

    v2
    In v2.0.0-beta.31, the styles.botBubble.dangerouslySetInnerHtml and styles.userBubble.dangerouslySetInnerHtml properties 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
      ]} 
    />
  12. Understand the conversation block lifecycle

    v2

    A conversation block represents the current state of the chat. It operates on a cycle of pre-processing and post-processing:

    1. 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.
    2. User Input: Triggered by sending a message, selecting an option, submitting checkboxes, or uploading a file.
    3. Post-processing: After input, post-processing attributes (like function, file, path) are executed. If a path is 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.