From 3be6c33d4ab1767227a9cc79b654be7d7ea11a7f Mon Sep 17 00:00:00 2001 From: Yuki Kishimoto Date: Thu, 14 Dec 2023 10:19:46 +0100 Subject: [PATCH] Add `markdown` support Closes #3 --- README.md | 15 +++++++++++---- examples/blocking.rs | 9 +++++---- examples/client.rs | 9 +++++---- src/dispatcher/mod.rs | 12 ++++++++++-- src/payload/mod.rs | 21 ++++++++++++++++++--- 5 files changed, 49 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 8d904e4..92ecb4e 100644 --- a/README.md +++ b/README.md @@ -37,14 +37,15 @@ async fn main() -> Result<(), NtfyError> { ); let payload = Payload::new("mytopic") - .message("Hello, World!") // Add optional message + .message("Hello, **World**!") // Add optional message .title("Alert") // Add optiona title .tags(vec!["warning".into()]) // Add optional tags .priority(Priority::High) // Edit priority - .actions(vec![action]) // Add optional actions + .actions([action]) // Add optional actions .click(Url::parse("https://example.com")?) // Add optional clickable url .attach(Url::parse("https://example.com/file.jpg")?) // Add optional url attachment - .delay(Local::now() + Duration::minutes(1)); // Add optional delay + .delay(Local::now() + Duration::minutes(1)) // Add optional delay + .markdown(true); // Use markdown dispatcher.send(&payload).await.unwrap(); @@ -64,4 +65,10 @@ The following crate feature flags are available: ## License -This project is distributed under the MIT software license - see the [LICENSE](LICENSE) file for details \ No newline at end of file +This project is distributed under the MIT software license - see the [LICENSE](LICENSE) file for details + +## Donations + +⚡ Tips: + +⚡ Lightning Address: yuki@getalby.com \ No newline at end of file diff --git a/examples/blocking.rs b/examples/blocking.rs index c23786f..bd64b95 100644 --- a/examples/blocking.rs +++ b/examples/blocking.rs @@ -19,14 +19,15 @@ fn main() -> Result<(), NtfyError> { ); let payload = Payload::new("mytopic") - .message("Hello, World!") // Add optional message + .message("Hello, **World**!") // Add optional message .title("Alert") // Add optiona title - .tags(vec!["warning".into()]) // Add optional tags + .tags(["warning"]) // Add optional tags .priority(Priority::High) // Edit priority - .actions(vec![action]) // Add optional actions + .actions([action]) // Add optional actions .click(Url::parse("https://example.com")?) // Add optional clickable url .attach(Url::parse("https://example.com/file.jpg")?) // Add optional url attachment - .delay(Local::now() + Duration::minutes(1)); // Add optional delay + .delay(Local::now() + Duration::minutes(1)) // Add optional delay + .markdown(true); // Use markdown dispatcher.send(&payload).unwrap(); diff --git a/examples/client.rs b/examples/client.rs index a8d32ed..093791c 100644 --- a/examples/client.rs +++ b/examples/client.rs @@ -20,14 +20,15 @@ async fn main() -> Result<(), NtfyError> { ); let payload = Payload::new("mytopic") - .message("Hello, World!") // Add optional message + .message("Hello, **World**!") // Add optional message .title("Alert") // Add optiona title - .tags(vec!["warning".into()]) // Add optional tags + .tags(["warning"]) // Add optional tags .priority(Priority::High) // Edit priority - .actions(vec![action]) // Add optional actions + .actions([action]) // Add optional actions .click(Url::parse("https://example.com")?) // Add optional clickable url .attach(Url::parse("https://example.com/file.jpg")?) // Add optional url attachment - .delay(Local::now() + Duration::minutes(1)); // Add optional delay + .delay(Local::now() + Duration::minutes(1)) // Add optional delay + .markdown(true); // Use markdown dispatcher.send(&payload).await.unwrap(); diff --git a/src/dispatcher/mod.rs b/src/dispatcher/mod.rs index ba69456..d2e4eae 100644 --- a/src/dispatcher/mod.rs +++ b/src/dispatcher/mod.rs @@ -52,13 +52,21 @@ impl Dispatcher { #[cfg(not(feature = "blocking"))] pub async fn send(&self, payload: &Payload) -> Result<(), NtfyError> { log::debug!("{:?}", payload); - request(self.client.post(self.url.as_str()).json(payload)).await + let mut builder = self.client.post(self.url.as_str()); + if payload.markdown { + builder = builder.header("Markdown", "yes"); + } + request(builder.json(payload)).await } /// Send payload to ntfy server #[cfg(feature = "blocking")] pub fn send(&self, payload: &Payload) -> Result<(), NtfyError> { log::debug!("{:?}", payload); - request(self.client.post(self.url.as_str()).json(payload)) + let mut builder = self.client.post(self.url.as_str()); + if payload.markdown { + builder = builder.header("Markdown", "yes"); + } + request(builder.json(payload)) } } diff --git a/src/payload/mod.rs b/src/payload/mod.rs index b4c85a8..b58ef15 100644 --- a/src/payload/mod.rs +++ b/src/payload/mod.rs @@ -24,6 +24,8 @@ pub struct Payload { pub filename: Option, pub delay: Option, pub email: Option, + #[serde(skip)] + pub markdown: bool, } impl Payload { @@ -44,6 +46,7 @@ impl Payload { filename: None, delay: None, email: None, + markdown: false, } } @@ -70,8 +73,9 @@ impl Payload { } /// Set tags - pub fn tags(self, tags: Vec) -> Self + pub fn tags(self, tags: I) -> Self where + I: IntoIterator, S: Into, { Self { @@ -86,9 +90,12 @@ impl Payload { } /// Set actions - pub fn actions(self, actions: Vec) -> Self { + pub fn actions(self, actions: I) -> Self + where + I: IntoIterator, + { Self { - actions: Some(actions), + actions: Some(actions.into_iter().collect()), ..self } } @@ -138,4 +145,12 @@ impl Payload { ..self } } + + /// Use markdown + /// + /// + pub fn markdown(mut self, markdown: bool) -> Self { + self.markdown = markdown; + self + } }