All Collections
Other Articles
Google Script Custom Webhook
Google Script Custom Webhook
Luiza Gircoveanu avatar
Written by Luiza Gircoveanu
Updated over a week ago

Overview

Webhooks are powerful tools used to trigger actions via API. Within Audits and Journeys, users can add custom webhook URLs to be triggered at the completion of the audit/Journey.

The webhook itself can be customized to do virtually anything you want with ObservePoint's API and any other API you have access to. One common use case is using a webhook to send alerts to Slack instead of to your email.

Google Script Webhook

Webhooks can be created with a variety of tools, but our favorite (which is free) is Google Script.

  1. Visit script.google.com. If needed, log in to your Google account.

  2. Click New Project.

  3. Name the project, and then add the following code snippet to the default .gs file.

// This is a function that fires when the webapp receives a GET request
function doGet(e) {
return HtmlService.createHtmlOutput("request received");
}
// This is a function that fires when the webapp receives a POST request
function doPost(e) {
var myData = JSON.parse(e.postData.contents);

var itemType = myData.itemType; // The item type will be an 'audit', 'web-journey'.
var itemId = myData.itemId; // The item ID will contain the ID of the Audit or Web Journey
var runId = myData.runId; // The run ID will contain the ID for the specific run of the Audit or Web Journey.

// DO WHATEVER YOU WANT!!!

}
  1. Make it your own! ObservePoint provides the webhook script with the necessary IDs to leverage our API. For example, with the Audit ID and the Run ID, you can trigger a specific export every time an audit completes. For another example, demonstrated below, you can send a custom email using Gmail's API.

//this is a function that fires when the webapp receives a GET request
function doGet(e) {
return HtmlService.createHtmlOutput("request received");
}

//this is a function that fires when the webapp receives a POST request
function doPost(e) {
var myData = JSON.parse(e.postData.contents);

var itemType = myData.itemType;
var itemID = myData.itemId;
var runID = myData.runId;

var email = '[email protected]';
var subject = 'Webhook Email Subject';
var htmlBody = 'This is where you will put a custom message. </br>' + 'Item Type: ' + itemType + ', Item ID: ' + itemID + ', Run ID: ' + runID;

//send email via Gmail API
MailApp.sendEmail(
email,
subject,
'',
{
htmlBody: htmlBody,
}
);
return HtmlService.createHtmlOutput("post request received");
}

Next, click on Deploy then New Deployment

  1. Then, under the setting icon, click on Web app.

  2. Next, select your google account as the executor and give access to 'Anyone' (so ObservePoint can properly trigger it).

  3. Finally, click Deploy and copy the Current web app URL, then paste it into the audit or Journey web-hook field.

Note: Each time you update the web-hook script, you will need to change the project version to New or the changes will not be published. Also, the URL does not need changed, so you don't have to worry about updating audit and Journey configurations.

Debugging

The fastest and simplest method of debugging / testing is to hard-code in your own values for:

var itemType = myData.itemType; // The item type will be an 'audit' or 'web-journey'.
var itemId = myData.itemId; // The item ID will contain the ID of the Audit or Web Journey.
var runId = myData.runId; // The run ID will contain the ID for the specific run of the Audit or Web Journey.

and comment out:

var myData = JSON.parse(e.postData.contents);


like so:

//var myData = JSON.parse(e.postData.contents);


Then you can run as many tests as you need using Google Scripts Logger.

Using ObservePoint's API

We provide great API documentation for your reference when building these webhooks.

Did this answer your question?