Making Live Preview Component in SUSI.AI Botbuilder

SUSI botbuilder enables users to make their own private skill and deploy a chatbot widget in their websites. While creating the private skill and designing the bot, the user can see a live preview of the chatbot including its designs in a component on the right side. This component is made in react for updating the theme on the fly. It looks exactly similar to the actual chatbot widget which will be deployed on the user’s website. This blog explains how the preview component is made and how the theme is updated instantly.

Passing skill and design data to preview

The skill’s data is stored in the skill language in text format. This skill data needs to be passed to the preview component so that it can use it. Also the design settings are stored as state variables. These data are passed to the preview component as props. These data are passed as designData and skill props. Inside the Preview component, they are used for applying the private skill in the preview chat and applying the design.



Fetching chat response

The preview component needs to fetch response from the server for the latest private skill typed by the user. For this, we use the instant feature of the SUSI chat API. We pass the entire text of the skill code inside this instant parameter and also the query parameter. The server applies this skill and sends the appropriate response.

// Send request to SUSI API
send = text => {
  let url = urls.API_URL + '/susi/chat.json?q=' + encodeURIComponent(text);
  url += '&instant=' + encodeURIComponent(this.props.skill);
  var thisMsgNumber = this.msgNumber;
    type: 'GET',
    url: url,
    contentType: 'application/json',
    dataType: 'json',
    success: function(data) {
      this.main(data, thisMsgNumber);
    error: function(e) {
      this.main(null, thisMsgNumber);


Applying theme dynamically

The preview component has to update the theme of the chatbot dynamically based on user’s settings. The applyTheme function updates the theme of the chatbot by changing the CSS of the chatbot elements dynamically.

// to apply custom theme
applyTheme = () => {
    // user message container
    $('.susi-comment-by-user .susi-comment-body-container').css(
        `<style>.susi-comment-body-container-user:after {
          border-color: transparent transparent ${
          } ${this.state.botbuilderUserMessageBackground} !important}</style>`,
    $('.susi-comment-by-user .susi-comment-body-container').css(
    // bot message container
    $('.susi-comment-by-susi .susi-comment-body-container').css(
    $('.susi-comment-by-susi .susi-comment-body-container').css(
      "url('" + this.state.botbuilderIconImg + "')",