Programmatic control of the Aparsoft chatbot widget via hooks, events, and the global controller.
When the widget loads, it registers window.AparsoftChatbot as the global controller instance.
| Method | Signature | Description |
|---|---|---|
open() |
() => void |
Open the chat panel |
close() |
() => void |
Close the chat panel |
toggle() |
() => void |
Toggle the chat panel open/closed |
sendMessage(message) |
(message: string) => void |
Send a message and open the chat panel |
destroy() |
() => void |
Remove the widget from the page and clean up |
| Property | Type | Description |
|---|---|---|
isReady |
boolean |
Whether the widget has finished initializing |
state |
string |
Current widget state ('open', 'closed') |
import { useAparsoftChatbot } from '@aparsoft/chatbot-react';
// or
import { useAparsoftChatbot } from '@aparsoft/chatbot-nextjs';| Property | Type | Description |
|---|---|---|
getWidget() |
() => AparsoftChatbotInstance | null |
Get the raw controller instance |
openChat() |
() => void |
Open the chat panel |
closeChat() |
() => void |
Close the chat panel |
toggleChat() |
() => void |
Toggle the chat panel |
sendMessage(message) |
(message: string) => void |
Programmatically send a message |
import { useAparsoftChatbot } from '@aparsoft/chatbot-react';
function SupportButton() {
const { openChat, sendMessage } = useAparsoftChatbot();
return (
<button onClick={() => {
sendMessage('I need help with my order');
openChat();
}}>
Get Support
</button>
);
}import { useAparsoftChatbot } from '@aparsoft/chatbot-react';
function PricingPage() {
const { openChat, sendMessage } = useAparsoftChatbot();
return (
<div>
<h1>Pricing</h1>
<button onClick={() => {
sendMessage('Can you explain the Pro plan benefits?');
openChat();
}}>
Ask about Pro Plan
</button>
</div>
);
}The Vue component emits a ready event with the controller instance:
<template>
<AparsoftChatbot
api-key="cb_your_api_key"
@ready="onWidgetReady"
/>
</template>
<script>
export default {
methods: {
onWidgetReady(controller) {
controller.open();
controller.sendMessage('Welcome from Vue!');
}
}
};
</script>The Angular component emits ready and error outputs:
@Component({
selector: 'app-root',
standalone: true,
imports: [AparsoftChatbotComponent],
template: `
<aparsoft-chatbot
apiKey="cb_your_api_key"
(ready)="onReady($event)"
(error)="onError($event)"
></aparsoft-chatbot>
`,
})
export class AppComponent {
onReady(controller: AparsoftChatbotInstance) {
controller.open();
}
onError(error: Error) {
console.error('Chatbot error:', error);
}
}The widget dispatches browser custom events that you can listen to from any context:
Fired when the widget finishes initializing.
window.addEventListener('aparsoft-chatbot:ready', (event) => {
const controller = event.detail.controller;
console.log('Widget ready', controller);
});Fired when the widget state changes (open/closed).
window.addEventListener('aparsoft-chatbot:state', (event) => {
console.log('Widget state:', event.detail.state);
});The Next.js and React packages export TypeScript interfaces:
import type { AparsoftChatbotConfig, AparsoftChatbotInstance } from '@aparsoft/chatbot-nextjs';interface AparsoftChatbotConfig {
apiKey: string;
position?: 'bottom-right' | 'bottom-left';
showBranding?: boolean;
autoOpenDelayMs?: number;
configEndpoint?: string;
websocketUrl?: string;
primaryColor?: string | null;
secondaryColor?: string | null;
widgetTitle?: string | null;
widgetSubtitle?: string | null;
welcomeMessage?: string | null;
title?: string | null;
}interface AparsoftChatbotInstance {
isReady?: boolean;
state?: string;
open?: () => void;
close?: () => void;
toggle?: () => void;
sendMessage?: (message: string) => void;
destroy?: () => void;
}