Crates.io | slack-hook |
lib.rs | slack-hook |
version | |
source | src |
created_at | 2014-12-30 02:39:05.349285 |
updated_at | 2025-02-13 00:47:59.144768 |
description | A rust crate for sending messages to Slack via webhooks. |
homepage | |
repository | https://github.com/frostly/rust-slack |
max_upload_size | |
id | 665 |
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` |
size | 0 |
A rust crate for sending messages to Slack via webhooks.
Slack is a messaging platform for team collaboration.
Upgrading? See the CHANGELOG.
native-tls
rustls
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:?}")
}
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.
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.