> ## Documentation Index
> Fetch the complete documentation index at: https://docs.ycloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 联系人及便签更新 Webhook 示例

> 处理联系人属性变更和联系人便签更新。

> [!NOTE]
>
> 如需查看基于 Schema 衍生的完整目录，请参阅[所有示例](https://docs.ycloud.com/zh/api-reference/guides/examples/webhook-examples/webhook-payload-examples)。

## 功能概述

处理 `contact.attributes_changed` 和 `contact.note.updated` 事件。

## 准备工作

* 在您的应用程序中创建一个公开的 HTTPS 端点。
* 为您需要的事件类型配置 YCloud Webhook 端点。
* 安全存储端点签名密钥。
* 确保事件处理具备幂等性。

## 工作原理

当事件发生时，YCloud 会发送一个 HTTP `POST` 请求。验证签名，持久化记录该事件，返回 `2xx` 响应，并异步处理耗时任务。

## 请求

以下场景展示了投递到您的 Webhook URL 的请求。将事件 `id` 视为投递标识符，并使用 `type` 对有效载荷进行路由。

## 响应

在接收事件后返回 `2xx` 状态。

```http theme={"theme":{"light":"github-light","dark":"github-dark"}}
HTTP/1.1 200 OK
```

> [!NOTE]
>
> 有关端点设置、签名验证和重试机制，请参阅[配置 Webhook](https://docs.ycloud.com/zh/api-reference/guides/api-fundamentals/configure-webhooks.md)。

当联系人的属性被修改时触发。Webhook 有效载荷包含有关更改了哪些属性的信息，包括其旧值和新值。

## 标签变更示例（添加标签）

### 请求

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{
  "id": "evt_1234567892",
  "type": "contact.attributes_changed",
  "apiVersion": "v2",
  "createTime": "2024-01-01T12:00:00.000Z",
  "contactAttributesChanged": {
    "id": "1824266594102064128",
    "updateTime": "2024-01-01T12:00:00.000Z",
    "changedAttributes": {
      "tags": {
        "oldValue": ["premium", "newsletter"],
        "newValue": ["premium", "newsletter", "vip"],
        "extra": [
          {
            "action": "ADDED",
            "id": "686dd294334be8606a5bf312",
            "value": "vip"
          }
        ]
      }
    }
  }
}
```

### 响应

在持久化接收事件后确认投递。

```http theme={"theme":{"light":"github-light","dark":"github-dark"}}
HTTP/1.1 200 OK
```

### 说明

根据 `type` 路由事件，通过 `id` 进行去重，并将耗时或容易失败的任务移至异步处理器。

## 标签变更示例（移除标签）

### 请求

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{
  "id": "evt_1234567893",
  "type": "contact.attributes_changed",
  "apiVersion": "v2",
  "createTime": "2024-01-01T12:00:00.000Z",
  "contactAttributesChanged": {
    "id": "1824266594102064128",
    "updateTime": "2024-01-01T12:00:00.000Z",
    "changedAttributes": {
      "tags": {
        "oldValue": ["premium", "newsletter", "vip"],
        "newValue": ["premium", "newsletter"],
        "extra": [
          {
            "action": "REMOVED",
            "id": "686dd294334be8606a5bf312",
            "value": "vip"
          }
        ]
      }
    }
  }
}
```

### 响应

在持久化接收事件后确认投递。

```http theme={"theme":{"light":"github-light","dark":"github-dark"}}
HTTP/1.1 200 OK
```

### 说明

根据 `type` 路由事件，通过 `id` 进行去重，并将耗时或容易失败的任务移至异步处理器。

## 变更示例

### 请求

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{
  "id": "evt_1234567900",
  "type": "contact.attributes_changed",
  "apiVersion": "v2",
  "createTime": "2024-01-01T12:00:00.000Z",
  "contactAttributesChanged": {
    "id": "1824266594102064128",
    "updateTime": "2024-01-01T12:00:00.000Z",
    "changedAttributes": {
      "waba_id": {
        "oldValue": "waba_old_123456",
        "newValue": "waba_new_789012",
        "extra": [
          {
            "action": "CHANGED"
          }
        ]
      }
    }
  }
}
```

### 响应

在持久化接收事件后确认投递。

```http theme={"theme":{"light":"github-light","dark":"github-dark"}}
HTTP/1.1 200 OK
```

### 说明

* **正确结构**：`contactAttributesChanged` 对象包含三个必填字段：`id`、`updateTime` 和 `changedAttributes`。
* **已更改的属性**：`changedAttributes` 字段是一个对象，其中每个键为属性名称，值为 `ContactAttributeChange` 对象。
* **事件属性配置**：启用 `contact.attributes_changed` 事件时，必须在 Webhook 端点中配置 `eventProperties` 字段，以指定应包含在 Webhook 有效载荷中的属性。
* **属性类型**：`oldValue` 和 `newValue` 字段可包含不同的数据类型：
  * String：文本值
  * Number：数值（整数或小数）
  * Boolean：布尔值（true/false）
  * Array：字符串数组
  * Object：包含嵌套属性的复杂对象
  * Null：当属性被移除或取消设置时
* **标签变更**：对于标签属性，`extra` 字段提供有关添加或移除标签的详细信息，包括其 ID 和值。
* **Null 值**：当属性被移除或设置为 null 时，`newValue` 将为 `null`。当新增属性时，`oldValue` 可能为 `null`。
* **自定义属性**：自定义属性与标准属性一样包含在 `changedAttributes` 对象中，并使用自定义键作为属性名称。
* **变更检测**：只有实际发生更改的属性才会包含在 `changedAttributes` 对象中。
* **额外字段**：`extra` 字段是可选的，主要用于与标签相关的更改，以提供有关所执行特定操作的附加上下文。

## 联系人便签已更新

当联系人便签更新时，使用 `contact.note.updated`。

### 请求

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{
  "id": "evt_2234567890abcdef12345678",
  "type": "contact.note.updated",
  "apiVersion": "v2",
  "createTime": "2024-01-02T12:00:00.000Z",
  "contactNote": {
    "id": "6a3de646e18f344f743aaa4d",
    "contactId": "1824266594102064128",
    "username": "alice_01",
    "phoneNumber": "+16315551111",
    "content": "Customer now prefers afternoon follow-up.",
    "createTime": "2024-01-01T12:00:00.000Z",
    "updateTime": "2024-01-02T12:00:00.000Z"
  }
}
```

### 响应

在持久化接收事件后确认投递。

```http theme={"theme":{"light":"github-light","dark":"github-dark"}}
HTTP/1.1 200 OK
```

### 说明

使用 `contactNote.id` 标识便签，使用 `contactId` 标识其归属的联系人。`username` 和 `phoneNumber` 描述该联系人，且可以为 null。有效载荷包含面向客户的便签快照，包括 `content`，不包含内部操作员标识符。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.