some matrix improvements with added html messages
This commit is contained in:
parent
5aa6eb8179
commit
57f211036e
|
@ -1,10 +1,7 @@
|
|||
use std::cell::OnceCell;
|
||||
use std::error::Error;
|
||||
use std::io::Read;
|
||||
|
||||
use clap::builder::Str;
|
||||
use colored::Colorize;
|
||||
|
||||
use nom::Err;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
@ -104,6 +101,7 @@ impl MatrixClient {
|
|||
) -> Result<Value, Box<dyn Error>> {
|
||||
let client = reqwest::blocking::Client::new();
|
||||
let url = format!("{}/_matrix/client{}", self.homeserver_url, endpoint);
|
||||
print!("url: {}", url.yellow());
|
||||
|
||||
// Construct URL with query parameters
|
||||
let mut request = client.request(method, &url);
|
||||
|
@ -144,17 +142,17 @@ impl MatrixClient {
|
|||
self.request(reqwest::Method::POST, endpoint, query_data, json_data, unauth)
|
||||
}
|
||||
|
||||
fn get(
|
||||
&self, endpoint: &str, query_data: Option<&HashMap<String, String>>, unauth: bool,
|
||||
) -> Result<Value, Box<dyn Error>> {
|
||||
self.request::<HashMap<String, String>>(
|
||||
reqwest::Method::GET,
|
||||
endpoint,
|
||||
query_data,
|
||||
None,
|
||||
unauth,
|
||||
)
|
||||
}
|
||||
// fn get(
|
||||
// &self, endpoint: &str, query_data: Option<&HashMap<String, String>>, unauth: bool,
|
||||
// ) -> Result<Value, Box<dyn Error>> {
|
||||
// self.request::<HashMap<String, String>>(
|
||||
// reqwest::Method::GET,
|
||||
// endpoint,
|
||||
// query_data,
|
||||
// None,
|
||||
// unauth,
|
||||
// )
|
||||
// }
|
||||
|
||||
pub fn login(&mut self, username: &str, password: &str) -> Result<(), Box<dyn Error>> {
|
||||
let login_request = LoginRequest {
|
||||
|
@ -176,10 +174,44 @@ impl MatrixClient {
|
|||
current
|
||||
}
|
||||
|
||||
pub fn pandoc_convert_md_to_html(markdown: String) -> Result<String, Box<dyn Error>> {
|
||||
let (output, errors, status) = crate::pipe(
|
||||
"pandoc",
|
||||
&mut ["--from", "markdown-auto_identifiers", "--to", "html5"],
|
||||
markdown,
|
||||
)?;
|
||||
if status.success() {
|
||||
println!("Resultat von Pandoc: {}", output);
|
||||
Ok(output)
|
||||
} else {
|
||||
Err(format!("Pandoc error, exit code {:?}\n{}", status, errors).into())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn pandoc_convert_text_to_md(markdown: String) -> Result<String, Box<dyn Error>> {
|
||||
let (output, errors, status) = crate::pipe(
|
||||
"pandoc",
|
||||
&mut ["--from", "markdown-auto_identifiers", "--to", "html5"],
|
||||
markdown,
|
||||
)?;
|
||||
if status.success() {
|
||||
println!("Resultat von Pandoc: {}", output);
|
||||
Ok(output)
|
||||
} else {
|
||||
Err(format!("Pandoc error, exit code {:?}\n{}", status, errors).into())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn send_room_message(
|
||||
&mut self, room_id: &str, text: &str,
|
||||
) -> Result<Value, Box<dyn Error>> {
|
||||
let content = HashMap::from([("msgtype", "m.text"), ("body", text)]);
|
||||
let formatted_text = Self::pandoc_convert_md_to_html(text.to_string())?;
|
||||
let content = HashMap::from([
|
||||
("msgtype", "m.text"),
|
||||
("body", text),
|
||||
("format", "org.matrix.custom.html"),
|
||||
("formatted_body", &formatted_text),
|
||||
]);
|
||||
self.send_room_event(&room_id, "m.room.message", &content)
|
||||
}
|
||||
|
||||
|
@ -187,13 +219,15 @@ impl MatrixClient {
|
|||
&mut self, room: &str, event_type: &str, content: &impl Serialize,
|
||||
) -> Result<Value, Box<dyn Error>> {
|
||||
let endpoint = format!("/r0/rooms/{}/send/{}/{}", room, event_type, self.txn_id());
|
||||
println!("room event:{}", &endpoint.red());
|
||||
self.put(&endpoint, None, Some(content), false)
|
||||
}
|
||||
pub fn send_short_and_long_messages_to_two_rooms(
|
||||
&mut self, short_message: &str, long_message: &str,
|
||||
) -> Result<(), Box<dyn Error>> {
|
||||
self.send_room_message(&long_message, &self.room_id_for_long_messages.clone())?;
|
||||
self.send_room_message(&short_message, &self.room_id_for_short_messages.clone())?;
|
||||
self.send_room_message( &self.room_id_for_long_messages.clone(), &long_message,)?;
|
||||
self.send_room_message( &self.room_id_for_short_messages.clone(), &short_message,)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue