/webhooks-examples

Reusable and customizable code examples for how to utilize Mode webhooks.

MIT LicenseMIT

mode logo


webhooks-examples

Reusable and customizable code examples for how to utilize Mode webhooks.

Payload Enrichment

The hookrich module acts as an intermediate enrichment layer. It takes the content of a Mode webhook as inputs. For Example, a webhook for a Report Run Completed event:

{
  "event": "report_run_completed",
  "report_run_url": "https://modeanalytics.com/api/ORG/reports/REPORT_TOKEN/runs/REPORT_RUN_TOKEN"
}

and returns a dictionary with supplemental information about the event:

{
  "report_run": {
    "id": report_run_id,
    "token": report_run_token,
    "created_at": report__run_created_at,
    "state": report_run_state,
    "results": []
    ...
  },
  "report": {
    "name": report_name,
    "id": report_id,
    "created_at": report_created_at,
    "token": report_token,
    "url": report_url,
    ...
  },
  "space": {
    "name": space_name,
    "id": space_id,
    "token": space_token,
    "url": space_url,
    ...
  }
}

This result can then be used as the payload in a POST request. A library such as the requests python library will automatically form-encode the dictionary when the request is made.


Actions

The following examples are meant to serve as inspiration for building workflows using webhooks. The following examples are built to run on AWS Lambda and are triggered by an outgoing Mode webhook.

post_to_destination (source)

This module uses the output of the hookrich module and POSTs it to the specified destination URL. This destination could be a service such as Zapier, Slack, etc.

post_to_slack (source)

This module uses the output of the hookrich module to contextually create a Slack message depending on the event. This module also can be customized to send alerts based on query results.

Example output can be viewed below:

alt text

log_usage_csv (source)

This module uses Mode webhooks to log your organizations usage of Mode to a csv file.


Preparing Code for AWS Lambda

The easiest way to utilize this code in AWS lambda is to create a deployment package. The following steps serve as an example of how to go about creating a deployment package for the post_to_slack module.

cd ~
mkdir lambda-slack-deployment
cp ~/path-to/repo/examples/enrichment/hookrich.py ~/lambda-slack-deployment/
cp ~/path-to/repo/examples/aws_lambda/post_to_slack.py ~/lambda-slack-deployment/
pip install requests -t ~/lambda-slack-deployment

Once you have all the necessary files in the deployment package directory, you need to zip the contents of the directory. Once you have a .zip file containing all of the necessary code for the Lambda function, you can upload this file in the Lambda console.