

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 設定 Amazon Connect Cases 事件串流
<a name="case-event-streams-enable"></a>

本主題介紹如何設定和使用案例事件串流。某些入職步驟需要您呼叫 [Amazon Connect Cases API](https://docs.aws.amazon.com/cases/latest/APIReference/Welcome.html)。

## 步驟 1：建立 Amazon Connect 執行個體並啟用客戶設定檔
<a name="step1-case-event-streams-enable"></a>

1. 確保您在可使用 Cases 的 AWS 區域 內有可用的 Amazon Connect 執行個體。請參閱 [案例可用性 (按區域)](regions.md#cases_region)。

1. 啟用 Amazon Connect Customer Profiles。如需說明，請參閱[為您的 Amazon Connect 執行個體啟用客戶設定檔。](enable-customer-profiles.md)。

   Amazon Connect Cases 需要客戶設定檔，因為每個案例都必須與客戶設定檔服務中的客戶個人檔案關聯。

## 步驟 2：新增案例網域到 Amazon Connect 執行個體
<a name="step2-case-event-streams-enable"></a>

如需說明，請參閱[透過 Amazon Connect 主控台啟用 Cases](enable-cases.md)。

如果您想要使用 API 新增案例網域，請參閱 *Amazon Connect Cases API Reference* 中的 [CreateDomain](https://docs.aws.amazon.com/cases/latest/APIReference/API_CreateDomain.html) API。

## 步驟 3：建立案件範本
<a name="step3-case-event-streams-enable"></a>

[建立案例範本](case-templates.md)。在*步驟 6：測試案例事件串流*中，您將使用範本。

如果您想要使用 API 建立案例範本，請參閱 *Amazon Connect Cases API 參考*中的 [CreateTemplate](https://docs.aws.amazon.com/cases/latest/APIReference/API_CreateTemplate.html) API。

## 步驟 4：啟用案例事件流和設定，將事件接收到 SQS 佇列
<a name="step4-case-event-streams-enable"></a>

執行下列命令，為您的案例網域啟用案例事件串流。執行此命令之後，當案例建立或更新時，事件會發佈到您帳戶中 EventBridge 服務的預設匯流排 （它必須與您的 Cases 網域 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 Cases 事件的目標，以便將所有案例事件傳送到 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 主控台，並檢查新建立案例的案例事件 (類型：`CASE.CREATED`) 是否可在 SQS 佇列中使用。同樣，您可以修改上面建立的案例，並在 SQS 佇列中獲得相應的案例事件 (類型：`CASE.UPDATED`)。您可以將聯絡人與案例關聯，並在案例上留下註解，以取得這些動作的案例事件。

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

每次建立案例、更新案例、聯絡人與案例關聯，以及針對案例新增註解時，Case 事件串流會發布事件。您可以將這些事件用於：
+ 指標、分析和儀表板
+ 建置會通知使用者的應用程式 (例如，傳送電子郵件)
+ 根據特定類型的案例更新觸發的自動化動作

例如，您可以使用 EventBridge 上的 SQS 目標 (如步驟 4 所示) 將案例事件暫時儲存在 SQS 佇列中，並使用 Lambda 函數來處理 SQS 中的事件，以建置自訂應用程式，例如在案例更新時傳送電子郵件給客戶、自動解決與案例連結的任何工作等等。同樣地，您可以使用 EventBridge 上的 Firehose 目標將案例事件存放至 S3 儲存貯體，然後使用 AWS Glue 進行 ETL、使用 Athena 進行臨機操作分析，以及使用 Quick 進行儀表板。