

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# Amazon Connect Cases 活动流
<a name="case-event-streams"></a>

当在您的 Amazon Connect Cases 域中创建或修改案例时，Amazon Connect Cases 事件流将为您提供近实时更新。发布到流的事件包括这些资源事件：
+ 已创建案例
+ 已修改案例
+ 相关内容（评论、通话、聊天、任务）已添加到案例中

您可以使用案例事件流将流集成到您的数据湖解决方案中，创建显示案例绩效指标的控制面板，根据案例事件实施业务规则或自动操作，以及配置警报工具以触发特定案例活动的自定义通知。

**Topics**
+ [设置案例事件流](case-event-streams-enable.md)
+ [支持案例向 Contact Lens 规则发送更新](cases-rules-integration-onboarding.md)
+ [案例事件有效负载和架构](case-event-streams-sample.md)

# 设置 Amazon Connect Cases 活动流
<a name="case-event-streams-enable"></a>

本主题介绍如何设置和使用案例事件流。有些入职步骤要求您致电 [Amazon Connect 案例 APIs](https://docs.aws.amazon.com/cases/latest/APIReference/Welcome.html)。

## 步骤 1：创建 Amazon Connect 实例并启用 Customer Profiles
<a name="step1-case-event-streams-enable"></a>

1. 确保在其中一个 Cases 可用的 AWS 区域 中有一个正在运行的 Amazon Connect 实例。请参阅[不同区域的案例可用性](regions.md#cases_region)。

1. 启用 Amazon Connect Customer Profiles。有关说明，请参阅[为您的 Amazon Connect 实例启用 Customer Profiles](enable-customer-profiles.md)。

   Amazon Connect Cases 需要 Customer Profiles，因为每个案例都必须与 Customer Profiles 服务中的客户资料相关联。

## 步骤 2：将 Cases 域添加到您的 Amazon Connect 实例中
<a name="step2-case-event-streams-enable"></a>

有关说明，请参阅[使用 Amazon Connect 控制台启用 Cases](enable-cases.md)。

如果您想使用 API 添加案例域，请参阅《*Amazon Connect 案例 [CreateDomain](https://docs.aws.amazon.com/cases/latest/APIReference/API_CreateDomain.html)API 参考》中的 API*。

## 步骤 3：创建案例模板
<a name="step3-case-event-streams-enable"></a>

[创建案例模板](case-templates.md)。在*步骤 6：测试案例事件流*中，您将使用该模板。

如果您想使用 API 创建案例模板，请参阅 *Amazon Connect 案例 API 参考中的 API*。[CreateTemplate](https://docs.aws.amazon.com/cases/latest/APIReference/API_CreateTemplate.html)

## 步骤 4：启用案例事件流并设置为将事件接收到 SQS 队列中
<a name="step4-case-event-streams-enable"></a>

运行以下命令，为您的 Cases 域启用案例事件流。运行此命令后，当创建或更新案例时，事件将发布到您账户中 EventBridge 服务的默认总线（该总线必须与您的案例域 AWS 区域 相同）。

```
aws connectcases put-case-event-configuration --domain-id dad5efb6-8485-4a55-8241-98a88EXAMPLE --event-bridge enabled=true
```

默认情况下，Amazon Connect Cases 发布的事件仅包含有关案例的元数据，例如 `templateId`、`caseId`、`caseArn`、`approximateChangeTime` 等。您可以运行以下命令来获取有关要包含在事件中的案例（在生成事件时）的更多信息。

**注意**  
如果您想在事件中包含自定义字段，请使用自定义字段 ID。有关如何查找自定义字段 ID 的说明，请参阅[查找自定义字段 ID](cases-block.md#get-case-properties-find-uuid)。

```
# You can include any other field defined in your cases domain in the fields section.
# To list the fields that are defined in your cases domain, call the Cases ListFields API.
# To include case fields that you create (custom fields) in the event, enter the custom field ID.
aws connectcases put-case-event-configuration --domain-id YOUR_CASES_DOMAIN_ID --event-bridge "{
    \"enabled\": true, 
    \"includedData\": {
       \"caseData\": {
          \"fields\": [
          {
          \"id\": \"status\"
          },
          {
          \"id\": \"title\"
          },
          {
          \"id\": \"customer_id\"
          },
         {
          \"id\": \"your custom field ID\"
          }
        ]
      },
      \"relatedItemData\": {
      \"includeContent\": true
      }
    }
  }"
```

接下来，创建一个 Amazon SQS 队列并将其设置为 EventBridge 总线上的 Amazon Connect 案例事件的目标，以便将所有案例事件传送到 SQS 队列以供日后处理。

```
# Create an SQS queue
aws sqs create-queue --queue-name case-events-queue --attributes "{\"Policy\": \"{ \\\"Version\\\": \\\"2012-10-17\\\", \\\"Statement\\\": [{ \\\"Sid\\\": \\\"case-event-subscription\\\", \\\"Effect\\\": \\\"Allow\\\", \\\"Principal\\\": { \\\"Service\\\": \\\"events.amazonaws.com\\\"}, \\\"Action\\\": \\\"SQS:SendMessage\\\", \\\"Resource\\\": \\\"*\\\"}]}\"}"

# Create an rule on the EventBridge default bus that represents the case events
aws events put-rule --name case-events-to-sqs-queue --event-pattern "{\"source\": [\"aws.cases\"]}" --state ENABLED

# Ask event bridge to publish case events to the SQS queue.
aws events put-targets --rule case-events-to-sqs-queue --target "[{
\"Id\": \"target-1\",
\"Arn\": \"arn:aws:sqs:The AWS Region of your Amazon Connect instance:your AWS account ID:case-events-queue\"
}]"
```

## 步骤 5：测试案例事件流
<a name="step5-case-event-streams-enable"></a>

使用 Amazon Connect 座席应用程序执行以下操作：

1. 接受聊天联系人。

1. 创建客户资料并将其与聊天联系人相关联。

1. 创建案例。
**注意**  
在您接受联系人并将该联系人与客户资料相关联之前，**案例**选项卡上的**创建案例**按钮处于非活动状态。

导航至 Amazon SQS 控制台，检查您的 SQS 队列中是否有针对新创建案例的案例事件（类型：`CASE.CREATED`）。同样，您可以修改上一步中创建的案例，并在 SQS 队列中获取相应的案例事件（类型：`CASE.UPDATED`）。您可以将联系人与问题相关联，并对问题发表评论，以同样获取针对这些操作的案例事件。

## 步骤 6：案例事件流的使用案例
<a name="step6-case-event-streams-enable"></a>

每当创建案例、更新案例、联系人与案例关联以及对案例添加评论时，案例事件流都会发布事件。您可以将这些事件用于：
+ 指标、分析和控制面板
+ 构建通知用户（例如，发送电子邮件）的应用程序
+ 根据特定类型的案例更新触发的自动操作

例如，您可以在 on 上使用 SQS 目标 EventBridge （如步骤 4 所示）将案例事件临时存储在 SQS 队列中，并使用 Lambda 函数处理 SQS 中的事件以构建自定义应用程序，例如在客户案例更新时向其发送电子邮件、自动解决与该案例相关的所有任务等。同样，你可以使用上的 Firehose 目标将案例事件存储 EventBridge 到 S3 存储桶中，然后使用 ETL，Athena AWS Glue 用于临时分析，Quick 用于仪表板。

# 支持 Amazon Connect Cases 向 Contact Lens 规则发送更新
<a name="cases-rules-integration-onboarding"></a>

**注意**  
要执行此程序中的说明，您需要具备开发人员技能，或具有使用 Amazon Connect CLI 的经验。

完成此一次性程序，用户就可以设置在创建或更新案例时运行的规则。

1. 验证您的 Amazon Connect 实例是否[启用了](enable-cases.md) Amazon Connect Cases。

1. 完成启用 Amazon Connect Cases 活动流的步骤。有关更多信息，请参阅 [设置 Amazon Connect Cases 活动流](case-event-streams-enable.md)。请注意程序的以下变化：

   1. 您可以跳过要求您创建 SQS 队列的部分，因为这不是必需的。

   1. 运行以下 `put-case-event-configuration` CLI 命令以在事件中包含所有案例字段信息。确保包含运行规则引擎所需的所有字段。
**注意**  
为确保案例 SLA 违规规则正常运行，您必须将 `relatedItemData.includeContent` 设置为 `true`，如以下示例所示。

      ```
      aws connectcases put-case-event-configuration --domain-id 01310a0e-24ba-4a3c-89e9-9e1daeaxxxx --event-bridge "{
          \"enabled\": true, 
          \"includedData\": {
             \"caseData\": {
                 \"fields\": [
                   {
                     \"id\": \"status\"
                   },
                   {
                     \"id\": \"title\"
                   },
                   {
                     \"id\": \"assigned_queue\"
                   },
                   {
                     \"id\": \"assigned_user\"
                   },
                   {
                     \"id\": \"case_reason\"
                   },
                   {
                     \"id\": \"last_closed_datetime\"
                   },
                   {
                     \"id\": \"created_datetime\"
                   },
                   {
                     \"id\": \"last_updated_datetime\"
                   },
                   {
                     \"id\": \"reference_number\"
                   },
                   {
                     \"id\": \"summary\"
                   }
                 ]
            },
            \"relatedItemData\": {
            \"includeContent\": true
            }
          }
        }"
      ```

   1. 如果有自定义案例字段，请确保在之前的有效负载的字段数组中也包含一个自定义字段 ID。您可以 IDs 通过运行以下 `list-fields` CLI 命令来查找字段：

      ```
      aws connectcases list-fields --domain-id 01310a0e-24ba-4a3c-89e9-9e1daeaxxxx
      ```

   1. 如果需要添加新的自定义字段，请重复步骤 2。

1. 调用 [CreateEventIntegration](https://docs.aws.amazon.com/appintegrations/latest/APIReference/API_CreateEventIntegration.html)API 或运行 `create-event-integration` CLI 命令，如以下示例命令所示。
   + 有效负载：

     ```
     aws appintegrations create-event-integration --name amazon-connect-cases --description amazon-connect-cases --event-filter '{"Source":"aws.cases"}' --event-bridge-bus default
     ```
   + 输出与以下示例类似：

     ```
     {
         "EventIntegrationArn": "arn:aws:app-integrations:us-west-2:111222333444:event-integration/amazon-connect-cases"
     }
     ```

1. 调用 [CreateIntegrationAssociation](https://docs.aws.amazon.com/connect/latest/APIReference/API_CreateIntegrationAssociation.html)API 或运行 `create-integration-association` CLI 命令，如以下示例命令所示。
   + 有效负载：

     `IntegrationArn` 是您从步骤 3 中得到的响应。

     ```
     aws connect create-integration-association --instance-id bba5df5c-6a5f-421f-a81d-9c16402xxxx --integration-type EVENT --integration-arn arn:aws:app-integrations:us-west-2:111222333444:event-integration/amazon-connect-cases --source-type CASES
     ```
   + 输出将类似于以下示例：

     ```
     {
         "IntegrationAssociationId": "d49048cd-497d-4257-ab5c-8de797a123445",
         "IntegrationAssociationArn": "arn:aws:connect:us-west-2:111222333444:instance/bba5df5c-6a5f-421f-a81d-9c16402bxxxx/integration-association/d49048cd-497d-4257-ab5c-8de797a123445"
     }
     ```

现在，用户应该可以创建在创建或更新案例时运行的规则。

# Amazon Connect Cases 中的案例活动有效载荷和架构
<a name="case-event-streams-sample"></a>

当您请求在事件有效负载中包含案例数据时，数据会反映该特定编辑后的案例版本。

Amazon Connect 案例的默认限制可保证有效负载小于 256KB（ EventBus 事件的最大大小）。由于您可以自定义案例对象模型（例如，您可以在案例对象上定义自定义字段以捕获特定于业务的信息），因此案例事件架构反映了对案例对象所做的自定义，如以下示例所示（例如，查看如何将特定于客户的 UUIDs 用作 JSON 属性）。

## 案例资源的示例案例事件有效负载
<a name="example-case-event-payload"></a>

```
// Given the limits on the "includedData" configuration
// this payload is guaranteed to less than 256KB at launch.
{
    "version": "0",
    "id": "event ID",
    "detail-type": "Amazon Connect Cases Change",
    "source": "aws.cases",
    "account": "your AWS account ID",
    "time": "2022-03-16T23:43:26Z",
    "region": "The AWS Region of your Amazon Connect instance",
    "resources": [
        "arn:aws:cases:your Amazon Connect AWS Region:your AWS account ID:domain/case domain ID",
        "arn:aws:cases:your Amazon Connect AWS Region:your AWS account ID:domain/case domain ID/case/case ID"
    ],
    "detail": {
        "version": "0",
        "eventType": "CASE.UPDATED",
        "approximateChangeTime": "2022-03-16T23:16:57.893Z",  // Can be used for ordering
        "changedFieldIds": ["status", "last_updated_datetime"],
        "performedBy": {
            "user": {
                "userArn": "arn:aws:connect:your Amazon Connect AWS Region:your AWS account ID:instance/connect instance ID/user/connect user ID"        
            },
            "iamPrincipalArn": "arn:aws:iam::your Amazon Connect AWS Region:role/role name"
        },       
        "case": {
            "caseId": "case ID",
            "templateId": "template ID",
            "createdDateTime": "2022-03-16T23:16:57.893Z",
            
            // This section contains only non-null field values for the 
            // fields that customers have configured in the "includedData".
           
            // Field values included in this section reflects the case
            // after this particular change is applied.
            "fields": {
                "status": {
                    "value": {
                        "stringValue": "open"
                   }
                },
                "case_reason": {
                    "value": {
                        "stringValue": "Shipment lost"
                    }
                },
                "custom-field-uuid-1": {
                    "value": {
                        "stringValue": "Customer didn't receive the product"
                    }
                }
            }
        }
    }
}
```

## 相关项目资源的示例案例事件有效负载
<a name="example-case-event-payload"></a>

```
// Given the limits on the "includedData" configuration
// this payload is guaranteed to less than 256KB
{
    "version": "0",
    "id": "event ID",
    "detail-type": "Amazon Connect Cases Change",
    "source": "aws.cases",
    "account": "your AWS account ID",
    "time": "2022-03-16T23:43:26Z",
    "region": "The AWS Region of your Amazon Connect instance",
    "resources": [
        "arn:aws:cases:your Amazon Connect AWS Region:your AWS account ID:domain/case domain ID",
        "arn:aws:cases:your Amazon Connect AWS Region:your AWS account ID:domain/case domain ID/case/case ID/related-item/related-item ID"
    ],
    
    "detail": {   
        "version": "0",
        "eventType": "RELATED_ITEM.CREATED",
        "approximateChangeTime": "2022-03-16T23:16:57.893Z", // Can be used for ordering
        "changedAttributes": ["comment.commentText"],
        "performedBy": {
            "user": {
                "userArn": "arn:aws:connect:your Amazon Connect AWS Region:your AWS account ID:instance/connect instance ID/user/connect user ID"        
            },
            "iamPrincipalArn": "arn:aws:iam::your Amazon Connect AWS Region:role/role name"
        },        
        "relatedItem": {
            "relatedItemType": "Comment",
            "relatedItemId": "related-item ID",
            "caseId": "case id that this related item is a sub-resource of",
            "createdDateTime": "2022-03-16T23:16:57.893Z",
            
            // This section includes any attributes that customers have configured
            // in the "includedData" configuration.
            "comment": {               
                "body": "Gave a $5 refund to customer to make them happy",
            },
            
            // if the related item was of type contact.
            // "contact": {
            //      "contactArn": ".......",
            // }
        }
    }
}
```

## 自定义实体执行的案例资源的示例案例事件有效载荷
<a name="example-case-event-payload-case-resource-custom-entity"></a>

```
// Given the limits on the "includedData" configuration
// this payload is guaranteed to less than 256KB at launch.
{
    "version": "0",
    "id": "event ID",
    "detail-type": "Amazon Connect Cases Change",
    "source": "aws.cases",
    "account": "your AWS account ID",
    "time": "2022-03-16T23:43:26Z",
    "region": "The AWS Region of your Amazon Connect instance",
    "resources": [
        "arn:aws:cases:your Amazon Connect AWS Region:your AWS account ID:domain/case domain ID",
        "arn:aws:cases:your Amazon Connect AWS Region:your AWS account ID:domain/case domain ID/case/case ID"
    ],
    "detail": {
        "version": "0",
        "eventType": "CASE.UPDATED",
        "approximateChangeTime": "2022-03-16T23:16:57.893Z",  // Can be used for ordering
        "changedFieldIds": ["status", "last_updated_datetime"],
        "performedBy": {
            "user": {
                "customEntity": "your custom entity"        
            },
            "iamPrincipalArn": "arn:aws:iam::your Amazon Connect AWS Region:role/role name"
        },       
        "case": {
            "caseId": "case ID",
            "templateId": "template ID",
            "createdDateTime": "2022-03-16T23:16:57.893Z",
            
            // This section contains only non-null field values for the 
            // fields that customers have configured in the "includedData".
           
            // Field values included in this section reflects the case
            // after this particular change is applied.
            "fields": {
                "status": {
                    "value": {
                        "stringValue": "open"
                   }
                },
                "case_reason": {
                    "value": {
                        "stringValue": "Shipment lost"
                    }
                },
                "custom-field-uuid-1": {
                    "value": {
                        "stringValue": "Customer didn't receive the product"
                    }
                }
            }
        }
    }
}
```