Mizlul
Mizlul

Reputation: 2290

How to use Dialogflow communicating with Heroku

I have currently build a DialogFlow chatbot using Google Cloud Functions, Firebase. It all works good, but I would like to not use firebase at all, I would like to use Heroku, how can I make so that DialogFlow makes request to Heroku service.

I already know that I should add the URL of heroku and that has to be HTTPS and proper response format.

What I dont know is, how can I make the connection between DailogFlow and Heroku which is using node.js so I can send the responses such as :

sendResponse('Hello, this is responsing from Heroku')

Using firebase I have to had a function like this:

    exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
  console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
  console.log('Dialogflow Request body: ' + JSON.stringify(request.body));
  if (request.body.result) {
    processV1Request(request, response);
  } else if (request.body.queryResult) {
    // processV2Request(request, response);
  } else {
    console.log('Invalid Request');
    return response.status(400).end('Invalid Webhook Request (expecting v1 or v2 webhook request)');
  }
});

I dont know if I need this when using outside Firebase, at this case Heroku! also since I am not using firebase what will happen with this code:

functions.https.onRequest((request, response) => {

I dont have "functions" variable if I am not using firebase.

Upvotes: 2

Views: 2010

Answers (1)

Prisoner
Prisoner

Reputation: 50711

Most of the code can be used unchanged - Firebase Cloud Functions use node.js/express.js under the covers, and the Dialogflow libraries assume that the request and response objects are those from express.js with the JSON body parser middleware.

The line in question is syntactic sugar to have the Firebase Cloud Functions processor discover it and handle it. So you would replace that line

exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {

with something more like this

const express = require('express');
const app = express();
app.use( express.json() );

app.get('/', (req, res) => processWebhook( req, res ));

app.listen(3000, () => console.log('App listening on port 3000!'));

var processWebhook = function( request, response ){
  // ... the console logging and all the processing goes here

Upvotes: 2

Related Questions