fix: title can be more than one word

This commit is contained in:
Patrick 2024-10-23 15:51:16 +02:00
parent 280acaa8b0
commit 7a4253f723
Signed by: patrick
GPG key ID: 451F95EFB8BECD0F

View file

@ -70,7 +70,7 @@ struct Message {
time: String, time: String,
} }
async fn generate_post(config: &Config, client: &mut Client) -> Result<String> { async fn generate_post(config: &Config, client: &mut Client, title: &str) -> Result<String> {
let day: DateTime<Local> = Local::now(); let day: DateTime<Local> = Local::now();
let file = PathBuf::from(format!("{}/texts.json", config.data_folder)); let file = PathBuf::from(format!("{}/texts.json", config.data_folder));
let tempdir = tempdir()?; let tempdir = tempdir()?;
@ -84,6 +84,7 @@ async fn generate_post(config: &Config, client: &mut Client) -> Result<String> {
.ok_or_eyre("Gestern gab es nicht?????????")? .ok_or_eyre("Gestern gab es nicht?????????")?
.format("%d.%m.%Y") .format("%d.%m.%Y")
.to_string(), .to_string(),
title: title.to_string(),
..Default::default() ..Default::default()
}; };
let mut msgs = Vec::new(); let mut msgs = Vec::new();
@ -209,7 +210,12 @@ async fn main() -> Result<()> {
if let Some(text) = text { if let Some(text) = text {
// ©ontrol character // ©ontrol character
let cmds: Vec<&str> = text.split_whitespace().collect(); let cmds: Vec<&str> = text.split_whitespace().collect();
if cmds.len() >= 2 && cmds[0] == "©" { if text.starts_with("©") {
if cmds.len() == 1 {
let text = "\nAvailable commands:\n - post: Generate post for yesterday\n - tz: change timezone\n"
.to_string();
send(&client, sender, text).await?;
}
match cmds[1] { match cmds[1] {
"help" => { "help" => {
let text = "\nAvailable commands:\n - post: Generate post for yesterday\n - tz: change timezone\n" let text = "\nAvailable commands:\n - post: Generate post for yesterday\n - tz: change timezone\n"
@ -217,15 +223,15 @@ async fn main() -> Result<()> {
send(&client, sender, text).await?; send(&client, sender, text).await?;
} }
"post" => { "post" => {
if cmds.len() != 3 { if cmds.len() < 3 {
send( send(&client, sender, "Missing title".to_string()).await?;
&client,
sender,
"Wrong number of arguments.\nExpected: 1(title)".to_string(),
)
.await?;
} else { } else {
let res = generate_post(&config, &mut client.borrow_mut()).await; let res = generate_post(
&config,
&mut client.borrow_mut(),
&cmds[2..].join(" "),
)
.await;
if let Ok(res) = res { if let Ok(res) = res {
send( send(
&client, &client,