Skip to main content

Hooks

This page documents all the available custom hooks provided by the library. These hooks allow you to interact (externally from your own components) with various parts of the chatbot, such as toggling audio, handling messages, and controlling the chat window state!

Tip

Hooks are only relevant if you intend to interact with the chatbot functionalities (e.g. toggle audio or chat window) from outside the chatbot and within your own component. If you have no such use case, you may skip hooks entirely!

Note that in order to import and use these custom hooks within your components, you first need to import ChatBotProvider and nest your components as its children. We'll look at 2 scenarios below.

Scenario 1: If you have no need for custom hooks, then you do not need to import ChatBotProvider and can simply use ChatBot. This is likely the scenario for a vast majority of users:

import ChatBot from "react-chatbotify";

const MyComponent = () => {
return (
<ChatBot/> {/* no custom hooks needed */}
);
};

Scenario 2: If you require custom hooks to interact with the chatbot (e.g. toggle audio) from within your own components, you need to import ChatBotProvider and ensure that your components that need the hooks are nested within it:

import ChatBot, { ChatBotProvider } from "react-chatbotify";

const MyComponent = () => {
return (
<>
<MyNotNestedComponent> {/* unable to access custom hooks */}
<ChatBotProvider>
<MyNestedComponent> {/* able to access custom hooks (e.g. useAudio) */}
<ChatBot/>
</ChatBotProvider>
</>
);
};
import { useAudio } from "react-chatbotify";

const MyNestedComponent = () => {
// can use custom hook
const { toggleAudio } = useAudio();

return (
<button onClick={toggleAudio}></button>
)
};

const MyNotNestedComponent = () => {
// error, cannot use custom hook since outside of ChatBotProvider
const { toggleAudio } = useAudio();

return (
<button onClick={toggleAudio}></button>
)
};
Warning

It is a common mistake to import these custom hooks from a component outside of <ChatBotProvider>. If you're running into issues, make sure to check that your component is nested properly as a child of <ChatBotProvider>!

Tip

An extensive example featuring how various hooks may be used can be found here.

Overview

Below is a list of available hooks along with a brief description for each of them:

NameDescription
useAudioManages audio functionalities, such as toggling audio on or off.
useBotIdAllows retrieving of bot id (mainly for plugin developers).
useChatHistoryHandles loading, retrieving and setting of chat history messages.
useChatWindowManages the state of the chatbot window (open/close).
useFirstInteractionDetects and tracks the user's first interaction with the chatbot.
useFlowAllows retrieving and restarting chatbot flow and tracks if flow has started.
useMessagesHandles sending, clearing, and retrieving messages in the chatbot.
useNotificationsManages chatbot notifications, such as toggling notifications on or off.
usePathsManages chatbot conversation paths, such as navigation
useSettingsAccesses and modifies chatbot settings.
useStylesAccesses and modifies chatbot styles.
useTextAreaManages the text input area of the chatbot, including setting and clearing values.
useToastsManages toasts shown within the chatbot, such as showing or missing them.
useVoiceManages voice functionality, such as toggling voice on or off

Hook Details

Below is a detailed description of each hook and the functions they expose.


useAudio

Description

The useAudio hook allows you to track and manage the chatbot's audio functionality.

Return Values

NameTypeDescription
audioToggledOnboolean Indicates if the chatbot audio is currently on or off.
toggleAudiofunctionToggles the chatbot audio on or off.

Code Example

import { useAudio } from "react-chatbotify";

const MyNestedComponent = () => {
const { toggleAudio } = useAudio();

return (
<button onClick={toggleAudio}></button>
)
};

useBotId

Description

The useBotId hook allows you to retrieve the bot id (mainly for plugin developers).

Return Values

NameTypeDescription
getBotIdfunction Retrieves the bot id.

Code Example

import { useBotId } from "react-chatbotify";

const MyNestedComponent = () => {
const { getBotId } = useBotId();

return (
<button onClick={getBotId}></button>
)
};

