diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..220d5e7 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "mastobot" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +elefren = "0.22" +ureq = { version = "2", features = ["json"] } diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..df7a48f --- /dev/null +++ b/src/main.rs @@ -0,0 +1,61 @@ +use elefren::{helpers::cli, prelude::*, entities::{prelude::Event, notification::NotificationType}, status_builder::Visibility}; +use ureq::serde_json::Value; + +fn fact() -> Result { + if let Some(v) = ureq::get("https://uselessfacts.jsph.pl/random.json?language=en") + .call()? + .into_json::()? + .get("text") { Ok(v.as_str().unwrap().to_string()) } + else { Ok("".to_string()) } +} + +fn main() { + let registration = Registration::new("https://social.alemi.dev") + .client_name("elefren_test") + .scopes(Scopes::all()) // TODO only get necessary ones + .build().unwrap(); + let mastodon = cli::authenticate(registration).unwrap(); + + for event in mastodon.streaming_user().unwrap() { + match event { + Event::Notification(ref notification) => { + match notification.notification_type { + NotificationType::Mention => { + if let Some(status) = ¬ification.status { + if status.content.contains("fact") { + let dm = if status.content.contains("private") { true } else { false }; + let f = match fact() { + Ok(t) => t, + Err(e) => format!("Error getting random fact: {:?}", e), + }; + let text = if dm { + format!("@{} {}", notification.account.acct, f) + } else { f }; + let s = StatusBuilder::new() + .status(text) + .visibility(if dm { Visibility::Direct } else { Visibility::Public }) + .in_reply_to(&status.id) + .build().unwrap(); + if let Err(e) = mastodon.new_status(s) { + println!("ERROR answering: {:?}", e); + } + } + } + }, + NotificationType::Reblog => { + println!("{} boosted!", notification.account.acct); + }, + NotificationType::Favourite => { + println!("{} favourited!", notification.account.acct); + }, + NotificationType::Follow => { + println!("{} followed!", notification.account.acct); + }, + } + }, + Event::Update(ref _status) => { }, + Event::Delete(ref _id) => { }, + Event::FiltersChanged => { }, + } + } +}