Dialogflow v2 Nodejs Client Library UpdateIntent when adding Training Phrases

此生再无相见时 提交于 2020-01-03 03:31:10

问题


I am trying to use the updateIntent function that is part of the Dialogflow v2 Client library for Node.js . The reason I am trying to use it, is to be able to add training phrases to an intent.

I cannot seem to get passed this one. Here is the code I am using for it!:

My GetIntent Function:

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)
    })
  }
}

My UpdateIntent Function:

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)
    })
  } 
}

The Function that Calls it:

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')
  }
}

Now the weird thing is - I am getting a 200 response from the client library call. The Api doc states that upon a successful response you will get an intent object. I am getting an intent object with the training phrases inside...

    [![{ 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] 

This is what dialogflow has. Only two training phrases here, the one I added programmatically does not show up. So my question is, how can I format the request so I can update the training phrases without a problem? Is there an example I can run off?


回答1:


After trying out a lot, understood that my code worked because i removed update mask. And the languageCode as well, because it was giving me an error. The code is as below and works fine. Check it up.

This is the getIntent function:

    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)
        })
    }
}

The function that calls it:

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')
    }
 }

The UpdateIntent function:

 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)
        })
    }
}


来源:https://stackoverflow.com/questions/52881992/dialogflow-v2-nodejs-client-library-updateintent-when-adding-training-phrases

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!