Skip to content

Commit

Permalink
Merge #4: Add markdown support
Browse files Browse the repository at this point in the history
  • Loading branch information
yukibtc committed Dec 14, 2023
2 parents 1e2433b + 3be6c33 commit 916cf29
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 17 deletions.
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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
This project is distributed under the MIT software license - see the [LICENSE](LICENSE) file for details

## Donations

⚡ Tips: <https://getalby.com/p/yuki>

⚡ Lightning Address: [email protected]
9 changes: 5 additions & 4 deletions examples/blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
9 changes: 5 additions & 4 deletions examples/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
12 changes: 10 additions & 2 deletions src/dispatcher/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
21 changes: 18 additions & 3 deletions src/payload/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ pub struct Payload {
pub filename: Option<String>,
pub delay: Option<String>,
pub email: Option<String>,
#[serde(skip)]
pub markdown: bool,
}

impl Payload {
Expand All @@ -44,6 +46,7 @@ impl Payload {
filename: None,
delay: None,
email: None,
markdown: false,
}
}

Expand All @@ -70,8 +73,9 @@ impl Payload {
}

/// Set tags
pub fn tags<S>(self, tags: Vec<S>) -> Self
pub fn tags<I, S>(self, tags: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
Self {
Expand All @@ -86,9 +90,12 @@ impl Payload {
}

/// Set actions
pub fn actions(self, actions: Vec<Action>) -> Self {
pub fn actions<I>(self, actions: I) -> Self
where
I: IntoIterator<Item = Action>,
{
Self {
actions: Some(actions),
actions: Some(actions.into_iter().collect()),
..self
}
}
Expand Down Expand Up @@ -138,4 +145,12 @@ impl Payload {
..self
}
}

/// Use markdown
///
/// <https://docs.ntfy.sh/publish/#markdown-formatting>
pub fn markdown(mut self, markdown: bool) -> Self {
self.markdown = markdown;
self
}
}

0 comments on commit 916cf29

Please sign in to comment.