•10 months ago
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 and Stream's powerful chat API 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.
You can find the completed project on GitHub in the Samples folder inside the Swift Lambda repository.
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. 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.
Also, copy the secret, as it is needed to generate the server-side JWT. You can use 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.
Finally, to set the Presend Webhook URL, you can run the curl command below in the terminal. Make sure to replace
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
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.
You should add that to the
Handle the message
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.
main.swift file, paste the code below.
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 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.
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.
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 and Stream Chat's documentation for Swift.