

# IVS Chat Message Review Handler
Chat Message Review Handler

A message review handler allows you to review and/or modify messages before they are delivered to a room. When a message review handler is associated with a room, it is invoked for each SendMessage request to that room. The handler enforces your application’s business logic and determines whether to allow, deny, or modify a message. Amazon IVS Chat supports AWS Lambda functions as handlers.

## Creating a Lambda Function


Before setting up a message review handler for a room, you must create a lambda function with a resource-based IAM policy. The lambda function must be in the same AWS account and AWS region as the room with which you will use the function. The resource-based policy gives Amazon IVS Chat permission to invoke your lambda function. For instructions, see [Resource-Based Policy for Amazon IVS Chat](security-iam.md#security-chat-policy-examples).

### Workflow


![\[Workflow to create a lambda function with a resource-based IAM policy.\]](http://docs.aws.amazon.com/ivs/latest/ChatUserGuide/images/Chat_Message_Review_Handler_Workflow.png)


### Request Syntax


When a client sends a message, Amazon IVS Chat invokes the lambda function with a JSON payload:

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

### Request Body



| Field | Description | 
| --- | --- | 
| `Attributes` | Attributes associated with the message. | 
| `Content` |  Original content of the message. | 
| `MessageId` | The message ID. Generated by IVS Chat. | 
| `RoomArn` | The ARN of the room where messages are sent. | 
| `Sender` | Information about the sender. This object has several fields: [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ivs/latest/ChatUserGuide/chat-message-review-handler.html)  | 

### Response Syntax


The handler lambda function must return a JSON response with the following syntax. Responses that do not correspond to the syntax below or satisfy the field constraints are invalid. In this case, the message is allowed or denied depending on the `FallbackResult` value that you specify in your message review handler; see [MessageReviewHandler](https://docs.aws.amazon.com/ivs/latest/ChatAPIReference/API_MessageReviewHandler.html) in the *Amazon IVS Chat API Reference*.

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

### Response Fields



| Field | Description | 
| --- | --- | 
| `Attributes` |  Attributes associated with the message returned from the lambda function. If `ReviewResult` is `DENY`, a `Reason` may be provided in `Attributes`; e.g.: `"Attributes": {"Reason": "denied for moderation` In this case, the sender client receives a WebSocket 406 error with the reason in the error message. (See [WebSocket Errors](https://docs.aws.amazon.com/ivs/latest/chatmsgapireference/error-messages.html#websocket-errors) in the *Amazon IVS Chat Messaging API Reference*.) [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ivs/latest/ChatUserGuide/chat-message-review-handler.html)  | 
| `Content` |  Content of the message returned from the Lambda function. It could be edited or original depending on the business logic. [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ivs/latest/ChatUserGuide/chat-message-review-handler.html)  | 
| `ReviewResult` | The result of review processing on how to handle the message. If allowed, the message is delivered to all users connected to the room. If denied, the message is not delivered to any user.  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ivs/latest/ChatUserGuide/chat-message-review-handler.html)  | 

### Sample Code


Below is a sample lambda handler in Go. It modifies the message content, keeps the message attributes unchanged, and allows the message.

```
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
}
```

## Associating and Dissociating a Handler with a Room


Once you have the lambda handler set up and implemented, use the [Amazon IVS Chat API](https://docs.aws.amazon.com/ivs/latest/ChatAPIReference/Welcome.html):
+ To associate the handler with a room, call CreateRoom or UpdateRoom and specify the handler.
+ To disassociate the handler from a room, call UpdateRoom with an empty value for `MessageReviewHandler.Uri`.

## Monitoring Errors with Amazon CloudWatch


You can monitor errors occurring in message review with Amazon CloudWatch, and you can create alarms or dashboards to indicate or respond to the changes of specific errors. If an error occurs, the message is allowed or denied depending on the `FallbackResult` value you specify when you associate the handler with a room; see [MessageReviewHandler](https://docs.aws.amazon.com/ivs/latest/ChatAPIReference/API_MessageReviewHandler.html) in the *Amazon IVS Chat API Reference*.

There are several types of errors:
+ `InvocationErrors` occur when Amazon IVS Chat cannot invoke a handler.
+ `ResponseValidationErrors` occur when a handler returns a response that is invalid.
+ AWS Lambda `Errors` occur when a lambda handler returns a function error when it has been invoked.

For more information on invocation errors and response-validation errors (emitted by Amazon IVS Chat), see [Monitoring Amazon IVS Chat](chat-health.md). For more information on AWS Lambda errors, see [Working with Lambda Metrics](https://docs.aws.amazon.com/lambda/latest/dg/monitoring-metrics.html).