Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Capturing Webhooks With Google Sheets

DZone's Guide to

Capturing Webhooks With Google Sheets

In this blog post, I'm going to walk through using Google Sheets as a quick alternative to listening for webhook notifications.

· Integration Zone ·
Free Resource

Learn more about how to Prevent Slow or Broken APIs From Affecting Your Bottom Line.

Runscope has a number of different integrations that allow you to leverage other tools in conjunction with your Runscope API monitors. However, for tools that are not included in our list of built-in integrations, we allow you to use Webhook notifications to automate your process. Webhook notifications are POST requests that are made at the completion of a test and include a payload with details about the test run.

Here's a snippet of what the webhook JSON data looks like:

{
   "test_id": "76598752-cbda-4e1d-820f-6274a62f74ff",
   "test_name": "Buckets Test",
   "team_name": "Acme Inc.",
   "environment_uuid": "98290cfc-a008-4ab7-9ea4-8906f12b228f",
   "environment_name": "Staging Settings",
   "bucket_name": "Rocket Sled",
   "bucket_key": "bucket_key",
   "test_run_url": "https://www.runscope.com/radar/bucket_key/76598752-cbda-4e1d-820f-6274a62f74ff/results/9c15aa62-21f0-48f2-a819-c99bdf8e4543",
   "result": "pass",
   "started_at": 1384281308.548077,
   "finished_at": 1384281310.680218,
   "region_name": "US East - Northern Virginia",
   ...
}

Runscope has two options for webhooks:

  • Basic Webhooks: fired on completion of every test run. They can be activated in your test environment settings -> Webhooks.
  • Custom Webhooks: you can specify failure only for a threshold of consecutive failures (3, for example) before sending the POST request. They can be added by going to Connected Services and setting the required parameters, and then activated in your test environment settings -> Integrations.

Either way, Runscope makes it easy for you to build your own integrations. However, receiving a webhook requires some piece of infrastructure that will receive and process the POST request, and some customers might not have their own infrastructure readily available, or the resources to create custom endpoints.

In this blog post, I'm going to walk through using Google Sheets as a quick alternative to listening for webhook notifications.

Using Google Sheets as a Web App

Google Sheets has a rich scripting language that allows you to do all sorts of automation for your spreadsheets. Last year I wrote about using a script with a Google Sheets to easily trigger a Runscope test using different initial variables. One feature Google includes is the ability to deploy your script as a web app. When you deploy your script as a web app, you then have a URL you can make GET or POST requests to in order to initiate your script.

Using this Google Sheets feature, we can then receive POST requests without needing a custom piece of infrastructure. For this post, I wrote a sample script to process webhooks that you can find here.

To deploy a script as a web app, Google requires you to have two functions -- doGet and doPost. In this case, we won't be using the doGet function. The webhook notification will fire the doPost(e) function, which is what we will focus on.

In order to enable this script, follow these steps:

  1. From your Google Sheet, from the Tools menu select Script Editor...
  2. Paste the script from above into the script code editor and hit Save.Image title
  3. From the Publish menu, select Deploy as web app…
  4. Choose to execute the app as yourself, and allow Anyone, even anonymous to execute the script. (Note, depending on your Google Apps instance, this option may not be available. You will need to contact your Google Apps administrator, or else use a Gmail account.) Now click Deploy. You may be asked to review permissions now.Image title
  5. The URL that you get will be the webhook that you can use in your Runscope API monitor (for both basic and custom webhooks). You can test this webhook in your browser first by pasting it. It will say “request received” if you have things set up correctly. You should also see a cell in your spreadsheet populate. Note that depending on your Google Apps instance, you may need to adjust the URL to make it work. 

If your URL is not:

https://script.google.com/macros/s/UNIQUE_KEY_HERE/exec

Or:

https://script.google.com/a/YOUR_DOMAIN.com/macros/s/UNIQUE_KEY_HERE/exec

Take the unique key from the URL you have and use it in the first format above.

Customizing Your Google Apps Script

Now that you have a webhook receiver set up, you can customize what exactly happens if you want to do more than just capture the information in the spreadsheet. You can do this by editing the doPost function in the script.

For example, if you want to send a customized email notification for the test as well, you could append this to the doPost function:

var emailAddress = "email_to_send_to.com";
var subject = "Runscope Test Result: " + testRunName + " || Result: " + testRunResult;
var message = params; //"Sending emails from a Spreadsheet";
MailApp.sendEmail(emailAddress, subject, message, {
  name: 'Your Name Here'
});

After making changes to the script, be sure to save the script and then again do Publish|Deploy as Web App. The URL will stay the same, but the functionality will be updated.

Flying High

The Google sheets scripting language is very powerful, and it includes the ability to make external API requests or to interact with other Google services (such as mail). As I mentioned at the beginning, you can customize it so you get an email notification after an API monitor fails X amount of times, or just in certain locations.

Your imagination is really the limit of what you can do. 

Learn about the Five Steps to API Monitoring Success with Runscope

Topics:
integration ,webhooks ,google sheets ,api integration

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}