use crate::notify::{ConfigField, DeliverError, Delivery, Notification, NotificationChannel};
#[derive(Default)]
pub struct WebhookChannel;
#[async_trait::async_trait]
impl NotificationChannel for WebhookChannel {
fn kind(&self) -> &'static str {
"webhook"
}
fn label(&self) -> &'static str {
"Webhook (JSON)"
}
fn help(&self) -> &'static str {
"POSTs the notification as JSON. Use this for Zapier, n8n, Discord, or your own endpoint."
}
fn config_fields(&self) -> Vec<ConfigField> {
vec![ConfigField {
name: "url",
label: "Endpoint URL",
help: "Receives a JSON POST.",
secret: false,
required: true,
}]
}
async fn deliver(&self, d: &Delivery<'_>, msg: &Notification) -> Result<(), DeliverError> {
let Some(url) = d.config.get("url").filter(|s| !s.trim().is_empty()) else {
return Err(DeliverError::Permanent("no endpoint URL set".to_owned()));
};
let payload = serde_json::to_value(msg)
.map_err(|e| DeliverError::Permanent(format!("serialize: {e}")))?;
let mut req = super::client_for(url).await?
.post(url.as_str())
.json(&payload)
.map_err(|e| DeliverError::Permanent(format!("encode: {e}")))?;
if !d.secret.is_empty() {
req = req
.header("X-Rustango-Cms-Token", d.secret)
.map_err(|e| DeliverError::Permanent(format!("header: {e}")))?;
}
let resp = req
.send()
.await
.map_err(|e| DeliverError::Transient(e.to_string()))?;
let status = resp.status().as_u16();
if (200..300).contains(&status) {
return Ok(());
}
let body = resp.text().await.unwrap_or_default();
Err(super::classify(status, &body))
}
}
crate::register_notification_channel!(WebhookChannel);