node.jswebbotsdialogflow-esdemo

how to reply message from web demo on dialogflow via webhook


I'm currently trying to add chatbot to my website. I'm integrating web demo on the agent used for our lIne bot. which some responses are handled by lambda webhook.

what I'm asking is can we send responses to web demo using lambda? if can, then how do I send them? there suppose to be some id right? and do we use HTTP post request to send them? I couldn't find an example.

and for some intent which has more than one response handled by dialogflow it can only send one of them. why is that? and what should I do so that I can send all of them via dialogflow?


Solution

  • Yes, it can be achieved and you can refer to give NodeJs code for that,

    const express = require("express");
    const bodyParser = require("body-parser");
    const apiai = require("apiai");
    const request = require("request");
    
    const app = express();
    app.use(bodyParser.json());
    app.set("port", process.env.PORT || 5000);
    
    
    app.post("/", (req, res) => {
      //console.log(req.body)
      const action = req.body.result.action;
      if (!req.body || !req.body.result || !req.body.result.parameters) {
        return res.status(400).send("Bad Request");
      }
      console.log("--------------------------------");
      console.log("Action =>", action);
      console.log("--------------------------------");
      switch (action) {
        case "price.search":
                const webhookReply = `Sorry NO book found in store.`;
                res.status(200).json({
                  source: "webhook",
                  speech: webhookReply,
                  displayText: webhookReply
                });
          break;
    
        default:
          break;
      }
    });
    
    app.listen(app.get("port"), function() {
      console.log("* Webhook service is listening on port:" + app.get("port"));
    });
    

    For every intent, there will be an action of that we have to define in dialogFlow.

    So when the user enters any query your webhook will get triggered it will go in the switch case to find the particular action and form that case you can send back the replay to your bot.