Install the Slack SDK:
pip install slack-sdkThread timestamps are automatically used as session IDs, so each Slack thread maintains its own conversation context.
Setup and Configuration
Prerequisites
Ensure you have the following:
- A Slack workspace with admin privileges
- ngrok (for development)
- Python 3.7+
Create a Slack App
- Go to Slack App Directory
- Click “Create New App”
- Select “From scratch”
- Provide:
- App name
- Workspace to install to
- Click “Create App”
Configure OAuth & Permissions
- Navigate to “OAuth & Permissions” in your Slack App settings
- Under “Scopes”, click “Add an OAuth Scope”
- Add the following Bot Token Scopes:
app_mentionchat:writechat:write.customizechat:write.publicim:historyim:readim:write
- Scroll to the top and click “Install to Workspace”
- Click “Allow” to authorize the app
Setup Environment Variables
Create a Find these values in your Slack App settings:
.env file in your project root with the following content, replacing placeholder values with your actual credentials:- Bot User OAuth Token: Under “OAuth & Permissions”
- Signing Secret: Under “Basic Information” > “App Credentials”
Setup Webhook with ngrok
- Run ngrok to expose your local server, ensuring the port matches your app (7777):
- Copy the
https://URL provided by ngrok - In your Slack App settings, go to “Event Subscriptions”
- Enable events by toggling the switch
- Add your Request URL:
- Format:
https://your-ngrok-url.ngrok.io/slack/events
- Format:
- Wait for Slack to verify the endpoint (your app must be running)
Configure Event Subscriptions
- Under “Subscribe to bot events” in Event Subscriptions:
- Click “Add Bot User Event” and add:
app_mentionmessage.immessage.channelsmessage.groups
- Click “Save Changes”
- Reinstall your app to apply the new permissions
Enable App Home
- Go to “App Home” in your Slack App settings
- Under “Show Tabs”:
- Enable “Messages Tab”
- Check “Allow users to send Slash commands and messages from the messages tab”
- Save changes
ngrok is used only for local development and testing. For production deployments, see the deployment tutorials.