A production-ready, class-based Slack bot for integrating Slack with any Dify API-compatible application. Modular, secure, and easily extensible for all your AI-powered Slack automation needs.
- Class-based design for maintainability and scalability
- Simple configuration via
.envfile - Easy to extend for file uploads, conversation history, etc.
- Flask-based webhook for Slack events
- Works with any Dify-compatible API
- Supports both blocking and streaming responses
- Conversation management and history
- Thread support for organized discussions
- Error handling and retry logic
- File upload support (images, documents)
-
Clone the repository
git clone https://github.com/shamspias/dify-slack-bot.git cd dify-slack-bot -
Install dependencies
pip install -r requirements.txt
-
Configure environment variables
Copy
.env.exampleto.envand fill in your secrets:cp .env.example .env
-
Run the bot
python -m src.app
-
Set your Slack app's Event Subscription to
https://your-domain.com/slack/events
src/
app.py # Flask app, entry point
bot.py # SlackBot class (handles Slack events)
dify_client.py # DifyClient class (handles Dify API)
config.py # Config loading (env, secrets)
utils.py # Utility functions
tests/
test_bot.py # Example tests
.env.example # Template for your environment variables
requirements.txt # Python dependencies
README.md # You're reading it!
See .env.example for all required configuration:
SLACK_BOT_TOKEN: Your Slack bot user OAuth token (starts with xoxb-)SLACK_SIGNING_SECRET: Your Slack app's signing secretDIFY_API_KEY: Your Dify application API keyDIFY_BASE_URL: Dify API base URL (default: https://dify.com/v1)FLASK_PORT: Port for the Flask server (default: 3000)RESPONSE_MODE: Response mode for Dify API (blocking/streaming, default: blocking)LOG_LEVEL: Logging level (DEBUG/INFO/WARNING/ERROR, default: INFO)
- Create a new Slack app at https://api.slack.com/apps
- Add the following OAuth scopes:
app_mentions:read- Read messages that mention your appchat:write- Send messagesfiles:read- Read files shared in channelschannels:history- View messages in public channelsgroups:history- View messages in private channelsim:history- View direct messagesmpim:history- View group direct messages
- Enable Event Subscriptions and add:
app_mention- Listen for @mentionsmessage.channels- Listen for channel messages (optional)message.groups- Listen for private channel messages (optional)message.im- Listen for direct messages (optional)
- Install the app to your workspace
Simply mention the bot in any channel:
@dify-bot What are the specs of iPhone 13 Pro Max?
The bot maintains conversation context within threads:
@dify-bot Tell me about the latest iPhone
// Bot responds...
@dify-bot How does it compare to the previous model?
// Bot continues the conversation with context
Share images or documents with the bot:
@dify-bot [upload an image] What's in this image?
- Add more event handlers in
bot.py - Add more Dify API endpoints in
dify_client.py - Implement custom conversation storage in
utils.py - Add logging, monitoring, and error reporting
- Implement rate limiting and request queuing
- Add support for Slack interactive components
The bot exposes the following endpoints:
POST /slack/events- Slack event webhookGET /health- Health check endpoint
The bot includes comprehensive error handling:
- Validates Slack request signatures
- Handles Dify API errors gracefully
- Provides user-friendly error messages
- Logs errors for debugging
- All Slack requests are verified using signing secrets
- API keys are stored securely in environment variables
- No sensitive data is logged
- HTTPS recommended for production deployment
MIT