How to use AI Widget Client-side Functions
This tutorial guides you through the process of leveraging the new Client-side Functions feature in the ChatBotKit AI Widgets. We'll start by embedding the widget and then focus on defining client-side functions to enhance interactivity and functionality.
Embedding the Widget
First, ensure your widget is properly embedded within your web application using the ChatBotKit Widget SDK. You can embed the widget by including the following script tag in your HTML:
To pass additional parameters, use data attributes like so:
If no data-widget
property is present, the SDK initializes but does not instantiate any widgets. To instantiate a widget manually, use the custom HTML tag:
Implementing Client-Side Functions
Once your widget is embedded, you can enhance its capabilities using client-side functions. These functions allow you to provide dynamic data directly to the AI agent. Hereβs how you can define a simple client-side function to provide the user's current location to the AI bot:
In this example, we've used a static initialization approach where result.data
directly holds the value. This function allows the AI to access the user's current location dynamically, ensuring the widget can respond to changes in the user's environment or inputs.
Alternative Implementation Methods
While using the AI Widget SDK is the preferred method for setting up the widget, as it handles most of the configuration for you, there are situations where using the SDK may not be feasible. One common example is when you need to embed the widget directly as a frame, which is often the case with mobile applications.
In the absence of the Widget SDK, you can still assign client-side functions using the AI Widget postMessage protocol. This section discusses this process in detail.
Direct Embedding for Mobile Frameworks
First, embed the AI Widget Frame directly using the following method.
Mobile Embedding Variations
Note that you can also use other direct frame embedding techniques, such as WebKit WebViews but you need to adjust the code accordingly. We use iframes and JavaScript for illustrative purposes. Feel free to contact us if you need a concrete example for your framework of choice.
This will add the widget frame to the page, allowing you full control over the experience.
Next, declare your client-side functions:
Finally, to assign the functions, send a postMessage
event to the frame:
This event must be sent after the frame is fully loaded. One way to do this is:
By following these steps, you can successfully implement the AI Widget using direct frame embedding and assign client-side functions using the postMessage protocol.
Extending Functionality
You can define additional client-side functions based on your application's needs. Each function can interact with different parts of your application, such as fetching user settings, updating UI elements, or even handling API requests. For more complex functionalities and other forms of function declarations, please refer to the detailed documentation in the Widget Documentation.
By integrating client-side functions, you can make your AI widgets more responsive and intelligent, providing a seamless experience for users interacting with your application.