slacko 0.2.2

Comprehensive Rust SDK for the Slack API with stealth mode support
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
//! Block builders for Block Kit
//!
//! Blocks are visual components that can be stacked and arranged to create app layouts.

use super::objects::TextObject;
use serde::{Deserialize, Serialize};
use serde_json::Value;

/// Section block - displays text and optional accessory
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SectionBlock {
    #[serde(rename = "type")]
    type_field: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub text: Option<TextObject>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub block_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub fields: Option<Vec<TextObject>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub accessory: Option<Value>,
}

impl SectionBlock {
    /// Create a new section block
    pub fn new() -> Self {
        Self {
            type_field: "section".to_string(),
            text: None,
            block_id: None,
            fields: None,
            accessory: None,
        }
    }

    /// Set plain text
    pub fn text(mut self, text: impl Into<String>) -> Self {
        self.text = Some(TextObject::plain(text));
        self
    }

    /// Set markdown text
    pub fn markdown(mut self, text: impl Into<String>) -> Self {
        self.text = Some(TextObject::markdown(text));
        self
    }

    /// Set text object directly
    pub fn text_object(mut self, text: TextObject) -> Self {
        self.text = Some(text);
        self
    }

    /// Set block ID
    pub fn block_id(mut self, id: impl Into<String>) -> Self {
        self.block_id = Some(id.into());
        self
    }

    /// Add fields (up to 10)
    pub fn fields(mut self, fields: Vec<TextObject>) -> Self {
        self.fields = Some(fields);
        self
    }

    /// Add a field
    pub fn field(mut self, field: TextObject) -> Self {
        self.fields.get_or_insert_with(Vec::new).push(field);
        self
    }

    /// Set accessory element
    pub fn accessory(mut self, accessory: Value) -> Self {
        self.accessory = Some(accessory);
        self
    }

    /// Convert to JSON value
    pub fn build(self) -> Value {
        serde_json::to_value(self).expect("SectionBlock is always serializable")
    }
}

impl Default for SectionBlock {
    fn default() -> Self {
        Self::new()
    }
}

/// Actions block - holds interactive elements
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ActionsBlock {
    #[serde(rename = "type")]
    type_field: String,
    pub elements: Vec<Value>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub block_id: Option<String>,
}

impl ActionsBlock {
    /// Create a new actions block
    pub fn new() -> Self {
        Self {
            type_field: "actions".to_string(),
            elements: Vec::new(),
            block_id: None,
        }
    }

    /// Set block ID
    pub fn block_id(mut self, id: impl Into<String>) -> Self {
        self.block_id = Some(id.into());
        self
    }

    /// Add an element
    pub fn element(mut self, element: Value) -> Self {
        self.elements.push(element);
        self
    }

    /// Add multiple elements
    pub fn elements(mut self, elements: Vec<Value>) -> Self {
        self.elements.extend(elements);
        self
    }

    /// Add a button (convenience method)
    pub fn button(mut self, action_id: impl Into<String>, text: impl Into<String>) -> Self {
        use super::elements::ButtonElement;
        self.elements
            .push(ButtonElement::new(action_id, text).build());
        self
    }

    /// Convert to JSON value
    pub fn build(self) -> Value {
        serde_json::to_value(self).expect("ActionsBlock is always serializable")
    }
}

impl Default for ActionsBlock {
    fn default() -> Self {
        Self::new()
    }
}

/// Context block - displays contextual info
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ContextBlock {
    #[serde(rename = "type")]
    type_field: String,
    pub elements: Vec<Value>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub block_id: Option<String>,
}

impl ContextBlock {
    /// Create a new context block
    pub fn new() -> Self {
        Self {
            type_field: "context".to_string(),
            elements: Vec::new(),
            block_id: None,
        }
    }

    /// Set block ID
    pub fn block_id(mut self, id: impl Into<String>) -> Self {
        self.block_id = Some(id.into());
        self
    }

    /// Add a text element
    pub fn text(mut self, text: TextObject) -> Self {
        self.elements
            .push(serde_json::to_value(text).expect("TextObject is always serializable"));
        self
    }

    /// Add markdown text (convenience)
    pub fn markdown(mut self, text: impl Into<String>) -> Self {
        self.elements.push(
            serde_json::to_value(TextObject::markdown(text))
                .expect("TextObject is always serializable"),
        );
        self
    }

