xBot is a simple Python script that retrieves a random image from Picsum and posts it to X (formerly known as Twitter) using the Tweepy library. This script works using X's free tier API.
We use both v1 and v2 APIs in this project for the following reasons:
- v1 API: The v1 API is used for media upload functionality. This is because the media upload endpoint is not yet available in the v2 API.
- v2 API: The v2 API is used for posting tweets as you can't post tweets on free tier through v1 API.
Before you begin, ensure you have met the following requirements:
- You have Python 3.x installed on your machine.
- You have a Twitter/X Developer account and have created an app to obtain the necessary API keys and tokens (YOU NEED TO USE OAuth1 credentials!!).
- Read/Write permissions within your X app settings.
- Clone the repository or download the script.
- Navigate to the project directory.
- Install the required Python packages using pip:
pip install -r requirements.txt
Create a .env
file in the project directory and add your Twitter/X API credentials:
CONSUMER_KEY=your_consumer_key
CONSUMER_KEY_SECRET=your_consumer_key_secret
ACCESS_TOKEN=your_access_token
ACCESS_TOKEN_SECRET=your_access_token_secret
Ensure the .env
file is in the same directory as script.py
.
To run the script, execute the following command:
python script.py
The script will:
- Authenticate to X using the provided API credentials.
- Retrieve a random image from Picsum and save it to the working directory.
- Upload the image to X via v1 API.
- Post a tweet with the uploaded image via v2 API.
This project is licensed under the MIT License. See the LICENSE file for details.