简体   繁体   English

在Dialogflow Fulfillment中使用第三方API

[英]Using 3rd party API within Dialogflow Fulfillment

I've got a Dialogflow agent for which I'm using the Inline Editor (powered by Cloud Functions for Firebase). 我有一个Dialogflow代理,我正在使用内联编辑器(由Firebase的Cloud Functions提供支持)。 When I try to embed an HTTPS GET handler within the Intent handler, it crashes with log entry "Ignoring exception from a finished function". 当我尝试在Intent处理程序中嵌入HTTPS GET处理程序时,它会与日志条目“忽略已完成的函数中的异常”崩溃。 Maybe there's a better way to do this with promises but I'm new to that. 也许有更好的方法来实现承诺,但我是新手。 I can see that it does in fact execute the external query after upgrading to the Blaze plan so it's not a limitation of the billing account. 我可以看到它确实在升级到Blaze计划后执行外部查询,因此它不是计费帐户的限制。 Anyhow, here's the code: 无论如何,这是代码:

'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);
});

It isn't just that there is a "better" way to do it with Promises - agent.handleRequest() requires you to use Promises if you have any code that is being called asynchronously. 使用Promises的方法不仅仅是“更好”的方法 - agent.handleRequest()要求您使用Promises,如果您有任何异步调用的代码。 The issue is that your findWidget function is returning before the https.get finishes running, so the reply doesn't end up being sent. 问题是你的findWidget函数在https.get完成运行之前返回,因此回复不会最终被发送。

I tend to use the request-promise-native package to do HTTP calls, since it simplifies many things. 我倾向于使用request-promise-native包来进行HTTP调用,因为它简化了很多事情。 (However, you're free to wrap the call in a Promise yourself.) So the Promise version might look something like this (untested): (但是,您可以自己将调用包装在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 ); 
      });
    });  
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当对话流与 3rd 方服务对话时,在对话流实现中使用 Firebase Admin sdk 是否安全? - Is it safe to use Firebase Admin sdk in dialogflow fulfillment when dialogflow talking to 3rd party services? 如何在dialogflow的内联编辑器中进行第三方api调用 - How to make 3rd party api call in dialogflow's inline editor 使用AngularJS将数据从第三方API保存到CouchDB - Saving data from 3rd party API to CouchDB using AngularJS Node.js 第 3 方 API 使用 Express 调用 - Node.js 3rd party API call using Express REST API集成到第三方 - REST API integration to 3rd party 使用 Azure AD 与第 3 方进行 SSO - SSO with a 3rd party using Azure AD aws lambda - 使用 axios 调用第三方 API 时 503 服务不可用 - aws lambda - 503 service unavailable when calling 3rd party API using axios 使用Dialogflow实现的简单HTTP get API请求 - Simple HTTP get request to API using Dialogflow fulfillment 在适当的工作流程中将第三方JS库添加到Ionic - Adding 3rd party JS libraries to Ionic within the proper workflow 有关自己的REST API和使用第三方API提取的问题 - Question about own REST API and Fetch with 3rd Party API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM