#slack #messaging #webhook

slack-messaging

Support building messages for Slack Messaging API

8 releases

new 0.3.3 Dec 26, 2024
0.3.2 Aug 30, 2024
0.3.1 Apr 9, 2024
0.2.2 Mar 2, 2023
0.1.0 Feb 26, 2023

#294 in Web programming

Download history 667/week @ 2024-09-02 691/week @ 2024-09-09 493/week @ 2024-09-16 31/week @ 2024-09-23 155/week @ 2024-09-30 31/week @ 2024-10-07 26/week @ 2024-10-14 79/week @ 2024-10-21 48/week @ 2024-10-28 118/week @ 2024-11-04 34/week @ 2024-11-11 36/week @ 2024-11-18 8/week @ 2024-11-25 37/week @ 2024-12-02 35/week @ 2024-12-09 17/week @ 2024-12-16

100 downloads per month
Used in 2 crates

MIT license

685KB
6.5K SLoC

Slack Messaging

Version License Test

This is a library for Rust to support building messages for slack messaging api. Using this, you can build any messages in type-safe way like following.

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*<fakeLink.toEmployeeProfile.com|Fred Enriquez - New device request>*"))
                .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.

{
    "blocks": [
        {
            "type": "section",
            "text": {
                "type": "mrkdwn",
                "text": "You have a new request:\n*<fakeLink.toEmployeeProfile.com|Fred Enriquez - New device request>*"
            }
        },
        {
            "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 that can be enabled or disabled.

fmt

Enable fmt module and format messages in this way.

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),
    "<!date^1677468896^{date_short} at {time}|Feb 27, 2023 at 12:34 PM>"
);

// You can also set optional link when formatting.
assert_eq!(
    f.format_with_link(&dt, "https://example.com"),
    "<!date^1677468896^{date_short} at {time}^https://example.com|Feb 27, 2023 at 12:34 PM>"
);

// 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),
    "<!date^1677468896^{date_short} at {time}^https://example.com|Feb 27, 2023 at 12:34 PM>"
);

License

This software is released under the MIT License.

Dependencies

~0.7–2.4MB
~47K SLoC