如何使用nodejs SDK更新对话流(V2)中的现有意图



我正在使用dialogflow nodejs sdk(v2)在我的nodjs应用程序中集成对话流,因为我正在使用dialogFlow npm node node库。我能够创建意图并获得意图列表,并且也可以查询。但是我找不到任何方法来更新现有意图并根据意图ID获取意图详细信息。

您能帮我或指导我如何解决这个问题吗?

谢谢。

要更新意图,首先,您需要获取意图详细信息。如果您具有意图名称或ID,则可以简单地提出列出意图API的请求,并找到具有匹配意图名称的意图详细信息。

拥有要更新的意图详细信息(在此称为existingIntent),您可以使用以下代码对其进行更新。

async function updateIntent(newTrainingPhrases) {
  // Imports the Dialogflow library
  const dialogflow = require("dialogflow");
  // Instantiates clients
  const intentsClient = new dialogflow.IntentsClient();
  const intent = existingIntent; //intent that needs to be updated
  const trainingPhrases = [];
  let previousTrainingPhrases =
    existingIntent.trainingPhrases.length > 0
      ? existingIntent.trainingPhrases
      : [];
  previousTrainingPhrases.forEach(textdata => {
    newTrainingPhrases.push(textdata.parts[0].text);
  });
  newTrainingPhrases.forEach(phrase => {
    const part = {
      text: phrase
    };
    // Here we create a new training phrase for each provided part.
    const trainingPhrase = {
      type: "EXAMPLE",
      parts: [part]
    };
    trainingPhrases.push(trainingPhrase);
  });
  intent.trainingPhrases = trainingPhrases;
  const updateIntentRequest = {
    intent,
    languageCode: "en-US"
  };
  // Send the request for update the intent.
  const result = await intentsClient.updateIntent(updateIntentRequest);
  return result;
}

最新更新