feat: super simple but working implementation

This commit is contained in:
əlemi 2022-11-16 03:15:06 +01:00
parent 82bb5145f8
commit f6d10270c9
No known key found for this signature in database
GPG key ID: BBCBFE5D7244634E
2 changed files with 71 additions and 0 deletions

10
Cargo.toml Normal file
View file

@ -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"] }

61
src/main.rs Normal file
View file

@ -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<String, ureq::Error> {
if let Some(v) = ureq::get("https://uselessfacts.jsph.pl/random.json?language=en")
.call()?
.into_json::<Value>()?
.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) = &notification.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 => { },
}
}
}