Ideas for code separation in webhook-fulfillment nodejs and dialogflow

15 Views Asked by At

Community,

I'm taking my first steps in NodeJs. I'm stuck on how to separate the code of my chatbot. I am using Dialogflow and NodeJs. For this I do the webhook and fulfillment in a single file. Everything is running nice and smooth:

//File chatbot.js
    const express = require('express');
    const app = express();
    const { WebhookClient } = require('dialogflow-fulfillment');
    
    app.post('/webhook', express.json(), function (req, res) {
      const agent = new WebhookClient({ request: req, response: res });
      console.log('Dialogflow Request headers: ' + JSON.stringify(req.headers));
      console.log('Dialogflow Request body: ' + JSON.stringify(req.body));
    
    function probandoWebhook(agent) {
        agent.add("Dentro del Webhook"); 
    }
    
    let intentMap = new Map();        
      intentMap.set('ProbandoWebhook', probandoWebhook);

To do everything in the same file is going to hurt us because as we put more functionality it will be more complicated to maintain the code.

It occurs to me to create another file in a different directory and there to put the corresponding functions for the intents and then to call them from the main file.

enter image description here

...orientaciones\estudiantes\estudianteprofesor.js -> file where I will define the intents functions.

...\chatbot.js -> file where I will send to call the functions of the chatbot intents.

My doubts are the following:

  1. how should I declare my functions in studentteacher.js to be able to call it from chatbot.js?
  2. Which libraries and files should I call in studentprofessor.js and chatbot.js?

Greetings.

Put in a different file the code corresponding to the chatbot but I don't know how to declare the functions in another file and how to call them properly.

0

There are 0 best solutions below