slack_messaging/
lib.rs

1//! # Slack Messaging
2//!
3//! This is a library to support building [Slack Block Kit messages](https://docs.slack.dev/reference/block-kit).
4//! Using this, you can build any messages in type-safe way like following.
5//!
6//! ```
7//! use slack_messaging::{mrkdwn, Message};
8//! use slack_messaging::blocks::{elements::Button, Actions, Section};
9//!
10//! #[tokio::main]
11//! async fn main() {
12//!     let message = Message::builder()
13//!         .block(
14//!             Section::builder()
15//!                 .text(mrkdwn!("You have a new request:\n*<fakeLink.toEmployeeProfile.com|Fred Enriquez - New device request>*"))
16//!                 .build()
17//!         )
18//!         .block(
19//!             Section::builder()
20//!                 .field(mrkdwn!("*Type:*\nComputer (laptop)"))
21//!                 .field(mrkdwn!("*When:*\nSubmitted Aug 10"))
22//!                 .build()
23//!         )
24//!         .block(
25//!             Actions::builder()
26//!                 .element(
27//!                     Button::builder()
28//!                         .text("Approve")
29//!                         .value("approve")
30//!                         .primary()
31//!                         .build()
32//!                 )
33//!                 .element(
34//!                     Button::builder()
35//!                         .text("Deny")
36//!                         .value("deny")
37//!                         .danger()
38//!                         .build()
39//!                 )
40//!                 .build()
41//!         )
42//!         .build();
43//!
44//!     let req = reqwest::Client::new()
45//!         .post("https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX")
46//!         .json(&message);
47//!
48//!     if let Err(err) = req.send().await {
49//!         eprintln!("{err}");
50//!     }
51//! }
52//! ```
53//!
54//! The message payload of the above example is following.
55//!
56//! ```json
57//! {
58//!     "blocks": [
59//!         {
60//!             "type": "section",
61//!             "text": {
62//!                 "type": "mrkdwn",
63//!                 "text": "You have a new request:\n*<fakeLink.toEmployeeProfile.com|Fred Enriquez - New device request>*"
64//!             }
65//!         },
66//!         {
67//!             "type": "section",
68//!             "fields": [
69//!                 {
70//!                     "type": "mrkdwn",
71//!                     "text": "*Type:*\nComputer (laptop)"
72//!                 },
73//!                 {
74//!                     "type": "mrkdwn",
75//!                     "text": "*When:*\nSubmitted Aug 10"
76//!                 }
77//!             ]
78//!         },
79//!         {
80//!             "type": "actions",
81//!             "elements": [
82//!                 {
83//!                     "type": "button",
84//!                     "text": {
85//!                         "type": "plain_text",
86//!                         "text": "Approve"
87//!                     },
88//!                     "value": "approve",
89//!                     "style": "primary"
90//!                 },
91//!                 {
92//!                     "type": "button",
93//!                     "text": {
94//!                         "type": "plain_text",
95//!                         "text": "Deny"
96//!                     },
97//!                     "value": "deny",
98//!                     "style": "danger"
99//!                 }
100//!             ]
101//!         }
102//!     ]
103//! }
104//! ```
105//!
106//! ## Optional Features
107//!
108//! The following are a list of [Cargo features](https://doc.rust-lang.org/stable/cargo/reference/features.html#the-features-section) that can be enabled or disabled:
109//!
110//! - **fmt** : Enable [fmt] module.
111
112/// Objects from that [Message] is composed.
113pub mod blocks;
114/// Objects can be used inside of block elements.
115pub mod composition_objects;
116/// Format text for slack app. Require `fmt` feature.
117#[cfg(feature = "fmt")]
118pub mod fmt;
119#[macro_use]
120mod macros;
121mod message;
122
123pub use message::{Message, builder::MessageBuilder};