How to Use Google Apps Script to Automate Google Chat Webhooks

The Era of ChatOps

In modern corporate environments, email is becoming obsolete for urgent notifications. If a critical server goes offline, or if a high-value sales contract is signed, teams do not want an email buried in their inbox. They want an instant, highly visible alert dropped directly into their team’s chat platform.

If your organization uses Google Workspace, the central hub for team communication is Google Chat. To bridge the gap between static databases (like Google Sheets) and real-time team communication, developers use Google Apps Script to programmatically trigger Incoming Webhooks.

1. Generating the Webhook URL

Before you can write any code, you must generate a secure endpoint for Google Chat to receive the incoming data.

  1. Open the specific Google Chat Space (e.g., “DevOps Alerts”).
  2. Click the Space name at the top of the screen and select Apps & integrations.
  3. Click Manage webhooks.
  4. Give your webhook a name (e.g., “Server Monitor”) and copy the incredibly long URL it generates. Keep this URL highly secure; anyone with this link can instantly post messages to your chat room.

2. Building the Apps Script Payload

Google Chat expects incoming data to be formatted as a strict JSON payload. You cannot simply send a raw text string; it must be wrapped in a specific JSON object.

Open script.google.com and paste the following function:

function sendGoogleChatAlert() {
  
  // 1. Define the highly secure Webhook URL
  var webhookUrl = "https://chat.googleapis.com/v1/spaces/XXXX/messages?key=YYYY&token=ZZZZ";
  
  // 2. Build the JSON Payload
  var payload = {
    "text": "?? *CRITICAL ALERT:* The primary database is experiencing high latency. Immediate investigation required."
  };
  
  // 3. Package the HTTP Request Options
  var options = {
    "method": "post",
    "contentType": "application/json",
    "payload": JSON.stringify(payload)
  };
  
  // 4. Execute the HTTP POST request to Google Chat
  UrlFetchApp.fetch(webhookUrl, options);
}

When you run this function, the UrlFetchApp class bypasses the Google Sheets interface entirely, executes an HTTP POST request to the Google Chat server, and the alert instantly appears in the Space.

3. Building Advanced Interactive Cards

Sending plain text is useful, but Google Chat allows you to send incredibly complex, interactive “Cards” featuring buttons, custom images, and highly structured data tables.

To send a card, you simply modify the JSON payload structure.

var payload = {
  "cardsV2": [
    {
      "cardId": "unique-card-id",
      "card": {
        "header": {
          "title": "New Sales Contract Signed!",
          "subtitle": "Acme Corp Ltd.",
          "imageUrl": "https://example.com/success_icon.png"
        },
        "sections": [
          {
            "widgets": [
              {
                "buttonList": {
                  "buttons": [
                    {
                      "text": "View Contract in Drive",
                      "onClick": {
                        "openLink": {
                          "url": "https://drive.google.com/..."
                        }
                      }
                    }
                  ]
                }
              }
            ]
          }
        ]
      }
    }
  ]
};

When executing this advanced payload, the chat room doesn’t just receive a text message; they receive a beautiful, heavily formatted UI card with a clickable button that routes them directly to the necessary Google Drive document.

Conclusion

By leveraging the UrlFetchApp class to trigger incoming webhooks, Google Apps Script allows developers to transform passive data sets into highly aggressive, real-time ChatOps alerting systems, dramatically reducing response times for critical business events.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.