Skip to content

calc1f4r/web3-sec-twitter-bot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Web3 Security Twitter Bot

An intelligent Twitter bot that automatically finds and engages with tweets related to web3 security, blockchain technology, and programming topics. The bot uses Grok AI to generate thoughtful, contextual replies to relevant tweets.

What the Bot Does

This Twitter bot performs the following tasks:

  1. Searches for Relevant Tweets: Automatically searches for tweets containing keywords related to:

    • Web3 security
    • Blockchain security
    • Smart contract security
    • DeFi security
    • Crypto security
    • Web3 audits
    • Smart contract audits
    • Solidity security
    • Ethereum security
    • General programming and coding topics
  2. Intelligent Reply Generation: Uses Grok AI (xAI's language model) to generate:

    • Technical and knowledgeable responses for programming/security topics
    • Congratulatory messages for personal/professional achievements
    • Witty and engaging responses for other topics
    • All replies are kept under 280 characters
  3. Rate Limit Management: Implements smart rate limiting and delays to respect Twitter's API limits

  4. Database Management: Stores tweets in a local SQLite database to track which tweets have been replied to

Features

  • AI-Powered Responses: Uses Grok-3-mini-fast model for generating contextual replies
  • Smart Search: Targets specific keywords while filtering out retweets and non-English content
  • Rate Limit Compliance: Built-in delays and rate limit handling
  • Database Tracking: Prevents duplicate replies and manages tweet queue
  • Error Handling: Robust error handling for API failures and authentication issues

Requirements

  • Python 3.7+
  • Twitter Developer Account with API v2 access
  • xAI API access for Grok
  • Required Python packages (see requirements.txt)

Installation

  1. Clone the repository:

    git clone <repository-url>
    cd tweepy
  2. Install dependencies:

    pip install -r requirements.txt
  3. Set up environment variables (see Configuration section below)

Configuration

1. Twitter API Setup

  1. Create a Twitter Developer Account at developer.twitter.com
  2. Create a new app and generate the following credentials:
    • API Key (Consumer Key)
    • API Secret (Consumer Secret)
    • Access Token
    • Access Token Secret
    • Bearer Token

Important: Make sure your Twitter app has Read and Write permissions to post replies.

2. Grok API Setup

  1. Get access to xAI's API at x.ai
  2. Generate a Grok API key

3. Environment Variables

Create a .env file in the project root with the following variables:

# Twitter API Credentials
TWITTER_API_KEY=your_twitter_api_key
TWITTER_API_SECRET=your_twitter_api_secret
TWITTER_ACCESS_TOKEN=your_access_token
TWITTER_ACCESS_TOKEN_SECRET=your_access_token_secret
TWITTER_BEARER_TOKEN=your_bearer_token

# Grok API Credentials
GROK_API_KEY=your_grok_api_key

# Optional: Twitter OAuth 2.0 (if needed)
CLIENT_ID=your_client_id
CLIENT_SECRET=your_client_secret

4. Customization

You can modify the search query in bot.py by changing the search_query parameter in the fetch_tweets() function:

search_query="web3 security OR blockchain security OR smart contract security OR DeFi security OR crypto security OR web3 audit OR smart contract audit OR solidity security OR ethereum security OR programming OR coding tech -is:retweet lang:en"

Usage

Running the Bot

  1. Start the bot:

    python bot.py
  2. The bot will:

    • Initialize the SQLite database
    • Authenticate with Twitter and Grok APIs
    • Start the main loop:
      • Fetch new tweets when the database has fewer than 5 unreplied tweets
      • Generate AI responses using Grok
      • Post replies to Twitter
      • Wait between cycles to respect rate limits

Bot Behavior

  • Cycle Time: The bot waits 30-60 seconds between cycles
  • Fetch Timing: Waits 5-10 minutes after fetching new tweets
  • Processing Delays: Waits 1-3 minutes before processing tweets to avoid overwhelming
  • Tweet Selection: Randomly selects unreplied tweets from the database
  • Reply Length: Automatically truncates replies to 280 characters

Database

The bot uses SQLite database (tweets.db) with the following schema:

CREATE TABLE tweets (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    tweet_id TEXT NOT NULL UNIQUE,
    author_id TEXT NOT NULL,
    text TEXT NOT NULL,
    replied INTEGER DEFAULT 0
);

Error Handling

The bot handles various error scenarios:

  • Rate Limiting: Waits and retries when API limits are reached
  • Authentication Errors: Validates credentials on startup
  • Network Issues: Continues running despite temporary failures
  • Permission Errors: Alerts if Twitter app lacks write permissions

Monitoring

The bot provides console output for monitoring:

  • Authentication status
  • Tweet fetching progress
  • Reply generation and posting
  • Error messages and warnings
  • Cycle timing information

Troubleshooting

Common Issues

  1. 403 Forbidden Error: Your Twitter app likely doesn't have write permissions

    • Go to Twitter Developer Portal → Your App → Settings → User authentication settings
    • Enable "Read and write" permissions
  2. Authentication Failed: Check your API credentials in the .env file

  3. No Tweets Found: The search query might be too restrictive or there might be no recent tweets matching the criteria

  4. Rate Limit Exceeded: The bot should handle this automatically, but you can increase wait times if needed

Logs

Monitor the console output for detailed information about the bot's operation and any errors encountered.

License

This project is open source. Please ensure you comply with Twitter's API Terms of Service and xAI's usage policies when using this bot.

Disclaimer

This bot is for educational and demonstration purposes. Always ensure your bot's behavior complies with Twitter's automation rules and community guidelines. Be respectful and avoid spam-like behavior.

About

Twitter bot which uses grok-ai to write custom replies for tweets to boost engagement

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages