Skip to main content

pushover_rs/pushover/data/
attachment_message.rs

1use reqwest::multipart::{Form, Part};
2use serde::Serialize;
3use std::io;
4
5#[derive(Debug, Clone, Serialize)]
6/**
7 A message containing an attachment, to be used in conjunction with the send_pushover_request_with_attachment function.
8
9 Note: It is preferred to create a Message through the AttachmentMessageBuilder.
10 **/
11pub struct AttachmentMessage {
12    /* Required */
13    /// (Required) Your app API token, see https://pushover.net/apps/[your application ID]
14    pub app_token: String,
15    /// (Required) Your User key, see your dashboard (https://pushover.net/ top-right)
16    pub user_key: String,
17    /// (Required) Your message
18    pub message: String,
19    /// (Required) An attachment file path to send with the message. (Max size: 2,5MB)
20    pub attachment: String,
21
22    /* Optional */
23    /// The title of the message, otherwise your app's name will be used
24    #[serde(skip_serializing_if = "Option::is_none")]
25    pub title: Option<String>,
26    /// A supplementary URL to show with your message
27    #[serde(skip_serializing_if = "Option::is_none")]
28    pub url: Option<String>,
29    /// A title for your supplementary URL, otherwise just the URL is shown
30    #[serde(skip_serializing_if = "Option::is_none")]
31    pub url_title: Option<String>,
32    /// Send as -2 to generate no notification/alert, -1 to always send as a quiet notification, 1 to display as high-priority and bypass the user's quiet hours, or 2 to also require confirmation from the user
33    #[serde(skip_serializing_if = "Option::is_none")]
34    pub priority: Option<String>,
35    /// When the priority is set to 2, sets the amount of seconds between each retries. Must be at least 30 seconds.
36    #[serde(skip_serializing_if = "Option::is_none")]
37    pub retry: Option<String>, // Required if priority is set to 2
38    /// When the priority is set to 2, sets the amount of seconds before the notification is expired. The maximum value is 10800 (3 hours).be between 60 and 10800.
39    #[serde(skip_serializing_if = "Option::is_none")]
40    pub expire: Option<String>, // Required if priority is set to 2
41    /// The name of one of the sounds supported by device clients to override the user's default sound choice. (See sound list: https://pushover.net/api#sounds)
42    #[serde(skip_serializing_if = "Option::is_none")]
43    pub sound: Option<String>,
44    /// A Unix timestamp of your message's date and time to display to the user, rather than the time your message is received by our API
45    #[serde(skip_serializing_if = "Option::is_none")]
46    pub timestamp: Option<String>, // Year 2038 proof :p
47    /// A device name to send the push notifications to.
48    #[serde(skip_serializing_if = "Option::is_none")]
49    pub device: Option<String>,
50    /// A TTL (Time to Live) in seconds, after which the message will be automatically deleted from the recipient's inbox.
51    /// Setting *ttl* to None prevents this auto removal. Setting TTL to 0 will raise an error (ttl must be > 0).
52    #[serde(skip_serializing_if = "Option::is_none")]
53    pub ttl: Option<u32>,
54}
55
56impl AttachmentMessage {
57    pub fn into_form(self) -> Result<reqwest::blocking::multipart::Form, std::io::Error> {
58        let mut form: reqwest::blocking::multipart::Form = reqwest::blocking::multipart::Form::new()
59            .text("token", self.app_token.clone())
60            .text("user", self.user_key.clone())
61            .text("message", self.message.clone())
62            .text("title", self.title.clone().unwrap_or(String::from("")))
63            .text("url", self.url.clone().unwrap_or(String::from("")))
64            .text("url_title", self.url_title.clone().unwrap_or(String::from("")))
65            .text("priority", self.priority.unwrap_or(String::from("")))
66            .text("retry", self.retry.clone().unwrap_or(String::from("")))
67            .text("expire", self.expire.clone().unwrap_or(String::from("")))
68            .text("sound", self.sound.clone().unwrap_or(String::from("")))
69            .text("timestamp", self.timestamp.unwrap_or(String::from("")))
70            .text("device", self.device.clone().unwrap_or(String::from("")));
71        // TTL became required if it has a value, 0 doesn't work anymore.
72        if self.ttl.is_some() {
73            form = form.text("ttl", self.ttl.unwrap_or(999).to_string());
74        }
75        let attachment_part = reqwest::blocking::multipart::Part::bytes(std::fs::read(&self.attachment)?)
76            .file_name(self.attachment.clone());
77        Ok(form.part("attachment", attachment_part))
78    }
79
80    pub async fn into_form_async(self) -> Result<Form, io::Error> {
81        let mut form = Form::new()
82            .text("token", self.app_token.clone())
83            .text("user", self.user_key.clone())
84            .text("message", self.message.clone())
85            .text("title", self.title.clone().unwrap_or(String::from("")))
86            .text("url", self.url.clone().unwrap_or(String::from("")))
87            .text("url_title", self.url_title.clone().unwrap_or(String::from("")))
88            .text("priority", self.priority.unwrap_or(String::from("")))
89            .text("sound", self.sound.clone().unwrap_or(String::from("")))
90            .text("timestamp", self.timestamp.unwrap_or(String::from("")))
91            .text("device", self.device.clone().unwrap_or(String::from("")));
92        if self.ttl.is_some() {
93            form = form.text("ttl", self.ttl.unwrap_or(999).to_string());
94        }
95        let file_bytes = tokio::fs::read(&self.attachment).await?;
96        let part = Part::bytes(file_bytes)
97            .file_name(self.attachment.clone());
98        Ok(form.part("attachment", part))
99    }
100}
101
102impl Default for AttachmentMessage {
103    fn default() -> Self {
104        Self {
105            app_token: "".into(),
106            user_key: "".into(),
107            message: "".into(),
108            attachment: "".into(),
109            title: None,
110            url: None,
111            url_title: None,
112            priority: None,
113            retry: None,
114            expire: None,
115            sound: None,
116            timestamp: None,
117            device: None,
118            ttl: None,
119        }
120    }
121}