简体   繁体   English

Dialogflow 实现内联编辑器 api 请求

[英]Dialogflow fulfillment inline editor api request

I'am tryng to get the bot to answer information that received from a API, can't get it working tho.我试图让机器人回答从 API 收到的信息,但无法让它工作。

In firebase console log I can see the api indeed respond with the information that I need.在 firebase 控制台日志中,我可以看到 api 确实响应了我需要的信息。

All the code below:以下所有代码:


'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 console log: Firebase 控制台日志:

View post on imgur.com查看 imgur.com 上的帖子

The most likely reason is that you're not using a Promise or an async function call, so your Handler is returning nothing before your call to the API completes.最可能的原因是您没有使用Promiseasync function 调用,因此在您对 API 的调用完成之前,您的处理程序没有返回任何内容。

To fix this, callAPI() needs to return the Promise that axios.get() returns.要解决此问题, callAPI()需要返回axios.get()返回的 Promise。 Similarly, your Intent Handler that calls callAPI() needs to return that Promise (or another promise from the then() block) as well.同样,调用callAPI()的 Intent Handler 也需要返回 Promise(或then()块中的另一个 promise)。

The Dialogflow library requires this so it knows to wait for the API call to be completed (and the Promise to thus be resolved) before returning anything to the user. Dialogflow 库需要这样做,因此它知道在向用户返回任何内容之前等待 API 调用完成(并且 Promise 将因此得到解决)。

In your case, this is as simple as changing the call to axios.get() to something like在您的情况下,这就像axios.get()的调用更改为类似

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

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

相关问题 我可以在 Inline Fulfillment Dialogflow 编辑器中跨对话存储计数器吗? - Can I store a counter across conversation in Inline Fulfillment Dialogflow editor? 在使用内联编辑器实现的 DialogFlow Messenger 中,setContext() 干扰 setFollowupEvent() - in DialogFlow Messenger using inline editor for fulfillment, setContext() is interfering with setFollowupEvent() 如何在嵌入式编辑器dialogflow中发出发布请求? - how to make post request in inline editor dialogflow? 在DialogFlow内联编辑器中使用“ request-promise-native”包 - Use 'request-promise-native' package in DialogFlow inline editor 无法在Dialogflow内联编辑器中发出HTTP请求 - Unable to make http request from within Dialogflow inline editor 内联编辑器中的对话流捕获实体 - dialogflow capturing entity in inline editor 如何在环境Dialogflow->实现中发出http请求? - How do I make a http request in the environment Dialogflow -> Fulfillment? 是否可以在dialogflow内联编辑器中使用外部库? - Is it possible to use external libraries in the dialogflow inline editor? 如何从 IDE 而不是内联编辑器编写实现代码? - How to write fulfillment code from an IDE instead of the inline editor? Dialogflow - 使用 MySQL 实现 webhook - Dialogflow - Fulfillment webhook with MySQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM