https://github.com/kaicoh/slack-messaging
A library to build messages for slack app.
https://github.com/kaicoh/slack-messaging
incoming-webhook slack slack-api webhook
Last synced: 11 months ago
JSON representation
A library to build messages for slack app.
- Host: GitHub
- URL: https://github.com/kaicoh/slack-messaging
- Owner: kaicoh
- License: mit
- Created: 2023-02-22T14:10:14.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2025-06-22T06:54:06.000Z (about 1 year ago)
- Last Synced: 2025-06-22T07:29:49.721Z (about 1 year ago)
- Topics: incoming-webhook, slack, slack-api, webhook
- Language: Rust
- Homepage:
- Size: 310 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Slack Messaging
[](https://crates.io/crates/slack-messaging)
[](LICENSE)
[](https://github.com/kaicoh/slack-messaging/actions/workflows/test.yml)
This is a library for [Rust](https://www.rust-lang.org/) to support building [Slack Block Kit messages](https://docs.slack.dev/reference/block-kit).
Using this, you can build any messages in type-safe way like following.
```rust
use slack_messaging::{mrkdwn, Message};
use slack_messaging::blocks::{elements::Button, Actions, Section};
#[tokio::main]
async fn main() {
let message = Message::builder()
.block(
Section::builder()
.text(mrkdwn!("You have a new request:\n**"))
.build()
)
.block(
Section::builder()
.field(mrkdwn!("*Type:*\nComputer (laptop)"))
.field(mrkdwn!("*When:*\nSubmitted Aug 10"))
.build()
)
.block(
Actions::builder()
.element(
Button::builder()
.text("Approve")
.value("approve")
.primary()
.build()
)
.element(
Button::builder()
.text("Deny")
.value("deny")
.danger()
.build()
)
.build()
)
.build();
let req = reqwest::Client::new()
.post("https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX")
.json(&message);
if let Err(err) = req.send().await {
eprintln!("{err}");
}
}
```
The message payload of the above example is following.
```json
{
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "You have a new request:\n**"
}
},
{
"type": "section",
"fields": [
{
"type": "mrkdwn",
"text": "*Type:*\nComputer (laptop)"
},
{
"type": "mrkdwn",
"text": "*When:*\nSubmitted Aug 10"
}
]
},
{
"type": "actions",
"elements": [
{
"type": "button",
"text": {
"type": "plain_text",
"text": "Approve"
},
"value": "approve",
"style": "primary"
},
{
"type": "button",
"text": {
"type": "plain_text",
"text": "Deny"
},
"value": "deny",
"style": "danger"
}
]
}
]
}
```
## Optional Features
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.
### fmt
Enable `fmt` module and format messages in [this way](https://docs.slack.dev/messaging/formatting-message-text#date-formatting).
```rust
use chrono::prelude::*;
use slack_messaging::fmt::DateFormatter;
// Formatter without optional link.
let f = DateFormatter::builder()
.token("{date_short} at {time}")
.build();
let dt = DateTime::parse_from_rfc3339("2023-02-27T12:34:56+09:00").unwrap();
assert_eq!(
f.format(&dt),
""
);
// You can also set optional link when formatting.
assert_eq!(
f.format_with_link(&dt, "https://example.com"),
""
);
// Formatter with optional link.
let f = DateFormatter::builder()
.token("{date_short} at {time}")
.link("https://example.com")
.build();
// This time, format method returns text with link set to the formatter.
assert_eq!(
f.format(&dt),
""
);
```
## License
This software is released under the [MIT License](LICENSE).