An automated Twitter/X bot that generates and posts engaging AI and tech-related content using Google's Gemini API with integrated web search capabilities.
This bot automatically:
- Generates engaging tweets about AI, ML, and tech topics
- Uses Google's Grounding Search to find latest information
- Avoids posting duplicate content
- Posts to Twitter/X automatically
- Tracks all posted content in CSV format
Twitter/
βββ main.py # Main application entry point
βββ requirements.txt # Python dependencies
βββ run_main.bat # Windows batch script for automation
βββ core/
β βββ config.py # Configuration and credentials
β βββ genai.py # Content generation using Gemini
β βββ posting.py # Twitter/X posting functionality
β βββ web_search.py # Web search utilities
βββ data/
βββ post_history.csv # Track posted tweets
- Validates all required API credentials (Twitter & Gemini)
- Loads post history from CSV to avoid duplicates
The ContentGenerator class offers two modes:
generate_tweet_with_search(topic=None, previous_posts=None)- Uses Google's Grounding Search to find latest information
- Generates tweets based on current trends and news
- More relevant and timely content
generate_tweet_simple(topic=None, previous_posts=None)- Generates content without web search
- Faster but may be less current
- Uses Gemini's knowledge cutoff
Tweet Generation Process:
- Selects a random topic from predefined list (or uses specified topic)
- Builds a prompt with requirements:
- Max 280 characters
- Engaging question, code snippet, or interesting fact
- 1-2 relevant hashtags
- Educational but entertaining
- Uses Gemini API to generate content
- Cleans and truncates to Twitter limits
- Checks generated tweet against
previous_postslist - Prevents posting identical content
- Maintains post history in CSV file
post_tweet(text)- Uses Tweepy library with Twitter API v2
- Authenticates using OAuth 1.0a
- Posts tweet and returns tweet ID
- Handles errors gracefully
- Saves each post to
data/post_history.csv - Records: timestamp, topic, and tweet text
- Used for duplicate detection in future runs
# Twitter API Credentials
BEARER_TOKEN=your_bearer_token
API_KEY=your_api_key
API_SECRET=your_api_secret
ACCESS_TOKEN=your_access_token
ACCESS_TOKEN_SECRET=your_access_token_secret
# Gemini API
GEMINI_API_KEY=your_gemini_api_key# Topics for tweet generation
TOPICS = [
'Artificial Intelligence',
'Generative AI',
'Machine Learning',
'Python Programming',
'Data Science',
'Deep Learning',
'Natural Language Processing',
'Computer Vision',
'Neural Networks',
'Large Language Models',
# ... more topics
]
# Model configurations
GEMINI_TEXT_MODEL = "gemini-2.5-flash"
TEMPERATURE = 0.7
MAX_TWEET_LENGTH = 280cd c:\Users\ravid\Desktop\Codes\Projects\Twitterpip install -r requirements.txtCreate a .env file in the root directory with your API credentials:
BEARER_TOKEN=...
API_KEY=...
API_SECRET=...
ACCESS_TOKEN=...
ACCESS_TOKEN_SECRET=...
GEMINI_API_KEY=...mkdir datapython main.pyrun_main.batThis batch file can be scheduled using Windows Task Scheduler for automatic posting.
π Starting Twitter Bot...
======================================================================
β
All credentials validated successfully!
======================================================================
π Loaded 42 previous posts from history
======================================================================
π Generating tweet with latest information...
π Topic: Artificial Intelligence
π Generated tweet:
What if AI could explain its reasoning in plain language? π€
New research shows models with "chain-of-thought" prompting improve accuracy by 40%!
#AI #MachineLearning
======================================================================
π€ Posting tweet to X...
β
Tweet posted! ID: 1234567890
πΎ Saved to post_history.csv
======================================================================
β
Tweet posted successfully!
π Tweet ID: 1234567890
======================================================================
π Bot execution completed!
- tweepy (>=4.14.0) - Twitter API client
- google-genai (>=1.0.0) - Google Gemini API
- python-dotenv (>=1.0.0) - Environment variable management
- pandas (>=2.0.0) - Data processing for CSV
- Uses Google Grounding Search for latest information
- Generates diverse content types:
- Thought-provoking questions
- Code snippets and challenges
- Interesting facts and recent developments
- Educational content with entertainment value
- Maintains history of all posted tweets
- Compares new content against previous posts
- Prevents accidental reposts
- Validates credentials before execution
- Graceful error handling for API failures
- Detailed logging for debugging
- Easy topic customization
- Adjustable temperature for creativity
- Multiple model support (Gemini, OpenAI)
from core.genai import generate_tweet
topic, tweet = generate_tweet(
use_web_search=True,
topic="Quantum Computing",
previous_posts=[]
)from core.genai import ContentGenerator
generator = ContentGenerator()
article = generator.generate_tech_article()from core.web_search import WebSearcher
searcher = WebSearcher()
result = searcher.search_tech_news()Post history is stored in data/post_history.csv:
| timestamp | topic | text |
|---|---|---|
| 2025-11-26T10:30:00 | Artificial Intelligence | What if AI could... |
| 2025-11-26T11:45:00 | Machine Learning | Here's a cool Python one-liner... |
- Never commit your
.envfile - Keep API credentials secure
- Use environment variables for sensitive data
- Regularly rotate access tokens
To add new topics:
- Edit
TOPICSlist incore/config.py - Keep topics AI/tech-related for best results
To modify tweet style:
- Edit prompt in
_build_tweet_prompt()incore/genai.py - Adjust
TEMPERATUREfor creativity level
This project is for personal use. Ensure compliance with Twitter's automation rules and API terms of service.
- Use responsibly and follow Twitter's automation rules
- Avoid excessive posting (rate limits)
- Ensure generated content aligns with Twitter's policies
- Monitor bot activity regularly
- Check
.envfile exists and has all required variables - Verify API credentials are correct
- Check Twitter API rate limits
- Verify OAuth credentials are valid
- Ensure tweet length is under 280 characters
- Verify Gemini API key is valid
- Check internet connection for web search
- Review API quota limits
For issues or questions, review the error messages in the console output. Most issues are related to:
- Missing or invalid API credentials
- Network connectivity
- API rate limits
- Duplicate content detection
Built with β€οΈ using Google Gemini and Tweepy