Crates.io | tweety-rs |
lib.rs | tweety-rs |
version | 0.1.4 |
source | src |
created_at | 2024-08-29 11:14:50.113464 |
updated_at | 2024-09-16 12:37:25.427009 |
description | A Rust crate for interacting with the Twitter API. |
homepage | https://github.com/dxphilo/tweety-rs |
repository | https://github.com/dxphilo/tweety-rs |
max_upload_size | |
id | 1355979 |
size | 731,110 |
tweety-rs is a Rust crate for interacting with the Twitter API. It provides a convenient interface for performing actions such as posting tweets, managing followers, sending direct messages, and more.
Run the command:
cargo add tweety-rs
Then, in your main.rs
or lib.rs
:
use crate tweety_rs;
To authenticate with the Twitter API, you will need the following credentials:
You can obtain these from the Twitter Developer portal.
To get started, you'll need to create a TweetyClient with your Twitter API credentials.
use tweety_rs::client::TweetyClient;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = TweetyClient::new(
"your_consumer_key",
"your_consumer_key_secret",
"your_access_token",
"your_access_token_secret",
);
// Post a tweet
client.post_tweet("Hello, Twitter!", None).unwrap();
Ok(())
}
Make a tweet with an image appended to your tweet.
use tweety_rs::{
types::tweet::{Media, PostTweetParams},
TweetyClient,
};
use tokio;
#[tokio::main]
fn main() {
let client = TweetyClient::new(
"your_consumer_key",
"your_consumer_key_secret",
"your_access_token",
"your_access_token_secret",
);
let path = Path::new(&file_path); // path of the image to be uploaded
match client.upload_file(&path).await {
Ok(value) => {
let media_string = value.to_string();
let message = format!("#{}", self.file_content.1);
let params = PostTweetParams {
direct_message_deep_link: None,
for_super_followers_only: None,
geo: None,
media: Some(Media {
media_ids: vec![media_string].into(),
tagged_user_ids: None,
}),
poll: None,
quote_tweet_id: None,
reply: None,
reply_settings: None,
};
match client.post_tweet(&message, Some(params)).await {
Ok(status_code) => {
println!("Posted tweet: {:?}", status_code);
}
Err(err) => {
println!("Error posting tweet: {}", err);
}
}
}
Err(err) => {
println!("Error uploading images{}", err);
}
}
}
use tweety_rs::client::TweetyClient;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = TweetyClient::new(
"your_consumer_key",
"your_consumer_key_secret",
"your_access_token",
"your_access_token_secret",
);
// Retweet a tweet by ID
client.retweet("1234567890")?;
Ok(())
}
Here’s an example of how to use the get_direct_messages
function:
use tweety_rs::{client::TweetyClient,error::TweetyError,direct_messages::QueryParams};
use serde_json::Value;
#[tokio::main]
async fn main() -> Result<(), TweetyError> {
// Create an instance of TweetyClient
let client = TweetyClient::new(
"your_consumer_key",
"your_consumer_key_secret",
"your_access_token",
"your_access_token_secret",
);
// Define query parameters
let params = QueryParams {
dm_event_fields: Some(vec![DMEventField::Id, DMEventField::Text]),
event_types: Some(vec![EventType::MessageCreate]),
expansions: Some(vec![Expansion::SenderId]),
max_results: Some(50),
media_fields: Some(vec![MediaField::Url, MediaField::Type]),
tweet_fields: Some(vec![TweetField::CreatedAt, TweetField::Text]),
user_fields: Some(vec![UserField::Username, UserField::Verified]),
};
// Fetch direct messages
match client.get_direct_messages(params).await {
Ok(response) => {
println!("Direct messages: {}", response);
}
Err(e) => {
eprintln!("Error fetching direct messages: {:?}", e);
}
}
Ok(())
}
Twitter has a small window cap for the free tier, so it's important to be aware of the rate limits.
Check out the rate limits Documentation
The crate is organized into several modules, each responsible for different aspects of the Twitter API:
Authentication Issues: When authenticating requests to the Twitter API v2 endpoints, you must use keys and tokens from a Twitter developer App that is attached to a Project. You can create a project via the Twitter Developer Portal.
OAuth1 Permissions: Your client app might not be configured with the appropriate OAuth1 app permissions for this endpoint.
Endpoint Access: Some errors may indicate that you need to upgrade to the premium tier to access certain endpoints.
Contributions are welcome! Please feel free to submit a pull request or open an issue on GitHub.
This project is licensed under the MIT License. See the LICENSE file for details.
This project is inspired by the desire to make interacting with Twitter's API easier and more Rust-idiomatic.
If you found it helpful consider giving it a star ⭐️.