

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

# 使用 的 Amazon IVS 聊天範例 AWS CLI
<a name="cli_ivschat_code_examples"></a>

下列程式碼範例示範如何使用 AWS Command Line Interface 搭配 Amazon IVS Chat 執行動作和實作常見案例。

*Actions* 是大型程式的程式碼摘錄，必須在內容中執行。雖然動作會告訴您如何呼叫個別服務函數，但您可以在其相關情境中查看內容中的動作。

每個範例均包含完整原始碼的連結，您可在連結中找到如何設定和執行內容中程式碼的相關指示。

**Topics**
+ [動作](#actions)

## 動作
<a name="actions"></a>

### `create-chat-token`
<a name="ivschat_CreateChatToken_cli_topic"></a>

以下程式碼範例顯示如何使用 `create-chat-token`。

**AWS CLI**  
**建立聊天字符**  
下列 `create-chat-token` 範例會建立加密的聊天字符，此字符會用於建立與房間的個別 WebSocket 連線。字符有效期為一分鐘，而使用字符建立的連線 (工作階段) 在指定的持續時間內保持有效。  

```
aws ivschat create-chat-token \
    --roomIdentifier "arn:aws:ivschat:us-west-2:12345689012:room/g1H2I3j4k5L6", \
    --userId" "11231234" \
    --capabilities "SEND_MESSAGE", \
    --sessionDurationInMinutes" 30
```
輸出：  

```
{
    "token": "ACEGmnoq#1rstu2...BDFH3vxwy!4hlm!#5",
    "sessionExpirationTime": "2022-03-16T04:44:09+00:00"
    "state": "CREATING",
    "tokenExpirationTime": "2022-03-16T03:45:09+00:00"
}
```
如需詳細資訊，請參閱《Amazon 互動式影片服務使用者指南》**中的[步驟 3：驗證和授權聊天用戶端](https://docs.aws.amazon.com/ivs/latest/userguide/getting-started-chat.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [CreateChatToken](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ivschat/create-chat-token.html)。

### `create-logging-configuration`
<a name="ivschat_CreateLoggingConfiguration_cli_topic"></a>

以下程式碼範例顯示如何使用 `create-logging-configuration`。

**AWS CLI**  
**建立聊天 LoggingConfiguration 資源**  
下列 `create-logging-configuration` 範例會建立 LoggingConfiguration 資源，此資源允許用戶端存放和記錄傳送的訊息。  

```
aws ivschat create-logging-configuration \
    --destination-configuration s3={bucketName=demo-logging-bucket} \
    --name "test-logging-config" \
    --tags "key1=value1, key2=value2"
```
輸出：  

```
{
    "arn": "arn:aws:ivschat:us-west-2:123456789012:logging-configuration/ABcdef34ghIJ",
    "createTime": "2022-09-14T17:48:00.653000+00:00",
    "destinationConfiguration": {
        "s3": {
            "bucketName": "demo-logging-bucket"
        }
    },
    "id": "ABcdef34ghIJ",
    "name": "test-logging-config",
    "state": "ACTIVE",
    "tags": { "key1" : "value1", "key2" : "value2" },
    "updateTime": "2022-09-14T17:48:01.104000+00:00"
}
```
如需詳細資訊，請參閱《*Amazon 互動式影片服務使用者指南*》中的 [Amazon IVS 聊天功能入門](https://docs.aws.amazon.com/ivs/latest/userguide/getting-started-chat.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [CreateLoggingConfiguration](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ivschat/create-logging-configuration.html)。

### `create-room`
<a name="ivschat_CreateRoom_cli_topic"></a>

以下程式碼範例顯示如何使用 `create-room`。

**AWS CLI**  
**建立房間**  
下列 `create-room` 範例會建立新的房間。  

```
aws ivschat create-room \
    --name "test-room-1" \
    --logging-configuration-identifiers "arn:aws:ivschat:us-west-2:123456789012:logging-configuration/ABcdef34ghIJ" \
    --maximum-message-length 256 \
    --maximum-message-rate-per-second 5
```
輸出：  

```
{
    "arn": "arn:aws:ivschat:us-west-2:12345689012:room/g1H2I3j4k5L6",
    "id": "g1H2I3j4k5L6",
    "createTime": "2022-03-16T04:44:09+00:00",
    "loggingConfigurationIdentifiers": ["arn:aws:ivschat:us-west-2:123456789012:logging-configuration/ABcdef34ghIJ"],
    "maximumMessageLength": 256,
    "maximumMessageRatePerSecond": 5,
    "name": "test-room-1",
    "tags": {}
    "updateTime": "2022-03-16T07:22:09+00:00"
}
```
如需詳細資訊，請參閱《Amazon 互動式影片服務使用者指南》**中的[步驟 2：建立聊天室](https://docs.aws.amazon.com/ivs/latest/userguide/getting-started-chat.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [CreateRoom](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ivschat/create-room.html)。

### `delete-logging-configuration`
<a name="ivschat_DeleteLoggingConfiguration_cli_topic"></a>

以下程式碼範例顯示如何使用 `delete-logging-configuration`。

**AWS CLI**  
**刪除聊天 LoggingConfiguration 資源**  
下列 `delete-logging-configuration` 範例會刪除指定 ARN 的 LoggingConfiguration 資源。  

```
aws ivschat delete-logging-configuration \
    --identifier "arn:aws:ivschat:us-west-2:123456789012:logging-configuration/ABcdef34ghIJ"
```
此命令不會產生輸出。  
如需詳細資訊，請參閱《*Amazon 互動式影片服務使用者指南*》中的 [Amazon IVS 聊天功能入門](https://docs.aws.amazon.com/ivs/latest/userguide/getting-started-chat.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [DeleteLoggingConfiguration](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ivschat/delete-logging-configuration.html)。

### `delete-message`
<a name="ivschat_DeleteMessage_cli_topic"></a>

以下程式碼範例顯示如何使用 `delete-message`。

**AWS CLI**  
**從指定的房間刪除訊息**  
下列 `delete-message` 範例會將事件傳送至指定的房間，引導用戶端刪除指定的訊息：也就是將其從檢視中取消轉譯並從用戶端的聊天歷史記錄中刪除。  

```
aws ivschat delete-message \
    --roomIdentifier "arn:aws:ivschat:us-west-2:12345689012:room/g1H2I3j4k5L6" \
    --id "ABC123def456" \
    --reason "Message contains profanity"
```
輸出：  

```
{
    "id": "12345689012"
}
```
如需詳細資訊，請參閱《*Amazon 互動式影片服務使用者指南*》中的 [Amazon IVS 聊天功能入門](https://docs.aws.amazon.com/ivs/latest/userguide/getting-started-chat.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [DeleteMessage](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ivschat/delete-message.html)。

### `delete-room`
<a name="ivschat_DeleteRoom_cli_topic"></a>

以下程式碼範例顯示如何使用 `delete-room`。

**AWS CLI**  
**刪除房間**  
以下 `delete-room` 範例會刪除指定的房間。已連線的用戶端已中斷連線。成功時，其會傳回具有空白回應內文的 HTTP 204。  

```
aws ivschat delete-room \
    --identifier "arn:aws:ivschat:us-west-2:12345689012:room/g1H2I3j4k5L6"
```
此命令不會產生輸出。  
如需詳細資訊，請參閱《*Amazon 互動式影片服務使用者指南*》中的 [Amazon IVS 聊天功能入門](https://docs.aws.amazon.com/ivs/latest/userguide/getting-started-chat.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [DeleteRoom](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ivschat/delete-room.html)。

### `disconnect-user`
<a name="ivschat_DisconnectUser_cli_topic"></a>

以下程式碼範例顯示如何使用 `disconnect-user`。

**AWS CLI**  
**中斷使用者與房間的連線**  
下列 `disconnect-user` 範例會中斷指定使用者與指定房間的所有連線。成功時，其會傳回具有空白回應內文的 HTTP 200。  

```
aws ivschat disconnect-user \
    --roomIdentifier "arn:aws:ivschat:us-west-2:12345689012:room/g1H2I3j4k5L6" \
    --userId "ABC123def456" \
    --reason "Violated terms of service"
```
此命令不會產生輸出。  
如需詳細資訊，請參閱《*Amazon 互動式影片服務使用者指南*》中的 [Amazon IVS 聊天功能入門](https://docs.aws.amazon.com/ivs/latest/userguide/getting-started-chat.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI API 參考》**中的 [DisconnectUser](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ivschat/disconnect-user.html)。

### `get-logging-configuration`
<a name="ivschat_GetLoggingConfiguration_cli_topic"></a>

以下程式碼範例顯示如何使用 `get-logging-configuration`。

**AWS CLI**  
**取得 LoggingConfiguration 資源的相關資訊**  
下列 `get-logging-configuration` 範例取得指定 ARN 的 LoggingConfiguration 資源相關資訊。  

```
aws ivschat get-logging-configuration \
    --identifier "arn:aws:ivschat:us-west-2:123456789012:logging-configuration/ABcdef34ghIJ"
```
輸出：  

```
{
    "arn": "arn:aws:ivschat:us-west-2:123456789012:logging-configuration/ABcdef34ghIJ",
    "createTime": "2022-09-14T17:48:00.653000+00:00",
    "destinationConfiguration": {
        "s3": {
            "bucketName": "demo-logging-bucket"
        }
    },
    "id": "ABcdef34ghIJ",
    "name": "test-logging-config",
    "state": "ACTIVE",
    "tags": { "key1" : "value1", "key2" : "value2" },
    "updateTime": "2022-09-14T17:48:01.104000+00:00"
}
```
如需詳細資訊，請參閱《*Amazon 互動式影片服務使用者指南*》中的 [Amazon IVS 聊天功能入門](https://docs.aws.amazon.com/ivs/latest/userguide/getting-started-chat.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [GetLoggingConfiguration](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ivschat/get-logging-configuration.html)。

### `get-room`
<a name="ivschat_GetRoom_cli_topic"></a>

以下程式碼範例顯示如何使用 `get-room`。

**AWS CLI**  
**取得指定的房間**  
下列 `get-room` 範例取得指定房間的相關資訊。  

```
aws ivschat get-room \
    --identifier "arn:aws:ivschat:us-west-2:12345689012:room/g1H2I3j4k5L6"
```
輸出：  

```
{
    "arn": "arn:aws:ivschat:us-west-2:12345689012:room/g1H2I3j4k5L6",
    "createTime": "2022-03-16T04:44:09+00:00",
    "id": "g1H2I3j4k5L6",
    "loggingConfigurationIdentifiers": ["arn:aws:ivschat:us-west-2:123456789012:logging-configuration/ABcdef34ghIJ"],
    "maximumMessageLength": 256,
    "maximumMessageRatePerSecond": 5,
    "name": "test-room-1",
    "tags": {},
    "updateTime": "2022-03-16T07:22:09+00:00"
}
```
如需詳細資訊，請參閱《*Amazon 互動式影片服務使用者指南*》中的 [Amazon IVS 聊天功能入門](https://docs.aws.amazon.com/ivs/latest/userguide/getting-started-chat.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [GetRoom](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ivschat/get-room.html)。

### `list-logging-configurations`
<a name="ivschat_ListLoggingConfigurations_cli_topic"></a>

以下程式碼範例顯示如何使用 `list-logging-configurations`。

**AWS CLI**  
**若要取得處理 API 請求之 AWS 區域中使用者所有記錄組態的摘要資訊**  
下列`list-logging-configurations`範例列出處理 API 請求之 AWS 區域中使用者所有 LoggingConfiguration 資源的相關資訊。  

```
aws ivschat list-logging-configurations \
    --max-results 2 \
    --next-token ""
```
輸出：  

```
{
    "nextToken": "set-2",
    "loggingConfigurations": [
        {
            "arn": "arn:aws:ivschat:us-west-2:123456789012:logging-configuration/ABcdef34ghIJ",
            "createTime": "2022-09-14T17:48:00.653000+00:00",
            "destinationConfiguration": {
                "s3": {
                    "bucketName": "demo-logging-bucket"
                }
            },
            "id": "ABcdef34ghIJ",
            "name": "test-logging-config",
            "state": "ACTIVE",
            "tags": { "key1" : "value1", "key2" : "value2" },
            "updateTime": "2022-09-14T17:48:01.104000+00:00"
        }
        ...
    ]
}
```
如需詳細資訊，請參閱《*Amazon 互動式影片服務使用者指南*》中的 [Amazon IVS 聊天功能入門](https://docs.aws.amazon.com/ivs/latest/userguide/getting-started-chat.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [ListLoggingConfigurations](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ivschat/list-logging-configurations.html)。

### `list-rooms`
<a name="ivschat_ListRooms_cli_topic"></a>

以下程式碼範例顯示如何使用 `list-rooms`。

**AWS CLI**  
**取得目前區域中所有房間的摘要資訊**  
下列`list-rooms`範例會取得處理請求之 AWS 區域中所有房間的摘要資訊。結果會依 updateTime 的遞減順序排序。  

```
aws ivschat list-rooms \
    --logging-configuration-identifier "arn:aws:ivschat:us-west-2:123456789012:logging-configuration/ABcdef34ghIJ" \
    --max-results 10 \
    --next-token ""
```
輸出：  

```
{
    "nextToken": "page3",
    "rooms": [
        {
            "arn:aws:ivschat:us-west-2:12345689012:room/g1H2I3j4k5L6",
            "createTime": "2022-03-16T04:44:09+00:00",
            "id": "g1H2I3j4k5L6",
            "loggingConfigurationIdentifiers": ["arn:aws:ivschat:us-west-2:123456789012:logging-configuration/ABcdef34ghIJ"],
            "name": "test-room-1",
            "tags": {},
            "updateTime": "2022-03-16T07:22:09+00:00"
        }
    ]
}
```
如需詳細資訊，請參閱《*Amazon 互動式影片服務使用者指南*》中的 [Amazon IVS 聊天功能入門](https://docs.aws.amazon.com/ivs/latest/userguide/getting-started-chat.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [ListRooms](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ivschat/list-rooms.html)。

### `list-tags-for-resource`
<a name="ivschat_ListTagsForResource_cli_topic"></a>

以下程式碼範例顯示如何使用 `list-tags-for-resource`。

**AWS CLI**  
**列出 AWS 資源的所有標籤 （例如：房間）**  
下列 `list-tags-for-resource` 範例列出指定資源 ARN (Amazon Resource Name) 的所有標籤。  

```
aws ivschat list-tags-for-resource \
    --resource-arn arn:aws:ivschat:us-west-2:12345689012:room/g1H2I3j4k5L6
```
輸出：  

```
{
    "tags":
    {
        "key1": "value1",
        "key2": "value2"
    }
}
```
如需詳細資訊，請參閱《*Amazon Interactive Video Service API 參考*》中的[標記](https://docs.aws.amazon.com/ivs/latest/APIReference/Welcome.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [ListTagsForResource](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ivschat/list-tags-for-resource.html)。

### `send-event`
<a name="ivschat_SendEvent_cli_topic"></a>

以下程式碼範例顯示如何使用 `send-event`。

**AWS CLI**  
**將事件傳送至房間**  
下列 `send-event` 範例會將指定的事件傳送至指定的房間。  

```
aws ivschat send-event \
    --roomIdentifier "arn:aws:ivschat:us-west-2:12345689012:room/g1H2I3j4k5L6" \
    --eventName "SystemMessage" \
    --attributes \
        "msgType"="user-notification", \
        "msgText"="This chat room will close in 15 minutes."
```
輸出：  

```
{
    "id": "12345689012"
}
```
如需詳細資訊，請參閱《*Amazon 互動式影片服務使用者指南*》中的 [Amazon IVS 聊天功能入門](https://docs.aws.amazon.com/ivs/latest/userguide/getting-started-chat.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [SendEvent](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ivschat/send-event.html)。

### `tag-resource`
<a name="ivschat_TagResource_cli_topic"></a>

以下程式碼範例顯示如何使用 `tag-resource`。

**AWS CLI**  
**新增或更新 AWS 資源的標籤 （例如：房間）**  
下列 `tag-resource` 範例會新增或更新指定資源 ARN (Amazon Resource Name) 的標籤。成功時，其會傳回具有空白回應內文的 HTTP 200。  

```
aws ivschat tag-resource \
    --resource-arn arn:aws:ivschat:us-west-2:12345689012:room/g1H2I3j4k5L6 \
    --tags "tagkey1=tagkeyvalue1, tagkey2=tagkeyvalue2"
```
此命令不會產生輸出。  
如需詳細資訊，請參閱《*Amazon Interactive Video Service API 參考*》中的[標記](https://docs.aws.amazon.com/ivs/latest/APIReference/Welcome.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [TagResource](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ivschat/tag-resource.html)。

### `untag-resource`
<a name="ivschat_UntagResource_cli_topic"></a>

以下程式碼範例顯示如何使用 `untag-resource`。

**AWS CLI**  
**移除 AWS 資源的標籤 （例如：房間）**  
下列 `untag-resource` 範例會移除指定資源 ARN (Amazon Resource Name) 的指定標籤。成功時，其會傳回具有空白回應內文的 HTTP 200。  

```
aws ivschat untag-resource \
    --resource-arn arn:aws:ivschat:us-west-2:12345689012:room/g1H2I3j4k5L6 \
    --tag-keys "tagkey1, tagkey2"
```
此命令不會產生輸出。  
如需詳細資訊，請參閱《*Amazon Interactive Video Service API 參考*》中的[標記](https://docs.aws.amazon.com/ivs/latest/APIReference/Welcome.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [UntagResource](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ivschat/untag-resource.html)。

### `update-logging-configuration`
<a name="ivschat_UpdateLoggingConfiguration_cli_topic"></a>

以下程式碼範例顯示如何使用 `update-logging-configuration`。

**AWS CLI**  
**更新房間的記錄組態**  
下列 `update-logging-configuration` 範例會使用指定的資料更新 LoggingConfiguration 資源。  

```
aws ivschat update-logging-configuration \
    --destination-configuration s3={bucketName=demo-logging-bucket} \
    --identifier "arn:aws:ivschat:us-west-2:123456789012:logging-configuration/ABcdef34ghIJ" \
    --name "test-logging-config"
```
輸出：  

```
{
    "arn": "arn:aws:ivschat:us-west-2:123456789012:logging-configuration/ABcdef34ghIJ",
    "createTime": "2022-09-14T17:48:00.653000+00:00",
    "destinationConfiguration": {
        "s3": {
            "bucketName": "demo-logging-bucket"
        }
    },
    "id": "ABcdef34ghIJ",
    "name": "test-logging-config",
    "state": "ACTIVE",
    "tags": { "key1" : "value1", "key2" : "value2" },
    "updateTime": "2022-09-14T17:48:01.104000+00:00"
}
```
如需詳細資訊，請參閱《*Amazon 互動式影片服務使用者指南*》中的 [Amazon IVS 聊天功能入門](https://docs.aws.amazon.com/ivs/latest/userguide/getting-started-chat.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [UpdateLoggingConfiguration](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ivschat/update-logging-configuration.html)。

### `update-room`
<a name="ivschat_UpdateRoom_cli_topic"></a>

以下程式碼範例顯示如何使用 `update-room`。

**AWS CLI**  
**更新房間的組態**  
下列 `update-room` 範例會使用指定的資料更新指定的房間組態。  

```
aws ivschat update-room \
    --identifier "arn:aws:ivschat:us-west-2:12345689012:room/g1H2I3j4k5L6" \
    --logging-configuration-identifiers "arn:aws:ivschat:us-west-2:123456789012:logging-configuration/ABcdef34ghIJ" \
    --name "chat-room-a" \
    --maximum-message-length 256 \
    --maximum-message-rate-per-second 5
```
輸出：  

```
{
    "arn": "arn:aws:ivschat:us-west-2:12345689012:room/g1H2I3j4k5L6",
    "createTime": "2022-03-16T04:44:09+00:00",
    "id": "g1H2I3j4k5L6",
    "loggingConfigurationIdentifiers": ["arn:aws:ivschat:us-west-2:123456789012:logging-configuration/ABcdef34ghIJ"],
    "maximumMessageLength": 256,
    "maximumMessageRatePerSecond": 5,
    "name": "chat-room-a",
    "tags": {},
    "updateTime": "2022-03-16T07:22:09+00:00"
}
```
如需詳細資訊，請參閱《*Amazon 互動式影片服務使用者指南*》中的 [Amazon IVS 聊天功能入門](https://docs.aws.amazon.com/ivs/latest/userguide/getting-started-chat.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [UpdateRoom](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ivschat/update-room.html)。