A Rust-based Twitter bot application using Twitter API v2 for posting tweets both via web interface and directly from your terminal.
- 🐦 Tweet from Terminal: Interactive tweeting directly from your command line
- 🌐 Web Interface: OAuth2 authentication and web-based tweeting
- 🔒 Secure Authentication: OAuth2 flow with PKCE for enhanced security
- 🔄 Token Management: Automatic token refresh and revocation
- ⚡ Real-time: Post tweets instantly from terminal after authentication
- Rust (latest stable version)
- Twitter Developer Account with API v2 access
- Cargo package manager
- Go to Twitter Developer Portal
- Create a new app or use existing one
- Navigate to your app's "Keys and Tokens" section
- Generate OAuth 2.0 Client ID and Client Secret
- Set up OAuth 2.0 settings:
- Type of App: Web App
- Callback URI:
http://127.0.0.1:3000/callback - Website URL:
http://127.0.0.1:3000(or your domain)
-
Clone this repository:
git clone <your-repo-url> cd twitter-bot
-
Install dependencies:
cargo build
-
Set up environment variables: Create a
.envfile in the root directory with your Twitter API credentials:CLIENT_ID=your_twitter_client_id_here CLIENT_SECRET=your_twitter_client_secret_here RUST_LOG=info
-
Run the application:
cargo run
-
Start the server:
cargo run
-
Follow the authentication flow:
- The server will start on
http://127.0.0.1:3000 - Open
http://127.0.0.1:3000/loginin your browser - Authorize the app with Twitter
- You'll be redirected back to the callback URL
- The server will start on
-
Start tweeting from terminal:
- After successful authentication, the terminal will show: "login finish"
- You can now type tweets directly in the terminal
Once authenticated, you can tweet directly from your terminal:
enter tweet: Hello from my Rust Twitter bot! 🦀
Tweet posted successfully! ID: 1234567890123456789
enter tweet: This is amazing! I can tweet from my terminal now! ✨
Tweet posted successfully! ID: 1234567890123456790
enter tweet: quit
Goodbye! (Server will keep running)
Commands:
- Type any message (up to 280 characters) to tweet
- Type
quitorexitto stop terminal tweeting - Server continues running for web API access
GET /login- Start OAuth2 authenticationGET /callback- OAuth2 callback (automatic redirect)GET /tweets- View tweet status or post via URL parametersGET /tweets?text=Hello%20World- Post a tweet via webGET /revoke- Revoke OAuth token
Tweet via URL:
http://127.0.0.1:3000/tweets?text=Hello%20from%20the%20web%20interface!
Check authentication status:
http://127.0.0.1:3000/tweets
The application supports OAuth2 authentication by default. You can configure different authentication methods through Cargo features:
- Default: OAuth2 enabled
- OAuth1 only: Use
cargo build --no-default-featuresfor OAuth1 only
-
"could not find CLIENT_ID" error:
- Make sure your
.envfile is in the project root - Verify the CLIENT_ID and CLIENT_SECRET are correct
- Make sure your
-
Authentication fails:
- Check that your callback URL in Twitter Developer Portal matches:
http://127.0.0.1:3000/callback - Ensure your app has proper permissions (Read and Write)
- Check that your callback URL in Twitter Developer Portal matches:
-
Tweet posting fails:
- Verify your Twitter app has "Read and Write" permissions
- Check that tweets are under 280 characters
- Ensure you're authenticated (complete the web login first)
-
Server won't start:
- Make sure port 3000 is available
- Check if another instance is already running
Enable detailed logging by setting in your .env:
RUST_LOG=tweetterminal=debug,tower_http=debugThis project was built with significant help from the twitter-v2-rs library and examples. Most of the Twitter API integration code is based on or inspired by that excellent project.
Additional resources: