discord_webhook_client/
lib.rs

1//! `discord-webhook-client` is a super simple client to the Discord webhook service.
2//!
3//! This crate has one function, `send_message`, which should be used to send messages
4
5use discord_message::DiscordMessage;
6use reqwest::Response;
7
8/// Defines possible errors from this crate
9#[derive(Debug)]
10pub enum Error {
11    /// An error with message serialization
12    MessageSerializationError(discord_message::Error),
13
14    /// An error with the request
15    RequestError(reqwest::Error),
16}
17
18impl From<discord_message::Error> for Error {
19    fn from(e: discord_message::Error) -> Error {
20        Self::MessageSerializationError(e)
21    }
22}
23
24impl From<reqwest::Error> for Error {
25    fn from(e: reqwest::Error) -> Error {
26        Self::RequestError(e)
27    }
28}
29
30/// Send a message to a discord server via webhook
31pub async fn send_message(webhook: url::Url, message: &DiscordMessage) -> Result<Response, Error> {
32    let client = reqwest::Client::new();
33
34    // Set up and send the post request
35    Ok(client
36        .post(webhook)
37        .header("Content-Type", "application/json")
38        .header(
39            "User-Agent",
40            "discord-webhook-client/0.x Rust client for Discord webhooks",
41        )
42        .body(message.to_json()?)
43        .send()
44        .await?)
45}