#slack #messaging #webhook

slack-messaging

Support building messages for Slack Messaging API

4 releases

0.2.2 Mar 2, 2023
0.2.1 Feb 28, 2023
0.2.0 Feb 27, 2023
0.1.0 Feb 26, 2023

#1326 in Web programming

Download history 7/week @ 2023-12-01 12/week @ 2023-12-08 19/week @ 2023-12-15 10/week @ 2023-12-22 18/week @ 2024-01-19 28/week @ 2024-01-26 13/week @ 2024-02-02 26/week @ 2024-02-16 39/week @ 2024-02-23 26/week @ 2024-03-01 30/week @ 2024-03-08 124/week @ 2024-03-15

224 downloads per month

MIT license

385KB
3.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::Message;
use slack_messaging::blocks::{elements::Button, Actions, Section};

#[tokio::main]
async fn main() {
    let message = Message::new()
        .push_block(
            Section::new()
                .set_text_mrkdwn("You have a new request:\n*<fakeLink.toEmployeeProfile.com|Fred Enriquez - New device request>*")
        )
        .push_block(
            Section::new()
                .push_field_mrkdwn("*Type:*\nComputer (laptop)")
                .push_field_mrkdwn("*When:*\nSubmitted Aut 10")
        )
        .push_block(
            Actions::new()
                .push_element(
                    Button::new()
                        .text("Approve")
                        .set_value("approve")
                        .set_primary()
                )
                .push_element(
                    Button::new()
                        .text("Deny")
                        .set_value("deny")
                        .set_danger()
                )
        );

    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 Aut 10"
                }
            ]
        },
        {
            "type": "actions",
            "elements": [
                {
                    "type": "button",
                    "text": {
                        "type": "plain_text",
                        "text": "Approve",
                        "emoji": true
                    },
                    "value": "approve",
                    "style": "primary"
                },
                {
                    "type": "button",
                    "text": {
                        "type": "plain_text",
                        "text": "Deny",
                        "emoji": true
                    },
                    "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::DateFormat;

let dt = DateTime::parse_from_rfc3339("2023-02-27T12:34:56+09:00").unwrap();
let f = DateFormat::new(dt).token("{date_short} at {time}");

assert_eq!(
    format!("{f}"),
    "<!date^1677468896^{date_short} at {time}|Feb 27, 2023 at 12:34 PM>"
);

License

This software is released under the MIT License.

Dependencies

~0.4–1.8MB
~36K SLoC