

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# Amazon Connect Cases のイベントストリーム
<a name="case-event-streams"></a>

Amazon Connect Cases のイベントストリームは、Amazon Connect Cases のドメイン内でケースが作成または変更された場合に、ほぼリアルタイムの更新を提供します。ストリームに発行されたイベントには、次のリソースイベントが含まれます。
+ ケースが作成された
+ ケースが変更された
+ 関連項目 (コメント、通話、チャット、タスク) がケースに追加された

ケースイベントストリームを使用して、データレイクソリューションへのストリームの統合、ケースのパフォーマンスメトリクスを表示するダッシュボードの作成、ケースイベントに基づくビジネスルールまたは自動化されたアクションの実装、および特定のケースアクティビティのカスタム通知をトリガーするアラートツールの設定を行うことができます。

**Topics**
+ [ケースイベントストリームを設定する](case-event-streams-enable.md)
+ [Cases が Contact Lens ルールに更新を送信できるようにする](cases-rules-integration-onboarding.md)
+ [ケースイベントのペイロードとスキーマ](case-event-streams-sample.md)

# 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 インスタンスを作成し、Customer Profiles を有効にする
<a name="step1-case-event-streams-enable"></a>

1. ケースが使用可能な AWS リージョン のいずれかで、稼働する Amazon Connect インスタンスがあることを確認します。「[リージョン別の Cases の利用可能性](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: Amazon Connect インスタンスに Cases ドメインを追加する
<a name="step2-case-event-streams-enable"></a>

手順については、「[Amazon Connect コンソールを使用したケースを有効にする](enable-cases.md)」を参照してください。

API を使用してケースドメインを追加する場合は、「*Amazon Connect Cases API リファレンス*」の「[CreateDomain API](https://docs.aws.amazon.com/cases/latest/APIReference/API_CreateDomain.html)」を参照してください。

## ステップ 3: ケーステンプレートを作成する
<a name="step3-case-event-streams-enable"></a>

[ケーステンプレートを作成する](case-templates.md) *ステップ 6: ケースイベントストリームをテストする*では、テンプレートを使用します。

API を使用してケーステンプレートを作成する場合は、「*Amazon Connect Cases API リファレンス*」の「[CreateTemplate API](https://docs.aws.amazon.com/cases/latest/APIReference/API_CreateTemplate.html)」を参照してください。

## ステップ 4: ケースイベントストリームを有効にし、SQS キューにイベントを受信するように設定する
<a name="step4-case-event-streams-enable"></a>

次のコマンドを実行して、Cases ドメインのケースイベントストリームを有効にします。このコマンドの実行後、ケースが作成または更新されると、イベントがアカウントの 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. ケースを作成します。
**注記**  
**[Cases]** タブの **[Create case]** (ケースを作成) ボタンは、問い合わせを受け入れて、その問い合わせを顧客プロファイルに関連付けるまで非アクティブです。

Amazon SQS コンソールに移動し、新しく作成されたケースのケースイベント (タイプ:`CASE.CREATED`) が SQS キューで利用可能であることを確認します。同様に、上記で作成したケースを変更して、SQS キューで対応するケースイベント (タイプ: `CASE.UPDATED`) を取得できます。問い合わせをケースに関連付けて、ケースにコメントを残し、それらのアクションのケースイベントを取得することもできます。

## ステップ 6: ケースイベントストリームのユースケース
<a name="step6-case-event-streams-enable"></a>

ケースイベントストリームは、ケースの作成、ケースの更新、連絡先のケースへの関連付け、ケースへのコメントの追加のたびに、イベントを発行します。これらのイベントは次の目的で使用できます。
+ メトリクス、分析、ダッシュボード
+ ユーザーに通知する (E メールを送信するなど) アプリケーションを構築する
+ 特定のタイプのケース更新に基づいてトリガーされる自動アクション

例えば、EventBridge で SQS ターゲットを使用して (ステップ 4 を参照)、ケースイベントを SQS キューに一時的に保存したり、Lambda 関数を使用して SQS でイベントを処理して、カスタムアプリケーションを構築したりできます (ケースが更新されたときに顧客にメールを送信し、ケースにリンクされたタスクを自動的に解決するなど)。同様に、EventBridge の Firehose ターゲットを使用してケースイベントを S3 バケットに保存し、ETL AWS Glue には 、アドホック分析には Athena、ダッシュボードには Quick を使用できます。

# Amazon Connect Cases が Contact Lens ルールに更新を送信できるようにする
<a name="cases-rules-integration-onboarding"></a>

**注記**  
この手続きの手順を実行するには、デベロッパーのスキル、または Amazon Connect CLI の経験が必要です。

この 1 回限りの手続きを完了すると、ユーザーはケースの作成または更新時に実行されるルールを設定できます。

1. Amazon Connect インスタンスで Amazon Connect Cases が[有効](enable-cases.md)になっていることを確認します。

1. Amazon Connect Cases のイベントストリームを有効にする手順を完了します。詳細については、「[Amazon Connect Cases のイベントストリームの設定](case-event-streams-enable.md)」を参照してください。手順に対する以下の変更に注意してください。

   1. SQS キューの作成が求められる部分は必須ではないため、スキップしてもかまいません。

   1. `put-case-event-configuration` CLI コマンドを実行し、イベントにすべてのケースフィールド情報を含めます。ルールエンジンが動作するのに必要なすべてのフィールドは必ず含めます。
**注記**  
Cases 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 を含めます。以下の `list-fields` CLI コマンドを実行してフィールド ID を検索できます。

      ```
      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 Cases のデフォルトの制限により、ペイロードが 256 KB (Eventbus イベントの最大サイズ) 未満になることが保証されます。ケースオブジェクトモデルをカスタマイズできるため (例えば、ビジネス固有の情報を取得するためにケースオブジェクトでカスタムフィールドを定義できます)、ケースイベントスキーマには、次の例に示すように、ケースオブジェクトに対するカスタマイズが反映されます (例えば、顧客固有の UUID が 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"
                    }
                }
            }
        }
    }
}
```