Genius Chat Headless Chat SDK

The Genius Chat SDK allows you to build customised chat experiences powered by DigitalGenius.

Getting Started

Include your configuration and the SDK script in your HTML just before the closing body tag:

<!DOCTYPE html>
<html>
  <head></head>
  <body>
    <script>
      window.DG_SDK_CONFIG = {
        widgetId: 'your-widget-id',
        env: 'eu', // 'eu', 'us', or 'dev.us'
        generalSettings: {
          persistSession: true // whether the SDK reconnects to an existing session on page reload
        }
      }
    </script>
    <script src="https://chat.digitalgenius.com/init-sdk.js"></script>
  </body>
</html>

Initialisation

Initialise the SDK and subscribe to events

// Ensure the browser has loaded the SDK script
window.addEventListener('DGSDKScriptLoaded', () => {
  // Initialise the SDK
  window.DGChatSDK.init();

  // Fires once the SDK has initialised. You can now start using the SDK methods
  window.DGChatSDK.onReady(() => {
    console.log('DG Chat SDK is ready');

    window.DGChatSDK.startConversation();
  });

  // Subscribe to messages
  window.DGChatSDK.onMessage((message) => {
    console.log('Received message:', message);
  });

  // Subscribe to system events
  window.DGChatSDK.onSystemEvent((event) => {
    console.log('System event:', event);
  });
});

Methods

init

Initialises the SDK and establishes WebSocket connection. Must be called after the DGSDKScriptLoaded event.

window.DGChatSDK.init();

startConversation

Initiates a new conversation with the bot.

window.DGChatSDK.startConversation();

endConversation

Ends the current conversation, disconnects WebSocket, and clears session data.

window.DGChatSDK.endConversation();

restartConversation

Restarts the current conversation, returning to the beginning of the flow.

window.DGChatSDK.restartConversation();

sendTextMessage

Sends a text message from the user to the bot.

window.DGChatSDK.sendTextMessage('Hello, I need help with my order');

submitQuickReply

Submits a quick reply selection. The entire selected quick reply object should be passed.

window.DGChatSDK.submitQuickReply(selectedQuickReplyObject);

Event Listeners

onReady

Fired when the SDK is ready for interaction.

window.DGChatSDK.onReady(() => {
  console.log('SDK is ready');
});

onMessage

Fired when the bot sends a message.

window.DGChatSDK.onMessage((message) => {
  console.log('Received message:', message);

  switch (message.type) {
    case 'message':
      displayTextMessage(message);
      break;
    case 'carousel':
      displayCarousel(message);
      break;
    case 'quick_reply':
      displayQuickReplies(message);
      break;
  }
});

Supported Message Types:

  • message - Text messages
  • carousel - Product carousels
  • select_dropdown - Dropdown selections
  • quick_reply - Quick reply buttons
  • calendar - Calendar picker
  • form - Form inputs
  • list - List selections
  • attachment - File attachments
  • crm_handover - CRM integration handover
  • batch - Batch messages
  • order_tracker - Order tracking information
  • video - Video messages
  • return_overdue - Return process messages
  • advanced - Advanced message

onSystemEvent

Fired when the bot sends system events for chat state changes.

window.DGChatSDK.onSystemEvent((event) => {
  console.log('System event:', event);
  handleSystemEvent(event);
});

System Events

The SDK emits various system events to inform you about chat state changes:

General Events

  • typing_start - Bot / Agent started typing
  • typing_stop - Bot / Agent stopped typing
  • joined - Helpdesk agent joined
  • disconnect - Helpdesk agent disconnected
  • agent_joining - Helpdesk agent is joining
  • agent_transferred - Helpdesk agent transferred user to another agent
  • timeout - Chat session timed out (default: 24 hours)
  • system_message - Generic system message from the flow
  • uploadFileRequest - Flow requests file upload
  • uploadPictureRequest - Flow requests image upload
  • error - Error occurred in the flow
  • helpdesk_handover - Handover to helpdesk initiated
  • handover_error - Error during handover process
  • queue_update - Queue position updated
  • csat_request - CSAT feedback requested
  • unassigned - Agent unassigned from chat

Salesforce-Specific Events

  • idle_timeout_countdown - Warning before idle timeout
  • idle_timeout - Idle timeout reached
  • file_transfer_request - Agent requests file upload
  • file_transfer_cancel - Agent cancels file upload request