    /// Add an image
    pub fn image(mut self, image_url: impl Into<String>, alt_text: impl Into<String>) -> Self {
        self.elements.push(serde_json::json!({
            "type": "image",
            "image_url": image_url.into(),
            "alt_text": alt_text.into()
        }));
        self
    }

    /// Convert to JSON value
    pub fn build(self) -> Value {
        serde_json::to_value(self).expect("ContextBlock is always serializable")
    }
}

impl Default for ContextBlock {
    fn default() -> Self {
        Self::new()
    }
}

/// Divider block - visual separator
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DividerBlock {
    #[serde(rename = "type")]
    type_field: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub block_id: Option<String>,
}

impl DividerBlock {
    /// Create a new divider block
    pub fn new() -> Self {
        Self {
            type_field: "divider".to_string(),
            block_id: None,
        }
    }

    /// Set block ID
    pub fn block_id(mut self, id: impl Into<String>) -> Self {
        self.block_id = Some(id.into());
        self
    }

    /// Convert to JSON value
    pub fn build(self) -> Value {
        serde_json::to_value(self).expect("DividerBlock is always serializable")
    }
}

impl Default for DividerBlock {
    fn default() -> Self {
        Self::new()
    }
}

/// Header block - large text header
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HeaderBlock {
    #[serde(rename = "type")]
    type_field: String,
    pub text: TextObject,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub block_id: Option<String>,
}

impl HeaderBlock {
    /// Create a new header block
    pub fn new(text: impl Into<String>) -> Self {
        Self {
            type_field: "header".to_string(),
            text: TextObject::plain(text),
            block_id: None,
        }
    }

    /// Set block ID
    pub fn block_id(mut self, id: impl Into<String>) -> Self {
        self.block_id = Some(id.into());
        self
    }

    /// Convert to JSON value
    pub fn build(self) -> Value {
        serde_json::to_value(self).expect("HeaderBlock is always serializable")
    }
}

/// Image block - displays an image
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ImageBlock {
    #[serde(rename = "type")]
    type_field: String,
    pub image_url: String,
    pub alt_text: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub title: Option<TextObject>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub block_id: Option<String>,
}

impl ImageBlock {
    /// Create a new image block
    pub fn new(image_url: impl Into<String>, alt_text: impl Into<String>) -> Self {
        Self {
            type_field: "image".to_string(),
            image_url: image_url.into(),
            alt_text: alt_text.into(),
            title: None,
            block_id: None,
        }
    }

    /// Set title
    pub fn title(mut self, title: impl Into<String>) -> Self {
        self.title = Some(TextObject::plain(title));
        self
    }

    /// Set block ID
    pub fn block_id(mut self, id: impl Into<String>) -> Self {
        self.block_id = Some(id.into());
        self
    }

    /// Convert to JSON value
    pub fn build(self) -> Value {
        serde_json::to_value(self).expect("ImageBlock is always serializable")
    }
}

/// Input block - for collecting user input
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InputBlock {
    #[serde(rename = "type")]
    type_field: String,
    pub label: TextObject,
    pub element: Value,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub block_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub hint: Option<TextObject>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub optional: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub dispatch_action: Option<bool>,
}

impl InputBlock {
    /// Create a new input block
    pub fn new(label: impl Into<String>, element: Value) -> Self {
        Self {
            type_field: "input".to_string(),
            label: TextObject::plain(label),
            element,
            block_id: None,
            hint: None,
            optional: None,
            dispatch_action: None,
        }
    }

    /// Set block ID
    pub fn block_id(mut self, id: impl Into<String>) -> Self {
        self.block_id = Some(id.into());
        self
    }

    /// Set hint text
    pub fn hint(mut self, hint: impl Into<String>) -> Self {
        self.hint = Some(TextObject::plain(hint));
        self
    }

    /// Mark as optional
    pub fn optional(mut self) -> Self {
        self.optional = Some(true);
        self
    }

    /// Enable dispatch action
    pub fn dispatch_action(mut self) -> Self {
        self.dispatch_action = Some(true);
        self
    }

    /// Convert to JSON value
    pub fn build(self) -> Value {
        serde_json::to_value(self).expect("InputBlock is always serializable")
    }
}

/// File block - displays a remote file
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FileBlock {
    #[serde(rename = "type")]
    type_field: String,
    pub external_id: String,
    pub source: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub block_id: Option<String>,
}

impl FileBlock {
    /// Create a new file block
    pub fn new(external_id: impl Into<String>) -> Self {
        Self {
            type_field: "file".to_string(),
            external_id: external_id.into(),
            source: "remote".to_string(),
            block_id: None,
        }
    }

