A serverless application that integrates Amazon Bedrock with Slack, allowing users to interact with AI models through a Slack bot.
- Project Overview
- Architecture
- Project Structure
- DynamoDB Schema
- Prerequisites
- Slack Bot Setup
- Dependencies
- Deployment
- Development
- Content Moderation
- API Endpoints
- License
This project enables users to invoke Amazon Bedrock AI models directly from a Slack channel. The application:
- Receives messages from a Slack bot
- Stores profile from slack in DynamoDB
- Generate some tickets and the use them to generate a roast
- Returns the AI response back to the Slack channel
The application is built using:
- AWS Lambda: Serverless functions to handle requests
- Amazon Bedrock: AI model service for generating responses
- Amazon DynamoDB: NoSQL database for storing conversation context
- Slack API: For bot integration with Slack channels
- Serverless Framework: For infrastructure as code deployment
roaster-for-slack/
├── lambdas/ # Lambda function implementations
│ ├── bot.py # Main roast generation function
│ └── pic.py # Generation of images with amazon bedrock
├── response/ # Response formatting utilities
│ └── wrapper.py # Response wrapper functions
├── serverless.yml # Serverless Framework configuration
└── README.md # Project documentation
The application uses a DynamoDB table to store conversation context with the following schema:
| Attribute | Type | Description |
|---|---|---|
| user_id | String | Primary key - Slack user ID |
| sk | String | Sort key - Ticket or profile |
| ----------------------- | -------- | -------------------------------- |
When a user interacts with the bot, the following data is collected from Slack and stored in DynamoDB:
-
User Profile Information:
display_name: User's display name in Slackstatus_text: User's current status messagestatus_emoji: User's current status emojititle: User's job title or rolephone: User's phone number (if available in Slack profile)email: User's email address (if available in Slack profile)image_original: URL to user's profile image (original size)image_72: URL to user's profile image (72x72 px)
-
Record Structure:
-
For user profiles:
{ 'user_id': '<Slack user ID>', 'sk': '#USER#<user_name>', 'attributes': {<Slack profile data>}, 'updated_at': '<timestamp>' } -
For tickets:
{ 'user_id': '<Slack user ID>', 'sk': '#TICKET#<ticket_id>', 'comments': '<ticket content>' }
-
-
Data Usage:
- User profile data is used to personalize roasts
- Ticket information (up to 4 recent tickets) is used to generate context-aware roasts
- All data is stored with the user's Slack ID as the primary key for efficient retrieval
This data collection is designed to enhance the user experience by creating personalized roasts based on both profile information and recent tasks/tickets.
- Node.js and npm
- Serverless Framework CLI
- AWS CLI configured with appropriate permissions
- Slack workspace with bot creation permissions
- Go to Slack API Apps page and create a new app
- Under "OAuth & Permissions", add the following scopes:
users:read- To fetch user informationchat:write- To send messagesfiles:write- To upload files-imagesapp_mentions:read- To receive mention events
- Install the app to your workspace
- Copy the "Bot User OAuth Token" (starts with
xoxb-) to use as your SLACK_OAUTH_TOKEN
The project uses the following plugins:
- serverless-python-requirements: Automatically creates a Lambda layer from Python requirements.txt
Python dependencies are managed in the requirements.txt file and include:
- boto3: AWS SDK for Python
- slack_sdk: Slack API SDK for Python
- slack_bolt: Slack app framework for Python
-
Install Serverless Framework plugins:
serverless plugin install -n serverless-python-requirements -
Update Python dependencies (if needed):
# Edit requirements.txt to add or update dependencies -
Set up the Slack OAuth token:
# Set the environment variable for deployment export SLACK_OAUTH_TOKEN=xoxb-your-token-here export SLACK_SIGNING_SECRET=your-signing-secret -
Deploy to AWS:
serverless deploy -
Configure the Slack bot with the generated API endpoint
To add new lambda functions:
- Create a new file in the
lambdas/directory - Update the
serverless.ymlfile to include the new function - Import and use the function in
handler.py
The bot uses Amazon Bedrock's Claude model to generate roasts. Claude has built-in content moderation that may refuse to generate content it considers inappropriate or harmful. When this happens:
- The bot detects refusal messages from Claude by checking for common refusal phrases
- Instead of showing the raw refusal message to users, the bot provides a friendly message explaining that it can't generate the requested content
- The friendly message is in Spanish to match the language of the roasts: "Lo siento, no puedo generar una broma que pueda resultar ofensiva. ¿Qué tal si intentamos algo diferente? 😊"
This ensures a better user experience even when content moderation is triggered.
The following table shows the API endpoints and their corresponding Lambda functions:
| API Path | Invoked Lambda |
|---|---|
| / | lambdas/bot.lambda_handler |
| /pic | lambdas/pic.lambda_handler |
[License information]
`