1
0

mv bot.rs

This commit is contained in:
syui 2024-02-11 13:22:06 +09:00
parent 297a214524
commit f20bbfd7de
Signed by: syui
GPG Key ID: 5417CFEBAD92DF56
4 changed files with 202 additions and 90 deletions

77
src/bot.rs Normal file
View File

@ -0,0 +1,77 @@
use seahorse::Context;
use crate::refresh;
use crate::reply_link;
use crate::notify;
use crate::notify_read;
use crate::data::log_file;
use crate::data::w_cid;
use crate::data::c_char;
use crate::data::Notify as Notify;
pub fn c_bot(c: &Context) {
let h = async {
let mut notify = notify::get_request(100).await;
if notify == "err" {
refresh(c);
notify = notify::get_request(100).await;
}
let notify: Notify = serde_json::from_str(&notify).unwrap();
let n = notify.notifications;
let length = &n.len();
let su = 0..*length;
for i in su {
let reason = &n[i].reason;
let handle = &n[i].author.handle;
let did = &n[i].author.did;
let read = n[i].isRead;
let cid = &n[i].cid;
let uri = &n[i].uri;
let time = &n[i].indexedAt;
let mut cid_root = cid;
let mut uri_root = uri;
let check_cid = w_cid(cid.to_string(), log_file(&"n1"), false);
let check_cid_run = w_cid(cid.to_string(), log_file(&"n2"), false);
// thread
if ! n[i].record.reply.is_none() {
cid_root = &n[i].record.reply.as_ref().unwrap().root.cid;
uri_root = &n[i].record.reply.as_ref().unwrap().root.uri;
}
println!("{}", read);
println!("{}", handle);
println!("{} {}", cid, uri);
let mut text = "";
if ! n[i].record.text.is_none() {
text = &n[i].record.text.as_ref().unwrap();
}
let vec: Vec<&str> = text.split_whitespace().collect();
let rep_com = &vec[0..].join(" ");
if check_cid == false && { reason == "mention" || reason == "reply" } || check_cid_run == false && { reason == "mention" || reason == "reply" } {
w_cid(cid.to_string(), log_file(&"n2"), true);
if rep_com.contains("did") == true || rep_com.contains("/did") == true {
let link = "https://plc.directory/".to_owned() + &did + &"/log";
let s = 0;
let e = link.chars().count();
let d = "\n".to_owned() + &did.to_string();
let text_limit = c_char(d);
if text_limit.len() > 3 {
let str_rep = reply_link::post_request(text_limit.to_string(), link.to_string(), s, e.try_into().unwrap(), cid.to_string(), uri.to_string(), cid_root.to_string(), uri_root.to_string()).await;
let str_notify = notify_read::post_request(time.to_string()).await;
w_cid(cid.to_string(), log_file(&"n1"), true);
println!("{}", str_rep);
println!("{}", str_notify);
}
}
}
}
};
let res = tokio::runtime::Runtime::new().unwrap().block_on(h);
return res
}

View File

