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. Ensure that you set up your Google Cloud Platform to process functions and the functions return response is of application/json for Content-Type. 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.

The requestUrlTemplate is the Trigger URL which includes the function location (us-central1), project name (businesscallingapi) and function name (function-test-action), and is of the format https://{location-of-function}{project-owning-function}/{function-name}. 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://us-central1-businesscallingapi.cloudfunctions.net/function-test-action-math-example",
      "requestType": "POST",
      "headers": {}
    },
    "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, see About the Google data actions integration.