A Slack bot that integrates with CustomGPT's RAG (Retrieval-Augmented Generation) platform to provide intelligent responses based on your agent's knowledge base.
More information on GitHub - https://github.com/Poll-The-People/customgpt-integrations
Get you CustomGPT.ai RAG API key here, needed to use this integration.
Features
- Multi-Agent Support: Specify different agent IDs to access different knowledge bases
- Starter Questions: Display helpful starter questions to guide users
- Rate Limiting: Prevent abuse with configurable rate limits per user/channel
- Security: API key management, user authentication, and input validation
- Conversation Management: Maintain conversation context across messages
- Interactive UI: Buttons, modals, and rich formatting
- Thread Support: Keep conversations organized in threads with automatic follow-up responses
- Thread Follow-ups: Once mentioned in a thread, bot responds to all messages without requiring mentions
- Error Handling: Graceful error messages and retry logic
- Analytics: Track usage and performance metrics
Prerequisites
- Python 3.8+
- Slack Workspace with admin access
- CustomGPT account with API access
- API Key from CustomGPT
- Agent/Project ID from CustomGPT
Step-by-Step Slack App Setup
1. Create a Slack App
- Go to api.slack.com/apps
- Click "Create New App"
- Choose "From scratch"
- Enter app name (e.g., "CustomGPT Bot")
- Select your workspace
- Click "Create App"
2. Configure Bot Permissions
Navigate to "OAuth & Permissions" and add these scopes:
Bot Token Scopes:
app_mentions:read- Read mentions of the botchannels:history- View messages in public channelschannels:read- View basic channel infochat:write- Send messagescommands- Add slash commandsgroups:history- View messages in private channelsgroups:read- View basic private channel infoim:history- View direct messagesim:read- View basic DM infoim:write- Send direct messagesusers:read- View user info
3. Enable Event Subscriptions
- Go to "Event Subscriptions"
- Turn on "Enable Events"
- Add these bot events:
app_mention- When someone mentions the botmessage.im- Direct messages to the botmessage.channels- Messages in channels (optional)
- You'll need your Request URL later (after deploying)
4. Create Slash Commands (Optional)
Go to "Slash Commands" and create:
/customgpt [query]- Query the bot directly/customgpt-agent [agent_id]- Switch between agents/customgpt-help- Show help information
5. Install App to Workspace
- Go to "OAuth & Permissions"
- Click "Install to Workspace"
- Authorize the permissions
- Copy the "Bot User OAuth Token" (starts with
xoxb-)
Installation & Setup
Option 1: Traditional Python Deployment
# Clone or create the project
cd /path/to/customgpt-integrations/slack
# Install dependencies
pip install -r requirements.txt
# Set environment variables
export SLACK_BOT_TOKEN="xoxb-your-bot-token"
export SLACK_SIGNING_SECRET="your-signing-secret"
export CUSTOMGPT_API_KEY="your-customgpt-api-key"
export CUSTOMGPT_PROJECT_ID="your-default-project-id"
# Run the bot
python bot.pyOption 2: Google Apps Script Deployment
Google Apps Script provides a free, serverless option for hosting the bot:
- Go to script.google.com
- Create a new project
- Copy the Google Apps Script code (see
google-apps-script/Code.gs) - Set up script properties:
SLACK_BOT_TOKENSLACK_SIGNING_SECRETCUSTOMGPT_API_KEYCUSTOMGPT_PROJECT_ID
- Deploy as Web App
- Use the Web App URL as your Slack Request URL
Option 3: Docker Deployment
# Build the image
docker build -t customgpt-slack-bot .
# Run with environment variables
docker run -d \
-e SLACK_BOT_TOKEN="xoxb-your-token" \
-e SLACK_SIGNING_SECRET="your-secret" \
-e CUSTOMGPT_API_KEY="your-api-key" \
-e CUSTOMGPT_PROJECT_ID="your-project-id" \
-p 3000:3000 \
customgpt-slack-botFree Hosting Options
1. Google Apps Script (Recommended for simple bots)
- ✅ Completely free
- ✅ No credit card required
- ✅ Auto-scaling
- ⚠️ 6-minute execution time limit
- ⚠️ Limited to 20MB for responses
2. Railway.app
- ✅ $5 free credit monthly
- ✅ Easy deployment from GitHub
- ✅ Auto-SSL
- ✅ Environment variable management
3. Fly.io
- ✅ Free tier available
- ✅ Global deployment
- ✅ Auto-scaling
- ⚠️ Requires credit card
4. Render.com
- ✅ Free tier for web services
- ✅ Auto-deploy from GitHub
- ⚠️ Spins down after 15 minutes of inactivity
5. Glitch.com
- ✅ Free hosting
- ✅ Online code editor
- ⚠️ Project sleeps after 5 minutes
- ⚠️ Limited resources
Bot Usage
Basic Commands
Direct Message:
Hey @CustomGPT, what is your refund policy?
Slash Command:
/customgpt How do I get started with your product?
Switch Agent:
/customgpt-agent 12345
Advanced Features
1. Starter Questions The bot automatically displays relevant starter questions when:
- A user starts a new conversation
- A user types "help" or "start"
- Configured in the agent settings
2. Thread Conversations The bot maintains context within threads, allowing for follow-up questions.
3. Rate Limiting Default limits:
- 20 requests per user per minute
- 100 requests per channel per hour
- Configurable in
config.py
Configuration
Environment Variables
| Variable | Description | Required | Default |
|---|---|---|---|
SLACK_BOT_TOKEN | Bot User OAuth Token | Yes | - |
SLACK_SIGNING_SECRET | For verifying requests | Yes | - |
CUSTOMGPT_API_KEY | Your CustomGPT API key | Yes | - |
CUSTOMGPT_PROJECT_ID | Default agent/project ID | Yes | - |
REDIS_URL | Redis URL for caching | No | - |
LOG_LEVEL | Logging level (INFO, DEBUG) | No | INFO |
RATE_LIMIT_PER_USER | Requests per user per minute | No | 20 |
RATE_LIMIT_PER_CHANNEL | Requests per channel per hour | No | 100 |
THREAD_FOLLOW_UP_ENABLED | Enable thread follow-up responses | No | true |
THREAD_FOLLOW_UP_TIMEOUT | Thread follow-up timeout (seconds) | No | 3600 |
THREAD_FOLLOW_UP_MAX_MESSAGES | Max messages per thread | No | 50 |
IGNORE_BOT_MESSAGES | Ignore messages from bots | No | true |
Thread Follow-up Feature
The bot can automatically respond to follow-up messages in threads without requiring mentions:
- How it works: When someone mentions the bot in a thread, it "joins" the conversation
- Follow-ups: Users can continue asking questions without @mentioning the bot
- Timeout: Bot stops responding after
THREAD_FOLLOW_UP_TIMEOUTseconds of inactivity - Message limit: Bot stops after
THREAD_FOLLOW_UP_MAX_MESSAGESmessages - Bot protection: Ignores messages from other bots to prevent loops
Example:
User: @CustomGPT what is the weather?
Bot: The weather is sunny today.
User: What about tomorrow? (no @mention needed)
Bot: Tomorrow will be cloudy with a chance of rain.
Security Best Practices
-
API Key Security
- Never commit API keys to version control
- Use environment variables or secret management
- Rotate keys regularly
-
Input Validation
- Sanitize all user inputs
- Implement message length limits
- Filter potentially harmful content
-
Rate Limiting
- Implement per-user limits
- Add channel-wide limits
- Use exponential backoff for repeat offenders
-
Access Control
- Restrict bot to specific channels
- Implement user allowlists/blocklists
- Log all interactions for audit
Extending Functionality
1. Add Custom Commands
@app.command("/customgpt-search")
async def handle_search_command(ack, command, client):
await ack()
# Implementation2. Interactive Components
Add buttons and modals for better UX:
# Add feedback buttons
await client.chat_postMessage(
channel=channel,
text=response_text,
blocks=[
{
"type": "section",
"text": {"type": "mrkdwn", "text": response_text}
},
{
"type": "actions",
"elements": [
{
"type": "button",
"text": {"type": "plain_text", "text": "👍"},
"action_id": "feedback_positive"
},
{
"type": "button",
"text": {"type": "plain_text", "text": "👎"},
"action_id": "feedback_negative"
}
]
}
]
)3. Analytics Integration
Track usage patterns:
# Log to analytics service
analytics.track(
user_id=user_id,
event="query_submitted",
properties={
"agent_id": agent_id,
"query_length": len(query),
"channel_type": channel_type
}
)4. Multi-language Support
# Detect user language preference
user_lang = detect_user_language(user_id)
# Query with language parameter
response = await customgpt_client.send_message(
project_id=agent_id,
message=query,
lang=user_lang
)Troubleshooting
Common Issues
-
Bot not responding
- Check bot is in the channel/conversation
- Verify event subscriptions are enabled
- Check Request URL is correct
-
Authentication errors
- Verify all tokens are correct
- Check OAuth scopes
- Ensure bot is installed to workspace
-
Rate limiting
- Check CustomGPT API limits
- Verify rate limiter configuration
- Check Redis connection (if using)
Debug Mode
Enable debug logging:
export LOG_LEVEL=DEBUG
python bot.pyResources
CustomGPT Links
- CustomGPT Landing Page
- Live Demo
- CustomGPT Starter Kit
- CustomGPT Integrations
- API Documentation
- Postman Collection
- MCP Documentation
- Office Hours
- YouTube Channel
Video Tutorial
Platform Documentation
- Slack API Documentation: api.slack.com
Support
- CustomGPT Issues: Check the resources above or open an issue in this repository
- Platform-Specific Help: Refer to Slack API documentation
License
MIT License - see LICENSE file for details
