discord_webhook_lib/
lib.rs

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
67
68
69
70
use reqwest::header::CONTENT_TYPE;
use reqwest::{ClientBuilder};
use serde_json::{json, Result, Value};
use std::process;
use anyhow::{anyhow, Error};

pub async fn send_discord(
    webhook_url: &str,
    message: &str,
    username: &str,
) -> std::result::Result<(), Error> {
    if webhook_url.contains("https://discord.com/api/webhooks/") || webhook_url.contains("https://discordapp.com/api/webhooks/") {
        if let Ok(json_message) = jsonify(username, message) {
            push_message(webhook_url, json_message).await?;
        } else {
            return Err(anyhow!("Unable to Send Discord Webhook"))
        }
    } else {
        return Err(anyhow!("Invalid Discord URL"))
    }
    Ok(())
}

async fn push_message(api_url: &str, json_message: Value) -> reqwest::Result<String> {
    let client = ClientBuilder::new()
        .danger_accept_invalid_certs(true)
        .no_gzip()
        .build();

    let response = match client {
        Ok(r) => {
            r.post(api_url)
                .header(CONTENT_TYPE, "application/json")
                .json(&json_message)
                .send()
                .await
        }
        Err(_e) => process::exit(3),
    };
    return match response {
        Ok(r) => r.text().await,
        Err(e) => Err(e),
    };
}

pub fn jsonify(username: &str, message: &str) -> Result<Value> {
    let data = json!({
    "username": username,
    "content": message,
    });
    Ok(data)
}

#[cfg(test)]
mod tests {
    use std::env;
    use crate::send_discord;
    /// export DISC_WH="https://discordapp.com/api/webhooks/xxxxxxxxxxxxxxxxxxxxx"
    #[test]
    fn test_send_wh() {
        if let Ok(whu) = env::var("DISC_WH") {
            let rt = tokio::runtime::Runtime::new();
            rt.unwrap().block_on(send_discord(
                whu.as_str(),
                "Hello World",
                "Lazarus"
            )).unwrap();
        }
    }
}