5 releases
0.1.75 | Jun 23, 2023 |
---|---|
0.1.73 | Jun 6, 2023 |
0.1.7 | Jun 6, 2023 |
0.1.6 | Jun 6, 2023 |
0.1.5 | Jun 6, 2023 |
#2331 in Web programming
46 downloads per month
25KB
587 lines
Critter
Simple rust library to interact with the twitter V2 api.
Getting Started
Before proceeding, ensure you have your Twitter Developer App credentials handy - Consumer Key, Consumer Secret, Access Token, and Access Token Secret.
Installation
Simply run cargo add critter
or
Include the following in your Cargo.toml
:
[dependencies]
critter = "0.1.75"
Basic Examples
Creating a Client - OAuth 1.0a User Context (With Provided OAuth Tokens)
use critter::{ TwitterClient, auth::TwitterAuth };
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let auth = TwitterAuth::from_oa1uc(
&env::var("CONSUMER_KEY").unwrap(),
&env::var("CONSUMER_SECRET").unwrap(),
&env::var("ACCESS_TOKEN").unwrap(),
&env::var("ACCESS_TOKEN_SECRET").unwrap()
);
let mut twitter = TwitterClient::new(auth)?;
Ok(())
}
Getting the Details of The Authenticated User
match twitter.me(None).await {
Ok(data) => println!("My name is {}", data.name()),
Err(e) => println!("Error: {}", e) // Can be something like ratelimit
}
An example of obtaining additional details such as description
and created_at
is provided here.
Posting a simple Tweet
match twitter.tweet(|tweet|
tweet.text("Hello from Rust!") // The tweet's text
).await {
Ok(data) => println!("Tweet id: {:?}", data.id()),
Err(e) => println!("Error: {}", e)
}
Uploading Media
// Upload the media
let pic = match twitter.upload_media("/path/to/file.jpg", Some("pic.jpg".into())).await {
Ok(pic) => Some(pic),
Err(e) => {
eprintln!("Error uploading media: {}", e);
None
},
};
// Make a tweet with said media attached
match twitter.tweet(|tweet|
tweet.text("This is a file.") // The tweet's text
.media(|m| { // You are able to add multiple medias
m.add(pic) // Add the media we uploaded
})
).await {
Ok(data) => println!("Tweet id: {:?}", data.id()),
Err(e) => println!("Error: {}", e)
}
Dependencies
~9–23MB
~338K SLoC