Compare commits
2 commits
3092ce7f5f
...
280acaa8b0
Author | SHA1 | Date | |
---|---|---|---|
Patrick | 280acaa8b0 | ||
Patrick | 00d3f49445 |
10
Cargo.lock
generated
10
Cargo.lock
generated
|
@ -259,6 +259,15 @@ version = "0.7.2"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1462739cb27611015575c0c11df5df7601141071f07518d56fcc1be504cbec97"
|
||||
|
||||
[[package]]
|
||||
name = "clokwerk"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bd108d365fcb6d7eddf17a6718eb6a33db18ba4178f8cc6b667f480710f10d76"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "color-eyre"
|
||||
version = "0.6.3"
|
||||
|
@ -937,6 +946,7 @@ dependencies = [
|
|||
"base64",
|
||||
"chrono",
|
||||
"clap",
|
||||
"clokwerk",
|
||||
"color-eyre",
|
||||
"copy_dir",
|
||||
"futures",
|
||||
|
|
|
@ -8,6 +8,7 @@ askama = "0.12.1"
|
|||
base64 = "0.22.1"
|
||||
chrono = "0.4.38"
|
||||
clap = { version = "4.5.17", features = ["derive"] }
|
||||
clokwerk = "0.4.0"
|
||||
color-eyre = "0.6.3"
|
||||
copy_dir = "0.1.3"
|
||||
futures = "0.3.30"
|
||||
|
|
96
src/main.rs
96
src/main.rs
|
@ -1,8 +1,15 @@
|
|||
use copy_dir::copy_dir;
|
||||
use std::borrow::BorrowMut;
|
||||
use std::fs::{create_dir_all, read_to_string, remove_file, write, File, OpenOptions};
|
||||
use std::io::{BufRead, BufReader, Read, Write};
|
||||
use std::io::{BufRead, BufReader, Write};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::process::Stdio;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
mod jsonrpc;
|
||||
mod recsend;
|
||||
use crate::jsonrpc::RpcClient;
|
||||
use crate::recsend::{Receiver, Sender};
|
||||
|
||||
use askama::Template;
|
||||
use base64::prelude::BASE64_STANDARD;
|
||||
|
@ -10,19 +17,17 @@ use base64::Engine;
|
|||
use chrono::offset::FixedOffset;
|
||||
use chrono::{DateTime, Days, Local, Utc};
|
||||
use clap::{command, Parser};
|
||||
use clokwerk::{AsyncScheduler, Job, TimeUnits};
|
||||
use color_eyre::eyre::{eyre, OptionExt, Result};
|
||||
use copy_dir::copy_dir;
|
||||
use futures::future::join_all;
|
||||
use jsonrpsee::async_client::{Client, ClientBuilder};
|
||||
use serde_json::Value;
|
||||
use tempfile::tempdir;
|
||||
use tokio::process::Command;
|
||||
use tokio::sync::Mutex;
|
||||
use tokio_util::codec::{FramedRead, LinesCodec};
|
||||
|
||||
use crate::jsonrpc::RpcClient;
|
||||
mod jsonrpc;
|
||||
use crate::recsend::{Receiver, Sender};
|
||||
mod recsend;
|
||||
|
||||
const COMMAND_PATH: &str = "signal-cli";
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
|
@ -52,6 +57,7 @@ struct Config {
|
|||
#[derive(Debug, Default, Template)]
|
||||
#[template(path = "post.md")]
|
||||
struct PostTemplate {
|
||||
title: String,
|
||||
date: String,
|
||||
date_title: String,
|
||||
messages: Vec<Message>,
|
||||
|
@ -151,17 +157,43 @@ async fn main() -> Result<()> {
|
|||
inner: FramedRead::new(cmd.stdout.take().unwrap(), LinesCodec::new()),
|
||||
};
|
||||
|
||||
let mut client = ClientBuilder::default().build_with_tokio(sender, receiver);
|
||||
let mut stream = client.subscribe_receive(None).await?;
|
||||
let client = Arc::new(Mutex::new(
|
||||
ClientBuilder::default().build_with_tokio(sender, receiver),
|
||||
));
|
||||
|
||||
let mut scheduler = AsyncScheduler::new();
|
||||
let c = client.clone();
|
||||
let sender = config.allowed_sender.clone();
|
||||
scheduler.every(1_u32.day()).at("9:00").run(move || {
|
||||
let c = c.clone();
|
||||
let sender = sender.clone();
|
||||
async move {
|
||||
let c = c.lock().await;
|
||||
let _ = send(
|
||||
&c,
|
||||
&sender,
|
||||
"This is the signal-to-blog bot reminding you to post today.".to_string(),
|
||||
)
|
||||
.await;
|
||||
}
|
||||
});
|
||||
tokio::spawn(async move {
|
||||
loop {
|
||||
scheduler.run_pending().await;
|
||||
tokio::time::sleep(Duration::from_secs(1)).await;
|
||||
}
|
||||
});
|
||||
let mut stream = client.lock().await.subscribe_receive(None).await?;
|
||||
loop {
|
||||
let v = stream.next().await.unwrap()?;
|
||||
let mut client = client.lock().await;
|
||||
// which message doesn't have a sender?
|
||||
let sender = get_msg_sender(&v).expect("which message doesn't have a sender?");
|
||||
let text = get_msg_text(&v);
|
||||
let at = get_msg_attachments(&mut client, &v).await;
|
||||
if text.is_none() && at.is_none() {
|
||||
continue;
|
||||
} else if sender != &config.allowed_sender {
|
||||
} else if sender != config.allowed_sender {
|
||||
println!("{:?}", sender);
|
||||
send(
|
||||
&client,
|
||||
|
@ -180,30 +212,39 @@ async fn main() -> Result<()> {
|
|||
if cmds.len() >= 2 && cmds[0] == "©" {
|
||||
match cmds[1] {
|
||||
"help" => {
|
||||
let text = "
|
||||
Available commands:
|
||||
- post: Generate post for yesterday
|
||||
- tz: change timezone
|
||||
"
|
||||
let text = "\nAvailable commands:\n - post: Generate post for yesterday\n - tz: change timezone\n"
|
||||
.to_string();
|
||||
send(&client, sender, text).await?;
|
||||
}
|
||||
"post" => {
|
||||
let res = generate_post(&config, &mut client).await;
|
||||
if let Ok(res) = res {
|
||||
if cmds.len() != 3 {
|
||||
send(
|
||||
&client,
|
||||
sender,
|
||||
format!("Generated blog post at: {}/{}", config.url, res),
|
||||
"Wrong number of arguments.\nExpected: 1(title)".to_string(),
|
||||
)
|
||||
.await?;
|
||||
} else {
|
||||
send(
|
||||
&client,
|
||||
sender,
|
||||
format!("Error generating blog post:\n{:?}", res),
|
||||
)
|
||||
.await?;
|
||||
let res = generate_post(&config, &mut client.borrow_mut()).await;
|
||||
if let Ok(res) = res {
|
||||
send(
|
||||
&client,
|
||||
sender,
|
||||
format!(
|
||||
"Generated blog post at: {}/{}",
|
||||
config.url,
|
||||
res.replace("[+:.]", "-").to_lowercase()
|
||||
),
|
||||
)
|
||||
.await?;
|
||||
} else {
|
||||
send(
|
||||
&client,
|
||||
sender,
|
||||
format!("Error generating blog post:\n{:?}", res),
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
}
|
||||
"tz" => {
|
||||
|
@ -214,14 +255,15 @@ Available commands:
|
|||
println!("{}", tz_data_path);
|
||||
let mut file = OpenOptions::new()
|
||||
.create(true)
|
||||
.truncate(true)
|
||||
.write(true)
|
||||
.open(&tz_data_path)?;
|
||||
file.write_all(cmds[2].as_bytes())?;
|
||||
config.timezone = cmds[2].to_string();
|
||||
|
||||
send(&client, sender, format!("Updated tz data")).await?;
|
||||
send(&client, sender, "Updated tz data".to_string()).await?;
|
||||
} else {
|
||||
send(&client, sender, format!("Error parsing")).await?;
|
||||
send(&client, sender, "Error parsing".to_string()).await?;
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
|
@ -235,7 +277,7 @@ Available commands:
|
|||
.append(true)
|
||||
.create(true)
|
||||
.open(format!("{}/texts.json", config.data_folder))?;
|
||||
writeln!(file, "{}", v.to_string())?;
|
||||
writeln!(file, "{}", v)?;
|
||||
}
|
||||
}
|
||||
async fn send(client: &Client, sender: &str, msg: String) -> Result<()> {
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
+++
|
||||
title = "Journal for {{ date_title }}"
|
||||
title = "{{title}} ({{ date_title }})"
|
||||
date = {{ date }}
|
||||
draft = false
|
||||
|
||||
[taxonomies]
|
||||
tags = ["Signal to blog"]
|
||||
tags = ["Signal to blog" "Journal"]
|
||||
+++
|
||||
|
||||
{% for msg in messages %}
|
||||
|
|
Loading…
Reference in a new issue