Moderate Chat Content with Swift on AWS Lambda
Tags: ios, swift, aws, beginners
Published: 2020-10-02 12:28:03

---
title: Moderate Chat Content with Swift on AWS Lambda
published: true
description: 
tags: ios, swift, aws, beginners
canonical_url: https://getstream.io/blog/swift-lambda-chat-moderation/
---

Most of the time, when building a chat application, it's essential to have some level of control over what your users can share and say to each other.

In this tutorial, we'll use [Swift Lambda](https://github.com/getstream/swift-lambda) and [Stream's powerful chat API](https://getstream.io/chat/) to build a content moderation system that can prevent users from sending unwanted content. In this case, we'll filter out possible credit card data to prevent our users from sharing it with scammers. However, you can adapt it to other sensitive data or to block bad words.

![Animation shows a chat screen with a message containing credit card information being typed and then redacted after submission](https://i.imgur.com/KgssyhZ.gif)

You can find the completed project on GitHub in the [Samples folder inside the Swift Lambda repository](https://github.com/GetStream/swift-lambda/tree/main/Samples).

## Requirements
- [Set up a Chat Application](https://getstream.io/blog/inapp-support-chat-ios/)
- [Set up Swift Lambda](https://getstream.io/blog/swift-lambda-aws-serverless/)

## Set the Presend Webhook URL

After you've built your iOS chat app and the Swift Lambda is up and running, you need to configure the Presend Webhook URL using the [Stream Chat REST API](https://getstream.io/chat/docs_rest/#update-application). To do this, you'll need your API Key, a server-side JWT, and your AWS Lambda URL.

To get the API Key, go to the Stream Chat dashboard. It will be there, similar to the image below.

![Image shows a Stream application with the API Key and Secret in the Stream Dashboard](https://i.imgur.com/gp7Yslh.png)

Also, copy the secret, as it is needed to generate the server-side JWT. You can use [jwt.io](https://jwt.io/) to generate it. Just paste the secret inside the text field on the right side and copy the JWT on the left side.

![Image shows JWT.io website with arrows pointing to the generated JWT and the text field where the secret is pasted](https://i.imgur.com/5wFDu5h.png)

Finally, to set the Presend Webhook URL, you can run the curl command below in the terminal. Make sure to replace `[api_key]`, `[jwt]`, and `[aws_lambda_endpoint_url]`.

{% gist https://gist.github.com/cardoso/0ee5210d6be9118ba675977c9c660564 %}

## Configure Swift Lambda

At first, your Swift Lambda will handle `GET` requests. Change it to process `POST` requests instead, which can be done by editing the `serverless.yml` file and swapping the line `method: get` with `method: post`.

## Filter out credit card numbers

Now that the Swift Lambda is configured, we can get to the code part.

First, we need a function to redact credit card numbers from a message by replacing the numbers with asterisks. The code below does just that by using regular expressions to match valid numbers of known credit card operators.

{% gist https://gist.github.com/cardoso/a0778e023bcb5b1f7e30bce14d490791 %}

You should add that to the `main.swift` file.

## Handle the message

When the `POST` request hits your lambda, it will contain a JSON object describing the new message. To see which fields you can expect in this payload, see the [documentation](https://getstream.io/chat/docs/presend_message_hook/?language=js). 

In the `main.swift` file, paste the code below.

{% gist https://gist.github.com/cardoso/a0a04ca80cc2d2c159fc1f7b791c6e79 %}

That code will parse the JSON body into a dictionary and modify the message object by running its "text" field through the credit card redaction function. As specified in the documentation, we return this modified message object to submit the redacted version.

## Deploying and testing the content moderation

The main advantage of using [Swift Lambda](https://github.com/GetStream/swift-lambda) is that it's possible to iterate fast with it. After writing the moderation code, just run the `./Scripts/deploy.sh` script again and wait a few seconds.

![This animation shows the deploy script running and finishing successfully](https://i.imgur.com/JO0nsi2.gif)

After the upload is finished, you can run the chat app and type something in any channel. If it contains a seemingly valid credit card number such as `6011881485017922`, it should be replaced with asterisks.

![Image shows a chat UI running on iPhone simulator with a chat screen containing redacted messages](https://i.imgur.com/0fnxECF.png)

## Next steps with Swift Lambda

Congratulations! You've just uploaded a real Swift backend to AWS. This moderation code is simple and made to demonstrate a usage of Swift Lambda to empower your chat apps. There are many ways to build even richer use cases with Swift Lambda. To find out more, check out the [chatbot tutorial with Swift Lambda](https://getstream.io/blog/swift-lambda-chat-bot/) and [Stream Chat's documentation](https://getstream.io/chat/docs/?language=swift).