我正尝试使用Google上的动作和dialogflow发出API请求,但在调用解决后,我想conv.ask不会算作最终响应。
我的代码:
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);
错误: Google行动的回应:
{
"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控制台:
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?
说我需要返回一个promise,我猜想是一个“ resolve”方法,但是我什至尝试做出我的promise但它不起作用,fetch方法中的“ then”已经是一个promise返回的resolve方法。我尝试了node-fetch,request,request-promise-native。
在部署到Firebase后,仅在获取块外部运行conv.ask即可正常工作。
在DialogFlow / Google行动上,有正确的方法来进行外部请求吗?
答案 0 :(得分:2)
您需要return
Promise对象,以便Webhook知道要等到Promise完成。
由于fetch()
已经返回了Promise(之后的所有.then()
调用也是如此,因为这通常是使用Promise的方式),因此您只需要返回该Promise。因此您可以将行更改为
return fetch(API_URL).then(function(res) {
let data = res.json();
}).then(function(data) {
conv.ask('Test');
}).catch(function(err) {
console.log(err);
});