如何在对话框中使用内联编辑器获取外部api数据

Abd*_*fey 0 fulfillment actions-on-google dialogflow-es

我有一个Dialogflow代理,我正在使用它的内联编辑器(由Cloud Functions for Firebase驱动)。当我尝试使用来获取外部api数据时,request-promise-native我会一直进入Ignoring exception from a finished functionFirebase控制台。

function video(agent) {
    agent.add(`You are now being handled by the productivity intent`);
    const url = "https://reqres.in/api/users?page=2";
    return request.get(url)
        .then(jsonBody => {
            var body = JSON.parse(jsonBody);
            agent.add(body.data[0].first_name)
            return Promise.resolve(agent);
        });
}
Run Code Online (Sandbox Code Playgroud)

Pri*_*ner 5

您的代码看起来正确。这种情况下的例外情况可能是您没有使用付费帐户,因此Google外部的网络访问被阻止。您可以通过添加catch块来查看确切的异常:

function video(agent) {
    agent.add(`You are now being handled by the productivity intent`);
    const url = "https://reqres.in/api/users?page=2";
    return request.get(url)
        .then(jsonBody => {
            var body = JSON.parse(jsonBody);
            agent.add(body.data[0].first_name)
            return Promise.resolve(agent);
        })
        .catch(err => {
            console.error('Problem making network call', err);
            agent.add('Unable to get result');
            return Promise.resolve(agent);
        });
}
Run Code Online (Sandbox Code Playgroud)

(如果这样做,则可能要使用日志中的确切错误来更新您的问题。)