sfr_slack_api/builder/
chat_post_message.rs1use sfr_types as st;
4
5use crate::api::chat_post_message::ChatPostMessage;
6
7#[derive(Debug, Default)]
9pub struct ChatPostMessageBuilder {
10 channel: Option<String>,
12
13 attachments: Option<Vec<(String, String)>>,
15
16 blocks: Option<Vec<st::Block>>,
18
19 text: Option<String>,
21
22 as_user: Option<bool>,
24
25 icon_emoji: Option<String>,
27
28 icon_url: Option<String>,
30
31 link_names: Option<bool>,
33
34 metadata: Option<String>,
36
37 mrkdwn: Option<bool>,
39
40 parse: Option<String>,
42
43 reply_broadcast: Option<bool>,
45
46 thread_ts: Option<String>,
48
49 unfurl_links: Option<bool>,
51
52 unfurl_media: Option<bool>,
54
55 username: Option<String>,
57}
58
59impl ChatPostMessageBuilder {
60 pub fn new() -> Self {
62 Self::default()
63 }
64
65 pub fn build(self) -> Result<ChatPostMessage, st::Error> {
67 let channel = self
68 .channel
69 .ok_or_else(|| st::Error::required_value_is_missing(&["channel"]))?;
70
71 let content = match (self.attachments, self.blocks, self.text) {
72 (Some(attachments), _, _) => {
73 let attachments = attachments
74 .into_iter()
75 .map(|attachments| st::Attachments {
76 pretext: attachments.0,
77 text: attachments.1,
78 })
79 .collect();
80 st::ChatPostMessageContent::Attachments(attachments)
81 }
82
83 (None, Some(blocks), _) => st::ChatPostMessageContent::Blocks(blocks.clone()),
84
85 (None, None, Some(text)) => st::ChatPostMessageContent::Text(text.clone()),
86
87 (None, None, None) => {
88 return Err(st::Error::required_value_is_missing(&[
89 "attachments",
90 "blocks",
91 "text",
92 ]))
93 }
94 };
95
96 let result = st::ChatPostMessageRequest {
97 channel,
98 content,
99 as_user: self.as_user,
100 icon_emoji: self.icon_emoji,
101 icon_url: self.icon_url,
102 link_names: self.link_names,
103 metadata: self.metadata,
104 mrkdwn: self.mrkdwn,
105 parse: self.parse,
106 reply_broadcast: self.reply_broadcast,
107 thread_ts: self.thread_ts,
108 unfurl_links: self.unfurl_links,
109 unfurl_media: self.unfurl_media,
110 username: self.username,
111 };
112
113 Ok(result.into())
114 }
115
116 pub fn channel(mut self, channel: String) -> Self {
118 self.channel = Some(channel);
119 self
120 }
121
122 pub fn attachments(mut self, attachments: Vec<(String, String)>) -> Self {
124 self.attachments = Some(attachments);
125 self.blocks = None;
126 self.text = None;
127 self
128 }
129
130 pub fn blocks(mut self, blocks: Vec<st::Block>) -> Self {
132 self.attachments = None;
133 self.blocks = Some(blocks);
134 self.text = None;
135 self
136 }
137
138 pub fn text(mut self, text: String) -> Self {
140 self.attachments = None;
141 self.blocks = None;
142 self.text = Some(text);
143 self
144 }
145
146 #[allow(clippy::wrong_self_convention)]
148 pub fn as_user(mut self, as_user: bool) -> Self {
149 self.as_user = Some(as_user);
150 self
151 }
152
153 pub fn icon_emoji(mut self, icon_emoji: String) -> Self {
155 self.icon_emoji = Some(icon_emoji);
156 self
157 }
158
159 pub fn icon_url(mut self, icon_url: String) -> Self {
161 self.icon_url = Some(icon_url);
162 self
163 }
164
165 pub fn link_names(mut self, link_names: bool) -> Self {
167 self.link_names = Some(link_names);
168 self
169 }
170
171 pub fn metadata(mut self, metadata: String) -> Self {
173 self.metadata = Some(metadata);
174 self
175 }
176
177 pub fn mrkdwn(mut self, mrkdwn: bool) -> Self {
179 self.mrkdwn = Some(mrkdwn);
180 self
181 }
182
183 pub fn parse(mut self, parse: String) -> Self {
185 self.parse = Some(parse);
186 self
187 }
188
189 pub fn reply_broadcast(mut self, reply_broadcast: bool) -> Self {
191 self.reply_broadcast = Some(reply_broadcast);
192 self
193 }
194
195 pub fn thread_ts(mut self, thread_ts: String) -> Self {
197 self.thread_ts = Some(thread_ts);
198 self
199 }
200
201 pub fn unfurl_links(mut self, unfurl_links: bool) -> Self {
203 self.unfurl_links = Some(unfurl_links);
204 self
205 }
206
207 pub fn unfurl_media(mut self, unfurl_media: bool) -> Self {
209 self.unfurl_media = Some(unfurl_media);
210 self
211 }
212
213 pub fn username(mut self, username: String) -> Self {
215 self.username = Some(username);
216 self
217 }
218}