setBotAuthTokenProvider

Omnichannel for Customer Service offers a suite of capabilities that extend the power of Dynamics 365 Customer Service Enterprise to enable organizations to instantly connect and engage with their customers across digital messaging channels. An additional license is required to access Omnichannel for Customer Service. For more information, see the Dynamics 365 Customer Service pricing overview and Dynamics 365 Customer Service pricing plan pages.

The bot authentication token provider function, when called with a callback function as a parameter, returns the botAuthUrlto send the token to the bot and invokes the callback function to set the sign-in card visibility.

Syntax

Microsoft.Omnichannel.LiveChatWidget.SDK.setBotAuthTokenProvider(authTokenProvider);

Parameters

Parameter Type Description
authTokenProvider Function Function which when invoked with a callback function as parameter, returns the botAuthUrl to send the token to the bot and invokes the callback function to set the sign-in card visibility.

Return value

Bot authentication URL.

Example

Here's a code sample that illustrates how you can pass an authentication token to an Azure or Copilot Studio bot during single sign-on.

const signInIds = [];
window.Microsoft.Omnichannel.LiveChatWidget.SDK.setBotAuthTokenProvider(async (botTokenUrl, callback) => {
  const urlSearchParams = new URLSearchParams(botTokenUrl);
  const signInId = urlSearchParams.get("state");

  if (signInIds.includes(signInId)) { // Ignore authenticated sign-in cards
    callback({show: false});  // Hide card
    return;
  }

  signInIds.push(signInId);

  const authUrl = ""; // Customer's Authentication API
  const authResponse = await fetch(authUrl, method: "POST"});
  const {token} = authResponse;  // Customer's Auth Token

  const data = {
    token: "token"
  };

  const payload = {
    method: "POST",
    headers: {
      "Content-Type": "application/json"
    },
    body: JSON.stringify(data)
  };

  try {
    const botAuthResponse = await fetch(botTokenUrl, payload); // Posts Auth Token to Bot directly
    
    // Sign in through Bot is successful
    if (botAuthResponse.status === 200) {
      signInIds.push(signInId); // Track authenticated sign-in card
      callback({show: false}); // Hide card
    }

    if (botAuthResponse.status === 404 || botAuthResponse.status == 202) {
      callback({show: false}); // Hide card
      return;
    } else {
      // Other condition handling    
    }

    return;
  } catch (error) {
  
  }

  callback({show: true});  // Show sign-in card by default
});

JavaScript API reference for live chat SDK
Pass authentication token to bot during single sign-on in live chat