Trulience Docs
iFrame

Developer

iFrame

The simplest way to embed a Trulience avatar in your website

Adding an Avatar iFrame to a Web Page

Trulience avatars can be embedded in a web page using an iFrame:

<iframe 
    height="600px"
    src="https://www.trulience.com/avatar/<your-avatar-id>"
    allow="camera; microphone; fullscreen; accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"
    allowfullscreen>
    frameborder="0" 
</iframe>

Here’s how this embed code looks in action:

Authentication

By default, newly created avatars are configured to use a default encryption key. This prevents abuse of your avatar by restricting who can access it.

The default Trulience encryption key won’t work when embedding your avatars elsewhere. We use this key to protect your avatars when they are accessed, for example by pressing the CONNECT button in the dashboard. You will need to adjust your avatars’ encryption setting in the ADVANCED tab to either:

  • Set encryption to ‘None’ - this disables authentication entirely and allows anyone to access your avatar.
  • Restrict access using your own encryption keys - you will need to implement backend code to generate corresponding JWT tokens.

If you wish to retain authentication on your site you will have to generate your own token and append it onto the iFrame’s src attribute like this:

<iframe src="https://www.trulience.com/avatar/<your-avatar-id>?token=<your-token>"></iframe>

Setting encryption to ‘None’ means you can omit the token from the URL.

Configuring the UI

You will notice that the avatar embed comes with some inbuilt UI. You can configure this UI by appending query parameters to the avatar URL or by editing your avatars’ client JSON configuration via the dashboard.

Configuration Parameters

The following parameters can be used to configure the behaviour of the iframe:

ParameterDescription
connectIf set to true, the client will connect automatically when visiting the avatar link - bypassing the ‘dial’ screen
micOffIf set to true, the microphone will be muted by default
speakerOffIf set to true, the speaker will be muted by default
hideFSIf set to true, hides the full screen button
hideChatInputIf set to true, hides the chat input box
hideChatHistoryIf set to true, hides the chat history
hideLetsChatBtnIf set to true, hides the dial button
hideMicButtonIf set to true, hides the microphone button preventing the users from being able to mute/unmute
hideHangUpButtonIf set to true, hides the hang up button
hideSpeakerButtonIf set to true, hides the speaker button
dialButtonTextText that appears on the dial button
msgOnConnectMessage (string) sent to the avatar on connection
screenAspectRatioSets the aspect ratio of the visible video area e.g. 16:9, 4:3, 1:1 etc.
chatInputBoxWidthCan be set to the width of the visible video area above, using window
showLogoIf set to true, the client will show either the default Trulience logo or the provided logoSrc
logoPositionCan be right or left
logoSrcThe https location of the logo image to be displayed in top right or left of visible video window
registerTrlEventsList of events that should be notified to iframe’s parent as and when they occur
fullscreenIf set to true, the avatar will be displayed in fullscreen mode
disableDraggingIf set to true, dragging to resize the avatar will be disabled
tokenProvide the JWT token to verify that the client sending the request is valid. Requires setup with an encryption key and creating a JWT token.
controlButtonPositionCan be center, right or left
hideToastIf set to true, the client will not display toast alerts

Adjusting Colour

Here’s a list of colour attributes you can adjust using CSS colour strings, e.g. #ffffff or pink:

  • dialPageBackground
  • dialButtonTextColor
  • dialButtonBackground
  • chatScreenBGColor
  • userChatBubbleBGColor
  • avatarChatBubbleBGColor
  • userChatBubbleBorderColor
  • avatarChatBubbleBorderColor
  • userChatBubbleTextColor
  • avatarChatBubbleTextColor
  • inputBoxBGColor
  • inputBoxBorderColor
  • inputBoxTextColor
  • sendButtonBGColor
  • sendButtonArrowColor
  • sendButtonBorderColor
  • borderColorBetweenInputAndScreen
  • overlayButtonColor
  • loadingBarColor

Example

Here’s an example, demonstrating styling using query params:

<iframe 
    height="700px" 
    allow="camera; microphone; fullscreen" 
    src="https://www.trulience.com/avatar/<id>?chatScreenBGColor=#e7e7e7&userChatBubbleBGColor=#ff6200&avatarChatBubbleBGColor=#000000&userChatBubbleBorderColor=none&avatarChatBubbleBorderColor=none&userChatBubbleTextColor=white&avatarChatBubbleTextColor=white&inputBoxBGColor=#fff&inputBoxBorderColor=#fff&inputBoxTextColor=inherit&sendButtonBGColor=white&sendButtonArrowColor=#000000&sendButtonBorderColor=none&borderColorBetweenInputAndScreen=#f0f0f0" 
    frameborder="0"
    allow="camera; microphone; fullscreen; accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"
    allowfullscreen>
</iframe>

iFrame messaging

Behind the scenes, the iFrame embed is loading the Trulience SDK. You can interact with the SDK through iFrame messaging.

Listening to SDK Events

You can register which SDK events you want to listen to by appending them to your iFrame’s src attribute via a query param, for example:

<iframe src="https://www.trulience.com/avatar/<id>?registerTrlEvents=auth-success,auth-fail,mic-update,speech-recognition-start,speech-recognition-end,speech-recognition-final-transcript" />

Extra iFrame Events

On top of the existing SDK events, the iframe also dispatches the following events:

Event NameDescriptionParams
trl-chatFired when a chat message is received from the server.{message, agentName, sttResponse, senderType}
trl-respond-avatar-photo-urlFired in response to the trl-request-avatar-photo-url request from the iframe’s parent.URL pointing to the photo of the avatar.

Please refer to our SDK documentation to find out more about events like auth-success.

Example

You can listen to iFrame events like this:

window.addEventListener("message", (event) => {
    
    // Verify event.origin as necessary.
    if (event.origin) {
        // Test the origin here and return if it does not match the expected value.
    }

    if (event.data !== null && event.data !== undefined) {
        let eventData = event.data;
        console.log("Event name = " + eventData.name + " | Event parameters = " + eventData.params);
    }
}, false);

Sending Events

The iframe listens and reacts to the following events:

Message NameDescriptionParams
trl-request-avatar-photo-urlRequest for a link to the avatar photo that is set in the avatar configN/A
trl-unregister-eventsSend this to unregister for notifications of various events registered in the iframe Source URL.Comma-separated list of event names to unregister, ideally matching those registered.
trl-chatSend a message for Trulience to process and make the avatar speak the responseText to be processed by Trulience.
trl-mic-statusSend this message to mute/unmute the mictrue to unmute, false to mute the mic.
trl-set-speaker-statusSend this message to mute/unmute the speakertrue to unmute, false to mute the speaker.
end-callMessage to terminate the ongoing session with the avatarN/A

Example

You can define functions to send iFrame events like this:

const sendChatMessage = (message) => {
    const dataToSend = { "command" : "trl-chat", "message" : message};
    const iframe = document.getElementById('iframeId');
    iframe.contentWindow.postMessage(dataToSend, "*");
}