This repository contains a Python script designed to automate interacting with Twitter using scraped data and generating insightful replies using OpenAI's language model. The script searches for tweets containing specific keywords and replies to them with contextually relevant comments.
- Automated Tweet Search: Searches for tweets containing configurable keywords.
- OpenAI Integration: Generates insightful replies using OpenAI's GPT-4 model.
- Engagement-Based Sorting: Sorts tweets based on engagement metrics such as likes, retweets, and replies.
- Configurable Frequency: Controls the frequency of tweet responses to avoid rate limits and spam detection.
- Python 3.7 or higher
twscrape
library for scraping tweetsopenai
library for interacting with OpenAI's API- A Twitter account with accessible cookie data
- OpenAI API key
-
Clone the repository
git clone https://github.com/ihuzaifashoukat/twitter-automation-ai.git cd twitter-automation-ai
-
Create a virtual environment (optional but recommended)
python3 -m venv venv source venv/bin/activate # On Windows use `venv\Scripts\activate`
-
Install dependencies
pip install -r requirements.txt
-
OpenAI API Key
- Replace the placeholder value in the script:
openai_api_key = "your-openai-api-key"
-
Twitter Cookie Data
- Replace the placeholder value in the script with your Twitter account cookies:
twitter_cookie_data = '{"ct0": "your-ct0-value", "auth_token": "your-auth-token"}'
-
Target Keywords
- You can customize the keywords to search for:
target_keywords = ["Linkedin", "Content", "Marketing", "Copywriting", "Ghostwriting"]
-
Response Interval
- Adjust the base interval to control how frequently replies are sent:
response_interval = 300 # 5 minutes
-
Maximum Tweets Per Keyword
- Set the maximum number of tweets to fetch per keyword to prioritize top accounts:
max_tweets_per_keyword = 15
To run the script:
python app.py
The script will:
- Initialize the Twitter and OpenAI clients.
- Search for tweets containing the specified keywords.
- Filter and sort tweets based on engagement metrics.
- Generate replies using OpenAI and post them to Twitter.
- Repeat the process at intervals defined by
response_interval
.
- Rate Limits: Be cautious with the number of requests to avoid hitting Twitter's rate limits. The script includes handling for rate limits and waits for 15 minutes if limits are exceeded.
- Engagement Metrics: Adjust the
filter_and_sort_tweets
function if you need a different sorting algorithm or additional filtering criteria. - Logging: Enhance the
print
statements with proper logging if you're planning to run this script in a production environment.
- Respect Twitter's Terms of Service: Ensure that your usage complies with Twitter's rules and guidelines, especially regarding automation.
- OpenAI's Use Case Policy: Make sure your application adheres to OpenAI's use case policy.
- Ethical Considerations: Use this script responsibly. Avoid using it for spamming, trolling, or any other unethical activity.
Note: This script is a basic implementation and might require adjustments and refinements based on your specific use case and requirements. Always test thoroughly in a controlled environment before deploying it in production.