Skip to content

mkorovkin2/twitter-computer-use-agent

Repository files navigation

Twitter Computer Use Agent

A Python-based automation agent that uses Anthropic's Claude AI with Computer Use capabilities to browse Twitter/X, identify relevant posts, and post contextual comments. The agent uses screenshot analysis and simulated mouse/keyboard actions to interact with your browser.

How It Works

  1. You open Chrome/Browser and navigate to X/Twitter (logged in)
  2. Agent takes screenshots of your screen
  3. Claude analyzes the screenshots to find relevant posts
  4. Agent performs actions (clicking, typing) to comment on posts
  5. Rinse and repeat until time limit is reached

Features

  • πŸ–ΌοΈ Computer Vision: Uses Claude's vision API to analyze screenshots
  • 🎯 Smart Filtering: Only engages with posts related to your focus area
  • πŸ’¬ AI Comments: Generates thoughtful, contextual comments
  • 🚫 Duplicate Prevention: Tracks commented posts to avoid repeats
  • πŸ›‘οΈ Fault Tolerant: Comprehensive error handling and retry logic
  • ⏱️ Time-Based: Runs for a specified duration with automatic shutdown
  • πŸ€– Natural Behavior: Realistic delays between actions

Prerequisites

  • macOS (uses AppleScript and screencapture)
  • Python 3.8 or higher
  • Chrome or any browser
  • Anthropic API key (get one here)
  • Twitter/X account (stay logged in)

Installation

  1. Clone or download this repository

  2. Install dependencies:

pip install -r requirements.txt
  1. Create a .env file in the project directory:
ANTHROPIC_API_KEY=your_anthropic_api_key_here
USER_FOCUS=crypto markets and unemployment
RUN_DURATION_MINUTES=30

Environment Variables

Variable Description Example
ANTHROPIC_API_KEY Your Anthropic API key (required) sk-ant-...
USER_FOCUS Topic focus for identifying relevant posts crypto markets, AI safety, web3
RUN_DURATION_MINUTES How long the agent should run (in minutes) 30

Usage

Step 1: Open Twitter/X

  1. Open Chrome (or your preferred browser)
  2. Navigate to https://twitter.com or https://x.com
  3. Make sure you're logged in
  4. Keep the browser window visible (agent needs to see it)

Step 2: Run the Agent

cd /Users/mkorovkin/Desktop/twitter-computer-use-agent
source venv/bin/activate
python twitter_agent.py

Or use the quick start script:

./run.sh

Step 3: Let It Run

  • The agent will start in 5 seconds
  • It will take screenshots and analyze the feed
  • When it finds relevant posts, it will:
    • Click the reply button
    • Type a comment
    • Click the Post button
  • You can watch it work in real-time!
  • Press Ctrl+C to stop anytime

What Happens

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  1. Screenshot Twitter feed         β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
               β”‚
               β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  2. Claude analyzes screenshot      β”‚
β”‚     - Finds relevant posts          β”‚
β”‚     - Locates reply button coords   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
               β”‚
               β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  3. For each relevant post:         β”‚
β”‚     - Generate comment with Claude  β”‚
β”‚     - Click reply button            β”‚
β”‚     - Type comment                  β”‚
β”‚     - Click Post button             β”‚
β”‚     - Save post ID (no duplicates)  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
               β”‚
               β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  4. Scroll down                     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
               β”‚
               β–Ό
        Repeat until time limit

File Structure

twitter-computer-use-agent/
β”œβ”€β”€ twitter_agent.py          # Main agent script
β”œβ”€β”€ requirements.txt           # Python dependencies
β”œβ”€β”€ .env                       # Configuration (create this)
β”œβ”€β”€ env.template              # Example configuration template
β”œβ”€β”€ commented_posts.json       # Tracks commented posts (auto-generated)
β”œβ”€β”€ agent.log                  # Detailed log file (auto-generated)
└── README.md                  # This file

Configuration Tips

Adjusting Focus

