# Receiving and sending Messages to Users through a Google Chat App

## 1. Receiving Events

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

  <div class="docs-screenshot-compact">

  ![Event ingestion via synchronous webhook](./assets/event-ingestion-via-synchronous-webhook.png)

  </div>

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

   <div class="docs-screenshot-compact">

   ![You can now use google chat](./assets/you-can-now-use-google-chat.png)

   </div>

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

   <div class="docs-screenshot-compact">

   ![Payload also contains information about commands](./assets/payload-also-contains-information-about-commands.png)

   </div>

## 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.

:::note

## Requirements

- **Authentication:** Your supporting Mindflow credential must have the appropriate OAuth scopes (e.g., `https://www.googleapis.com/auth/chat.messages.create` and `https://www.googleapis.com/auth/admin.directory.user.readonly`).
- **Admin Privileges:** Since the app is admin-installed, ensure the Service Account or OAuth client used has been granted Domain-Wide Delegation or the necessary permissions in the Google Cloud Console.

:::

### Step 1: Collect the User ID

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.

   <div class="docs-screenshot-compact">

   ![Admin privileges](./assets/admin-privileges.png)

   </div>

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

```bash
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"`).

### Step 2: Find the Direct Message Space

   <div class="docs-screenshot-compact">

   ![Key field](./assets/key-field.png)

   </div>

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`

```bash
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"`).

### Step 3: Create and Send the Message

   <div class="docs-screenshot-compact">

   ![Key field](./assets/key-field-2.png)

   </div>

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`

```bash
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."
  }'
```
