Skip to content

Receiving and sending Messages to Users through a Google Chat App

  • Event ingestion via synchronous webhook. You can define an automatic response upon receiving the event through the webhook response.

    Event ingestion via synchronous webhook

  • You can now use the Google Chat API or leverage AI via AI··Task to let the AI automatically format and send the message.

    You can now use google chat

  • The payload also contains information about the commands defined in the configuration panel, allowing you to set conditions based on the triggered commands.

    Payload also contains information about commands

2. Sending Messages as Admin-Installed App

Section titled “2. Sending Messages as Admin-Installed App”

When an app is admin-installed, it has the authority to interact with users across the domain. To send a direct message (DM) to a specific user as the bot, you must follow a three-step sequence: identify the user, locate the specific DM space, and then post the message.

To message a user, you first need their internal Google unique identifier. You can retrieve this by filtering the Directory API using the user’s email address.

Admin privileges

Endpoint: GET https://admin.googleapis.com/admin/directory/v1/users

Terminal window
curl --request GET \
'https://admin.googleapis.com/admin/directory/v1/users?domain=yourdomain.com&query=email:jane.doe@yourdomain.com' \
--header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
--header 'Accept: application/json'
  • Key Field: From the JSON response, extract the id field (e.g., "12345678901234567890").

Key field

Google Chat requires a spaceName to send a message. For DMs, you use the findDirectMessage method. This looks up the existing DM space between your bot and the target user or creates one if it doesn’t exist.

Endpoint: GET https://chat.googleapis.com/v1/spaces:findDirectMessage

Terminal window
curl --request GET \
'https://chat.googleapis.com/v1/spaces:findDirectMessage?name=users/12345678901234567890' \
--header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
--header 'Accept: application/json'
  • Query Param: The name parameter must be formatted as users/{userId} using the ID from Step 1.
  • Key Field: Extract the name field from the response (e.g., "spaces/abcd12345").

Key field

Now that you have the spaceId, you can push the message content to that space.

Endpoint: POST https://chat.googleapis.com/v1/spaces/abcd12345/messages

Terminal window
curl --request POST \
'https://chat.googleapis.com/v1/spaces/abcd12345/messages' \
--header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
--header 'Content-Type: application/json' \
--data '{
"text": "Hello! This is a message from your admin-installed bot."
}'