在Dialogflow Fulfillment中使用第三方API

时间:2018-04-10 02:16:05

标签: node.js firebase google-cloud-functions actions-on-google dialogflow

我有一个Dialogflow代理,我使用Inline编辑器(由Cloud Functions for Firebase提供支持)。当我尝试在Intent处理程序中嵌入HTTPS GET处理程序时,它会崩溃并显示日志条目"忽略已完成函数的异常"。也许有更好的方式来实现这一承诺,但我是新手。我可以看到它确实在升级到Blaze计划后执行外部查询,因此它不是计费帐户的限制。无论如何,这里是代码:

'use strict';

const functions = require('firebase-functions');
//const rp = require('request-promise');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');

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 welcome(agent) {
    agent.add(`Welcome to my agent!`);
  }

  function fallback(agent) {
    agent.add(`I didn't understand`);
    agent.add(`I'm sorry, can you try again?`);
  }

  function findWidget(agent) {

    const https = require("https");
    const url = "https://api.site.com/sub?query=";

    agent.add(`Found a widget for you:`);

    var widgetName = getArgument('Name');

    https.get(url + widgetName, res => {
      res.setEncoding("utf8");
      let body = "";
      res.on("data", data => {
        body += data;
      });
      res.on("end", () => {

        body = JSON.parse(body);

        agent.add(new Card({
            title: `Widget ` + body.name,
            text: body.description,
            buttonText: 'Open link',
            buttonUrl: body.homepage
          })
        );
      });
    });  
  }

// Run the proper function handler based on the matched Dialogflow intent name
let intentMap = new Map();
intentMap.set('Default Welcome Intent', welcome);
intentMap.set('Default Fallback Intent', fallback);
intentMap.set('Find Old Movie Intent', findOldMovie);
intentMap.set('Find Movie Intent', findMovie);
// intentMap.set('your intent name here', googleAssistantHandler);
agent.handleRequest(intentMap);
});

1 个答案:

答案 0 :(得分:7)

并不只是有一个更好的"使用Promises的方法 - agent.handleRequest()要求您使用Promises,如果您有任何异步调用的代码。问题是您的findWidget功能在https.get完成运行之前返回,因此回复最终不会被发送。

我倾向于使用request-promise-native包来进行HTTP调用,因为它简化了很多事情。 (但是,您自己可以自由地将调用包装在Promise中。)所以Promise版本可能看起来像这样(未经测试):

  var findWidget = function(agent) {

    const request = require('request-promise-native');
    const url = "https://api.site.com/sub?query=";

    agent.add(`Found a widget for you:`);

    var widgetName = getArgument('Name');

    return request.get( url+widgetName )
      .then( jsonBody => {
        var body = JSON.parse(jsonBody);
        agent.add(new Card({
          title: `Widget ` + body.name,
          text: body.description,
          buttonText: 'Open link',
          buttonUrl: body.homepage
        }));
        return Promise.resolve( agent ); 
      });
    });  
  }