Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect webhook of my local to dialogflow?

I have a question about webhook connect.
I edited usually by inline editor of dialogflow.
But now I want to edit in my local.
So I did some setting watching two examples.

https://chatbotsmagazine.com/creating-nodejs-webhook-for-dialogflow-2b050f76cd75

https://github.com/dialogflow/fulfillment-temperature-converter-nodejs

[1] I made file,
      (1) Users/a/firebase.js
      (2) Users/a/functions/index.js (with package module)
      (3) webhook server by ngrok.
      (4) I attached this link 'https://ngrok~~/webhook' on dialogflow webhook

[2] firebase.js has

{}

[3] index.js has

'use strict';

const express = require('express');
const bodyParser = require('body-parser');
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
const request = require('request');
const { dialogflow } = require('actions-on-google');
const app = dialogflow();
const admin = require('firebase-admin');
const server = express();

//admin.initializeApp();

process.env.DEBUG = 'dialogflow:debug';

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

  let intentMap = new Map();
  intentMap.set('hello', hello);
  intentMap.set('Default Fallback Intent', fallback);

  agent.handleRequest(intentMap);
});

  var port = process.env.PORT || 3000;
  // create serve and configure it.

  server.get('/getName',function (req,res){
      res.send('Swarup Bam');
  });
  server.listen(port, function () {
      console.log("Server is up and running...");
  });



And server starts locally with ngrok port 3000.

I've written server.listen in my code.
But it seems that it doesn't have webhook post in my code.

So, in conclusion, when I write 'hello' in my dialogflow , ngrok gives a 404 not found error.

like image 472
DD DD Avatar asked Nov 06 '22 23:11

DD DD


1 Answers

It looks like you have several things that you've mixed together.

Your node.js program is using two different methods to listen on a port, a way that is designed to use Cloud Functions for Firebase and a way that uses the express library, but you haven't indicated that you're running the program using either. Let's look at each thing that is (or should be) running

ngrok

ngrok is a port forwarder, so it assumes that there is another program that is running that is listening on the port you've specified. It does not start anything listening on that port itself.

Cloud Functions for Firebase

The portion starting with exports.dialogflowFirebaseFulfillment is for Cloud Functions for Firebase. Most examples from Google use this because it is easy to setup and use for beginners, scales well in production, and can use the monthly cloud credit from Google once you've deployed your Action. All the code inside this block is your webhook handler.

Code written for Cloud Functions for Firebase generally run on Google's servers, but for testing you can use the firebase serve --only functions command to run it locally.

express library

You've written code that starts listening on port 3000 and for a particular path (/getName), but what you're returning there doesn't call any of the webhook code that you have previously.

The req and res parameters match the request and response parameters in the Cloud Functions section (Cloud Functions just use express under the hood), so you could move your intent handling code to inside this express handler if you wanted.

Code that is written using the express library is started using the node command. You'll need a public server when you go to release your code - don't try to run a production-level server through ngrok to your home network connection.

like image 67
Prisoner Avatar answered Nov 10 '22 01:11

Prisoner