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.
To asynchronously send, update, or delete a message in a Chat space with a Chat app, see Create, read, update, delete messages.
Prerequisites
To run the example in this guide, you need:
Python
- Python 3.10.7 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.
Apps Script
- Access to the internet and a web browser.
- A Google Workspace account with access to Google Chat.
- An existing Google Chat space.
Java
- Java 11 or greater.
- Apache Maven
- 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.
- Open the space to which you want to add a webhook.
- Next to the space title, click the expand more arrow, and then click Apps & integrations.
- Click Add webhooks.
- In the Name field, enter
Quickstart Webhook
. - In the Avatar URL field, enter
https://developers.google.com/chat/images/chat-product-icon.png
. - Click Save.
- To copy the webhook URL, click More, and then click Copy link.
Step 2: Write the webhook script
The example webhook script posts a message to the space in which the webhook is registered by POSTing a create message request to the webhook URL. Google Chat API responds with an instance of Message
.
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.
Apps Script
Go to the Apps Script page.
Click New Project
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.
Java
In your working directory, create a file named
pom.xml
.In
pom.xml
, copy and paste the following:In your working directory, create the following directory structure
src/main/java
.In the
src/main/java
directory, create a file namedApp.java
.In
App.java
, 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.
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
Apps Script
- Click Run
Java
mvn compile exec:java -Dexec.mainClass=App
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. Each threadKey
is unique to the app that sets it. If two different Chat apps or webhooks set the same threadKey
, two different threads start.
Start a message thread
To post the first message of a thread with a webhook, append the threadKey
and messageReplyOption
parameters 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&messageReplyOption=REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD
Reply to a message thread
To send a message to an existing message thread, append the threadKey
and messageReplyOption
parameters 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
and messageReplyOption
is REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD
:
https://chat.googleapis.com/v1/spaces/SPACE_ID/messages?threadKey=MY-THREAD&messageReplyOption=REPLY_MESSAGE_FALLBACK_TO_NEW_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 Chat app interaction events.
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.