slack-hook

Crates.ioslack-hook
lib.rsslack-hook
version
sourcesrc
created_at2014-12-30 02:39:05.349285
updated_at2025-02-13 00:47:59.144768
descriptionA rust crate for sending messages to Slack via webhooks.
homepage
repositoryhttps://github.com/frostly/rust-slack
max_upload_size
id665
Cargo.toml error:TOML parse error at line 19, column 1 | 19 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include`
size0
CosmicHorror (CosmicHorrorDev)

documentation

README

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();

Text with Links

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.

Commit count: 245

cargo fmt