

# IVS 聊天功能消息审核处理程序
<a name="chat-message-review-handler"></a>

消息审核处理程序允许您在将消息传送到房间之前审查和/或修改消息。当消息审核处理程序与房间关联时，会对该房间的每个 SendMessage 请求调用该处理程序。处理程序会强制执行应用程序的业务逻辑，并确定是否允许、拒绝或修改消息。Amazon IVS Chat 支持将 AWS Lambda 函数作为处理程序。

## 创建 Lambda 函数
<a name="create-lambda-function"></a>

在为房间设置消息审核处理程序之前，您必须使用基于资源的 IAM Policy 创建 Lambda 函数。Lambda 函数必须与将要使用该函数的房间位于同一亚马逊云科技账户和亚马逊云科技区域中。基于资源的策略允许 Amazon IVS Chat 调用 Lambda 函数。有关说明，请参阅 [Amazon IVS 聊天功能基于资源的策略](security-iam.md#security-chat-policy-examples)。

### 工作流
<a name="create-lambda-function-workflow"></a>

![\[使用基于资源的 IAM Policy 创建 Lambda 函数的工作流。\]](http://docs.aws.amazon.com/zh_cn/ivs/latest/ChatUserGuide/images/Chat_Message_Review_Handler_Workflow.png)


### 请求语法
<a name="create-lambda-function-request-syntax"></a>

当客户端发送消息时，Amazon IVS Chat 会使用 JSON 有效负载调用 Lambda 函数：

```
{
   "Content": "string",
   "MessageId": "string",
   "RoomArn": "string",
   "Attributes": {"string": "string"},
   "Sender": {
      "Attributes": { "string": "string" },
      "UserId": "string",
      "Ip": "string"
   }
}
```

### 请求正文
<a name="create-lambda-function-request-body"></a>


| 字段 | 说明 | 
| --- | --- | 
| `Attributes` | 与消息关联的属性。 | 
| `Content` |  消息的原始内容。 | 
| `MessageId` | 消息 ID。由 IVS Chat 生成。 | 
| `RoomArn` | 发送消息的房间的 ARN。 | 
| `Sender` | 有关发件人的信息。此对象具有多个字段： [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/ivs/latest/ChatUserGuide/chat-message-review-handler.html)  | 

### 响应语法
<a name="create-lambda-function-response-syntax"></a>

处理程序 Lambda 函数必须返回包含以下语法的 JSON 响应。不符合以下语法或不满足字段约束的响应视为无效。在此情况下，允许或拒绝消息取决于您在消息审核处理程序中指定的 `FallbackResult` 值；请参阅《Amazon IVS Chat API 参考》中的 [MessageReviewHandler](https://docs.aws.amazon.com/ivs/latest/ChatAPIReference/API_MessageReviewHandler.html)。

```
{
   "Content": "string",
   "ReviewResult": "string",
   "Attributes": {"string": "string"},
}
```

### 响应字段
<a name="create-lambda-function-response-fields"></a>


| 字段 | 说明 | 
| --- | --- | 
| `Attributes` |  与从 Lambda 函数返回的消息相关联的属性。 如果 `ReviewResult` 是 `DENY`，则可以在 `Attributes` 中提供 `Reason`；例如： `"Attributes": {"Reason": "denied for moderation` 在此情况下，发件人客户端会收到 WebSocket 406 错误，错误消息中包含原因。（请参阅《Amazon IVS Chat Messaging API 参考》中的 [WebSocket 错误](https://docs.aws.amazon.com/ivs/latest/chatmsgapireference/error-messages.html#websocket-errors)。） [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/ivs/latest/ChatUserGuide/chat-message-review-handler.html)  | 
| `Content` |  从 Lambda 函数返回的消息的内容。它可以根据业务逻辑进行编辑或原创。 [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/ivs/latest/ChatUserGuide/chat-message-review-handler.html)  | 
| `ReviewResult` | 关于如何处理消息的审核处理结果。如果允许，则会将消息传送到连接到房间的所有用户。如果拒绝，则不会将消息传送给任何用户。 [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/ivs/latest/ChatUserGuide/chat-message-review-handler.html)  | 

### 示例代码
<a name="create-lambda-function-example"></a>

以下是 Go 中的 Lambda 处理程序示例。它会修改消息内容，保持消息属性不变，并允许消息。

```
package main

import (
   "context"
   "github.com/aws/aws-lambda-go/lambda"
)

type Request struct {
   MessageId string
   Content string
   Attributes map[string]string
   RoomArn string
   Sender Sender
}

type Response struct {
   ReviewResult string
   Content string
   Attributes map[string]string
}

type Sender struct {
   UserId string
   Ip string
   Attributes map[string]string
}

func main() {
   lambda.Start(HandleRequest)
}

func HandleRequest(ctx context.Context, request Request) (Response, error) {
   content := request.Content + "modified by the lambda handler"
   return Response{
       ReviewResult: "ALLOW",
       Content: content,
   }, nil
}
```

## 将处理程序与房间关联和取消关联
<a name="associate-disassociate-room"></a>

设置并实施 Lambda 处理程序后，请使用 [Amazon IVS Chat API](https://docs.aws.amazon.com/ivs/latest/ChatAPIReference/Welcome.html)：
+ 要将处理程序与房间关联，请调用 CreateRoom 或 UpdateRoom 并指定处理程序。
+ 要取消处理程序与房间的关联，请使用 `MessageReviewHandler.Uri` 的空值调用 UpdateRoom。

## 使用 Amazon CloudWatch 监控错误
<a name="monitor-errors-with-cloudwatch"></a>

您可以使用 Amazon CloudWatch 监控消息审核中出现的错误，也可以创建告警或控制面板来指示或响应特定错误的更改。如果出现错误，则根据您在将处理程序与房间关联时指定的 `FallbackResult` 值来允许或拒绝该消息；请参阅《Amazon IVS Chat API 参考》中的 [MessageReviewHandler](https://docs.aws.amazon.com/ivs/latest/ChatAPIReference/API_MessageReviewHandler.html)。

有几种类型的错误：
+ 当 Amazon IVS Chat 无法调用处理程序时，会出现 `InvocationErrors`。
+ 当处理程序返回的响应无效时，会出现 `ResponseValidationErrors`。
+ 当调用 Lambda 处理程序并返回函数错误时，会出现 AWS Lambda `Errors`。

有关调用错误和响应验证错误（由 Amazon IVS 聊天功能发出）的更多信息，请参阅[监控 Amazon IVS 聊天功能](chat-health.md)。有关 AWS Lambda 错误的更多信息，请参阅[使用 Lambda 指标](https://docs.aws.amazon.com/lambda/latest/dg/monitoring-metrics.html)。