Google Chat Cloud Function

Here is an overview of how to set up Google Chat Workspace as an alerting destination using Metaplane Webhooks.

Note: This method DOES NOT require Zapier (or similar) Middleware. This method will utilize Google's Cloud Function to transform the Metaplane payload into the expected format to send our Webhooks to your desired Google Chat Workspace. Please reach out with any questions after you set up.

Steps:

  1. Add a Webhook to Google Chat
  2. Set up a Google Cloud Function.
  3. Write a function that receives the Metaplane webhook data, extracts or restructures it, and then sends a new request to Google Chat's webhook URL.
  4. Deploy and configure the function to listen to the Metaplane webhook.

How to add a Webhook in Google Chat:

Open Google Chat:

  1. Navigate to Google Chat in your browser or open the Google Chat app.
  2. Select the chat space or room where you want to add the webhook

Access Space Settings:

  1. In the chat space, click the space name at the top of the window to open the space details.
  2. Click on the gear icon or "Manage webhooks and apps" to manage the integrations for that space.

Add a Webhook:

  1. Scroll down to the Integrations section.
  2. Click Add webhook. If the option is greyed out or not available, it might be due to permissions or plan limitations (e.g., Google Workspace Individual plan).
  3. Give the Webhook a name (e.g., "Metaplane Alerts").
  4. Click Save or Create.

Copy the Webhook URL:

  1. Once the webhook is created, you will be provided with a Webhook URL.
  2. Copy this URL, as it will be used in your Google Cloud Function in the steps below

How to set up a Google Cloud Function:

Create a Google Cloud Project

  1. Go to the Google Cloud Console.
  2. Click the project dropdown at the top of the page, and select New Project.
  3. Name your project and click Create.

Enable Cloud Functions API

  1. From your Google Cloud Console open the Navigation Menu (☰) > APIs & Services > Library.
  2. Search for "Cloud Functions" and click Enable.

Create a Cloud Function

  1. In the Navigation Menu, go to Cloud Functions > Create Function.
  2. Configure your function:
    Name: Provide a name for your function (e.g., metaplane-to-googlechat).
    Region: Choose the region where you'd like to deploy the function.
    Trigger Type: Set the trigger to HTTP (this allows the function to respond to HTTP POST requests)
    Authentication: Leave it set to "Allow unauthenticated invocations" for now. You can adjust security later.

Write the Cloud Function Code

  1. On the Code page, in the Runtime section, select your language (e.g., Python 3.9, Node.js, etc.) I’ve provided the set up for Python so select that unless writing your own code

Python Example:

  1. In the Inline Editor, modify the main.py file:

Be sure to grab your GChat Webhook URL you created from within your workspace and add it to the script below for the google_chat_webhook_url

import json
import requests
from flask import Flask, request, jsonify

app = Flask(__name__)

# Hardcoded webhook URL
WEBHOOK_URL = 'INSERT_GCHAT_WEBHOOK_URL_HERE'

def metaplane_to_googlechat(request):
    try:
        # Parse the incoming request
        incoming_payload = request.get_json()

        if not incoming_payload:
            raise ValueError("No JSON payload received")

        # Reformat the Metaplane payload if necessary
        reformatted_payload = {
            "text": f"Received payload: {json.dumps(incoming_payload)}"
        }

        # Send the reformatted payload to Google Chat
        response = requests.post(WEBHOOK_URL, json=reformatted_payload)

        if response.status_code == 200:
            return jsonify({"status": "success"}), 200
        else:
            app.logger.error(f"Google Chat response: {response.text}")
            return jsonify({"status": "failure", "message": response.text}), response.status_code

    except Exception as e:
        app.logger.error(f"Exception occurred: {str(e)}")
        return jsonify({"status": "error", "message": str(e)}), 500

# Define the entry point for Cloud Functions
def entry_point(request):
    return metaplane_to_googlechat(request)

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8080)

  • Note that if you renamed your Entry point, you will want to update that within the script above. In the example above I have the entry point as metaplane_to_googlechat
  1. In the requirements.txt file, if using the provided script, replace the text with the following:

    Flask==2.3.2 requests==2.31.0

Deploy the Function

  1. After writing the code, click Deploy. The deployment process will take a few minutes.
  2. Once the function is deployed, it will give you an HTTP endpoint (URL) that you can use as your Metaplane Webhook. (eg., https://us-central1-webhook-testing.cloudfunctions.net/Metaplane-goooglechat-webhook)

Set up the Webhook in Metaplane

  • Within your Metaplane settings, enable the Webhooks integration giving it a name of your choice and entering in the URL from the previous step

Verify

  • Once the Webhooks is enabled, from the Actions dropdown, click on Verify to have Metaplane send you a sample incident to your Webhook destination