Initial language detection with a Welcome message

Hi,

We have upgraded our bot to support French in addition to the original English. We have a welcome message “Hi how can we help?” trigger by the OnConnect event from the Web channel. We can switch language using the cheat code or detected from a user utterance, and our Welcome task works fine in both languages for existing users.

When a NEW user connects to our bot, Kore prompts for what language to use (English or French). This is unacceptable. We would prefer to use the Browser language by default for new users. How can we pass a language selection in the Webkit JS to Kore?

This problem can be easily demonstrated by connecting to a multi-lingual bot in Incognito mode in Chrome.

The documentation suggests the bot is using Kore’s in-house language detection algorithm. If so, what factors does this consider for new, never seen before, no utterance yet received, users?

Paul

Minor update: Following another publish of the bot, it seems to now default to always English for new users in the OnConnect message, rather than asking what language to use. Unsure what changed that.

We have also explored the meta field within BotUserSession, which has a language field. This seems to successfully show the users browser/OS language, but only after the first utterance is received. The OnConnect dialog only sees some previous unreliable cached value.

@paul.osborn
There may be other implementations but here is what I found as a possible solution.

I am assuming that

  1. You have a bot with English and French enabled.
  2. The bot’s ‘on connect event’ calls a task which displays a just message.
  3. In the respective English and French versions of the dialog task, you have proper translations to show the appropriate message in the selected language.
  4. You know how to set up BotKit and WebSDK.

That said, when the user is initiating the conversation with the bot, we need to get something from the web-SDK to know which language to set, before the task sets in. Then tap it in the BotKit (middleware) and set the bot language as per the attribute received from the web-SDK.

Steps:

  1. Set a customData (Under botOptions.botInfo) which will automatically read and be put in the bot context (do not use interactiveLanguage key:
    "customData": { "initialLanguage": "fr"}
    image
  2. This customData (say initialLanguage) will be available in the BotKit under two paths. data.context.session.UserContext.customData.initialLanguage and data.context.session.BotUserSession.lastMessage.messagePayload.botInfo.customData.initialLanguage depending on whether a user is completely new to the platform (this depends on botOptions.userIdentity)
  3. The on_user_message code should check for the data.channel.botEvent and if the event is ON_CONNECT_EVENT, try to set the botLanguage through metaInfo.
    on_user_message: function (requestId, data, callback) {
    console.log("on_user_message > requestId: " + requestId +
    "\n data.context.currentLanguage: " + data.context.currentLanguage);
    console.log(new Date() + "message: " + JSON.stringify(data.message));
    //HANDLE ON CONNECT EVENT CUSTOM DATA INITIAL LANGUAGE
    var initLangInUserContext = false;
    var initLangInBotUserSessionContext = false;
    if (data.context.session.UserContext.customData && data.context.session.UserContext.customData.initialLanguage) {
    console.log("data.context.session.UserContext.customData.initialLanguage: " + data.context.session.UserContext.customData.initialLanguage);
    initLangInUserContext = true;
    } else {
    console.log(“An element in the path data.context.session.UserContext.customData.initialLanguage does not exist”)
    }
    if (data.context.session.BotUserSession.lastMessage
    && data.context.session.BotUserSession.lastMessage.messagePayload.botInfo.customData
    && data.context.session.BotUserSession.lastMessage.messagePayload.botInfo.customData.initialLanguage) {
    console.log("data.context.session.BotUserSession.lastMessage.messagePayload.botInfo.customData.initialLanguage: " + data.context.session.BotUserSession.lastMessage.messagePayload.botInfo.customData.initialLanguage);
    initLangInBotUserSessionContext = true;
    } else {
    console.log(“An element in the path data.context.session.BotUserSession.lastMessage.messagePayload.botInfo.customData.initialLanguage does not exist”)
    }
    if (data.channel.botEvent === ‘ON_CONNECT_EVENT’
    && (initLangInUserContext || initLangInBotUserSessionContext)
    ) {
    var initLang = (initLangInBotUserSessionContext) ? data.context.session.BotUserSession.lastMessage.messagePayload.botInfo.customData.initialLanguage : data.context.session.UserContext.customData.initialLanguage;
    console.log("ON_CONNECT_EVENT > SETTING data.metaInfo.setBotLanguage to " + initLang);
    console.log("ON CONNECT DATA OBJECT => " + JSON.stringify(data));
    data.metaInfo = {
    setBotLanguage: initLang
    };
    }
    return sdk.sendBotMessage(data, callback);
    }

    image
  4. Now when initialLanguage in WebSDK is set to ‘fr’ The bot shows below greeting message.
    image
  5. When initialLanguage in WebSDK is set to ‘en’ The bot shows below greeting message.
    image

I hope this helps achieve your use case.

1 Like