无法在 DialogFlow 和 actions-on-google 中发出 API 请求。没有设置响应

Can't make a API request in DialogFlow and actions-on-google. No response has been set

我正在尝试使用 actions-on-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?

说我需要 return 一个承诺,我想是 "resolve" 方法,但我什至尝试做出我的承诺但它不起作用,"then" 来自 fetch方法已经是一个解析方法 return 来自承诺。我尝试了 node-fetch、request、request-promise-native..

运行 只有 fetch 块外的 conv.ask 在部署到 firebase 后正常工作。

在DialogFlow/actions-on-google环境下有正确的外部请求方式吗?

您需要 return promise 对象,以便 webhook 知道要等到 promise 完成。

因为 fetch() 已经 return 是一个 Promise(就像它之后的所有 .then() 调用一样,因为这是人们通常使用 Promise 的方式),你只需要 return 那个承诺。所以你可以将行更改为

            return fetch(API_URL).then(function(res) {
                let data = res.json();
            }).then(function(data) {
                conv.ask('Test');
            }).catch(function(err) {
                console.log(err);
            });