Unable to Call Cloud Function to Dialog Flow CX

37 Views Asked by At

I have written the following Cloud Function in node.js 20

exports.calculateHRA = (req, res) => {
  try{
  const {metro,basicSalary,DA,rentPaid} = req.body; // Assuming data is passed in the request body with properties: metro, basicSalary, DA, rentPaid

  // Validate input data
  if (!metro || !basicSalary || !DA || !rentPaid) {
    return res.status(400).send('Invalid input. Please provide metro, basicSalary, DA, and rentPaid.');
  }

  const isMetro = metro.toLowerCase() === 'metro';

  // Calculate HRA based on metro or non-metro
  let hraPercentage;
  if (isMetro) {
    hraPercentage = 50;
  } else {
    hraPercentage = 40; // Assuming non-metro HRA percentage is 40%
  }

  const hra = (hraPercentage / 100) * (basicSalary + DA) - 0.1 * (basicSalary + DA) + rentPaid;
if(req.body.fulfillmentInfo && req.body.fulfillmentInfo.tag){
        let tag = req.body.fulfillmentInfo.tag;
        if(tag=="trial"){
          msg = hra;
        }
        else{
          msg="It doesn't work"
        }
    }

// Prepare the response for Dialogflow CX
    const response = {
      fulfillmentResponse: {
        messages: [
          {
            text: {
              text: [`Your HRA is calculated as: ${msg}`],
            },
          },
        ],
      },
    };

    // Send the response back to Dialogflow CX
   return res.status(200).json(response);
  } catch (error) {
    console.error('Error handling request:', error);
    res.status(500).send('Internal Server Error');
  }
};

In DiaglogFlow CX ,when the user types "yes" in Test Agent the above cloud Function needs to be called and the reply should be "Your HRA is calculated as: ${msg}" where ${msg} will be the value calculated and stored in the constant hra as per the above code.

Currently when user types yes there is no further response

Webhook configuration when Agent asks to confirm details

enter image description here

Test Agent Simulation

enter image description here

This is currently how my test agent responds I am not getting a response from cloud function after user enters yes. what am I missing?

I am trying to call cloud function after user confirms the details. The response from the cloud function Your HRA is calculated as: {some value} is what I am expecting. What I am getting is blank

Flow:

enter image description here

0

There are 0 best solutions below