

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

# 在 Amazon Connect 中啟用即時聊天訊息串流
<a name="chat-message-streaming"></a>

Amazon Connect Chat 提供的 [API](https://docs.aws.amazon.com/connect/latest/APIReference/Welcome.html) 可讓您訂閱聊天訊息的即時串流。使用這些 API，您可以：
+ 建立新的聊天聯絡人時，即時串流聊天訊息。
+ 擴充目前的 Amazon Connect 聊天功能以支援使用案例，例如建置與 SMS 解決方案和第三方傳訊應用程式的整合、啟用行動推送通知，以及建立分析儀表板來監控和追蹤聊天訊息活動。

**注意**  
此頁面說明如何訂閱 SNS 端點，以在 Amazon Connect 中即時串流聊天訊息。如果您嘗試在 Amazon Connect 中啟用對話式 AI 互動的訊息串流，請參閱 [為採用 AI 技術的聊天啟用訊息串流](message-streaming-ai-chat.md)。

## 訊息串流 API 的運作方式
<a name="how-chat-message-streaming-apis-work"></a>

當 Amazon Connect Chat 聯絡人內發生某些事件時，會觸發 [Amazon Connect 訊息串流 API](https://docs.aws.amazon.com/connect/latest/APIReference/Welcome.html)。例如，當客戶傳送新的聊天訊息時，該事件會將[裝載](sns-payload.md)傳送至指定端點，其中包含剛傳送訊息的相關資料。訊息會使用 [Amazon Simple Notification Service](https://docs.aws.amazon.com/sns/latest/dg/welcome.html) (Amazon SNS) 發布到特定端點。

本主題介紹如何使用 Amazon Connect 和 Amazon SNS 來設定即時訊息串流。步驟如下：

1. 使用 Amazon SNS 主控台建立新的標準 SNS 主題並設定訊息。

1. 呼叫 [StartChatContact](https://docs.aws.amazon.com/connect/latest/APIReference/API_StartChatContact.html) API 以啟動聊天聯絡人。

1. 呼叫 [StartContactStreaming](https://docs.aws.amazon.com/connect/latest/APIReference/API_StartContactStreaming.html) API 以啟動訊息串流。

1. 呼叫 [CreateParticipantConnection](https://docs.aws.amazon.com/connect-participant/latest/APIReference/API_CreateParticipantConnection.html) API 來建立參與者的連線。

## 步驟 1：建立標準 Amazon SNS 主題
<a name="step1-chat-streaming"></a>

1. 登入 Amazon SNS 主控台。

1. [在帳戶中建立 SNS 主題](https://docs.aws.amazon.com/sns/latest/dg/sns-create-topic.html)。 AWS 在 **詳細資料** 區段中，選擇 **標準** 作為 **類型**，輸入主題的名稱，然後選擇 **建立主題**。
**注意**  
目前，訊息串流 API 僅支援使用標準 SNS 進行即時訊息串流。SNS 動作不支援 [Amazon SNS FIFO (先進先出) 主題](https://docs.aws.amazon.com/sns/latest/dg/sns-fifo-topics.html)。

1. 主題建立後，其 Amazon Resource Name (ARN) 會顯示於 **詳細資訊** 區段中。將主題 ARN 複製到剪貼簿。您將在下一個步驟和 [步驟 3：啟用聯絡人的訊息串流](#step3-chat-streaming) 中使用主題 ARN。

   此範例的輸出結果類似如下：

   ```
   arn:aws:sns:{{us-east-1}}:{{123456789012}}:{{MyTopic}}                                
   ```

1. 選擇 **存取政策** 標籤，選擇 **編輯**，然後在 SNS 主題上新增資源型政策，以便 Amazon Connect 具有發布許可。下列為 SNS 政策範例，您可以將其複製並貼入 JSON 編輯器中，然後使用您的值進行自訂：

------
#### [ JSON ]

****  

   ```
   {
      "Version":"2012-10-17",		 	 	 
      "Statement":[
         {
            "Effect":"Allow",
            "Principal":{
               "Service":"connect.amazonaws.com"
            },
            "Action":"sns:Publish",
            "Resource":"arn:aws:sns:{{us-east-1}}:{{111122223333}}:{{TopicName}}",
            "Condition":{
               "StringEquals":{
                   "aws:SourceAccount":"{{111122223333}}"
               },
               "ArnEquals":{
               "aws:SourceArn":"arn:aws:connect:{{us-east-1}}:{{111122223333}}:instance/{{InstanceId}}"
               }
            }
         }
      ]
   }
   ```

------
**注意**  
預設 **存取政策** 隨附套用至 `sourceOwner` 的條件，例如：  

   ```
   "Condition": {
           "StringEquals": {
             "AWS:SourceOwner": "921772911154"
           }
         }
   ```
確認將其刪除並取代為 `SourceAccount`，例如：  

   ```
   "Condition":{
               "StringEquals":{
                  "aws:SourceAccount":"YOUR_AWS_ACCOUNT_ID"
               },
               "ArnEquals":{
                  "aws:SourceArn":"YOUR_CONNECT_INSTANCE_ARN"
               }
            }
   ```
這樣可以防止[跨服務混淆代理人](cross-service-confused-deputy-prevention.md)問題。

1. 如果您在 SNS 上使用伺服器端加密，請確認您已啟用了對 KMS key的 `connect.amazonaws.com` 許可。以下是政策範例：

------
#### [ JSON ]

****  

   ```
   {
       "Version":"2012-10-17",		 	 	 
       "Id": "key-consolepolicy-3",
       "Statement": [
           {
               "Sid": "Enable IAM User Permissions",
               "Effect": "Allow",
               "Principal": {
                   "AWS": "arn:aws:iam::{{111122223333}}:root",
                   "Service": "connect.amazonaws.com"
               },
               "Action": "kms:*",
               "Resource": "*"
           },
           {
               "Sid": "Allow access for Key Administrators",
               "Effect": "Allow",
               "Principal": {
                   "AWS": "arn:aws:iam::{{111122223333}}:root",
                   "Service": "connect.amazonaws.com"
               },
               "Action": [
                   "kms:Create*",
                   "kms:Describe*",
                   "kms:Enable*",
                   "kms:List*",
                   "kms:Put*",
                   "kms:Update*",
                   "kms:Revoke*",
                   "kms:Disable*",
                   "kms:Get*",
                   "kms:Delete*",
                   "kms:TagResource",
                   "kms:UntagResource",
                   "kms:ScheduleKeyDeletion",
                   "kms:CancelKeyDeletion"
               ],
               "Resource": "*"
           }
       ]
   }
   ```

------

## 步驟 2：啟動聊天聯絡人
<a name="step2-chat-streaming"></a>

1. 呼叫 Amazon Connect [StartChatContact](https://docs.aws.amazon.com/connect/latest/APIReference/API_StartChatContact.html) API 以啟動聊天聯絡人。

   如需有關如何建立 SDK 用戶端以呼叫 Amazon Connect API 的資訊，請參閱下列主題：
   + [Class AmazonConnectClientBuilder](https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/connect/AmazonConnectClientBuilder.html) 
   + [建立服務用戶端](https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/creating-clients.html) 

1. 記錄 [StartChatContact](https://docs.aws.amazon.com/connect/latest/APIReference/API_StartChatContact.html) 的 `ContactId` 和 `ParticipantToken` 回應，因為這些回應屬性會用於呼叫啟用串流所需的其他聊天 API。下個步驟會介紹這一點。

## 步驟 3：啟用聯絡人的訊息串流
<a name="step3-chat-streaming"></a>
+ 呼叫 [StartContactStreaming](https://docs.aws.amazon.com/connect/latest/APIReference/API_StartContactStreaming.html)，以啟用即時訊息串流至您的 SNS 主題。
  + **限制**：每個聯絡人最多可訂閱兩個 SNS 主題。
  + 當您呼叫 [StartContactStreaming](https://docs.aws.amazon.com/connect/latest/APIReference/API_StartContactStreaming.html) 時，您需要提供 SNS 主題的 Amazon Resource Name (ARN) (請參閱 [步驟 1：建立標準 Amazon SNS 主題](#step1-chat-streaming))。

    單一 SNS 主題 ARN 可用於多個 AWS 帳戶，但必須與 Amazon Connect 執行個體位於相同的區域。例如，如果您的主題 ARN 位於 **us-east-1**，您的 Amazon Connect 執行個體必須位於 **us-east-1**。
  + 對於在串流端點上未收到的初始聊天訊息，您可以呼叫 [GetTranscript](https://docs.aws.amazon.com/connect-participant/latest/APIReference/API_GetTranscript.html) API 來接收初始訊息。

## 步驟 4：建立參與者連線
<a name="step4-chat-streaming"></a>
+ 使用傳遞為 true 的 `ConnectParticipant` 屬性呼叫 [CreateParticipantConnection](https://docs.aws.amazon.com/connect-participant/latest/APIReference/API_CreateParticipantConnection.html)。
  + 您必須在建立聊天後的五分鐘內呼叫 [CreateParticipantConnection](https://docs.aws.amazon.com/connect-participant/latest/APIReference/API_CreateParticipantConnection.html)。
  + 只有在 [步驟 2：啟動聊天聯絡人](#step2-chat-streaming) 中啟用了串流，且呼叫參與者為 `Customer` 時，才能在 `ConnectParticipant` 設定為 true 的情況下呼叫 [CreateParticipantConnection](https://docs.aws.amazon.com/connect-participant/latest/APIReference/API_CreateParticipantConnection.html)。
  + 如果您已經使用 `WEBSOCKET` 成功連接到聊天聯絡人，則此步驟 (建立參與者連線) 是可選的。

## 後續步驟
<a name="nextsteps-chat-streaming"></a>

您已準備好使用訊息串流 API。

1. 若要確認是否正常運作，請檢查訊息是否已發布至您建立的 SNS 主題。您可以使用 Amazon CloudWatch 指標來執行此操作。如需指示，請參閱[使用 Amazon CloudWatch 監控 Amazon SNS 主題](https://docs.aws.amazon.com/sns/latest/dg/sns-monitoring-using-cloudwatch.html)。

1. 由於 SNS 的[保留有限](https://aws.amazon.com/blogs//aws/sns-ttl-control/)，我們建議您設定 [Amazon Simple Queue Service (Amazon SQS)](https://aws.amazon.com/sqs/) [Amazon Kinesis](https://aws.amazon.com/kinesis/) 或其他保留訊息的服務。

1. 使用 [StopContactStreaming](https://docs.aws.amazon.com/connect/latest/APIReference/API_StopContactStreaming.html) 是可選的，如果是透過聯絡流程將聊天[中斷連線](disconnect-hang-up.md)或客戶中斷與聊天的連線則不需要。但是，`StopContactStreaming` 提供針對 SNS 主題停止訊息串流的選項，即使聊天為作用中且正在進行。