Connect chat translation is not working in chat board

anbarasanaa
Tera Contributor

Hi Everyone, 

 

Our requirement:
 When an end user initiates a chat conversation with a live agent from service portal Chameleon API is called from ServiceNow.  Chameleon does the language translation and sends the translated message as a response back to ServiceNow which the end user will be able to see it.   

(Mainly using for communication between end users and support agents within the ServiceNow Connect Chat interface by leveraging Chat Chameleon, a third-party SaaS platform that provides real-time translation services via REST APIs. Chat Chameleon facilitates the users to communicate with the ServiceDesk Agents in their native language.  It translates the messages from user (in native language) to ServiceDesk Agent Language (English). 

By translating each message in real time, the solution ensures that both parties can converse naturally in their native languages while maintaining the context, tone, and intent of the conversation).

 

Issue we are facing: 

Using the below script in business rule we are getting response from API and message also got translated as per the below screenshot. In system logs we can see the integration is working fine. But in chat board we are not able to see the translated message(text)attaching the screenshot for your references. PFA

 

Table name/ business rule: live_message

 

(function executeRule(current, gS) {
    if (current.operation() !== "insert" || !current.chat_message)
        return;

    try {
        var senderId = current.profile.document + '';
        var senderLang = 'es'; // default
        gs.info("[ChatLog] Sender ID: " + senderId);

        var senderGR = new GlideRecord('sys_user');
        if (senderGR.get(senderId) && senderGR.preferred_language) {
            senderLang = senderGR.preferred_language + '';
            gs.info("[ChatLog] Sender preferred language: " + senderLang);
        }

        // Get one recipient from the group
        var targetLang = 'en'; // default
        var groupGR = new GlideRecord('live_group_member');
        groupGR.addQuery('group', current.group);
        groupGR.query();
        while (groupGR.next()) {
            var memberId = groupGR.member + '';
            if (memberId !== senderId) {
                var userGR = new GlideRecord('sys_user');
                if (userGR.get(memberId) && userGR.preferred_language) {
                    targetLang = userGR.preferred_language + '';
                    gs.info("[ChatLog] Found recipient with language: " + targetLang);
                    break;
                }
            }
        }

        if (senderLang === targetLang) {
            gs.info("[ChatLog] Sender and target language are the same. Skipping translation.");
            return;
        }

        // --- Step 1: Get Access Token ---
        var oAuthClient = new sn_auth.GlideOAuthClient();
        var tokenRequestBody = {
            "grant_type": "client_credentials",
            "resource": "api://78d10c57-e1a5-4587-8a3f-532fa04156a2",
            "client_id": "78d10c57-e1a5-4587-8a3f-532fa04156a2",
            "client_secret": "4H68Q~CzcEWQVll3MOM1C.QEiszdg9aJE6MkPcDL"
        };

        var tokenResponse = oAuthClient.requestToken("Chat Chameleon", JSON.stringify(tokenRequestBody));
        var accessToken = tokenResponse.getToken();

        if (!accessToken) {
            gs.error("[ChatLog] No access token received from OAuth");
            return;
        }
        gs.info("[ChatLog] Access token received");

        // --- Step 2: Call Chameleon API ---
        var r = new sn_ws.RESTMessageV2('Chat Chameleon_rest', 'Default Post');
        r.setRequestHeader('Authorization', 'Bearer ' + accessToken);
        r.setRequestHeader('Content-Type', 'application/json');

        var requestPayload = {
            OrgName: "Nissan",
            From: senderLang,
            To: targetLang,
            Message: current.formatted_message + ''
        };

        gs.info("[ChatLog] Sending payload: " + JSON.stringify(requestPayload));
        r.setRequestBody(JSON.stringify(requestPayload));
        var response = r.execute();
        var responseBody = response.getBody();
        var status = response.getStatusCode();

        gs.info("[ChatLog] API Status: " + status);
        gs.info("[ChatLog] API Response: " + responseBody);

        if (status === 200) {
            var jsonResponse = JSON.parse(responseBody);
            var translated = jsonResponse.TranslatedMessage || '';

            current.u_translationtext = translated;
            current.message = translated;
            current.formatted_message = translated;

            // Call current.update() to save the updated fields to the record
            current.update();

            gs.info("[ChatLog] Message translated: " + translated);
            gs.addInfoMessage("Translated message: " + translated);
        } else {
            gs.error("[ChatLog] Translation API failed. Status: " + status + " Body: " + responseBody);
            gs.addErrorMessage("Translation failed with status: " + status);
        }
    } catch (ex) {
        gs.error("[ChatLog] Translation error: " + ex.message);
    }

})(current, gs);

 

anbarasanaa_1-1744783922393.png

 

 

anbarasanaa_0-1744783881653.png

 

0 REPLIES 0