javascriptnode.jsactions-on-googledialogflow-esfulfillment

How do I make a http request in the environment Dialogflow -> Fulfillment?


I'm trying to make a request to api of my site in order to correct the answer for google assistant but I do not have anything.

'use strict';

var requestNode = require('request');
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 });

  function test(agent) {
    return new Promise((resolve, reject) => {
        callApi().then((output) => {
            //This method works and the Agent says "output: abc"
            agent.add(output);
            resolve();
        });
    }); 
  }

  function callApi(){
     return new Promise((resolve, reject) => {
       const options = {
         url:    'https://mysite....',
         method: 'GET',
         headers: {'Accept': 'application/json'}
       };

       requestNode(options, function(error, requestInternal, body) {
         resolve(JSON.parse(body).title);
       });

     });

  }


  function fallback(agent) {
    agent.add(`I didn't understand`);
    agent.add(`I'm sorry, can you try again?`);
  }

  let intentMap = new Map();
  intentMap.set('test', test);
  intentMap.set('Default Fallback Intent', fallback);
  agent.handleRequest(intentMap);
});

I have not found the information how to make requests from Fulfillment. What is the way to do http/https request to my costum api?


Solution

  • If you are using the free tier of firebase, you will not be able to make API calls to third party services. You are required to upgrade to the paid account on firebase for that.