DialogFlow CX - Is it possible to switch languages mid-conversation with the Agent?

201 Views Asked by At

In DialogFlow CX, I want to create a multilingual agent that will start the conversation in English (default) and then prompt the user for their language and continue the conversation with the language they selected (if valid). Eventually this Agent will be connected to the Twilio Telephony connector but for the time being I tried to approach this issue via webhooks using Google Cloud Functions.

To start I added English and Spanish to a flow I created via the DialogFlow GUI. I added a Webhook to the page where I prompt "Would you like to try Spanish", if they select yes, the following code is called:

const agentId = 'agent-id';
const locationId = 'location-id';
const projectId = 'project-id';
const query = 'Hola';
const languageCode = 'es';

// Imports the Google Cloud Some API library
const {SessionsClient} = require('@google-cloud/dialogflow-cx');
const client = new SessionsClient();

exports.handleWebhookRequest = async (req, res) => {
        // Parse the request body
        const {fulfillmentInfo, sessionInfo} = req.body;
       
        // Extract the session ID
        const sessionId = sessionInfo.session.split('/').pop();

    async function detectIntentText(sessionId) {
      const sessionPath = client.projectLocationAgentSessionPath(
        projectId,
        locationId,
        agentId,
        sessionId
      );

      console.log('SessionPath: ' + sessionPath);

      const request = {
        session: sessionPath,
        queryInput: {
          text: {
            text: query,
          },
          languageCode : languageCode,
        },
      };
      const [response] = await client.detectIntent(request);
      for (const message of response.queryResult.responseMessages) {
        if (message.text) {
          console.log(`Agent Response: ${message.text.text}`);
        }
      }
      if (response.queryResult.match.intent) {
        console.log(
          `Matched Intent: ${response.queryResult.match.intent.displayName}`
        );
      }
      console.log(
        `Current Page: ${response.queryResult.currentPage.displayName}`
      );
    }

    detectIntentText(sessionId);
}

In Google log explorer, the session path looks correct and the JSON Payload shows:

  jsonPayload: {
    queryInput: {
      languageCode: "es"
      text: {
       text: "Hola"
      }
  }
  ...

It also shows the logged 'Agent Response' with the expected responses in Spanish. However in the Test Simulator, it just goes to the next English page.

I also tried transitioning the Agent to a specified page:

exports.handleWebhookRequest = async (req, res) => {
    const response = {
        queryInput: {
          text: {
            text: 'Hola',
          },
          languageCode : 'es',
        },
        target_page : 'projects/project-id/locations/location-id/agents/agent-id/flows/flow-id/pages/page-id',    
    };

  res.json(response);
}

This transitioned to the page I wanted but only the English version.

0

There are 0 best solutions below