[英]Can't make a API request in DialogFlow and actions-on-google. No response has been set
I'm trying to make a API request using actions-on-google and dialogflow, but after the call has been resolved, the conv.ask does not count as a final response i guess. 我正在尝试使用Google上的动作和dialogflow发出API请求,但是在调用解决之后,conv.ask不会算作最终响应。
My code: 我的代码:
const functions = require('firebase-functions'); const {dialogflow,Permission} = require('actions-on-google'); const request = require('request'); const rp = require('request-promise-native'); const fetch = require('node-fetch'); const app = dialogflow(); const API_URL = "https://pokeapi.co/api/v2/pokemon/1/"; app.intent('CriarLista - produtos - yes', (conv) => { conv.data.requestedPermission = 'DEVICE_PRECISE_LOCATION'; return conv.ask(new Permission({ context: 'Para te localizar', permissions: conv.data.requestedPermission, })); }); app.intent('CriarLista - location_permission', (conv, params, permissionGranted) => { if (permissionGranted) { const {requestedPermission} = conv.data; if (requestedPermission === 'DEVICE_PRECISE_LOCATION') { const {coordinates} = conv.device.location; if (coordinates) { let lat = coordinates.latitude; let long = coordinates.longitude; fetch(API_URL).then(function(res) { let data = res.json(); }).then(function(data) { conv.ask('Test'); }).catch(function(err) { console.log(err); }); } else { return conv.close('Sorry, I could not figure out where you are.'); } } } else { return conv.close('Sorry, permission denied.'); } }); exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app);
Errors: actions-on-google response: 错误:谷歌行动响应:
{
"responseMetadata": {
"status": {
"code": 10,
"message": "Failed to parse Dialogflow response into AppResponse because of empty speech response",
"details": [
{
"@type": "type.googleapis.com/google.protobuf.Value",
"value": "{\"id\":\"74f26d40-869c-4ccb-9d75-1473f42e9aee\",\"timestamp\":\"2018-10-06T15:33:01.004Z\",\"lang\":\"pt-br\",\"result\":{},\"status\":{\"code\":206,\"errorType\":\"partial_content\",\"errorDetails\":\"Webhook call failed. Error: 500 Internal Server Error\"},\"sessionId\":\"ABwppHEUqmeurK1aqRe3QRDCo2BrPyZOu4cI447He8ZgA882v72AICpeqPCyzHEA6QCKTeo4cn4CzIZ9ACozv15L\"}"
}
]
}
}
}
Firebase console: Firebase控制台:
FetchError: request to https://pokeapi.co/api/v2/pokemon/1/ failed, reason: getaddrinfo EAI_AGAIN pokeapi.co:443
Error: No response has been set. Is this being used in an async call that was not returned as a promise to the intent handler?
Says that i need to return a promise, i guess a "resolve" method, but i tried even making my promise and it does not work, the "then" from the fetch method is already a resolve method returned from a promise. 说我需要返回一个承诺,我猜想是一个“解决”方法,但是我什至试图做出我的承诺,但它不起作用,fetch方法中的“那么”已经是一个从承诺中返回的解决方法。 I tried node-fetch, request, request-promise-native..
我尝试了节点获取,请求,请求承诺本机..
Running only the conv.ask outside the fetch block works normally after deploying to firebase. 部署到Firebase后,仅在提取块外部运行conv.ask即可正常工作。
There is a right way of doing external requests in DialogFlow/actions-on-google environment? 在DialogFlow / Google行动上的环境中,有正确的方式处理外部请求吗?
You need to return
the promise object so the webhook knows to wait until the promise is completed. 您需要
return
promise对象,以便webhook知道要等到promise完成。
Since fetch()
already returns a Promise (as do all the .then()
calls after it, since that's how one typically uses a Promise), you just need to return that promise. 由于
fetch()
已经返回了Promise(它之后的所有.then()
调用也是如此,因为这通常是使用Promise的方式),因此您只需要返回该Promise。 So you can change the line to 因此您可以将行更改为
return fetch(API_URL).then(function(res) {
let data = res.json();
}).then(function(data) {
conv.ask('Test');
}).catch(function(err) {
console.log(err);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.