订阅 Google Chat 活动

本页介绍了 Google Chat 应用可以使用 Google Workspace Events API 订阅的 Google Chat 事件。在确定所需的事件类型后,请创建订阅以开始接收来自 Google Chat 的事件。

除了订阅事件之外,您还可以通过调用 Chat API 来查询事件。通过调用 Chat API,您可以定期检索事件,或获取因服务中断而可能错过的订阅事件。如需了解如何接收和响应 Chat 事件,请参阅 Chat 文档中的处理来自 Google Chat 的事件

支持的 Chat 目标资源

Google Workspace Events API 支持订阅:

  • 聊天室,表示为 space 资源
  • 用户,表示为 Cloud Identity API user 资源

支持的 Chat 事件

通过 Google Workspace 订阅,您可以收到有关 Chat 中以下类型的更改的事件:

  • 聊天室中的新消息、更新消息或已删除的消息
  • 对消息新建或移除的回应
  • 新增、更新或移除聊天室中的成员
  • 对您订阅的聊天室的更改,例如更新后的聊天室名称或说明。

用于创建订阅的事件类型

创建订阅时,您可以使用 eventTypes[] 字段指定要接收哪些类型的事件。事件类型的格式遵循 CloudEvents 规范,例如 google.workspace.APPLICATION.RESOURCE.VERSION.ACTION

例如,如需接收关于用户加入 Chat 聊天室的事件,请将该聊天室指定为目标资源,并将事件类型指定为 google.workspace.chat.membership.v1.created。如需接收关于加入任何空间的给定用户的事件,请将该用户指定为目标资源,并将事件类型指定为 google.workspace.chat.membership.v1.created。如需详细了解事件的运作方式,请参阅 Google Workspace 事件的结构

下表列出了聊天室订阅和用户订阅支持哪些事件类型。如需详细了解触发事件的例外情况,请参阅限制

Event type 格式 资源数据
聊天室订阅  
消息已发布。

google.workspace.chat.message.v1.created

space.message

消息已更新。

google.workspace.chat.message.v1.updated

space.message

消息被删除。

google.workspace.chat.message.v1.deleted

space.message

系统会创建回应。

google.workspace.chat.reaction.v1.created

space.message.reaction

系统会删除回应。

google.workspace.chat.reaction.v1.deleted

space.message.reaction

系统会将一位成员添加到聊天室。

google.workspace.chat.membership.v1.created

space.membership

有成员在聊天室中更新。

google.workspace.chat.membership.v1.updated

space.membership

系统会将一位成员从聊天室中移除。

google.workspace.chat.membership.v1.deleted

space.membership

聊天室已更新。

google.workspace.chat.space.v1.updated

space

聊天室已被删除。

google.workspace.chat.space.v1.deleted

space

用户的订阅  
用户成为聊天室成员。

并非所有新成员都会触发事件。如需了解详情,请参阅限制

google.workspace.chat.membership.v1.created

space.membership

系统会更新该用户在聊天室中的成员资格。

google.workspace.chat.membership.v1.updated

space.membership

该用户不再是聊天室的直接成员。

google.workspace.chat.membership.v1.deleted

space.membership

批量事件类型(仅限输出)

除了接收您订阅的事件类型外,您的 Chat 应用可能还会接收批量事件。批量事件是指在短时间内发生的许多相同类型的事件。批量事件的载荷包含所有已更改资源的列表。

例如,如果用户同时将 20 位用户添加到聊天室,您的 Chat 应用可能会收到批量事件 (google.workspace.chat.membership.v1.batchCreated)。事件载荷包含用户将成员添加到聊天室时创建的所有新 Membership 资源的列表。

您订阅的任何事件类型都会收到批量事件,因此您无需在创建订阅时指定批量事件。例如,如果您订阅了新的回应 (google.workspace.chat.reaction.v1.created),则您的 Chat 应用会自动配置为接收批量回应事件 (google.workspace.chat.reaction.v1.batchCreated)。

下表显示了订阅可能的批量事件:

批量事件类型 格式
发布了多条消息。

google.workspace.chat.message.v1.batchCreated

系统会更新多条消息。

google.workspace.chat.message.v1.batchUpdated

系统会删除多封邮件。

google.workspace.chat.message.v1.batchDeleted

系统会创建多个回应。

google.workspace.chat.reaction.v1.batchCreated

系统会删除多条回应。

google.workspace.chat.reaction.v1.batchDeleted

多个成员被添加到订阅的聊天室中,或者订阅的用户被添加到多个聊天室。

google.workspace.chat.membership.v1.batchCreated

系统会在订阅聊天室中更新多个成员资格,或者为订阅用户更新多个成员资格。

google.workspace.chat.membership.v1.batchUpdated

系统会将多个成员从订阅的聊天室中移除,或者将订阅的用户从多个聊天室中移除。

google.workspace.chat.membership.v1.batchDeleted

此聊天室有多项更新。

google.workspace.chat.space.v1.batchUpdated

事件数据

本部分介绍了 Chat 中的事件数据和事件载荷示例。

当您的 Google Workspace 订阅收到来自 Chat 的事件时,data 字段会包含该事件的载荷。此载荷包含有关发生更改的 Google Workspace 资源的信息。例如,如果您在聊天室中订阅了会员活动,则这些事件的载荷将包含有关发生更改的 spaces.membership 资源的信息。

事件载荷中的资源数据

创建订阅时,您可以指定是希望载荷包含资源的详细信息,还是仅包含资源名称。例如,如果您想接收 Chat 聊天室中成员的事件,则可以指定要在事件载荷中接收成员资格资源的哪些字段。

