[英]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 控制台日志:
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.最可能的原因是您没有使用
Promise
或async
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.