#slack #webhook #hook #api-bindings

slack-hook

A rust crate for sending messages to Slack via webhooks

17 releases (9 breaking)

0.9.1 Feb 13, 2025
0.8.0 Oct 23, 2018
0.6.0 Sep 26, 2017
0.5.0 Jul 24, 2017
0.0.1 Dec 30, 2014

#47 in Web programming

Download history 412/week @ 2024-12-25 713/week @ 2025-01-01 1021/week @ 2025-01-08 1381/week @ 2025-01-15 1648/week @ 2025-01-22 1427/week @ 2025-01-29 2698/week @ 2025-02-05 1933/week @ 2025-02-12 1562/week @ 2025-02-19 1506/week @ 2025-02-26 1916/week @ 2025-03-05 1741/week @ 2025-03-12 2242/week @ 2025-03-19 1724/week @ 2025-03-26 1385/week @ 2025-04-02 1096/week @ 2025-04-09

6,691 downloads per month
Used in 7 crates (6 directly)

MIT/Apache

42KB
875 lines

slack-hook

CI Documentation crates.io MIT licensed Apache licensed

A rust crate for sending messages to Slack via webhooks.

Slack is a messaging platform for team collaboration.

Upgrading? See the CHANGELOG.

Features

  • blocking: Provides a synchronous "blocking" slack client
  • default-tls (enabled by default): Provides TLS support to connect over HTTPS
  • native-tls: Enables TLS functionality provided by native-tls
  • rustls-tls: Enables TLS functionality provided by rustls

Usage

Simply run this to add it to your Cargo.toml:

cargo add slack-hook --features=blocking

and then start sending messages!

use slack_hook::{blocking::Slack, PayloadBuilder};

let slack = Slack::new("https://hooks.slack.com/services/abc/123/45z").unwrap();
let payload = PayloadBuilder::new()
    .text("test message")
    .channel("#testing")
    .username("My Bot")
    .icon_emoji(":chart_with_upwards_trend:")
    .build()
    .expect("we know this payload is valid");

match slack.send(&payload) {
    Ok(()) => println!("Message sent!"),
    Err(err) => eprintln!("Error: {err:?}")
}

Attachments

To create a payload with just an attachment:

use slack_hook::{PayloadBuilder, AttachmentBuilder};

let attachment = AttachmentBuilder::new("my text")
    .color("#b13d41")
    .build()
    .unwrap();
let _payload = PayloadBuilder::new()
    .attachments(vec![attachment])
    .build()
    .unwrap();

Slack messaging API permits you to send links within text. However, given the different formatting rules, these text fragments need to be specified as follows:

use slack_hook::{PayloadBuilder, SlackTextContent, SlackLink};

let text = [
    SlackTextContent::Text("Hello".into()),
    SlackTextContent::Link(SlackLink::new("https://google.com", "Google")),
    SlackTextContent::Text(", nice to know you.".into())
];
let _ = PayloadBuilder::new()
.text(text.as_slice())
.build()
.unwrap();

Sending this payload will display the following in slack (note: each element of the Vec has been space-separated):

      Hello Google, nice to know you.

This technique can be used for any function that has the Into<SlackText> trait bound.

License

This library is distributed under similar terms to Rust: dual licensed under the MIT license and the Apache license (version 2.0).

See LICENSE-APACHE, LICENSE-MIT, and COPYRIGHT for details.

Dependencies

~5–21MB
~338K SLoC