This document details the setup and deployment of a serverless Slack application that integrates with OpenAI and Stability AI to process and respond to Slack messages. The app utilizes AWS Lambda, AWS Secrets Manager, DynamoDB, and the Slack, OpenAI, and Stability AI SDKs.
- AWS Account
- Slack Account
- OpenAI Account
- Stability AI Account
Store Slack Bot Token, OpenAI API Key, and Stability AI API Key securely in AWS Secrets Manager:
aws secretsmanager create-secret --name SlackBotToken --secret-string '{"SLACK_BOT_TOKEN":"your_slack_bot_token"}'
aws secretsmanager create-secret --name OpenAIApiKey --secret-string '{"OPENAI_API_KEY":"your_openai_api_key"}'
aws secretsmanager create-secret --name StabilityApiKey --secret-string '{"STABILITY_API_KEY":"your_stability_api_key"}'
-
Create a Slack App:
- Go to the Slack API Apps page.
- Click "Create New App" and follow the prompts.
-
Configure OAuth Scopes:
- Under OAuth & Permissions, add the following OAuth scopes under Bot Token Scopes:
app_mentions:read
channels:history
channels:read
chat:write
chat:write.public
commands
files:read
files:write
groups:history
im:history
incoming-webhook
mpim:history
- Under OAuth & Permissions, add the following OAuth scopes under Bot Token Scopes:
-
Event Subscriptions:
- Enable Event Subscriptions and add the following Bot Events:
message.channels
message.groups
message.im
message.mpim
- Enable Event Subscriptions and add the following Bot Events:
-
Install the App:
- Install the app to your workspace and copy the OAuth access token.
Deploy the application using AWS SAM CLI:
sam build
sam deploy --guided
Follow the prompts to configure your stack name, AWS region, and parameter overrides for SlackBotTokenSecretName
, OpenAIApiKeySecretName
, and StabilityApiKeySecretName
.
Set the following environment variables in the AWS Lambda configuration:
SLACK_BOT_TOKEN_SECRET_NAME
with the name of the secret containing the Slack Bot API Token.OPENAI_API_KEY_SECRET_NAME
with the name of the secret containing the OpenAI API Key.STABILITY_API_KEY_SECRET_NAME
with the name of the secret containing the Stability API Key.DYNAMODB_TABLE_NAME
with the name of the DynamoDB table created.
Upon deployment, AWS SAM will provide an API endpoint URL. Configure this URL in your Slack application settings under Event Subscriptions.
This function processes incoming Slack events, checks for new messages, and responds appropriately. It can generate text responses or images based on commands received.
The application listens for specific text commands to trigger responses. For example, if a message contains "згенеруй зображення:", the bot will generate an image based on the provided description.
The bot uses the OpenAI DALL-E model and Stability AI's sd3 model to generate images based on text descriptions. The generated images are then uploaded to Slack with a specified initial comment.
The bot can analyze images and other file types uploaded to Slack. When a file is shared, the bot processes the file, determines its type, and then either analyzes the image content or processes other document types accordingly.
Utilizes AWS Secrets Manager to securely store and retrieve the Slack Bot Token, OpenAI API Key, and Stability API Key.
A DynamoDB table is used to store the last responded message timestamp for each thread, ensuring that the bot responds only once per message.
Uses the OpenAI Python SDK and Stability AI API to generate responses or images based on the text received from Slack messages.
Comprehensive logging and error handling are implemented to manage and debug API calls and response generation.
The bot includes a stop word functionality to prevent further responses in a thread if a specific word or phrase is detected. This allows users to stop the bot from responding in a particular thread by using the configured stop word.
- Command: згенеруй зображення: [description]
- Example: згенеруй зображення: сонячний день у парку з дітьми, що граються
- Command: generate image: [description]
- Example: generate image: picturesque sunset on the beach
- Ensure that your AWS IAM roles and policies are correctly set up to allow Lambda functions to access Secrets Manager, DynamoDB, and post logs.
- Update your Slack, OpenAI, and Stability API keys periodically for security.
- For a detailed guide on creating a Slack app and adding permissions, refer to the Slack API documentation.
By following these steps, your Serverless Slack App with OpenAI and Stability AI integration should be fully functional, allowing interactive, AI-powered responses within your Slack channels.