【问题标题】:Dialogflow fulfillment inline editor api requestDialogflow 实现内联编辑器 api 请求
【发布时间】:2019-11-10 23:43:34
【问题描述】:

我正在尝试让机器人回答从 API 接收到的信息,但无法让它正常工作。

在 firebase 控制台日志中,我可以看到 api 确实响应了我需要的信息。

以下所有代码:


'use strict';

const axios = require('axios');

const functions = require('firebase-functions');
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 callAPI(agent){

    const food = agent.parameters.Food;
    const subject = agent.parameters.Subject;
    const number = agent.parameters.number;

    const question = subject + " "+number +" "+food;
    const questionReady = question.replace(/ /g, '+');

    const apiKey = "key";
    const baseUrl = "https://api.spoonacular.com/recipes/quickAnswer?q=";

    const apiUrl =  baseUrl + questionReady + "&apiKey=" + apiKey;

    axios.get(apiUrl).then((result) => {
        console.log(result);   
        console.log(result.data);   
        console.log(result.data.answer);

        agent.add(result);
        agent.add(result.data);
        agent.add(result.data.answer);

    });


  }


  let intentMap = new Map();
  intentMap.set('Default Welcome Intent', welcome);
  intentMap.set('Default Fallback Intent', fallback);
  intentMap.set('food', callAPI);
  agent.handleRequest(intentMap);
});

Firebase 控制台日志:

View post on imgur.com

【问题讨论】:

  • 您到底遇到了什么错误?给我们一些反馈
  • 调用外部 API 确保您的计费已启用
  • 你的函数应该返回promise
  • 我没有收到任何类型的错误,只是无法在机器人上显示回答 api 返回的内容,目前我正在使用 blaze 付款计划,缺少的是来自功能

标签: javascript dialogflow-es dialogflow-es-fulfillment


【解决方案1】:

最可能的原因是您没有使用 Promiseasync 函数调用,因此在您对 API 的调用完成之前,您的 Handler 没有返回任何内容。

要解决这个问题,callAPI() 需要返回 axios.get() 返回的 Promise。同样,调用 callAPI() 的 Intent Handler 也需要返回该 Promise(或来自 then() 块的另一个 Promise)。

Dialogflow 库需要这样做,因此它知道在向用户返回任何内容之前等待 API 调用完成(并因此解决 Promise)。

在您的情况下,这很简单,只需将对 axios.get() 的调用更改为类似

return axios.get(apiUrl).then((result) => {
  // Rest of this call here

【讨论】:

  • 答案已更新,但请参阅我对上述“答案”的评论。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-16
  • 1970-01-01
  • 2019-11-05
  • 2019-12-13
  • 1970-01-01
相关资源
最近更新 更多