Skip to main content

rustango_cms/notify/channels/
teams.rs

1//! Microsoft Teams — incoming webhook (MessageCard).
2
3use crate::notify::{ConfigField, DeliverError, Delivery, Notification, NotificationChannel};
4use serde_json::json;
5
6/// Posts a MessageCard to a Teams incoming webhook.
7///
8/// MessageCard rather than the newer Adaptive Card on purpose: connector
9/// webhooks accept it everywhere, including the "Incoming Webhook"
10/// connector most teams already have, whereas Adaptive Cards need a
11/// Workflows/Power Automate endpoint. The older format is the one that
12/// works without asking an admin for anything.
13#[derive(Default)]
14pub struct TeamsChannel;
15
16#[async_trait::async_trait]
17impl NotificationChannel for TeamsChannel {
18    fn kind(&self) -> &'static str {
19        "teams"
20    }
21    fn label(&self) -> &'static str {
22        "Microsoft Teams"
23    }
24    fn help(&self) -> &'static str {
25        "Add an Incoming Webhook connector to a Teams channel and paste its URL."
26    }
27    fn config_fields(&self) -> Vec<ConfigField> {
28        vec![ConfigField {
29            name: "webhook_url",
30            label: "Webhook URL",
31            help: "https://….webhook.office.com/webhookb2/…",
32            secret: true,
33            required: true,
34        }]
35    }
36
37    async fn deliver(&self, d: &Delivery<'_>, msg: &Notification) -> Result<(), DeliverError> {
38        if d.secret.is_empty() {
39            return Err(DeliverError::Permanent("no webhook URL set".to_owned()));
40        }
41        let facts: Vec<serde_json::Value> = msg
42            .fields
43            .iter()
44            .map(|(k, v)| json!({ "name": k, "value": super::slack::truncate(v, 1000) }))
45            .collect();
46        let mut card = json!({
47            "@type": "MessageCard",
48            "@context": "https://schema.org/extensions",
49            "summary": msg.title,
50            "themeColor": "4A6D9E",
51            "title": msg.title,
52            "sections": [{ "text": msg.summary, "facts": facts }],
53        });
54        if !msg.url.is_empty() {
55            card["potentialAction"] = json!([{
56                "@type": "OpenUri",
57                "name": "Open",
58                "targets": [{ "os": "default", "uri": msg.url }],
59            }]);
60        }
61        super::slack::post_json(d.secret, &card).await
62    }
63}
64
65crate::register_notification_channel!(TeamsChannel);