titanium_http/
webhook.rs

1use crate::error::HttpError;
2use crate::HttpClient;
3use serde::Serialize;
4use titanium_model::{Message, Snowflake, Webhook};
5
6impl HttpClient {
7    /// Create a webhook.
8    pub async fn create_webhook(
9        &self,
10        channel_id: Snowflake,
11        name: &str,
12        avatar: Option<&str>, // Data URI
13    ) -> Result<Webhook<'static>, HttpError> {
14        #[derive(Serialize)]
15        struct CreateWebhook<'a> {
16            name: &'a str,
17            avatar: Option<&'a str>,
18        }
19
20        let route = format!("/channels/{}/webhooks", channel_id);
21        self.post(&route, CreateWebhook { name, avatar }).await
22    }
23
24    /// Get channel webhooks.
25    pub async fn get_channel_webhooks(
26        &self,
27        channel_id: Snowflake,
28    ) -> Result<Vec<Webhook<'static>>, HttpError> {
29        let route = format!("/channels/{}/webhooks", channel_id);
30        self.get(&route).await
31    }
32
33    /// Get guild webhooks.
34    pub async fn get_guild_webhooks(
35        &self,
36        guild_id: Snowflake,
37    ) -> Result<Vec<Webhook<'static>>, HttpError> {
38        let route = format!("/guilds/{}/webhooks", guild_id);
39        self.get(&route).await
40    }
41
42    /// Execute webhook (send message).
43    ///
44    /// Note: Webhooks can be executed without a bot token if usage via `id/token` url.
45    /// This method complies with the library structure using the client's auth or just payload.
46    pub async fn execute_webhook(
47        &self,
48        webhook_id: Snowflake,
49        webhook_token: &str,
50        params: &titanium_model::builder::ExecuteWebhook,
51    ) -> Result<Option<Message<'static>>, HttpError> {
52        let route = format!("/webhooks/{}/{}", webhook_id, webhook_token);
53        // wait=true to get message back
54        self.post_with_query(&route, params, &[("wait", "true")])
55            .await
56    }
57}
58
59// Params struct removed in favor of titanium_model::builder::ExecuteWebhook