Ant*_*llo 2 google-api node.js dialogflow-es
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)
})
}
}
Run Code Online (Sandbox Code Playgroud)
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)
})
}
}
Run Code Online (Sandbox Code Playgroud)
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')
}
}
Run Code Online (Sandbox Code Playgroud)
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]
Run Code Online (Sandbox Code Playgroud)
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?
尝试了很多之后,明白我的代码有效,因为我删除了更新掩码。还有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)
})
}
}
Run Code Online (Sandbox Code Playgroud)
调用它的函数:
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')
}
}
Run Code Online (Sandbox Code Playgroud)
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)
})
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1439 次 |
| 最近记录: |