Build a website assistant
Add one Convai character to every page of your website that knows which page the visitor is on and can navigate, highlight elements, and fill in forms for them.
In this tutorial we will add a website assistant to a multi-page site: one Convai character that follows the visitor from page to page, answers questions about what is on screen, and acts on requests such as "take me to pricing" or "put my email in the contact form". By the end, you will have an assistant panel on every page that moves the visitor around the site by text or voice.
What we will build
We will build an assistant for a sample site with a Home, Pricing, Docs, and Contact page. The assistant does four things:
Knows where the visitor is. Each time the page changes, the site sends the character the page name, its elements, and its text.
Acts on the site. The character chooses actions such as
Navigate To Pricing pageorHighlight Start free trial button, and the site runs them.Hears back. After running an action, the site tells the character what happened, so its next reply is accurate.
Keeps one conversation. The visitor keeps talking to the same session across page changes and reloads.
Your application stays responsible for every action: the character only names one, and your code decides whether and how to run it.
The steps use framework-free TypeScript and a History API router. The same calls work in React, Vue, or a server-rendered site. See Use it in React for the React version.
Prerequisites
Convai Web SDK 1.9.4 or later. See Web SDK quickstart.
A Convai character and an API key. For production, connect with an auth token instead of an API key. See Connect with an auth token.
The pages of your site and the elements the assistant may point at.
The assistant works with Convai and with self-hosted deployments, over the default realtime transport and over the SSE transport.
Describe your site
List the pages and elements the assistant may use. Each name is what the character says when it picks an action target, so make the names short and unambiguous. Each description helps the character choose.
Declare the actions
Pass the actions and their possible targets in actionConfig. Pages and elements both go into objects, so the character can target either one.
The configuration is sent when the session connects, so declare every page and element the assistant may need, not only those on the current page. For the full action contract, see Handle character actions.
Tell the character where the visitor is
Write a function that describes the current page. Start it with the assistant's role, because each update replaces the previous dynamic info as a whole.
Send the description on connect with dynamicInfo, then again with updateDynamicInfo whenever the page changes. updateDynamicInfo does nothing until the session is ready, so also send it on botReady.
Keep the page text short. A summary of what is visible is enough for the character to answer questions about it.
Keep one conversation across pages
endUserId identifies the visitor, and characterSessionId resumes their conversation. Store the visitor ID for good, and store the session ID for the browser tab, so a reload or a full page load continues the same conversation.
In a single-page app, create the client once at the root and keep it connected while the visitor moves between routes. In a site with full page loads, create it on every page: the stored session ID picks the conversation up where it stopped. The client's chat messages start empty on each connection, so the panel is blank after a full page load while the character still remembers the conversation. To show earlier turns, keep your own copy of the transcript in sessionStorage. To give a signed-in visitor long-term memory, use their account ID as endUserId. See Use long-term memory.
Run the actions
Subscribe to actionResponse. Each event carries the actions for one turn in order, so run them one after another.
Convai can also return the target inside the action name, as in Navigate To: Contact page with no target. normalize splits a declared action name from the rest, so both shapes run the same way. runActions in Report what happened applies it before runAction:
The other helpers scroll an element into view and outline it, and type a value into a field:
Match targets against your own lists and ignore anything else, so the character can only reach pages and elements you declared. Leave irreversible steps, such as submitting a form or making a payment, to the visitor: highlight the button instead of pressing it.
Read values from the visitor's message
An action target names one of your declared objects. A free-form value, such as an email address the visitor dictates, is not a declared object, so the action can arrive without a target. Keep the visitor's last message and read the value from it when the target is missing.
Call rememberVisitorText before sendUserTextMessage, and from messagesChange for spoken requests, as the next section shows. pageNamedIn covers the occasional Navigate To that arrives without a target.
Report what happened
After the actions run, append the results to the character's context without asking for a reply. The character reads them on the next turn, so it can confirm what happened or try again.
Without results, a character that chose an action cannot tell whether it ran, and it may wrongly tell the visitor it did. For the other updateContext options, see Update character context at runtime.
Add the chat panel
Render the conversation, send typed messages, and let the visitor talk. AudioRenderer plays the character's voice.
Browsers block sound until the visitor clicks, taps or presses a key on the page, and the client usually connects before that. AudioRenderer handles this: when the browser blocks playback, it starts the voice on the visitor's first interaction, such as typing a message or pressing the microphone button.
The panel is plain HTML: an ordered list for the transcript and a form with a microphone button, a text input, and a Send button. Fix it to the side of the page so it stays put while the page underneath changes.
Try the assistant
Start your site, wait for the panel to connect, and try these requests in order:
"Take me to the pricing page." The site opens the Pricing page.
"What does the Team plan cost?" The character answers from the page text.
"Show me the free trial button." The button scrolls into view and is outlined.
"Go to the contact page and put jane@example.com in the email field." The site opens the Contact page and fills in the email field, from one request.
"Did that work?" The character confirms, based on the action results.
Select the microphone and say the same requests out loud. Spoken requests produce the same actions.
Use it in React
useConvaiClient takes the same options. Update the context from an effect that runs on route changes, and handle actions in another.
Mount the hook once in your root layout, so the conversation survives route changes.
Next steps
Handle character actionsUpdate character context at runtimeConnect with an auth tokenConnect to a self-hosted deploymentLast updated
Was this helpful?