Incoming webhooks let you send asynchronous messages into Google Chat from applications that aren't Chat apps. For example, you can configure a monitoring application to notify on-call personnel on Google Chat when a server goes down.
Prerequisites
To run the example in this guide, you need:
Python
- Python 2.6 or greater.
- Access to the internet and a web browser.
- A Google Workspace account with access to Google Chat.
- An existing Google Chat space.
The
httplib2
library. If necessary, run the following command-line interface (CLI) command to install the library using pip:pip install httplib2
Node.js
- Node.js & npm installed.
- Access to the internet and a web browser.
- A Google Workspace account with access to Google Chat.
- An existing Google Chat space.
Create a webhook
To create a webhook, register it in the Google Chat space you want to receive messages, then write a script that sends messages.
Step 1: Register the incoming webhook
- Open Google Chat in a web browser.
- Go to the space to which you want to add a webhook.
- At the top, next to space title, click
Manage webhooks.
Down Arrow > - If this space already has other webhooks, click Add another. Otherwise, skip this step.
- For Name, enter "Quickstart Webhook".
- For Avatar URL, enter
https://developers.google.com/chat/images/chat-product-icon.png
. - Click SAVE.
- Click Copy to copy the full webhook URL.
- Click outside the box to close the Incoming webhooks dialog.
Step 2: Write the webhook script
The example webhook script creates a message and sends it to the webhook URL with a POST request.
Choose a language below for specific instructions about how to create the webhook script:
Python
In your working directory, create a file named
quickstart.py
.In
quickstart.py
, copy-and-paste the following code:Replace the value for the
url
variable with the webhook URL you copied in Step 1: Register the incoming webhook.
Node.js
In your working directory, create a file named
index.js
.In
index.js
, copy-and-paste the following code:Replace the value for the
webhookURL
variable with the webhook URL you copied in Step 1: Register the incoming webhook.
Step 3: Run the webhook script
Run the sample by running the following command from your working directory in your CLI:
Python
python3 quickstart.py
Node.js
node index.js
When you run the sample code, the webhook sends a message to the space in which you registered it.
Start or reply to a message thread
You can start a message thread or reply to one by adding the threadKey
parameter to the webhook URL.
Start a message thread
To post the first message of a thread with a webhook, append the threadKey
parameter to the webhook URL. Set the threadKey
to an arbitrary string, but remember what it is; you'll need to specify it again to post a reply to the thread.
https://chat.googleapis.com/v1/spaces/SPACE_ID/messages?threadKey=ARBITRARY_STRING
Reply to a message thread
To send a message to an existing message thread, append the threadKey
parameter to the webhook URL set to the value used to start the thread. For example, sending a message to following URL posts a reply to the thread where threadKey
is MY-THREAD
:
https://chat.googleapis.com/v1/spaces/SPACE_ID/messages?threadKey=MY-THREAD
Limits and considerations
As you configure webhooks, take note of these limits and considerations:
Each webhook only works in the Chat space in which it's registered.
You can't publish webhooks on the Google Workspace Marketplace.
Webhooks aren't conversational. They can't respond to or receive messages from users or events from Google Chat.
If only select organizational units (OUs) in your domain have Chat apps enabled, incoming webhooks return the following error:
{ "error": { "code": 403, "message": "The caller does not have permission", "status": "PERMISSION_DENIED" } }
Incoming webhooks work in direct messages, but only when all users have Chat apps enabled.
Because
Manage webhooks is only available from a web browser, webhooks must be set up from the Chat web app. Webhooks are not configurable from the Chat mobile app.