The USER_FOCUS can be:

  • Broad topics: "technology", "sports", "politics"
  • Specific interests: "machine learning", "sustainable energy", "DeFi"
  • Multiple keywords: "AI safety and ethics"
  • Very specific: "GPT-4 benchmarks and performance"

Run Duration

  • Short runs (5-15 min): Good for testing
  • Medium runs (30-60 min): Regular engagement
  • Long runs (2+ hours): Extended monitoring

Troubleshooting

"ANTHROPIC_API_KEY not found"

  • Make sure your .env file exists in the project directory
  • Check that the API key is correctly formatted

Agent can't see the screen

  • Make sure Chrome window is visible and not minimized
  • The agent needs to see the Twitter feed on screen
  • Don't switch to other windows while it's running

Clicks are in wrong positions

  • Make sure your browser is in a standard window size
  • Try maximizing the browser window
  • The agent uses Claude to detect UI element positions

Agent not finding posts

  • Check your USER_FOCUS - it might be too specific
  • Make sure you're on the Twitter/X home feed (not profile/settings)
  • Try scrolling manually to see some posts first

Permission errors (macOS)

  • Go to System Preferences > Security & Privacy > Privacy
  • Give Terminal (or your terminal app) permissions for:
    • Screen Recording
    • Accessibility

Safety & Best Practices

Twitter Terms of Service

⚠️ Important: Automated posting may violate Twitter's ToS. This tool is for educational purposes only. Use at your own risk.

Ethical Considerations

  • Don't spam or post irrelevant comments
  • Keep your USER_FOCUS specific
  • Review generated comments periodically
  • Be transparent if asked about automation
  • Use responsibly and respectfully

Security

  • Never commit your .env file to version control
  • Keep your Anthropic API key secure
  • Monitor API usage to avoid unexpected charges
  • Consider using a test Twitter account

Logging

The agent maintains detailed logs in agent.log:

  • All actions taken (screenshots, clicks, comments)
  • Errors and warnings
  • Claude's analysis results
  • Timestamps for all events

Check this file for debugging or monitoring agent behavior.

State Persistence

commented_posts.json tracks all posts you've commented on:

  • Persists across runs
  • Prevents duplicate comments
  • Can be manually edited if needed (array of SHA256 hashes)
  • Delete this file to reset comment history

Technical Details

Computer Use Implementation

  • Uses macOS screencapture for screenshots
  • Uses AppleScript for mouse clicks and keyboard input
  • Claude analyzes screenshots to find UI elements
  • No browser automation framework needed
  • Works with any browser (Chrome, Safari, Firefox, etc.)

Why This Approach?

  • βœ… No browser login issues (you stay logged in)
  • βœ… No Selenium session problems
  • βœ… Works with any browser
  • βœ… More natural behavior
  • βœ… Simpler setup
  • ❌ Requires screen to be visible
  • ❌ macOS only (for now)

Limitations

  • macOS only (uses AppleScript)
  • Requires browser window to be visible
  • Can't run in background/headless
  • Slower than direct browser automation
  • API costs for Claude vision analysis
  • Screen must stay active (no sleep mode)

Future Improvements

  • Linux/Windows support
  • Multi-monitor support
  • More sophisticated click detection
  • Sentiment analysis before commenting
  • Dashboard for monitoring stats
  • Configurable comment styles

License

This project is provided as-is for educational purposes.

Support

For issues:

  1. Check agent.log for detailed errors
  2. Review this README's troubleshooting section
  3. Verify all dependencies are installed
  4. Ensure .env is configured correctly
  5. Check macOS permissions (Screen Recording + Accessibility)

Changelog

Version 2.0.0 (Computer Use)

  • BREAKING: Removed Selenium dependency
  • Now uses Computer Use approach
  • Takes screenshots and performs mouse/keyboard actions
  • No more browser session issues
  • User launches browser manually
  • More reliable and natural behavior

Version 1.0.0 (Initial Release - Deprecated)

  • Selenium-based browser automation
  • Had login/session issues

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published