Leveraging Amazon Rekognition and Amazon AppFlow for Slack image moderation using AWS Solutions Constructs
Table of Contents
Overview
The code in this repo automates the solution in the "Moderating Image Content in Slack with Amazon Rekognition and Amazon AppFlow" Technical Guide
About
This project deploys a fully serverless pipeline to moderate images posted to a Slack channel using the AWS CDK along with AWS Solutions Constructs. Services used are: Amazon Rekognition, Amazon AppFlow, AWS Lambda,Amazon S3 and Amazon SQS
Use Cases
Ensuring all aspects of the virtual work environment are inclusive and safe is a priority for many organizations. Sharing images can be a powerful way to effectively convey concepts and thoughts. There are many popular ways to analyze text, but images present a different challenge. Organizations need a way to detect and react to posted images that violate company guidelines.
The content moderation strategy in this solution identifies images that violate sample chosen guidelines:
Images that contain themes of tobacco or alcohol using Amazon Rekognition content moderation.
Images that contain the following disallowed words using using Amazon Rekognition text detection:
- security
- private
These guidelines can be customized in the process-new-images/index.py file to fit your requirements.
Architecture Diagram
Requirements
General
- A Linux or MacOS-compatible machine
- An AWS account with sufficient permissions
- AWS CLI installed
- A Slack workspace that you have permissions to create an App in
Slack app
Create a Slack app and install the Slack app into your Slack workspace Follow the steps in the "Creating the Slack app in your Slack workspace" section of the Moderating Image Content in Slack with Amazon Rekognition and Amazon AppFlow Technical Guide
In your Slack Workspace, create a channel called 'testing-slack-moderation' (or something else you prefer)
AWS CDK
Install the AWS CDK - Getting Started Guide
Environment Variable Prep
Export the following environment variables in your terminal session. These are used by the CDK to configure the solution during deployment.
- SlackClientID (Obtained from the steps above when creating your Slack app)
- SlackClientSecret (Obtained from the steps above when creating your Slack app)
- SlackWorkspaceInstanceURL (Based on your Slack workspace name)
- SlackChannelParamID (Obtained by navigating to your Slack workspace and channel in the web interface of Slack. It is the last portion of the URL (following the last forward slash) )
- SlackOAuthAccessToken (Obtained from your Slack app's management page. Under the Features menu select the OAuth & Permissions option. Be sure to use the "User OAuth Token".)
E.g.:
export SlackClientID=####...###
export SlackClientSecret=...
export SlackWorkspaceInstanceURL=https://<<WORKSPACENAME>>.slack.com
export SlackChannelParamID=<<ChannelID>>
export SlackOAuthAccessToken=xoxp-...
Deployment
This project is set up like a standard Python AWS CDK project.
To manually create a virtualenv on MacOS and Linux:
$ python3 -m venv .venv
After the init process completes and the virtualenv is created, you can use the following step to activate your virtualenv.
$ source .venv/bin/activate
Once the virtualenv is activated, you can install the required dependencies.
$ pip install -r requirements.txt
Deploy the stack
cdk deploy
Testing
Trigger Violations
Post the following images to the moderated Slack Channel:
This image contains the disallowed word "private": https://i.imgur.com/uuAY133.png
This image which contains the disallowed "Tobacco" theme: https://i.imgur.com/XgAtyWU.png
Note:
If you are pasting the same link numerous times during testing, you may receive a "Pssst! I didn’t unfurl <> because it was already shared in this channel quite recently ..." error. You will need to click "Show Preview Anyway" for the image to be processed again.
Avoid using images over 2 MB because Slack doesn't expand them by default.
Checking results
Wait approximately 2 mins and then check the "new-violation-findings" SQS Queue.
In the AWS Console, navigate to Amazon SQS --> Queues -->new-violation-findings
Click "Send and receive messages"
Scroll down the the Receive Messages window and click "Poll for messages"
From there, you can select a message and view its details
You can get more information by selection the Attributes menu
You can also use the following to check violations via the CLI:
Set your AWS ACCOUNT ID
AWS_ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)
Set and export your default region to the region of your choice. - Ex:
export AWS_REGION=us-east-1
Run the following command to retrieve messages:
aws sqs receive-message --queue-url https://sqs.$AWS_REGION.amazonaws.com/$AWS_ACCOUNT_ID/new-violation-findings --attribute-names All --message-attribute-names All --max-number-of-messages 10
You should see messages with the following violations:
<snip>
"Body": "Image with \"private\" found",
"Attributes": {
<snip>
"MessageAttributes": {
"slack_msg_id": {
"StringValue": "<ID>",
"DataType": "String"
},
"url": {
"StringValue": "https://i.imgur.com/uuAY133.png",
"DataType": "String"
}
}
<snip>
"Body": "Image with \"Tobacco\" found",
"Attributes": {
<snip>
"MessageAttributes": {
"slack_msg_id": {
"StringValue": "<ID>",
"DataType": "String"
},
"url": {
"StringValue": "https://i.imgur.com/XgAtyWU.png",
"DataType": "String"
}
}
Clean up
Delete the CDK stack:
cdk destroy