

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

以下代码示例演示如何通过将 AWS Command Line Interface与 Amazon IVS 聊天功能结合使用，来执行操作和实现常见场景。

*操作是大型程序的代码摘录*，必须在上下文中运行。您可以通过操作了解如何调用单个服务函数，还可以通过函数相关场景的上下文查看操作。

每个示例都包含一个指向完整源代码的链接，您可以从中找到有关如何在上下文中设置和运行代码的说明。

**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 Interactive Video Service 用户指南》**中的[步骤 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 Interactive Video Service 用户指南》**中的 [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 Interactive Video Service 用户指南》**中的[步骤 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 Interactive Video Service 用户指南》**中的 [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 Interactive Video Service 用户指南》**中的 [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 Interactive Video Service 用户指南》**中的 [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 Interactive Video Service 用户指南》**中的 [Amazon IVS 聊天功能入门](https://docs.aws.amazon.com/ivs/latest/userguide/getting-started-chat.html)。  
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [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 Interactive Video Service 用户指南》**中的 [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 Interactive Video Service 用户指南》**中的 [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 Interactive Video Service 用户指南》**中的 [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 Interactive Video Service 用户指南》**中的 [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 资源名称）的所有标签。  

```
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 Interactive Video Service 用户指南》**中的 [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 资源名称）添加或更新标签。成功后，它将返回带有空响应正文的 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 资源名称）的指定标签。成功后，它将返回带有空响应正文的 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 Interactive Video Service 用户指南》**中的 [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 Interactive Video Service 用户指南》**中的 [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)。