

# IVS 聊天功能訊息審查處理常式
<a name="chat-message-review-handler"></a>

訊息審查處理常式可讓您在訊息傳遞到聊天室之前審查和/或修改訊息。當某個訊息審查處理常式與聊天室相關聯時，每次對該聊天室進行 SendMessage 請求時都會叫用它。該處理常式強制執行應用程式的業務邏輯，以確定是允許、拒絕還是修改訊息。Amazon IVS 聊天功能支援將 AWS Lambda 函數作為處理常式。

## 建立 Lambda 函數
<a name="create-lambda-function"></a>

在為聊天室設定訊息審查處理常式之前，您必須使用以資源為基礎的 IAM 政策建立一個 lambda 函數。該 lambda 函數必須與您將使用該函數的聊天室位於同一 AWS 帳戶和 AWS 區域中。該以資源為基礎的政策授予 Amazon IVS 聊天功能叫用 lambda 函數的許可。如需說明，請參閱[適用於 Amazon IVS 聊天功能的資源型政策](security-iam.md#security-chat-policy-examples)。

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

![\[使用以資源為基礎的 IAM 政策建立 lambda 函數的工作流程。\]](http://docs.aws.amazon.com/zh_tw/ivs/latest/ChatUserGuide/images/Chat_Message_Review_Handler_Workflow.png)


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

當用戶端傳送訊息時，Amazon IVS 聊天功能使用 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>


| 欄位 | Description | 
| --- | --- | 
| `Attributes` | 與訊息相關聯的屬性。 | 
| `Content` |  訊息的原始內容。 | 
| `MessageId` | 訊息 ID。由 IVS Chat 產生。 | 
| `RoomArn` | 訊息要傳送到的聊天室的 ARN。 | 
| `Sender` | 與傳送者相關的資訊。此物件包含幾個欄位： [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/ivs/latest/ChatUserGuide/chat-message-review-handler.html)  | 

### 回應語法
<a name="create-lambda-function-response-syntax"></a>

該處理常式 lambda 函數必須傳回具有以下語法的 JSON 回應。不符合以下語法或不滿足欄位限制的回應將無效。在這種情況下，允許或拒絕訊息取決於您在訊息審查處理常式中指定的 `FallbackResult` 值；請參閱**《Amazon IVS 聊天功能 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>


| 欄位 | Description | 
| --- | --- | 
| `Attributes` |  與從 lambda 函數傳回的訊息關聯的屬性。 如果 `ReviewResult` 為 `DENY`，則可以在 `Reason` 中提供 `Attributes`；例如： `"Attributes": {"Reason": "denied for moderation` 在這種情況下，傳送者用戶端會收到一個 WebSocket 406 錯誤，並且錯誤訊息中會包含該原因。(請參閱 *Amazon IVS 聊天功能訊息 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_tw/ivs/latest/ChatUserGuide/chat-message-review-handler.html)  | 
| `Content` |  從 Lambda 函數傳回的訊息的內容。它可以根據業務邏輯進行編輯或保持原樣。 [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/ivs/latest/ChatUserGuide/chat-message-review-handler.html)  | 
| `ReviewResult` | 關於如何處理訊息的審查結果。如果允許，則訊息會傳遞給連線至聊天室的所有使用者。如果拒絕，則訊息不會傳遞給任何使用者。 [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/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 聊天功能 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 聊天功能 API 參考》中的 [MessageReviewHandler](https://docs.aws.amazon.com/ivs/latest/ChatAPIReference/API_MessageReviewHandler.html)。

錯誤類型有幾種：
+ `InvocationErrors` 在 Amazon IVS 聊天功能無法叫用處理常式時發生。
+ `ResponseValidationErrors` 在處理常式傳回無效回應時發生。
+ AWS Lambda `Errors` 在 lambda 處理常式被叫用的過程中傳回函數錯誤時發生。

如需有關調用錯誤和回應驗證錯誤 (由 Amazon IVS 聊天功能發出) 的詳細資訊，請參閱[監控 Amazon IVS 聊天功能](chat-health.md)。如需有關 AWS Lambda 錯誤的詳細資訊，請參閱[使用 Lambda 指標](https://docs.aws.amazon.com/lambda/latest/dg/monitoring-metrics.html)。