

# IVS 챗 메시지 검토 핸들러
<a name="chat-message-review-handler"></a>

메시지 검토 핸들러를 사용하면 메시지가 방으로 전송되기 전에 메시지를 검토 및/또는 수정할 수 있습니다. 메시지 검토 핸들러가 방과 연결되면 해당 방에 대한 각 SendMessage 요청에 대해 호출됩니다. 핸들러는 애플리케이션의 비즈니스 로직을 적용하고 메시지를 허용, 거부 또는 수정할지 결정합니다. Amazon IVS Chat은 AWS Lambda 함수를 핸들러로 지원합니다.

## Lambda 함수 생성
<a name="create-lambda-function"></a>

방에 대한 메시지 검토 핸들러를 설정하기 전에 리소스 기반 IAM 정책으로 lambda 함수를 생성해야 합니다. lambda 함수는 함수를 사용할 방과 동일한 AWS 계정 및 AWS 리전에 있어야 합니다. 리소스 기반 정책은 Amazon IVS Chat에 lambda 함수를 호출할 수 있는 권한을 부여합니다. 지침은 [Amazon IVS Chat에 대한 리소스 기반 정책](security-iam.md#security-chat-policy-examples)을 참조하세요.

### 워크플로
<a name="create-lambda-function-workflow"></a>

![\[리소스 기반 IAM 정책으로 lambda 함수를 생성하는 워크플로입니다.\]](http://docs.aws.amazon.com/ko_kr/ivs/latest/ChatUserGuide/images/Chat_Message_Review_Handler_Workflow.png)


### Request Syntax
<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/ko_kr/ivs/latest/ChatUserGuide/chat-message-review-handler.html)  | 

### Response Syntax
<a name="create-lambda-function-response-syntax"></a>

핸들러 lambda 함수는 다음 구문을 사용하여 JSON 응답을 반환해야 합니다. 아래 구문에 해당하지 않거나 필드 제약 조건을 충족하지 않는 응답은 유효하지 않습니다. 이 경우 메시지 검토 핸들러에서 지정한 `FallbackResult` 값에 따라 메시지가 허용되거나 거부됩니다. *Amazon IVS Chat API Reference*(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`이면 `Reason`이 `Attributes`에 제공될 수 있습니다. 예를 들면 다음과 같습니다. `"Attributes": {"Reason": "denied for moderation` 이 경우 발신자 클라이언트는 오류 메시지의 이유와 함께 WebSocket 406 오류를 수신합니다. *Amazon IVS Chat Messaging API Reference*(Amazon IVS Chat Messaging API 참조)의 [WebSocket Errors](https://docs.aws.amazon.com/ivs/latest/chatmsgapireference/error-messages.html#websocket-errors)(WebSocket 오류)를 참조하세요. [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ko_kr/ivs/latest/ChatUserGuide/chat-message-review-handler.html)  | 
| `Content` |  Lambda 함수에서 반환된 메시지의 내용입니다. 비즈니스 로직에 따라 편집되었거나 원본일 수 있습니다. [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ko_kr/ivs/latest/ChatUserGuide/chat-message-review-handler.html)  | 
| `ReviewResult` | 메시지 처리 방법에 대한 검토 처리 결과입니다. 허용되면 방에 연결된 모든 사용자에게 메시지가 전송됩니다. 거부되면 사용자에게 메시지가 전송되지 않습니다. [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ko_kr/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 Reference](https://docs.aws.amazon.com/ivs/latest/ChatAPIReference/API_MessageReviewHandler.html)(Amazon IVS Chat API 참조)의 *MessageReviewHandler*를 참조하세요.

다음과 같은 여러 유형의 오류가 있습니다.
+ Amazon IVS Chat이 핸들러를 호출할 수 없는 경우 `InvocationErrors`가 발생합니다.
+ 핸들러가 잘못된 응답을 반환하는 경우 `ResponseValidationErrors`가 발생합니다.
+ lambda 핸들러가 호출되었을 때 함수 오류를 반환하는 경우 AWS Lambda `Errors`가 발생합니다.

간접 호출 오류 및 응답 검증 오류(Amazon IVS Chat에서 발생)에 대한 자세한 내용은 [Amazon IVS Chat 모니터링](chat-health.md)을 참조하세요. AWS Lambda 오류에 대한 자세한 내용을 알아보려면 [Lambda 함수 지표 작업](https://docs.aws.amazon.com/lambda/latest/dg/monitoring-metrics.html)을 참조하세요.