useChatHistory

Description

The useChatHistory hook allows you to show, retrieve and set chat history messages.

Return Values

NameTypeDescription
showChatHistoryfunctionShows the chat history messages on the chatbot.
getHistoryMessagesfunctionRetrieves the chat history messages.
setHistoryMessagesfunctionSets the chat history messages (note that this is permanent).

Code Example

import { useChatHistory } from "react-chatbotify";

const MyNestedComponent = () => {
const { showChatHistory } = useChatHistory();

return (
<button onClick={showChatHistory}></button>
)
};

useChatWindow

Description

The useChatWindow hook allows you to track and manage the chatbot's window state.

Return Values

NameTypeDescription
isChatWindowOpenbooleanIndicates if the chat window is currently open or close.
toggleChatWindowfunctionToggles the chat window open or close.

Code Example

import { useChatWindow } from "react-chatbotify";

const MyNestedComponent = () => {
const { toggleChatWindow } = useChatWindow();

return (
<button onClick={toggleChatWindow}></button>
)
};

useFirstInteraction

Description

The useFirstInteraction hook allows you to track if a page has been interacted with.

Return Values

NameTypeDescription
hasInteractedPagebooleanIndicates if the page has been interacted with.

Code Example

import { useEffect } from "react";
import { useFirstInteraction } from "react-chatbotify";

const MyNestedComponent = () => {
const { hasInteractedPage } = useFirstInteraction();

useEffect(() => {
// do something if has interacted
}, [hasInteractedPage])

return (
<ExampleComponent/>
)
};

useFlow

Description

The useFlow hook allows you to get a flow, restart a flow and track if a flow has started.

Return Values

NameTypeDescription
hasFlowStartedbooleanIndicates if the chatbot flow has started.
getFlowfunctionRetrieves the chatbot flow.
restartFlowfunctionRestarts the chatbot flow.

Code Example

import { useEffect } from "react";
import { useFirstInteraction } from "react-chatbotify";

const MyNestedComponent = () => {
const { hasFlowStarted } = useFirstInteraction();

useEffect(() => {
// do something if flow has started
}, [hasFlowStarted])

return (
<ExampleComponent/>
)
};

useMessages

Description

The useMessages hook allows you to track and manage the chatbot's messages.

Return Values

NameTypeDescription
injectMessageasync functionA utility function used to inject a message into the chat, identical to params.injectMessage detailed here.
streamMessageasync functionA utility function used to stream messages into the chat, identical to params.streamMessage detailed here.
endStreamMessageasync functionA utility function used to indicate the end of an existing message stream, identical to params.endMessageStream detailed here.
removeMessageasync functionA utility function used to remove a message from the chat, identical to params.removeMessage detailed here.
messagesArray<Message>Array containing all messages currently shown in the chatbot.
setMessagesfunctionSetter for manipulating messages array directly, not recommended to use this so proceed with caution.

Code Example

import { useEffect } from "react";
import { useMessages } from "react-chatbotify";

const MyNestedComponent = () => {
const { injectMessage } = useMessages();

useEffect(() => {
// inject custom message
injectMessage("my custom message");
}, [])

return (
<ExampleComponent/>
)
};

useNotifications

Description

The useNotifications hook allows you to track and manage the chatbot's notifications functionality.

Return Values

NameTypeDescription
notificationsToggledOnboolean Indicates if the chatbot notifications is currently on or off.
toggleNotificationsfunctionToggles the chatbot notifications on or off.
playNotificationSoundfunctionPlays the notification sound.

Code Example

import { useNotifications } from "react-chatbotify";

const MyNestedComponent = () => {
const { toggleNotifications } = useNotifications();

return (
<button onClick={toggleNotifications}></button>
)
};

usePaths

Description

The usePaths hook allows you to track and manage the chatbot's paths.

Return Values

