Example Google Cloud Function with a data action

Note: This article applies to the Google data actions integration when used with Google Cloud Functions.

The following content includes an example Google Cloud Function and a data action that executes the function. You can add the function to your Google Cloud Platform project and import the data action into your Genesys Cloud organization for use with a Google data actions integration.

For more information, see Setup for Google Cloud Functions

Example Google Cloud Function

The following code is a Google Cloud Function written in JavaScript. Copy this code to create a function in your Google Cloud Platform project. Make sure to set up your Google Cloud Platform to process functions. For more information, see Configure Google Cloud Platform.

/**
* Responds to any HTTP request.
*
* @param {!express:Request} req HTTP request context.
* @param {!express:Response} res HTTP response context.
*/
exports.testAction = (req, res) => {
    if (req.body.inputNumber1 <=0 || req.body.inputNumber2 <= 0) {
        res.status(400).send( { error: 'Inputs must be greater than zero' } );
    } else {
        let response = createResponse(req);
        res.status(200).send(response);
    }
};
 
/**
* Responds to any HTTP request.
*
* @param {!express:Request} req HTTP request context.
*/
function createResponse(req) {
   
    var response = {};
    response.sumOfNumber1AndNumber2 = req.body.inputNumber1 + req.body.inputNumber2;
    return response;
}

Example data action

The following JSON is for a data action that executes the example function. You can download a compressed version of the JSON to import into a data action: Test-Cloud-Function data action (.zip). For more information, see Create a custom action for integrations and Import or export a data action for integrations.

Notice the requestUrlTemplate and requestTemplate. The requestUrlTemplate includes the project name (businesscallingapi), function location (us-central1), and function name (function-test-action). The requestTemplate converts JSON into stringified JSON, which is the value for data. For more information, see Add configuration to custom actions for integrations.

{
  "name": "Test-Cloud-Function-Math-Example - Exported 2020-07-28 @ 11:06",
  "integrationType": "google-cloud-data-actions",
  "actionType": "custom",
  "config": {
    "request": {
      "requestUrlTemplate": "https://cloudfunctions.googleapis.com/v1/projects/businesscallingapi/locations/us-central1/functions/function-test-action-math-example:call",
      "requestType": "POST",
      "headers": {},
      "requestTemplate": "{ \"data\": \"$esc.jsonString(${input.rawRequest})\" }"
    },
    "response": {
      "translationMap": {},
      "translationMapDefaults": {},
      "successTemplate": "${rawResult}"
    }
  },
  "contract": {
    "input": {
      "inputSchema": {
        "title": "sum-input",
        "type": "object",
        "properties": {
          "inputNumber1": {
            "type": "number"
          },
          "inputNumber2": {
            "type": "number"
          }
        },
        "additionalProperties": false
      }
    },
    "output": {
      "successSchema": {
        "title": "sum-response",
        "type": "object",
        "properties": {
          "sumOfNumber1AndNumber2": {
            "type": "number"
          }
        },
        "additionalProperties": false
      }
    }
  },
  "secure": false
}

Test the data action to make sure that the data action executes with no errors. For more information, see Test data actions for integrations.

For more information about the integration, see About the Google data actions integration.