node.js - 如何从 REST API 输出信息

标签 node.js dialogflow-es actions-on-google

我想让我的代理说出从 REST API 获得的信息。 但是如下代码无法响应任何消息,这意味着“queryResult.fulfillmentMessages.text”为空。

exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
  const agent = new WebhookClient({ request, response });
  console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
  console.log('Dialogflow Request body: ' + JSON.stringify(request.body));
 
  function intent1(agent) {
    callApi().then((output) => {
        //This log is correctly outputted to firebase console 
        console.log('output: ' + output);
        
        //But this method doesn't work and the Agent says nothing
        agent.add('output: ' + output);
    });
  }

  function callApi(){
    return new Promise((resolve, reject) => {
        let req = http.get('http://xxx', (res) => {
          let chunk = '';
          res.on('data', (d) => {
            chunk = d;
          });
          res.on('end', () => {
            let response = JSON.parse(chunk);
            let output = response['results'][0];
            
            resolve(output);
          });
        });
    });
  }

  let intentMap = new Map();
  intentMap.set('intent1', intent1);
  agent.handleRequest(intentMap);
});

我尝试了另一个代码如下,它表明回调函数不影响“agent.add”方法。 所以,我认为问题是由API请求过程或其他原因引起的......

'use strict';
 
const functions = require('firebase-functions');
const App = require('actions-on-google').DialogflowApp;
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');

const http = require('http');
 
process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements
 
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
  const agent = new WebhookClient({ request, response });
  console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
  console.log('Dialogflow Request body: ' + JSON.stringify(request.body));
 
  function intent1(agent) {
    callApi().then((output) => {
        //This method works and the Agent says "output: abc"
        agent.add('output:' + output);
    });
  }

  function callApi(){
    return new Promise((resolve, reject) => {
        let output = "abc";
        resolve(output);
    });
  }

  let intentMap = new Map();
  intentMap.set('intent1', intent1);
  agent.handleRequest(intentMap);
});

有谁知道解决问题的方法或其他从 REST API 输出信息的方法?

最佳答案

您的 intent1 函数也必须返回一个 Promise,并在您将响应添加到代理后解决它。

function intent1(agent) {
    return new Promise((resolve, reject) => {
        callApi().then((output) => {
            //This method works and the Agent says "output: abc"
            agent.add('output:' + output);
            resolve();
        });
    }); 
}

此外,在 callApi 函数中,每次接收到一些数据时,都会为 block 变量分配一个新值。您应该将接收到的数据添加到变量的当前值(只需在等号前添加一个“+”):

res.on('data', (d) => {
    chunk += d;
});

关于node.js - 如何从 REST API 输出信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51653727/

相关文章:

javascript - 有没有办法用另一个具有相同字段的对象添加/减去 JSON 中的所有字段?

node.js - 返回 null 的 GraphQL 嵌套查询

javascript - 使用 Firestore 查询的信息响应 Dialogflow

node.js - 使用 DialogFlowApp 的 actionMap 将多个意图映射到一个函数

node.js - TypeORM 获取多对多关系的一侧

node.js - Node.js 中的端口在服务器上如何工作?

javascript - 如何在 Dialogflow -> Fulfillment 环境中发出 http 请求?

Android DialogFlow V2 SDK Java 客户端库 Grpc 谷歌云平台检测实时音频流的 Intent

google-oauth - Google Home "Couldn' t 更新设置。检查您的连接。

java - 如何使用 Spring Boot 和 Java 库在 google 上创建操作