Skip to main content

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 (can be changed)
  • 🔧 Uses MCP Slack server integration
  • 🎯 Targets #random channel
  • 🐍 Written in Python with robust error handling
  • 📝 Comprehensive logging to file and console
  • 🔄 Graceful Fallback - Uses predefined content if AI generation fails

Setup

  1. Configure environment variables:

    cp .env.example .env
    # Edit .env with your Slack bot tokens
    

    check https://api.slack.com/apps to create a new app and get the bot token.

  2. Install Python dependencies:

    pip install -e .
    
  3. Or use the startup script (recommended):

    ./start-agent.sh
    

    This creates a virtual environment and installs dependencies automatically.

  4. Ensure MCP Slack server is configured (already done in your Cursor setup)

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

  1. Scheduling: Uses Python schedule library to trigger daily at 9:00 AM
  2. AI Generation: Uses Anthropic's Claude AI to generate fresh content on demand
  3. Content Selection: Randomly selects between jokes and trivia (60%/40% split)
  4. Fallback System: Uses predefined content if AI generation fails or API key not provided
  5. MCP Integration: Spawns the MCP Slack server process using subprocess
  6. Channel: Posts to #random channel (C1LLHJA4W)
  7. Logging: Logs to both console and slack_agent.log file with AI generation status

Configuration

The agent uses environment variables from .env file:

Required:

  • SLACK_BOT_TOKEN - Your Slack bot token (xoxb-...)
  • SLACK_TEAM_ID - Your Slack team/workspace ID
  • ANTHROPIC_API_KEY - Your Anthropic API key for Claude AI (sk-ant-...)

Optional:

  • SLACK_CHANNEL_IDS - Comma-separated list of allowed channels
  • DEFAULT_CHANNEL_ID - Default channel to post

Security:

  • All secrets loaded from .env file (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: Each message is freshly generated by Claude AI
  • Workplace Appropriate: AI prompts ensure clean, professional content
  • Automatic Fallback: Uses predefined content if AI generation fails
  • Smart Logging: Tracks AI generation success/failure for monitoring

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.log with 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

Managed via pyproject.toml:

Runtime Dependencies:

  • Python 3.8+
  • schedule>=1.2.0 - Task scheduling library
  • python-dotenv>=1.0.0 - Environment variable loading
  • anthropic>=0.40.0 - Anthropic Claude AI SDK

Development Dependencies (optional):

  • pytest>=7.0.0 - Testing framework
  • black>=22.0.0 - Code formatter
  • flake8>=4.0.0 - Linting
  • mypy>=0.950 - Type checking

Built-in modules:

  • subprocess - MCP server communication
  • json, random, logging, datetime - Standard library

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:

./release.sh  # Complete build, test, and publish workflow

Running Tests:

python test_slack_agent.py

Package Structure:

  • slack_agent.py - Main application
  • pyproject.toml - Modern Python packaging
  • LICENSE - MIT license
  • MANIFEST.in - Package file inclusion rules
  • test_slack_agent.py - Test suite
  • build.sh - Simple build script
  • release.sh - Full release workflow

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

slack_joke_agent-1.0.2.tar.gz (15.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

slack_joke_agent-1.0.2-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

Details for the file slack_joke_agent-1.0.2.tar.gz.

File metadata

  • Download URL: slack_joke_agent-1.0.2.tar.gz
  • Upload date:
  • Size: 15.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.13

File hashes

Hashes for slack_joke_agent-1.0.2.tar.gz
Algorithm Hash digest
SHA256 cf604873ec77ca1b954d1bdfdfc4695d1aec7c6e04e62cc26db4c7b361e6542f
MD5 d2a1dc88ff8c89b3e4e6ac63d24b7284
BLAKE2b-256 e6b6076584d297fd76a56f181bd99389d8694e09f308a1bb2270d28701e0b68a

See more details on using hashes here.

File details

Details for the file slack_joke_agent-1.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for slack_joke_agent-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 574f061ae131b008eedeb286002550b3d18d28ea2a8b83fae9df9d51a5eeff3c
MD5 d6e40733005f12c1e223a5d3931fde0f
BLAKE2b-256 396789bf6c86d1f10261319ef8c33167903e0ae707adc143b62b618514eeb648

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page