NameTypeDescription
getCurrPathfunctionRetrieves the current path of the user.
getPrevPathfunctionRetrieves the previous path of the user.
goToPathfunctionSends the user to a specified path, identical to params.goToPath detailed here.
pathsArray<string>Array containing all paths the user has taken in order.
setPathsfunctionSetter for manipulating paths array directly, not recommended to use this so proceed with caution.

Code Example

import { useEffect } from "react";
import { usePaths } from "react-chatbotify";

const MyNestedComponent = () => {
const { goToPath } = usePaths();

useEffect(() => {
// go to custom path
goToPath("my-custom-path");
}, [])

return (
<ExampleComponent/>
)
};

useSettings

Description

The useSettings hook allows you to track and manage the chatbot's settings.

Return Values

NameTypeDescription
settingsSettingsRepresents the current settings of the chatbot.
setSettingsfunctionSetter for manipulating bot settings.

Code Example

import { useEffect } from "react";
import { useSettings } from "react-chatbotify";

const MyNestedComponent = () => {
const { settings } = useSettings();

useEffect(() => {
if (settings.general?.embedded) {
// do something if chatbot is embedded
}
}, [])

return (
<ExampleComponent/>
)
};

useStyles

Description

The useStyles hook allows you to track and manage the chatbot's styles.

Return Values

NameTypeDescription
stylesStylesRepresents the current styles of the chatbot.
setStylesfunctionSetter for manipulating bot styles.

Code Example

import { useEffect } from "react";
import { useStyles } from "react-chatbotify";

const MyNestedComponent = () => {
const { styles, setStyles } = useStyles();

useEffect(() => {
// set header style
setStyles({...styles, headerStyle: {background: "red"}})
}, [])

return (
<ExampleComponent/>
)
};

useTextArea

Description

The useTextArea hook allows you to track and manage the chatbot's text area field.

Return Values

NameTypeDescription
textAreaDisabledbooleanIndicates if the text area is disabled.
toggleTextAreaDisabledfunctionToggles the text area disabled state.
textAreaSensitiveModebooleanIndicates if the text area is in sensitive mode.
toggleTextAreaSensitiveModefunctionToggles the text area sensitive mode.
getTextAreaValuefunctionRetrieves the string value inside the text area.
setTextAreaValuefunctionSets the value inside the text area, identical to params.setTextAreaValue detailed here.
focusTextAreafunctionFocuses on the text area.

Code Example

import { useEffect } from "react";
import { useTextArea } from "react-chatbotify";

const MyNestedComponent = () => {
const { focusTextArea } = useTextArea();

useEffect(() => {
// focus on chatbot input text area
focusTextArea()
}, [])

return (
<ExampleComponent/>
)
};

useToasts

Description

The useToasts hook allows you to track and manage the chatbot's toasts.

Return Values

NameTypeDescription
showToastfunctionShows a toast in chat, identical to params.showToast detailed here.
dismissToastfunctionDismisses a toast from chat, identical to params.dismissToast detailed here.
toastsArray<Toast>Array containing all toasts currently shown in the chatbot
setToastsfunctionSetter for manipulating toasts array directly, not recommended to use this so proceed with caution.

Code Example

import { useEffect } from "react";
import { useToasts } from "react-chatbotify";

const MyNestedComponent = () => {
const { showToast } = useToasts();

useEffect(() => {
// shows a toast for 3 seconds
showToast("Hello, I'm a toast message!", 3000)
}, [])

return (
<ExampleComponent/>
)
};

useVoice

Description

The useVoice hook allows you to track and manage the chatbot's voice functionality.

Return Values

NameTypeDescription
voiceToggledOnboolean Indicates if the chatbot voice is currently on or off.
toggleVoicefunctionToggles the chatbot voice on or off.

Code Example

import { useVoice } from "react-chatbotify";

const MyNestedComponent = () => {
const { toggleVoice } = useVoice();

return (
<button onClick={toggleVoice}></button>
)
};