我试图使用updateIntent函数,该函数是Node.js的Dialogflow v2客户端库的一部分。我尝试使用它的原因是能够向意图添加训练短语。

我似乎无法通过这个。这是我正在使用的代码!:

我的GetIntent函数:

async function getIntent(intentId) {
  try {
    let responses = await intentsClient.getIntent({name: intentId, intentView: 'INTENT_VIEW_FULL'})
    const response = responses[0]
          // console.log(response)

    return new Promise((resolve, reject) => {
      resolve(response)
    })
  } catch (err) {
    return new Promise((resolve, reject) => {
      reject(err)
    })
  }
}


我的UpdateIntent函数:

async function updateIntent(intent) {
  const request = {
    intent: intent,
    languageCode: 'en-US',
    updateMask: {
       paths: ['trainingPhrases']
    },
    intentView: 'INTENT_VIEW_FULL'
  }
  try {
    let responses = await intentsClient.updateIntent(request)
    return new Promise((resolve, reject) => {
      resolve(response)
    })
  } catch (err) {
    console.log(err)
    return new Promise((resolve, reject) => {
      reject(err)
    })
  }
}


调用它的函数:

async function testUpdateTraining () {
  try {
    let intent = await getIntent('projects/small-talk-1-406ae/agent/intents/ac7f0b68-de5c-4b6f-9393-358dd2b0c1bd')

    let trainingPhrase = { parts: [{ text: 'How should I behave on the trails?'}],
      type: 'EXAMPLE'}
    intent.trainingPhrases.push(trainingPhrase)
    try {
      let updatedIntent = await updateIntent(intent)
    } catch (e) {
      console.log(e)
      console.log('failed to update the intent')
    }
  } catch (err) {
    console.log('failed to get intent')
  }
}


现在,奇怪的是-我从客户端库调用中收到200条响应。 Api文档指出,成功响应后,您将获得一个意图对象。我收到一个带有训练短语的意图对象...

    [![{ inputContextNames: \[\],
  events: \[\],
  trainingPhrases:
   \[ { parts: \[Array\],
       name: 'ad0d1f6a-78cf-4e0b-84ca-ec62a45c75dc',
       type: 'EXAMPLE',
       timesAddedCount: 0 },
     { parts: \[Array\],
       name: 'e33cce4b-96ee-4e35-a151-5b09ff603817',
       type: 'EXAMPLE',
       timesAddedCount: 0 },
     { parts: \[Array\],
       name: '7d9b7c56-5fa8-4791-986f-e57b9f90d431',
       type: 'EXAMPLE',
       timesAddedCount: 0 } \],
  outputContexts: \[\],
  parameters: \[\],
  messages:
   \[ { platform: 'PLATFORM_UNSPECIFIED',
       text: \[Object\],
       message: 'text' } \],
  defaultResponsePlatforms: \[\],
  followupIntentInfo: \[\],
  name: 'projects/small-talk-1-406ae/agent/intents/ac7f0b68-de5c-4b6f-9393-358dd2b0c1bd',
  displayName: 'faq.offroad.card1answer',
  priority: 500000,
  isFallback: false,
  webhookState: 'WEBHOOK_STATE_UNSPECIFIED',
  action: 'faq.offroad.card1answer',
  resetContexts: false,
  rootFollowupIntentName: '',
  parentFollowupIntentName: '',
  mlDisabled: true }][1]][1]


这就是dialogflow所具有的。这里只有两个训练用语,我以编程方式添加的一个没有出现。
node.js - 添加培训短语时Dialogflow v2 Nodejs客户端库UpdateIntent-LMLPHP
所以我的问题是,如何格式化请求,以便我可以毫无问题地更新训练短语?有没有我可以逃脱的例子?

最佳答案

经过大量尝试后,了解到我的代码可以正常工作,因为我删除了更新掩码。还有languageCode,因为它给了我一个错误。
代码如下,并且工作正常。
检查它。

这是getIntent函数:

    async function getIntent(intentId) {
    try {
        let responses = await intentsClient.getIntent({
            name: intentId,
            intentView: 'INTENT_VIEW_FULL'
        })
        const response = responses[0];
        console.log(util.inspect(response, false, null, true /* enable colors */ ));

        return new Promise((resolve, reject) => {
            resolve(response)
        })
    } catch (err) {
        return new Promise((resolve, reject) => {
            reject(err)
        })
    }
}


调用它的函数:

async function testUpdateTraining () {
    try {
        let intent = await getIntent('<your_ID>')

        let trainingPhrase = {
            parts: [{
                text: 'let me call you?'
            }],
            type: 'EXAMPLE'
        }
        intent.trainingPhrases.push(trainingPhrase)
        try {
            let updatedIntent = await updateIntent(intent)
        } catch (e) {
            console.log(e)
            console.log('failed to update the intent')
        }
    } catch (err) {
        console.log('failed to get intent')
    }
 }


UpdateIntent函数:

 async function updateIntent(intent) {
    const request = {
        intent: intent,
        intentView: 'INTENT_VIEW_FULL'
    }
    try {
        let responses = await intentsClient.updateIntent(request)
        return new Promise((resolve, reject) => {
            resolve(responses)
        })
    } catch (err) {
        console.log(err)
        return new Promise((resolve, reject) => {
            reject(err)
        })
    }
}

10-05 22:37