Skip to main content

hookburn_slack/
message.rs

1// crates/hookburn-slack/src/message.rs
2use crate::error::SlackError;
3use serde::Serialize;
4
5/// A Slack webhook message.
6#[derive(Debug, Clone, Serialize, Default)]
7pub struct SlackMessage {
8    /// Plain text content.
9    #[serde(skip_serializing_if = "Option::is_none")]
10    pub text: Option<String>,
11    /// Block Kit blocks for rich formatting.
12    #[serde(skip_serializing_if = "Vec::is_empty")]
13    pub blocks: Vec<SlackBlock>,
14    /// Legacy attachments.
15    #[serde(skip_serializing_if = "Vec::is_empty")]
16    pub attachments: Vec<SlackAttachment>,
17    /// Thread timestamp to reply in a thread.
18    #[serde(skip_serializing_if = "Option::is_none")]
19    pub thread_ts: Option<String>,
20    /// Broadcast reply to the channel.
21    #[serde(skip_serializing_if = "Option::is_none")]
22    pub reply_broadcast: Option<bool>,
23    /// Override the webhook's username.
24    #[serde(skip_serializing_if = "Option::is_none")]
25    pub username: Option<String>,
26    /// Override the webhook's icon (emoji).
27    #[serde(skip_serializing_if = "Option::is_none")]
28    pub icon_emoji: Option<String>,
29    /// Override the webhook's icon (URL).
30    #[serde(skip_serializing_if = "Option::is_none")]
31    pub icon_url: Option<String>,
32}
33
34impl SlackMessage {
35    /// Builder for messages
36    pub fn builder() -> SlackMessageBuilder {
37        SlackMessageBuilder::default()
38    }
39}
40
41/// Block Kit block types.
42#[derive(Debug, Clone, Serialize)]
43#[serde(tag = "type")]
44pub enum SlackBlock {
45    /// Slack Section
46    #[serde(rename = "section")]
47    Section(SlackSectionBlock),
48    /// Slack Divicer
49    #[serde(rename = "divider")]
50    Divider,
51    /// Slack Header
52    #[serde(rename = "header")]
53    Header(SlackHeaderBlock),
54    /// Slack Actions
55    #[serde(rename = "actions")]
56    Actions(SlackActionsBlock),
57    /// Slack Context
58    #[serde(rename = "context")]
59    Context(SlackContextBlock),
60}
61
62/// A section block containing text, optional fields, and an optional accessory.
63#[derive(Debug, Clone, Serialize)]
64pub struct SlackSectionBlock {
65    /// The main text content of the section.
66    #[serde(skip_serializing_if = "Option::is_none")]
67    pub text: Option<SlackText>,
68    /// Additional fields displayed side-by-side below the text.
69    #[serde(skip_serializing_if = "Vec::is_empty")]
70    pub fields: Vec<SlackText>,
71    /// An optional accessory (image, button, etc.) displayed on the right.
72    #[serde(skip_serializing_if = "Option::is_none")]
73    pub accessory: Option<SlackAccessory>,
74}
75
76/// A header block.
77#[derive(Debug, Clone, Serialize)]
78pub struct SlackHeaderBlock {
79    /// Slack Text
80    pub text: SlackText,
81}
82
83/// An actions block containing interactive elements like buttons and date pickers.
84#[derive(Debug, Clone, Serialize)]
85pub struct SlackActionsBlock {
86    /// The interactive elements (buttons, date pickers, etc.).
87    pub elements: Vec<SlackElement>,
88}
89
90/// A context block with small text elements displayed in a subtle way.
91#[derive(Debug, Clone, Serialize)]
92pub struct SlackContextBlock {
93    /// The text elements displayed in the context block.
94    pub elements: Vec<SlackText>,
95}
96/// Interactive elements for actions blocks.
97#[derive(Debug, Clone, Serialize)]
98#[serde(tag = "type")]
99pub enum SlackElement {
100    /// Slack Button element
101    #[serde(rename = "button")]
102    Button(SlackButton),
103    /// Slack Date element
104    #[serde(rename = "datepicker")]
105    DatePicker(SlackDatePicker),
106}
107
108/// A button element for interactive Slack messages.
109#[derive(Debug, Clone, Serialize)]
110pub struct SlackButton {
111    /// The button label text.
112    pub text: SlackText,
113    /// The value sent when the button is clicked.
114    pub value: String,
115    /// Optional URL to navigate to when clicked (overrides action).
116    #[serde(skip_serializing_if = "Option::is_none")]
117    pub url: Option<String>,
118    /// Optional button style: "primary" (green) or "danger" (red).
119    #[serde(skip_serializing_if = "Option::is_none")]
120    pub style: Option<String>,
121}
122
123/// A date picker element for selecting dates in Slack messages.
124#[derive(Debug, Clone, Serialize)]
125pub struct SlackDatePicker {
126    /// The placeholder text displayed before a date is selected.
127    pub placeholder: SlackText,
128    /// The action ID used to identify the interaction.
129    pub action_id: String,
130}
131
132/// A text object with formatting options for Slack Block Kit.
133#[derive(Debug, Clone, Serialize)]
134pub struct SlackText {
135    /// The type of text formatting: plain text or markdown.
136    #[serde(rename = "type")]
137    pub text_type: SlackTextType,
138    /// The actual text content.
139    pub text: String,
140    /// Whether emoji should be rendered in plain text.
141    #[serde(skip_serializing_if = "Option::is_none")]
142    pub emoji: Option<bool>,
143}
144
145/// Supported text formatting types for Slack messages.
146#[derive(Debug, Clone, Serialize)]
147#[serde(rename_all = "lowercase")]
148pub enum SlackTextType {
149    /// Plain text with no formatting.
150    PlainText,
151    /// Markdown-style text with formatting support.
152    Mrkdwn,
153}
154
155/// An accessory element that can be added to section blocks (e.g., images).
156#[derive(Debug, Clone, Serialize)]
157pub struct SlackAccessory {
158    /// The type of accessory (e.g., "image").
159    #[serde(rename = "type")]
160    pub accessory_type: String,
161    /// The URL of the image.
162    pub image_url: String,
163    /// Alternative text for accessibility.
164    pub alt_text: String,
165}
166
167/// A legacy attachment for Slack messages (pre-Block Kit).
168#[derive(Debug, Clone, Serialize)]
169pub struct SlackAttachment {
170    /// Optional color accent (hex string like "#36a64f").
171    pub color: Option<String>,
172    /// Optional text displayed above the attachment.
173    pub pretext: Option<String>,
174    /// The main text of the attachment.
175    pub text: Option<String>,
176    /// List of fields displayed in the attachment.
177    pub fields: Vec<SlackAttachmentField>,
178}
179
180/// A field within a Slack attachment.
181#[derive(Debug, Clone, Serialize)]
182pub struct SlackAttachmentField {
183    /// The field title displayed in bold.
184    pub title: String,
185    /// The field value.
186    pub value: String,
187    /// Whether the field should be displayed side-by-side with others.
188    #[serde(skip_serializing_if = "Option::is_none")]
189    pub short: Option<bool>,
190}
191
192/// Builder for constructing Slack messages with a fluent API.
193#[derive(Default)]
194pub struct SlackMessageBuilder {
195    inner: SlackMessage,
196}
197
198impl SlackMessageBuilder {
199    /// Set the plain text content of the message.
200    pub fn text(mut self, text: impl Into<String>) -> Self {
201        self.inner.text = Some(text.into());
202        self
203    }
204
205    /// Add a Block Kit block to the message.
206    pub fn block(mut self, block: SlackBlock) -> Self {
207        self.inner.blocks.push(block);
208        self
209    }
210
211    /// Set all blocks for the message (replaces existing).
212    pub fn blocks(mut self, blocks: Vec<SlackBlock>) -> Self {
213        self.inner.blocks = blocks;
214        self
215    }
216
217    /// Override the webhook's default username.
218    pub fn username(mut self, username: impl Into<String>) -> Self {
219        self.inner.username = Some(username.into());
220        self
221    }
222
223    /// Override the webhook's icon using an emoji.
224    pub fn icon_emoji(mut self, emoji: impl Into<String>) -> Self {
225        self.inner.icon_emoji = Some(emoji.into());
226        self
227    }
228
229    /// Override the webhook's icon using a URL.
230    pub fn icon_url(mut self, url: impl Into<String>) -> Self {
231        self.inner.icon_url = Some(url.into());
232        self
233    }
234
235    /// Send the message as a threaded reply.
236    pub fn thread_ts(mut self, ts: impl Into<String>) -> Self {
237        self.inner.thread_ts = Some(ts.into());
238        self
239    }
240
241    /// Broadcast the reply to the channel.
242    pub fn reply_broadcast(mut self, broadcast: bool) -> Self {
243        self.inner.reply_broadcast = Some(broadcast);
244        self
245    }
246
247    /// Build the final Slack message.
248    ///
249    /// # Errors
250    ///
251    /// Returns `SlackError::EmptyMessage` if both `text` and `blocks` are empty.
252    pub fn build(self) -> Result<SlackMessage, SlackError> {
253        if self.inner.text.is_none() && self.inner.blocks.is_empty() {
254            return Err(SlackError::EmptyMessage);
255        }
256        Ok(self.inner)
257    }
258}