@ -4,12 +4,10 @@ use std::env;
use crate::ascii::c_ascii;
use crate::data::data_toml;
use crate::data::log_file;
use crate::data::url;
use crate::data::w_cfg;
use crate::data::w_cid;
use crate::data::c_char;
use data::Notify as Notify;
use crate::bot::c_bot;
//use data::Notify as Notify;
pub mod ascii;
pub mod data;
@ -22,6 +20,9 @@ pub mod reply;
pub mod reply_link;
pub mod describe;
pub mod timeline_author;
pub mod post;
pub mod post_link;
pub mod bot;
fn main() {
let args: Vec<String> = env::args().collect();
@ -72,6 +73,16 @@ fn main() {
.description("did <handle>")
.action(did)
)
.command(
Command::new("post")
.description("p <text>")
.alias("p")
.action(post)
.flag(
Flag::new("link", FlagType::String)
.alias("l"),
)
)
//.command(
// Command::new("like")
// .description("$ ai like <cid>\n\t\t\t$ ai like <cid> -u <uri>")
@ -120,13 +131,6 @@ fn main() {
// )
// )
// .command(
// Command::new("handle")
// .usage("atr h")
// .description("handle update\n\t\t\t$ atr -h example.com\n\t\t\t$ atr -h user.bsky.social")
// .alias("h")
// .action(c_handle)
// )
// .command(
// Command::new("feed")
// .usage("atr f")
// .description("feed user\n\t\t\t$ atr f\n\t\t\t$ atr f -u user.bsky.social")
@ -138,25 +142,7 @@ fn main() {
// .alias("u"),
// )
// )
// .command(
// Command::new("post")
// .description("$ ai p <text>\n\t\t\t$ ai p <text> -l https://syui.ai")
// .alias("p")
// .action(c_post)
// .flag(
// Flag::new("link", FlagType::String)
// .description("link flag(ex: $ atr p -l)")
// .alias("l"),
// )
// .flag(
// Flag::new("cid", FlagType::String)
// .description("link flag(ex: $ atr p -l)")
// )
// .flag(
// Flag::new("uri", FlagType::String)
// .description("link flag(ex: $ atr p -l)")
// )
// )
// .command(
// Command::new("reply")
// .usage("atr r {}")
@ -281,73 +267,24 @@ fn timeline(c: &Context) {
return res
}
fn c_bot(c: &Context) {
fn post(c: &Context) {
let m = c.args[0].to_string();
let h = async {
let mut notify = notify::get_request(100).await;
if notify == "err" {
refresh(c);
notify = notify::get_request(100).await;
}
let notify: Notify = serde_json::from_str(&notify).unwrap();
let n = notify.notifications;
let length = &n.len();
let su = 0..*length;
for i in su {
let reason = &n[i].reason;
let handle = &n[i].author.handle;
let did = &n[i].author.did;
let read = n[i].isRead;
let cid = &n[i].cid;
let uri = &n[i].uri;
let time = &n[i].indexedAt;
let mut cid_root = cid;
let mut uri_root = uri;
let check_cid = w_cid(cid.to_string(), log_file(&"n1"), false);
let check_cid_run = w_cid(cid.to_string(), log_file(&"n2"), false);
// thread
if ! n[i].record.reply.is_none() {
cid_root = &n[i].record.reply.as_ref().unwrap().root.cid;
uri_root = &n[i].record.reply.as_ref().unwrap().root.uri;
}
println!("{}", read);
println!("{}", handle);
println!("{} {}", cid, uri);
let mut text = "";
if ! n[i].record.text.is_none() {
text = &n[i].record.text.as_ref().unwrap();
}
let vec: Vec<&str> = text.split_whitespace().collect();
let rep_com = &vec[0..].join(" ");
if check_cid == false && { reason == "mention" || reason == "reply" } || check_cid_run == false && { reason == "mention" || reason == "reply" } {
w_cid(cid.to_string(), log_file(&"n2"), true);
if rep_com.contains("did") == true || rep_com.contains("/did") == true {
let link = "https://plc.directory/".to_owned() + &did + &"/log";
let s = 0;
if let Ok(link) = c.string_flag("link") {
let e = link.chars().count();
let d = "\n".to_owned() + &did.to_string();
let text_limit = c_char(d);
if text_limit.len() > 3 {
let str_rep = reply_link::post_request(text_limit.to_string(), link.to_string(), s, e.try_into().unwrap(), cid.to_string(), uri.to_string(), cid_root.to_string(), uri_root.to_string()).await;
let str_notify = notify_read::post_request(time.to_string()).await;
w_cid(cid.to_string(), log_file(&"n1"), true);
println!("{}", str_rep);
println!("{}", str_notify);
}
}
}
let s = 0;
let str = post_link::post_request(m.to_string(), link.to_string(), s, e.try_into().unwrap());
println!("{}",str.await);
} else {
let str = post::post_request(m.to_string());
println!("{}",str.await);
}
};
let res = tokio::runtime::Runtime::new().unwrap().block_on(h);
return res
}
fn bot(c: &Context) {
loop {
c_bot(c);

42
src/post.rs Normal file
View File

@ -0,0 +1,42 @@
extern crate reqwest;
use crate::data_toml;
use crate::url;
use serde_json::json;
use iso8601_timestamp::Timestamp;
pub async fn post_request(text: String) -> String {
let token = data_toml(&"access");
let did = data_toml(&"did");
let handle = data_toml(&"handle");
let url = url(&"record_create");
let col = "app.bsky.feed.post".to_string();
let d = Timestamp::now_utc();
let d = d.to_string();
let post = Some(json!({
"repo": handle.to_string(),
"did": did.to_string(),
"collection": col.to_string(),
"record": {
"text": text.to_string(),
"createdAt": d.to_string(),
},
}));
let client = reqwest::Client::new();
let res = client
.post(url)
.json(&post)
.header("Authorization", "Bearer ".to_owned() + &token)
.send()
.await
.unwrap()
.text()
.await
.unwrap();
return res
}

56
src/post_link.rs Normal file
View File

@ -0,0 +1,56 @@
extern crate reqwest;
use crate::data_toml;
use crate::url;
use serde_json::json;
use iso8601_timestamp::Timestamp;
pub async fn post_request(text: String, link: String, s: i32, e: i32) -> String {
let token = data_toml(&"access");
let did = data_toml(&"did");
let handle = data_toml(&"handle");
let url = url(&"record_create");
let col = "app.bsky.feed.post".to_string();
let d = Timestamp::now_utc();
let d = d.to_string();
let post = Some(json!({
"repo": handle.to_string(),
"did": did.to_string(),
"collection": col.to_string(),
"record": {
"text": link.to_string() + &" ".to_string() + &text.to_string(),
"createdAt": d.to_string(),
"facets": [
{
"index": {
"byteStart": s,
"byteEnd": e
},
"features": [
{
"$type": "app.bsky.richtext.facet#link",
"uri": link.to_string()
}
]
}
],
},
}));
let client = reqwest::Client::new();
let res = client
.post(url)
.json(&post)
.header("Authorization", "Bearer ".to_owned() + &token)
.send()
.await
.unwrap()
.text()
.await
.unwrap();
return res
}