├── LICENSE ├── README.md ├── config └── keys.py ├── data └── tweets.txt ├── requirements.txt └── src ├── __init__.py ├── functions.py ├── instantly-tweet-from-openai.py ├── schedule-daily-post-from-file.py ├── schedule-daily-post-from-openai.py ├── tweeter-from-code.py └── tweeter-random-from-file.py /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 Lewis Pour 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |
2 | 3 | # 🐦 Twitter Auto-Post Bot 🤖 4 | 5 | [![Python](https://img.shields.io/badge/python-v3.7+-blue.svg)](https://www.python.org/downloads/release/python-370/) 6 | [![Tweepy](https://img.shields.io/badge/tweepy-v4.14-blue)](http://docs.tweepy.org/en/latest/) 7 | [![OpenAI](https://img.shields.io/badge/openai-v1.11.1-blue.svg)](https://platform.openai.com/docs/libraries) 8 | 9 | [![Schedule](https://img.shields.io/badge/schedule-v1.2.1-blue)](https://schedule.readthedocs.io/en/stable/) 10 | [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) 11 | 12 | Automate your Twitter presence. Auto Post tweets from from openAI GPT4, from a file, from a string, schedule a new tweet to be posted daily or post the tweet instantly. 13 |
14 | 15 | --- 16 | 17 | ## 🌟 About The Project 18 | 19 | This Python-based Twitter Auto-Post Bot automates tweeting, Credit to the Tweepy library for making this easy, this project enables scheduled and random tweets, offering a dynamic and engaging Twitter experience. 20 | 21 | ### 📁 Files Overview 22 | #### Using OpenAI 23 | ##### Instantly: 24 | - `src/instantly-tweet-from-openai.py`: Immediately tweets a tweet from openAI api response, currently using GPT4, but you can change the model in [functions.py](https://github.com/lewispour/Twitter-auto-Post-Bot---X.com---Tweepy-python-bot/blob/main/src/functions.py#L21) 25 | 26 | - ###### Prompt defined [here](https://github.com/lewispour/Twitter-auto-Post-Bot---X.com---Tweepy-python-bot/blob/main/src/instantly-tweet-from-openai.py#L7) 27 | 28 | ##### Schedule to auto post, tweet daily at a time: 29 | - `src/schedule-daily-post-from-openai.py`: Automates daily tweets, Runs daily at a scheduled time and queries open ai api to create a tweet, the tweet returned is then automatically tweeted each day to fully automate twitter on auto pilot. By default the model is OPENAI GPT4 but you can change the model in [functions.py](https://github.com/lewispour/Twitter-auto-Post-Bot---X.com---Tweepy-python-bot/blob/main/src/functions.py#L21). 30 | - ###### Prompt defined [here](https://github.com/lewispour/Twitter-auto-Post-Bot---X.com---Tweepy-python-bot/blob/main/src/schedule-daily-post-from-openai.py#L12) 31 | - ###### Schedule time defined [here](https://github.com/lewispour/Twitter-auto-Post-Bot---X.com---Tweepy-python-bot/blob/main/src/schedule-daily-post-from-openai.py#L20) 32 | 33 | #### From File 34 | - `src/schedule-daily-post-from-file.py`: Automates daily tweets, randomly selecting from `tweets.txt`. To change the schedule time [edit this](https://github.com/lewispour/Twitter-auto-Post-Bot---X.com---Tweepy-python-bot/blob/main/src/schedule-daily-post-from-file.py#L22). 35 | - `src/tweeter-random-from-file.py`: Instantly posts a random tweet from `tweets.txt`. 36 | 37 | ###### Add your tweets to `data/tweets.txt`: one per line. They will be randomly selected and tweeted. 38 | 39 | #### Manually tweeting using script 40 | `src/tweeter-from-code.py`: Immediately tweets a pre-defined message with the current date, but you can change this to whatever you like. 41 | 42 | #### common files 43 | - `config/keys.py`: Holds both the creds for openai and twitter api. 44 | - `src/functions.py`: Shared functions for generating tweets from openai and tweet posting 45 | - `requirements.txt`: Lists all necessary Python packages. 46 | 47 | ### 📁 Upcoming Features 48 | - `Adding CLI`. 49 | 50 | ## 🚀 Getting Started 51 | 52 | ### Prerequisites 53 | 54 | - Python 3.x 55 | - Tweepy (Twitter API) 56 | 57 | ### Installation 58 | 59 | 1. Clone the repo: 60 | ```sh 61 | git clone git@github.com:lewispour/Twitter-auto-Post-Bot---X.com---Tweepy-python-bot.git 62 | ``` 63 | 2. Install Python packages: 64 | ```sh 65 | pip install -r requirements.txt 66 | ``` 67 | 68 | ### Setup 69 | 70 | 1. Obtain Twitter API credentials [here](https://developer.twitter.com/apps). 71 | 2. update `config/keys.py` file with your credentials: 72 | ```python 73 | bearer_token = "GET_KEY_FROM_developer.twitter.com/apps" 74 | api_key = "GET_KEY_FROM_developer.twitter.com/apps" 75 | api_secret = "GET_KEY_FROM_developer.twitter.com/apps" 76 | access_token = "GET_KEY_FROM_developer.twitter.com/apps" 77 | access_token_secret = "GET_KEY_FROM_developer.twitter.com/apps" 78 | openai_key = "GET_YOUR_OPENAI_API_KEY_FROM_https://platform.openai.com/api-keys" 79 | ``` 80 | 3. Customize `data/tweets.txt` with your tweets. (SKIP: If not using tweet from file) 81 | 82 | ## 🔧 Usage 83 | 84 | Run any script using Python: 85 | 86 | ```bash 87 | cd src/ 88 | python instantly-tweet-from-openai.py 89 | ``` 90 | 91 | ## 🤝 Contributing 92 | 93 | Contributions are what make the open-source community such an amazing place to learn, inspire, and create. Any contributions you make are **greatly appreciated**. 94 | 95 | ## 📝 License 96 | 97 | Distributed under the MIT License. See `LICENSE` for more information. 98 | 99 | ## ✉️ Contact 100 | Project Link: [https://github.com/lewispour/Twitter-auto-Post-Bot---X.com---Tweepy-python-bot](https://github.com/lewispour/Twitter-auto-Post-Bot---X.com---Tweepy-python-bot) 101 | 102 | ## ✉️ Status 103 | - Last tested and still working on 24/12/2024 ✅ 104 | -------------------------------------------------------------------------------- /config/keys.py: -------------------------------------------------------------------------------- 1 | bearer_token = "GET_KEY_FROM_developer.twitter.com/apps" 2 | api_key = "GET_KEY_FROM_developer.twitter.com/apps" 3 | api_secret = "GET_KEY_FROM_developer.twitter.com/apps" 4 | access_token = "GET_KEY_FROM_developer.twitter.com/apps" 5 | access_token_secret = "GET_KEY_FROM_developer.twitter.com/apps" 6 | openai_key = "GET_YOUR_OPENAI_API_KEY_FROM_https://platform.openai.com/api-keys" 7 | -------------------------------------------------------------------------------- /data/tweets.txt: -------------------------------------------------------------------------------- 1 | Here is a tweet 2 | here is a different tweet 3 | tweeet tweeet blah blah 4 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | tweepy 2 | schedule 3 | openai -------------------------------------------------------------------------------- /src/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lewispour/Twitter-auto-Post-Bot---X.com---Tweepy-python-bot/f949694d631969185fb0f5d8cf99e61ed9107831/src/__init__.py -------------------------------------------------------------------------------- /src/functions.py: -------------------------------------------------------------------------------- 1 | import tweepy 2 | import datetime 3 | import sys 4 | from openai import OpenAI 5 | sys.path.append('../config') 6 | import keys 7 | 8 | 9 | def initialize_tweepy(): 10 | client = tweepy.Client(keys.bearer_token, keys.api_key, keys.api_secret, keys.access_token, keys.access_token_secret) 11 | auth = tweepy.OAuthHandler(keys.api_key, keys.api_secret, keys.access_token, keys.access_token_secret) 12 | api = tweepy.API(auth) 13 | return client, api 14 | 15 | def get_formatted_date(): 16 | current_date = datetime.date.today() 17 | return current_date.strftime("%B %d, %Y") 18 | 19 | def generate_response(prompt): 20 | client = OpenAI(api_key=keys.openai_key) 21 | model = "gpt-4-1106-preview" 22 | messages = [ 23 | {"role": "system", "content": "You are a helpful assistant."}, 24 | {"role": "user", "content": prompt} 25 | ] 26 | 27 | response = client.chat.completions.create( 28 | model=model, 29 | messages=messages, 30 | temperature=0 31 | ) 32 | 33 | response_message = response.choices[0].message.content 34 | return response_message.strip() -------------------------------------------------------------------------------- /src/instantly-tweet-from-openai.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import os 3 | sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'config')) 4 | import keys 5 | from functions import generate_response, initialize_tweepy, get_formatted_date 6 | 7 | prompt = "Create a short tweet about Motorbikes." 8 | response = generate_response(prompt) 9 | 10 | def send_post(): 11 | client, _ = initialize_tweepy() 12 | tweet_text = f"{response}" 13 | client.create_tweet(text=tweet_text) 14 | print("Tweet posted successfully") 15 | 16 | send_post() 17 | -------------------------------------------------------------------------------- /src/schedule-daily-post-from-file.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import os 3 | import random 4 | import schedule 5 | import time 6 | 7 | sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'config')) 8 | import keys 9 | sys.path.append(os.path.join(os.path.dirname(__file__), '..')) 10 | from functions import initialize_tweepy, get_formatted_date 11 | 12 | def send_post(): 13 | client, _ = initialize_tweepy() 14 | 15 | with open(os.path.join(os.path.dirname(__file__), '..', 'data', 'tweets.txt'), 'r') as file: 16 | lines = file.readlines() 17 | tweet_text = random.choice(lines).strip() 18 | client.create_tweet(text=f"{tweet_text}") 19 | 20 | print("Tweet posted successfully") 21 | 22 | schedule.every().day.at("09:00").do(send_post) 23 | 24 | while True: 25 | schedule.run_pending() 26 | time.sleep(60) 27 | -------------------------------------------------------------------------------- /src/schedule-daily-post-from-openai.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import os 3 | import schedule 4 | import time 5 | import random 6 | 7 | sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'config')) 8 | import keys 9 | from functions import generate_response, initialize_tweepy, get_formatted_date 10 | 11 | def send_post(): 12 | prompt = "Create a short tweet about Motorbikes." 13 | response = generate_response(prompt) 14 | 15 | client, _ = initialize_tweepy() 16 | tweet_text = f"{response}" 17 | client.create_tweet(text=tweet_text) 18 | print("Tweet posted successfully") 19 | 20 | schedule.every().day.at("09:00").do(send_post) 21 | 22 | while True: 23 | schedule.run_pending() 24 | time.sleep(60) 25 | -------------------------------------------------------------------------------- /src/tweeter-from-code.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import os 3 | 4 | sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'config')) 5 | import keys 6 | sys.path.append(os.path.join(os.path.dirname(__file__), '..')) 7 | from functions import initialize_tweepy, get_formatted_date 8 | 9 | def send_post(): 10 | client, _ = initialize_tweepy() 11 | formatted_date = get_formatted_date() 12 | 13 | client.create_tweet(text=f"Hello Python 🐍. It is {formatted_date} today!🚀🚀.") 14 | print("Tweet posted successfully") 15 | 16 | send_post() 17 | -------------------------------------------------------------------------------- /src/tweeter-random-from-file.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import os 3 | import random 4 | 5 | sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'config')) 6 | import keys 7 | sys.path.append(os.path.join(os.path.dirname(__file__), '..')) 8 | from functions import initialize_tweepy 9 | 10 | def send_post(): 11 | client, _ = initialize_tweepy() 12 | 13 | with open(os.path.join(os.path.dirname(__file__), '..', 'data', 'tweets.txt'), 'r') as file: 14 | lines = file.readlines() 15 | tweet_text = random.choice(lines).strip() 16 | client.create_tweet(text=f"{tweet_text}") 17 | 18 | print("Tweet posted successfully") 19 | 20 | send_post() 21 | --------------------------------------------------------------------------------