下表提供了 Chat 聊天室 spaces/AAAABBBBBB 订阅的 JSON 载荷示例。对于订阅接收的每个事件,载荷都会显示在事件的 data 字段中:

示例 Event type JSON 载荷

用户在聊天室中发布一条内容为“Hello world”的消息。

google.workspace.chat.message.v1.created

包含资源数据
{
    "message":
    {
        "name": "spaces/AAAABBBBBB/messages/CCCCCCCCC.DDDDDDDDD",
        "sender":
        {
            "name": "users/1234567890987654321",
            "type": "HUMAN"
        },
        "createTime": "2023-09-07T21:37:36.260127Z",
        "text": "Hello world",
        "thread":
        {
            "name": "spaces/AAAABBBBBB/threads/EEEEEEEEEEEE"
        },
        "space":
        {
            "name": "spaces/AAAABBBBBB"
        },
        "argumentText": "Hello world"
    }
}
      
不包括资源数据
{
    "message":
    {
        "name": "spaces/AAAABBBBBB/messages/CCCCCCCCC.DDDDDDDDD"
    }
}
      
用户成为聊天室管理员。

google.workspace.chat.membership.v1.updated

包含资源数据
{
    "membership":
    {
        "name": "spaces/AAAABBBBBB/members/1234567890987654321",
        "state": "JOINED",
        "member":
        {
            "name": "users/1234567890987654321",
            "type": "HUMAN"
        },
        "createTime": "1970-01-01T00:00:00Z",
        "role": "ROLE_MANAGER"
    }
}
      
不包括资源数据
{
    "membership":
    {
        "name": "spaces/AAAABBBBBB/members/1234567890987654321"
    }
}
      
用户将聊天室说明更新为“Sales team for Cymbal Labs”。 google.workspace.chat.space.v1.updated
包含资源数据
{
    "space":
    {
        "name": "spaces/AAAABBBBBB",
        "displayName": "Cymbal Sales",
        "spaceThreadingState": "THREADED_MESSAGES",
        "spaceType": "SPACE",
        "spaceDetails":
        {
            "description": "Sales team for Cymbal Labs."
        },
        "spaceHistoryState": "HISTORY_ON"
    }
}
      
不包括资源数据
{
    "space":
    {
        "name": "spaces/AAAABBBBBB"
    }
}
      
此聊天室同时添加了两名 Chat 用户。 google.workspace.chat.membership.v1.batchCreated
包含资源数据
{
    "memberships": [
        {
          "membership": {
            "name": "spaces/AAAABBBBBB/members/1234567890987654321",
            "state": "JOINED",
            "member":
            {
                "name": "users/1234567890987654321",
                "type": "HUMAN"
            },
            "createTime": "1970-01-01T00:00:00Z",
            "role": "ROLE_MEMBER"
          }
        },
        {
          "membership": {
            "name": "spaces/AAAABBBBBB/members/987654321234567890",
            "state": "JOINED",
            "member":
            {
                "name": "users/987654321234567890",
                "type": "HUMAN"
            },
            "createTime": "1970-01-01T00:00:00Z",
            "role": "ROLE_MEMBER"
          }
        }
    ]
}
      
不包括资源数据
{
    "memberships": [
        {
          "membership": {
            "name": "spaces/AAAABBBBBB/members/1234567890987654321"
          }
        },
        {
          "membership": {
            "name": "spaces/AAAABBBBBB/members/98765432123456789019"
          }
        }
    ]
}
      
用户使用 😊? 表情符号回应消息。 google.workspace.chat.reaction.v1.created
包含资源数据
{
    "reaction":
    {
        "name": "spaces/AAAABBBBBB/messages/123456789.123456789/reactions/1111111111111111.222222222222222",
        "user":
        {
            "name": "users/1234567890987654321",
            "type": "HUMAN"
        },
        "emoji":
        {
            "unicode": "😊"
        }
    }
}
      
省略资源数据
{
    "reaction":
    {
        "name": "spaces/AAAABBBBBB/messages/123456789.123456789/reactions/1111111111111111.222222222222222"
    }
}
      
用户使用 😊? 表情符号和 😸? 表情符号来回应消息。 google.workspace.chat.reaction.v1.batchCreated
包含资源数据
{
    "reactions": [
        {
          "reaction": {
            "name": "spaces/AAAABBBBBB/messages/123456789.123456789/reactions/1111111111111111.222222222222222",
            "user":
            {
                "name": "users/1234567890987654321",
                "type": "HUMAN"
            },
            "emoji":
            {
                "unicode": "😊"
            }
          }
        },
        {
          "reaction": {
            "name": "spaces/AAAABBBBBB/messages/123456789.123456789/reactions/3333333333333333.444444444444444",
            "user":
            {
                "name": "users/98765431234564321",
                "type": "HUMAN"
            },
            "emoji":
            {
                "unicode": "😸"
            }
          }
        }
    ]
}
      
省略资源数据
{
    "reactions": [
      {
        "reaction": {
            "name": "spaces/AAAABBBBBB/messages/123456789.123456789/reactions/1111111111111111.222222222222222"
        },
        "reaction": {
            "name": "spaces/AAAABBBBBB/messages/123456789.123456789/reactions/3333333333333333.444444444444444",
        }
      }
    ]
}
      

限制

  • 对于用户的订阅,私信或未命名群聊 (google.workspace.chat.membership.v1.created) 中与新成员相关的事件,仅在第一条消息发布后触发。
  • 要接收会员活动,用户必须是聊天室的直接成员。如果通过 Google 群组向聊天室间接添加、更新或移除用户,则订阅不会接收这些成员资格事件。如需了解 Google 群组成员资格的工作原理,请参阅将 Google 群组添加到聊天室