pushover_rs/pushover/data/
attachment_message.rs

1use reqwest::blocking::multipart::Form;
2use serde::Serialize;
3
4#[derive(Debug, Clone, Serialize)]
5/**
6 A message containing an attachment, to be used in conjuction with the send_pushover_request_with_attachment function.
7
8 Note: It is preferred to create a Message through the AttachmentMessageBuilder.
9 **/
10pub struct AttachmentMessage {
11    /* Required */
12    /// (Required) Your app API token, see https://pushover.net/apps/[your application ID]
13    pub app_token: String,
14    /// (Required) Your User key, see your dashboard (https://pushover.net/ top-right)
15    pub user_key: String,
16    /// (Required) Your message
17    pub message: String,
18    /// (Required) An attachment file path to send with the message. (Max size: 2,5MB)
19    pub attachment: String,
20
21    /* Optional */
22    /// The title of the message, otherwise your app's name will be used
23    #[serde(skip_serializing_if = "Option::is_none")]
24    pub title: Option<String>,
25    /// A supplementary URL to show with your message
26    #[serde(skip_serializing_if = "Option::is_none")]
27    pub url: Option<String>,
28    /// A title for your supplementary URL, otherwise just the URL is shown
29    #[serde(skip_serializing_if = "Option::is_none")]
30    pub url_title: Option<String>,
31    /// 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
32    #[serde(skip_serializing_if = "Option::is_none")]
33    pub priority: Option<String>,
34    /// 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)
35    #[serde(skip_serializing_if = "Option::is_none")]
36    pub sound: Option<String>,
37    /// 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
38    #[serde(skip_serializing_if = "Option::is_none")]
39    pub timestamp: Option<String>, // Year 2038 proof :p
40    /// A device name to send the push notifications to.
41    #[serde(skip_serializing_if = "Option::is_none")]
42    pub device: Option<String>,
43    /// A TTL (Time to Live) in seconds, after which the message will be automatically deleted from the recipient's inbox.
44    /// Setting *ttl* to None prevents this auto removal. Setting TTL to 0 will raise an error (ttl must be > 0).
45    #[serde(skip_serializing_if = "Option::is_none")]
46    pub ttl: Option<u32>,
47}
48
49impl AttachmentMessage {
50    pub fn into_form(self) -> Result<Form, std::io::Error> {
51        let mut form = Form::new()
52            .text("token", self.app_token.clone())
53            .text("user", self.user_key.clone())
54            .text("message", self.message.clone())
55            .text("title", self.title.clone().unwrap_or(String::from("")))
56            .text("url", self.url.clone().unwrap_or(String::from("")))
57            .text("url_title", self.url_title.clone().unwrap_or(String::from("")))
58            .text("priority", self.priority.unwrap_or(String::from("")))
59            .text("sound", self.sound.clone().unwrap_or(String::from("")))
60            .text("timestamp", self.timestamp.unwrap_or(String::from("")))
61            .text("device", self.device.clone().unwrap_or(String::from("")));
62        // TTL became required if it has a value, 0 doesn't work anymore.
63        if self.ttl.is_some() {
64            form = form.text("ttl", self.ttl.unwrap_or(999).to_string());
65        }
66        form.file("attachment", self.attachment.clone())
67    }
68}
69
70impl Default for AttachmentMessage {
71    fn default() -> Self {
72        Self {
73            app_token: "".into(),
74            user_key: "".into(),
75            message: "".into(),
76            attachment: "".into(),
77            title: None,
78            url: None,
79            url_title: None,
80            priority: None,
81            sound: None,
82            timestamp: None,
83            device: None,
84            ttl: None,
85        }
86    }
87}