Search code examples
node.jsapiaxiosdialogflow-es

Dialogflow using external API with Axios


To study, I'm trying to make a bot that, in response to a request, will send an image. Used waifu pics api (https://waifu.pics/docs) just pictures. It sends the url, that's enough.

Gives the following errors:

Webhook call failed. Error: UNAVAILABLE, State: URL_UNREACHABLE, Reason: UNREACHABLE_5xx, HTTP status code: 500. Error: No responses defined for platform: DIALOGFLOW_CONSOLE at V2Agent.sendResponses_

Here is the code from 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);
}); 

And here is from 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"
  }
}

Solution

  • You will need to get your {url} in response.data, then send it to the agent.add().

    I also got the same problem, and here is working code:

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