dcsan
dcsan

Reputation: 12275

how to create Intents with Context from the DialogFlow API

I'm trying to batch create a bunch of intents, and I want to assign an Input context

As far as I can see this should not need a session as it's just a string context name. Like this in the GUI: enter image description here

The API call I make creates the intent, doesn't throw an error, but I can't get the contexts to show up.

Is there some weird format to the contexts parameter? I've exported the zip and looked at the JSON files, they're just an array of strings.

I've seen other code that seems to require a user conversation sessionId to create contexts. But the intents are global - not for a single conversation. And I assume these are just for tracking context within a single conversation session (or google astronaut engineering)

The data I'm POSTing looks like the below

There's a google example here that doesn't touch contexts https://cloud.google.com/dialogflow/es/docs/how/manage-intents#create_intent

I've tried contexts as various formats without success


  // this is the part that doesn't work
  // const contexts = [{
  //   // name: `${sessionPath}/contexts/${name}`,
  //   // name: 'test context name'
  // }]

  const contexts = [
    'theater-critics'
  ]
createIntentRequest {
  "parent": "projects/XXXXXXXX-XXXXXXXX/agent",
  "intent": {
    "displayName": "test 4",
    "trainingPhrases": [
      {
        "type": "EXAMPLE",
        "parts": [
          {
            "text": "this is a test phrase"
          }
        ]
      },
      {
        "type": "EXAMPLE",
        "parts": [
          {
            "text": "this is a another test phrase"
          }
        ]
      }
    ],
    "messages": [
      {
        "text": {
          "text": [
            "this is a test response"
          ]
        }
      }
    ],
    "contexts": [
      "theater-critics"
    ]
  }
}
Intent projects/asylum-287516/agent/intents/XXXXXXXX-e852-4c09-bda6-e524b8329db8 created

Full JS (TS) code below for anyone else


import { DfConfig } from './DfConfig'
const dialogflow = require('@google-cloud/dialogflow');

const testData = {
  displayName: 'test 4',
  trainingPhrasesParts: [
    "this is a test phrase",
    "this is a another test phrase"
  ],
  messageTexts: [
    'this is a test response'
  ]
}
// const messageTexts = 'Message texts for the agent's response when the intent is detected, e.g. 'Your reservation has been confirmed';

const intentsClient = new dialogflow.IntentsClient();

export const DfCreateIntent = async () => {

  const agentPath = intentsClient.agentPath(DfConfig.projectId);

  const trainingPhrases = [];

  testData.trainingPhrasesParts.forEach(trainingPhrasesPart => {
    const part = {
      text: trainingPhrasesPart,
    };

    // Here we create a new training phrase for each provided part.
    const trainingPhrase = {
      type: 'EXAMPLE',
      parts: [part],
    };

    // @ts-ignore
    trainingPhrases.push(trainingPhrase);
  });

  const messageText = {
    text: testData.messageTexts,
  };

  const message = {
    text: messageText,
  };

  // this is the part that doesn't work
  // const contexts = [{
  //   // name: `${sessionPath}/contexts/${name}`,
  //   // name: 'test context name'
  // }]

  const contexts = [
    'theater-critics'
  ]

  const intent = {
    displayName: testData.displayName,
    trainingPhrases: trainingPhrases,
    messages: [message],
    contexts
  };

  const createIntentRequest = {
    parent: agentPath,
    intent: intent,
  };

  console.log('createIntentRequest', JSON.stringify(createIntentRequest, null, 2))

  // Create the intent
  const [response] = await intentsClient.createIntent(createIntentRequest);
  console.log(`Intent ${response.name} created`);
}

// createIntent();

Upvotes: 0

Views: 949

Answers (1)

dcsan
dcsan

Reputation: 12275

update figured out based on this https://cloud.google.com/dialogflow/es/docs/reference/rest/v2/projects.agent.intents#Intent


  const contextId = 'runner'
  const contextName = `projects/${DfConfig.projectId}/agent/sessions/-/contexts/${contextId}`
  const inputContextNames = [
    contextName
  ]

  const intent = {
    displayName: testData.displayName,
    trainingPhrases: trainingPhrases,
    messages: [message],
    inputContextNames
  };

Upvotes: 1

Related Questions