簡體   English   中英

在Dialogflow Fulfillment中使用第三方API

[英]Using 3rd party API within Dialogflow Fulfillment

我有一個Dialogflow代理,我正在使用內聯編輯器(由Firebase的Cloud Functions提供支持)。 當我嘗試在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);
});

使用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 ); 
      });
    });  
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM