https://github.com/frostly/rust-slack
A rust crate for sending messages to Slack via webhooks
https://github.com/frostly/rust-slack
rust slack webhook
Last synced: 6 days ago
JSON representation
A rust crate for sending messages to Slack via webhooks
- Host: GitHub
- URL: https://github.com/frostly/rust-slack
- Owner: frostly
- License: other
- Created: 2014-08-07T02:02:50.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2022-09-03T12:05:25.000Z (almost 3 years ago)
- Last Synced: 2024-10-07T22:37:38.454Z (9 months ago)
- Topics: rust, slack, webhook
- Language: Rust
- Homepage: https://docs.rs/slack-hook/
- Size: 3.63 MB
- Stars: 167
- Watchers: 7
- Forks: 49
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# slack-hook
[](https://github.com/frostly/rust-slack/actions/workflows/basic.yml)
[](https://docs.rs/slack-hook/)
[](https://crates.io/crates/slack-hook)
[](./LICENSE-MIT)
[](./LICENSE-APACHE)A rust crate for sending messages to Slack via webhooks.
[Slack](https://slack.com/) is a messaging platform for team collaboration.
Upgrading? See the [CHANGELOG](./CHANGELOG.md).
# 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`](https://crates.io/crates/native-tls)
- **rustls-tls**: Enables TLS functionality provided by [`rustls`](https://crates.io/crates/rustls)# Usage
Simply run this to add it to your `Cargo.toml`:
```console
cargo add slack-hook --features=blocking
```and then start sending messages!
```rust,no_run
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:
```rust
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:```rust
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](https://google.com), nice to know you.
This technique can be used for any function that has the `Into`
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-APACHE), [LICENSE-MIT](LICENSE-MIT), and
[COPYRIGHT](COPYRIGHT) for details.