This application generates and posts tweets using Cloudflare Workers AI (specifically the Llama-3.1 model) and the Twitter (X) API to craft engaging and funny tweets according to pre-set prompts.
- AI-Powered Tweet Generation: Utilizes Cloudflare Workers AI (Llama-3.1) to create tweets based on predefined prompts.
- Twitter API Integration: Posts generated tweets directly to your Twitter account using Tweepy.
- Customizable Tweet Prompts: You can easily modify the tweet prompts to fit your desired topics.
Prerequisites Before you begin, ensure you have the following:
- Cloudflare API Keys
- You need an
ACCOUNT_ID
andAUTH_TOKEN
to use Cloudflare Workers AI. - Visit the Cloudflare Workers documentation to set up an account and get API keys.
- Twitter API Keys Obtain the following keys from the Twitter Developer Portal:
TWITTER_API_KEY
TWITTER_API_SECRET
TWITTER_ACCESS_TOKEN
TWITTER_ACCESS_TOKEN_SECRET
Set up a developer account with Twitter (X) and create a project/app to obtain these keys.
- .env File: Store all your keys in an
.env
file to ensure they are securely loaded into your environment. Example:
TWITTER_API_KEY=your-twitter-api-key
TWITTER_API_SECRET=your-twitter-api-secret
TWITTER_ACCESS_TOKEN=your-twitter-access-token
TWITTER_ACCESS_TOKEN_SECRET=your-twitter-access-token-secret
CLOUDFLARE_ACCOUNT_ID=your-cloudflare-account-id
CLOUDFLARE_AUTH_TOKEN=your-cloudflare-auth-token
- Clone the repository:
git clone https://github.com/elizabethsiegle/workers-ai-twitter-api-tweet.git
cd workers-ai-twitter-api-tweet
- Install the required Python packages:
pip install tweepy python-dotenv requests
- Set up your
.env
file with your Twitter and Cloudflare API keys as mentioned above. - Run the script:
python3 cf-tweet.py
- The app fetches a random tweet prompt from a list.
- It sends the selected prompt to Cloudflare Workers AI (Llama-3.1), which generates a tweet.
- The generated tweet is then posted on Twitter using Tweepy.
- Error Handling: The app retries if any errors occur, such as exceeding the tweet character limit or encountering API restrictions.
- Cloudflare Workers AI: Cloudflare Workers AI Docs
- Twitter (X) API: Twitter API Docs