    /// Set block ID
    pub fn block_id(mut self, id: impl Into<String>) -> Self {
        self.block_id = Some(id.into());
        self
    }

    /// Convert to JSON value
    pub fn build(self) -> Value {
        serde_json::to_value(self).expect("FileBlock is always serializable")
    }
}

/// Video block - displays a video
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VideoBlock {
    #[serde(rename = "type")]
    type_field: String,
    pub video_url: String,
    pub thumbnail_url: String,
    pub alt_text: String,
    pub title: TextObject,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub title_url: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub author_name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub provider_name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub provider_icon_url: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<TextObject>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub block_id: Option<String>,
}

impl VideoBlock {
    /// Create a new video block
    pub fn new(
        video_url: impl Into<String>,
        thumbnail_url: impl Into<String>,
        alt_text: impl Into<String>,
        title: impl Into<String>,
    ) -> Self {
        Self {
            type_field: "video".to_string(),
            video_url: video_url.into(),
            thumbnail_url: thumbnail_url.into(),
            alt_text: alt_text.into(),
            title: TextObject::plain(title),
            title_url: None,
            author_name: None,
            provider_name: None,
            provider_icon_url: None,
            description: None,
            block_id: None,
        }
    }

    /// Set title URL
    pub fn title_url(mut self, url: impl Into<String>) -> Self {
        self.title_url = Some(url.into());
        self
    }

    /// Set author name
    pub fn author_name(mut self, name: impl Into<String>) -> Self {
        self.author_name = Some(name.into());
        self
    }

    /// Set provider name
    pub fn provider_name(mut self, name: impl Into<String>) -> Self {
        self.provider_name = Some(name.into());
        self
    }

    /// Set description
    pub fn description(mut self, desc: impl Into<String>) -> Self {
        self.description = Some(TextObject::plain(desc));
        self
    }

    /// Set block ID
    pub fn block_id(mut self, id: impl Into<String>) -> Self {
        self.block_id = Some(id.into());
        self
    }

    /// Convert to JSON value
    pub fn build(self) -> Value {
        serde_json::to_value(self).expect("VideoBlock is always serializable")
    }
}

/// Message builder - composes a complete Block Kit message
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MessageBuilder {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub text: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub blocks: Option<Vec<Value>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub thread_ts: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub mrkdwn: Option<bool>,
}

impl MessageBuilder {
    /// Create a new message builder
    pub fn new() -> Self {
        Self {
            text: None,
            blocks: None,
            thread_ts: None,
            mrkdwn: None,
        }
    }

    /// Set fallback text (required for notifications)
    pub fn text(mut self, text: impl Into<String>) -> Self {
        self.text = Some(text.into());
        self
    }

    /// Add a block
    pub fn block(mut self, block: Value) -> Self {
        self.blocks.get_or_insert_with(Vec::new).push(block);
        self
    }

    /// Add multiple blocks
    pub fn blocks(mut self, blocks: Vec<Value>) -> Self {
        self.blocks.get_or_insert_with(Vec::new).extend(blocks);
        self
    }

    /// Set thread timestamp (reply in thread)
    pub fn thread_ts(mut self, ts: impl Into<String>) -> Self {
        self.thread_ts = Some(ts.into());
        self
    }

    /// Enable markdown in text
    pub fn mrkdwn(mut self) -> Self {
        self.mrkdwn = Some(true);
        self
    }

    /// Quick helper: add a header
    pub fn header(self, text: impl Into<String>) -> Self {
        self.block(HeaderBlock::new(text).build())
    }

    /// Quick helper: add a section with markdown
    pub fn section(self, text: impl Into<String>) -> Self {
        self.block(SectionBlock::new().markdown(text).build())
    }

    /// Quick helper: add a divider
    pub fn divider(self) -> Self {
        self.block(DividerBlock::new().build())
    }

    /// Quick helper: add an image
    pub fn image(self, url: impl Into<String>, alt: impl Into<String>) -> Self {
        self.block(ImageBlock::new(url, alt).build())
    }

    /// Build into JSON value
    pub fn build(self) -> Value {
        serde_json::to_value(self).expect("MessageBuilder is always serializable")
    }

    /// Build into blocks-only (for Views API)
    pub fn build_blocks(self) -> Vec<Value> {
        self.blocks.unwrap_or_default()
    }
}

impl Default for MessageBuilder {
    fn default() -> Self {
        Self::new()
    }
}