1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use std::env;

/// Base webhook URL for the Push More service.
static WEBHOOK_URL: &str = "https://pushmore.io/webhook/";

#[derive(Default)]
pub struct PushMore {
    key: String,
}

impl PushMore {
    /// Create a PushMore instance, taking the key from the `PUSH_MORE_KEY` environment variable.
    ///
    /// # Example
    ///
    /// ```ignore
    /// let client = PushMore::new();
    /// ```
    ///
    /// # Panics
    ///
    /// If the environment value is missing.
    pub fn new() -> Self {
        let env_key = "PUSH_MORE_KEY";

        match env::var(env_key) {
            Ok(val) => PushMore::new_with_key(val),
            Err(e) => panic!("Invalid value for {}: `{}`", env_key, e),
        }
    }

    /// Create a PushMore instance with the supplied key.
    ///
    /// # Arguments
    ///
    /// * `key`: Push More API key from [Push More](https://pushmore.io)
    ///
    /// # Example
    ///
    /// ```ignore
    /// let client = PushMore::new_with_key("<pushmore key>".to_string());
    /// ```
    pub fn new_with_key(key: String) -> Self {
        PushMore { key }
    }

    /// Send a String through the Push More service to Telegram.
    ///
    /// Returns whether it was successful or not.
    ///
    /// # Example
    ///
    /// ```ignore
    /// let success = client.send("hello!".to_string());
    /// ```
    pub fn send(&self, body: String) -> bool {
        let url = format!("{}{}", WEBHOOK_URL, self.key);

        if let Ok(resp) = ureq::post(&url).send_string(&body) {
            if let Ok(b) = resp.into_string() {
                return b.contains("OK");
            }
        }
        false
    }
}