Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't initialise Dialogflow Fulfillment WebhookClient in AWS Lambda

According to the DialogFLow Fulfillment docs, the WebhookClient constructor needs Express HTTP request and response objects. However, in Lambda function, I receive only the event (the request). How do I create the Express request and response objects?

I have tried this so far:

const {WebhookClient} = require('dialogflow-fulfillment');

exports.dialogflowFulfillment = async (event) => {
  let response = {};
  const agent = new WebhookClient({ event, response });

  function sayNiceThings(agent) {
    agent.add(`Nice to meet you!`);
  }

  let intentMap = new Map();
  intentMap.set('Say Nice Things', sayNiceThings);
  agent.handleRequest(intentMap);
};
like image 946
drishit96 Avatar asked Apr 25 '26 22:04

drishit96


1 Answers

Create an NodeJS Express App

Install serverless-http package for adding AWS Lambda bridge

Install dialogflow-fulfillment and actions-on-google npm packages.

  npm init -f
  npm install --save express serverless-http
  npm install dialogflow-fulfillment
  npm install actions-on-google

Create index.js file:

index.js:
=========
const serverless = require('serverless-http');
const bodyParser = require('body-parser');
const express = require('express');

const app = express();
app.use(bodyParser.json({ strict: false }));

const {WebhookClient, Card, Suggestion} = require('dialogflow-fulfillment');
const request = require('request');



app.get('/', function (req, res) {

  res.send('Hello World !!!\n');
  console.log("Testing express lambda\n");

})



app.post('/', function (req, res) {


    const agent = new WebhookClient({request: req, response: res});

    function test_handler(agent) {
      agent.add(`Welcome to my agent on AWS Lambda!`);
    }

    // Run the proper function handler based on the matched Dialogflow intent name
    let intentMap = new Map();
    intentMap.set('test-intent', test_handler);

    agent.handleRequest(intentMap);


})


module.exports.handler = serverless(app);

Add configurations in serverless.yml:

serverless.yml
================

service: example-express-lambda

provider:
  name: aws
  runtime: nodejs8.10
  stage: dev
  region: ap-southeast-1

functions:
  app:
    handler: index.handler
    events:
      - http: ANY /
      - http: 'ANY {proxy+}'

Then deploy the lambda function. Add the endpoint url in the Dialogflow fulfilment webhook url.

Reference: https://serverless.com/blog/serverless-express-rest-api/

like image 92
Biranchi Avatar answered May 01 '26 11:05

Biranchi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!