Skip to main content

slack_morphism/
lib.rs

1//! # Slack Morphism for Rust
2//!
3//! Slack Morphism is a modern client library for Slack Web/Events API and Block Kit.
4//!
5//! ## Slack Web API client
6//!
7//! ### Create a client instance:
8//! ```ignore
9//! use slack_morphism::prelude::*;
10//!
11//! let client = SlackClient::new(SlackClientHyperConnector::new());
12//!
13//! ```
14//!
15//! ### Make Web API methods calls
16//!
17//! For most of Slack Web API methods (except for OAuth methods, Incoming Webhooks and event replies)
18//! you need a Slack token to make a call.
19//! For simple bots you can have it in your config files, or you can obtain
20//! workspace tokens using Slack OAuth.
21//!
22//! In the example below, we’re using a hardcoded Slack token, but don’t do that for your production bots and apps.
23//! You should securely and properly store all of Slack tokens.
24//!
25//! ```ignore
26//!
27//! use slack_morphism::prelude::*;
28//!
29//!# async fn example() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
30//!
31//! let client = SlackClient::new(SlackClientHyperConnector::new());
32//!
33//! // Create our Slack API token
34//! let token_value: SlackApiTokenValue = "xoxb-89.....".into();
35//! let token: SlackApiToken = SlackApiToken::new(token_value);
36//!
37//! // Create a Slack session with this token
38//! // A session is just a lightweight wrapper around your token
39//! // not to specify it all the time for series of calls.
40//! let session = client.open_session(&token);
41//!
42//! // Make your first API call (which is `api.test` here)
43//! let test: SlackApiTestResponse = session
44//!         .api_test(&SlackApiTestRequest::new().with_foo("Test".into()))
45//!         .await?;
46//!
47//! // Send a simple text message
48//! let post_chat_req =
49//!     SlackApiChatPostMessageRequest::new("#general".into(),
50//!            SlackMessageContent::new().with_text("Hey there!".into())
51//!     );
52//!
53//! let post_chat_resp = session.chat_post_message(&post_chat_req).await?;
54//!
55//!# Ok(())
56//!# }
57//!
58//! ```
59//!
60//! ## Block Kit
61//!
62//! Blocks, block elements, and views are modeled as typed structs and enums,
63//! with the [`slack_blocks!`] macro (and [`md!`]/[`pt!`] for text objects)
64//! to build the block lists they hold:
65//!
66//! ```
67//! use slack_morphism::prelude::*;
68//!
69//! let show_footer = true;
70//!
71//! let blocks: Vec<SlackBlock> = slack_blocks![
72//!     SlackHeaderBlock::new(pt!("Deploy report")),
73//!     SlackSectionBlock::new()
74//!         .with_text(md!("Deployed *{}* to {}", "api", "us-east-1"))
75//!         .with_fields(vec![md!("*Duration:*\n42s"), md!("*Result:*\nsuccess")]),
76//!     SlackActionsBlock::new(slack_blocks![
77//!         SlackBlockButtonElement::new("rollback".into(), pt!("Rollback"))
78//!             .with_style(SlackBlockButtonStyle::Danger),
79//!         SlackBlockButtonElement::new("details".into(), pt!("View details")),
80//!     ]),
81//!     SlackRichTextBlock::new(vec![SlackRichTextSection::new(vec![
82//!         "Triggered by ".into(),
83//!         SlackRichTextText::new("the release bot".to_string()).bold().into(),
84//!     ])
85//!     .into()]),
86//!     SlackTableBlock::new(vec![
87//!         vec!["Service".into(), "Status".into()],
88//!         vec!["api".into(), "healthy".into()],
89//!     ]),
90//!     optionally(show_footer => SlackContextBlock::new(vec![md!("Posted automatically")])),
91//! ];
92//!
93//! let content = SlackMessageContent::new().with_blocks(blocks);
94//! # assert!(content.blocks.is_some());
95//! ```
96//!
97//! See the [`blocks::block_kit`] module docs for a full guide covering text
98//! objects, sections, actions and inputs, rich text, tables, templates, and
99//! views.
100//!
101//! ## Events API and OAuth support for Hyper and Axum
102//!
103//! The library provides two different ways to work with Slack Events API:
104//! - Using pure Hyper-based solution
105//! - Using more high-level solution for axum web framework.
106//!
107//! Also the library provides Slack events signature verifier (`SlackEventSignatureVerifier`)
108//! (which is already integrated in the routes implementation for you).
109//! All you need is provide your client id and secret configuration to route implementation.
110//!
111//! ## Socket Mode support
112//!
113//! The library provides Socket Mode support additionally Events API leveraging Web-sockets
114//! in cases you don't want/need to expose publicly available HTTP endpoint.
115//!
116//! # Docs and examples
117//!
118//! Please follow to the official [website](https://slack-rust.abdolence.dev).
119//! Examples available on: [github](https://github.com/abdolence/slack-morphism-rust/tree/master/examples).
120//!
121
122#![allow(
123    clippy::new_without_default,
124    clippy::needless_lifetimes,
125    unused_imports
126)]
127
128pub use client::*;
129pub use scroller::*;
130pub use socket_mode::*;
131pub use token::*;
132
133mod models;
134pub use models::*;
135
136pub mod api;
137mod client;
138pub mod errors;
139pub mod listener;
140mod ratectl;
141mod scroller;
142#[cfg(feature = "signature-verifier")]
143pub mod signature_verifier;
144pub mod socket_mode;
145
146pub mod multipart_form;
147mod token;
148
149#[cfg(feature = "hyper-base")]
150pub mod hyper_tokio;
151
152#[cfg(feature = "axum-base")]
153pub mod axum_support;
154
155pub mod prelude;