critter

Crates.iocritter
lib.rscritter
version0.1.75
sourcesrc
created_at2023-06-06 01:51:37.973167
updated_at2023-06-23 15:31:56.018717
descriptionA light rust library to interact with the Twitter V2 api.
homepagehttps://github.com/Mlemix/critter
repositoryhttps://github.com/Mlemix/critter
max_upload_size
id883463
size63,775
(Mlemix)

documentation

https://github.com/Mlemix/critter/blob/main/README.md

README

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)
}
Commit count: 14

cargo fmt