Daily Slack agent that sends jokes and trivia with MCP
Project description
Slack Joke Agent
An agent that sends jokes and trivia to your Slack channel daily.
Features
- AI-Generated Content - Fresh jokes and trivia powered by Claude AI
- Science/tech jokes (60% of messages)
- Science/tech history trivia facts (40% of messages)
- Automated posting at 9:00 AM daily (configurable)
- Uses MCP Slack server integration
- Targets #random channel by default
- Written in Python with robust error handling
- Comprehensive logging to file and console
- Graceful Fallback - Uses predefined content if AI generation fails
Setup
-
Configure environment variables:
cp .env.example .env # Edit .env with your Slack bot tokens
Create a Slack app at https://api.slack.com/apps to get the bot token.
-
Install dependencies:
pip install -e .
-
Or use the startup script:
./start-agent.sh
-
Ensure MCP Slack server is configured
Usage
Start the daily agent:
python3 slack_agent.py
Start with custom time:
python3 slack_agent.py --time 12:00 # Daily at noon
Test with immediate message:
python3 slack_agent.py --test
Use custom channel:
python3 slack_agent.py --channel C1234567890
Enable verbose logging:
python3 slack_agent.py --verbose
How it Works
- Scheduling: Uses Python
schedulelibrary to trigger daily at 9:00 AM - AI Generation: Uses Anthropic's Claude AI to generate fresh content
- Content Selection: Randomly selects between jokes and trivia (60%/40% split)
- Fallback System: Uses predefined content if AI generation fails
- MCP Integration: Spawns the MCP Slack server process
- Channel: Posts to #random channel by default
- Logging: Logs to both console and
slack_agent.logfile
Configuration
Environment variables in .env file:
Required:
SLACK_BOT_TOKEN- Your Slack bot token (xoxb-...)SLACK_TEAM_ID- Your Slack team/workspace IDANTHROPIC_API_KEY- Your Anthropic API key for Claude AI (sk-ant-...)
Optional:
SLACK_CHANNEL_IDS- Comma-separated list of allowed channelsDEFAULT_CHANNEL_ID- Default channel to post
Security:
- All secrets loaded from
.envfile (excluded from git) - No hardcoded API keys or tokens in source code
- Environment variable validation on startup
- Graceful handling of missing AI credentials
AI Features:
- Dynamic content freshly generated by Claude AI
- Workplace appropriate prompts ensure clean content
- Automatic fallback to predefined content if AI generation fails
- Smart logging tracks AI generation success/failure
Command Line Options
--test: Send a test message immediately--test-ai: Test AI generation without sending to Slack--channel CHANNEL_ID: Override the default channel--time HH:MM: Set daily schedule time (default: 09:00)--verbose, -v: Enable verbose debug logging
Logs
The agent creates comprehensive logs:
- Console output: Real-time status and messages
- File logging:
slack_agent.logwith detailed information - Timestamps: All log entries include precise timestamps
- Error tracking: Failed attempts are logged with details
Customization
Add more jokes/trivia:
Edit the jokes and trivia lists in slack_agent.py
Change schedule:
Modify the schedule configuration:
schedule.every().day.at("09:00").do(self.send_daily_message) # Daily at 9 AM (current)
schedule.every().day.at("12:00").do(self.send_daily_message) # Daily at noon
schedule.every().hour.at(":00").do(self.send_daily_message) # Every hour
schedule.every().monday.at("09:00").do(self.send_daily_message) # Weekly on Monday
Change channel:
Update the channel_id property or use --channel argument
Update Slack tokens:
Edit the .env file with your new tokens
Installation
From PyPI (Recommended):
pip install slack-joke-agent
slack-agent --help
From Source:
git clone <repository>
cd slack-joke-agent
pip install -e .
Quick Development Setup:
git clone <repository>
cd slack-joke-agent
./start-agent.sh
Dependencies
Runtime Dependencies:
- Python 3.8+
schedule>=1.2.0- Task schedulingpython-dotenv>=1.0.0- Environment variable loadinganthropic>=0.57.1- Anthropic Claude AI SDK
Development Dependencies:
pytest>=7.0.0- Testing frameworkblack>=22.0.0- Code formatterflake8>=4.0.0- Lintingmypy>=0.950- Type checking
Error Handling
The agent includes robust error handling:
- MCP server timeouts (30-second limit)
- Network connectivity issues
- Invalid channel configurations
- Graceful shutdown on Ctrl+C
Development
Building and Publishing:
Automatic Release:
./release.sh 1.0.3 # Automated release to PyPI
Interactive Release:
./release.sh # Interactive mode with prompts
Help:
./release.sh --help # Show usage information
Automatic mode will update version, run tests, build package, upload to PyPI, commit changes, create git tag, and push to repository.
Running Tests:
python test_slack_agent.py
Package Structure:
slack_agent.py- Main applicationpyproject.toml- Modern Python packagingLICENSE- MIT licenseMANIFEST.in- Package file inclusion rulestest_slack_agent.py- Test suiterelease.sh- Automated release script
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file slack_joke_agent-1.1.2.tar.gz.
File metadata
- Download URL: slack_joke_agent-1.1.2.tar.gz
- Upload date:
- Size: 15.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f84096ed8aecabd7dc764277e0cbc6b0080dec020f324208820357db339d708b
|
|
| MD5 |
9d1a4d647110787ff33b9d8be8c4d732
|
|
| BLAKE2b-256 |
283daf94b4d44688a0ba5ed7ed7fe6ffa1b8b0998300deee4a5fb52c6e422f59
|
File details
Details for the file slack_joke_agent-1.1.2-py3-none-any.whl.
File metadata
- Download URL: slack_joke_agent-1.1.2-py3-none-any.whl
- Upload date:
- Size: 9.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
180f03aaad97f0966a50a1e486fb5704e18dc482670a68d723de76ab8143af1c
|
|
| MD5 |
b765c515111ee590fc342342cb44d479
|
|
| BLAKE2b-256 |
994590240b11eaf23b5c846fcc92dd88e4fca6c18bff79074b9aa3b36c3b9649
|