slack_messaging/message/
mod.rs

1/// Builder object for Message.
2pub mod builder;
3
4use super::blocks::Block;
5use serde::Serialize;
6
7/// [Message](https://docs.slack.dev/messaging#payloads)
8/// representation.
9///
10/// # Example
11///
12/// See also [Header](crate::blocks::Header), [Section](crate::blocks::Section)
13/// and [any other blocks](crate::blocks) to know how to build these blocks.
14///
15/// ```
16/// use slack_messaging::{blocks::{Header, Section}, mrkdwn, Message};
17///
18/// let message = Message::builder()
19///     .text("New Paid Time Off request from Fred Enriquez")
20///     .block(
21///         Header::builder()
22///             .text("New request")
23///             .build()
24///     )
25///     .block(
26///         Section::builder()
27///             .field(mrkdwn!("*Type:*\nPaid Time Off"))
28///             .field(mrkdwn!("*Created by:*\n<example.com|Fred Enriquez>"))
29///             .build()
30///     )
31///     .block(
32///         Section::builder()
33///             .field(mrkdwn!("*When:*\nAug 10 - Aug 13"))
34///             .build()
35///     )
36///     .block(
37///         Section::builder()
38///             .text(mrkdwn!("<https://example.com|View request>"))
39///             .build()
40///     )
41///     .build();
42///
43/// let expected = serde_json::json!({
44///     "text": "New Paid Time Off request from Fred Enriquez",
45///     "blocks": [
46///         {
47///             "type": "header",
48///             "text": {
49///                 "type": "plain_text",
50///                 "text": "New request"
51///             }
52///         },
53///         {
54///             "type": "section",
55///             "fields": [
56///                 {
57///                     "type": "mrkdwn",
58///                     "text": "*Type:*\nPaid Time Off"
59///                 },
60///                 {
61///                     "type": "mrkdwn",
62///                     "text": "*Created by:*\n<example.com|Fred Enriquez>"
63///                 }
64///             ]
65///         },
66///         {
67///             "type": "section",
68///             "fields": [
69///                 {
70///                     "type": "mrkdwn",
71///                     "text": "*When:*\nAug 10 - Aug 13"
72///                 }
73///             ]
74///         },
75///         {
76///             "type": "section",
77///             "text": {
78///                 "type": "mrkdwn",
79///                 "text": "<https://example.com|View request>"
80///             }
81///         }
82///     ]
83/// });
84///
85/// let json = serde_json::to_value(message).unwrap();
86///
87/// assert_eq!(json, expected);
88/// ```
89#[derive(Debug, Clone, Serialize)]
90pub struct Message {
91    #[serde(skip_serializing_if = "Option::is_none")]
92    pub(super) text: Option<String>,
93
94    #[serde(skip_serializing_if = "Vec::is_empty")]
95    pub(super) blocks: Vec<Block>,
96
97    #[serde(skip_serializing_if = "Option::is_none")]
98    pub(super) thread_ts: Option<String>,
99
100    #[serde(skip_serializing_if = "Option::is_none")]
101    pub(super) mrkdwn: Option<bool>,
102
103    #[serde(skip_serializing_if = "Option::is_none")]
104    pub(super) response_type: Option<String>,
105
106    #[serde(skip_serializing_if = "Option::is_none")]
107    pub(super) replace_original: Option<bool>,
108
109    #[serde(skip_serializing_if = "Option::is_none")]
110    pub(super) delete_original: Option<bool>,
111
112    #[serde(skip_serializing_if = "Option::is_none")]
113    pub(super) reply_broadcast: Option<bool>,
114}