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.
- You open Chrome/Browser and navigate to X/Twitter (logged in)
- Agent takes screenshots of your screen
- Claude analyzes the screenshots to find relevant posts
- Agent performs actions (clicking, typing) to comment on posts
- Rinse and repeat until time limit is reached
- πΌοΈ 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
- 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)
-
Clone or download this repository
-
Install dependencies:
pip install -r requirements.txt- Create a
.envfile in the project directory:
ANTHROPIC_API_KEY=your_anthropic_api_key_here
USER_FOCUS=crypto markets and unemployment
RUN_DURATION_MINUTES=30| 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 |
- Open Chrome (or your preferred browser)
- Navigate to https://twitter.com or https://x.com
- Make sure you're logged in
- Keep the browser window visible (agent needs to see it)
cd /Users/mkorovkin/Desktop/twitter-computer-use-agent
source venv/bin/activate
python twitter_agent.pyOr use the quick start script:
./run.sh- 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+Cto stop anytime
βββββββββββββββββββββββββββββββββββββββ
β 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
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
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"
- Short runs (5-15 min): Good for testing
- Medium runs (30-60 min): Regular engagement
- Long runs (2+ hours): Extended monitoring
- Make sure your
.envfile exists in the project directory - Check that the API key is correctly formatted
- 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
- Make sure your browser is in a standard window size
- Try maximizing the browser window
- The agent uses Claude to detect UI element positions
- 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
- Go to System Preferences > Security & Privacy > Privacy
- Give Terminal (or your terminal app) permissions for:
- Screen Recording
- Accessibility
- Don't spam or post irrelevant comments
- Keep your
USER_FOCUSspecific - Review generated comments periodically
- Be transparent if asked about automation
- Use responsibly and respectfully
- Never commit your
.envfile to version control - Keep your Anthropic API key secure
- Monitor API usage to avoid unexpected charges
- Consider using a test Twitter account
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.
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
- Uses macOS
screencapturefor 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.)
- β 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)
- 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)
- Linux/Windows support
- Multi-monitor support
- More sophisticated click detection
- Sentiment analysis before commenting
- Dashboard for monitoring stats
- Configurable comment styles
This project is provided as-is for educational purposes.
For issues:
- Check
agent.logfor detailed errors - Review this README's troubleshooting section
- Verify all dependencies are installed
- Ensure
.envis configured correctly - Check macOS permissions (Screen Recording + Accessibility)
- 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
- Selenium-based browser automation
- Had login/session issues