【发布时间】: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 控制台日志:
【问题讨论】:
-
您到底遇到了什么错误?给我们一些反馈
-
调用外部 API 确保您的计费已启用
-
你的函数应该返回promise
-
我没有收到任何类型的错误,只是无法在机器人上显示回答 api 返回的内容,目前我正在使用 blaze 付款计划,缺少的是来自功能
标签: javascript dialogflow-es dialogflow-es-fulfillment