繁体   English   中英

Dialogflow 使用外部 API 和 Axios

[英]Dialogflow using external API with Axios

为了研究,我正在尝试制作一个机器人,它可以响应请求发送图像。 使用 waifu 图片 api ( https://waifu.pics/docs ) 只是图片。 它发送 url,这就够了。

给出以下错误:

Webhook 调用失败。 错误:不可用,State:URL_UNREACHABLE,原因:UNREACHABLE_5xx,HTTP 状态代码:500。错误:没有为平台定义响应:DIALOGFLOW_CONSOLE 在 V2Agent.sendResponses_

这是来自 index.js 的代码

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?`);
  }

  //Its not-worked function
function AnimePicHandler(agent){
    return axios.get(`https://api.waifu.pics/sfw/waifu`)
      .then((result) =>
      agent.add("result"));                                                 
  }


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

这是来自 package.json

{
  "name": "dialogflowFirebaseFulfillment",
  "description": "This is the default fulfillment for a Dialogflow agents using Cloud Functions for Firebase",
  "version": "0.0.1",
  "private": true,
  "license": "Apache Version 2.0",
  "author": "Google Inc.",
  "engines": {
    "node": "10"
  },
  "scripts": {
    "start": "firebase serve --only functions:dialogflowFirebaseFulfillment",
    "deploy": "firebase deploy --only functions:dialogflowFirebaseFulfillment"
  },
  "dependencies": {
    "actions-on-google": "^2.2.0",
    "firebase-admin": "^5.13.1",
    "firebase-functions": "^2.0.2",
    "dialogflow": "^0.6.0",
    "dialogflow-fulfillment": "^0.6.1",
    "axios": "^0.27.2"
  }
}

我在同样的问题上。 我的沟通有效。 但是 agent.add 不会产生任何影响。

在您的情况下,添加一条 catch 语句,仅用于记录其他信息或发生错误。

.catch((err) => {
  console.error(err);
});

另外,试着松开这个“回报”。 作为欢迎和后备功能的示例。 只需在最后添加到代理,不要返回任何内容。

您需要在 response.data 中获取您的 {url},然后将其发送到 agent.add()

我也遇到了同样的问题,这里是工作代码:

function AnimePicHandler(agent) {
      return axios.get(`https://api.waifu.pics/sfw/waifu`).then((response) => {
        const { url } = response.data;
        agent.add(`test, this is the url: ${url}`);
      });
    }

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM