#slack #webhook #messaging #hook

slack-hook3

A rust crate for sending messages to Slack via webhooks

1 unstable release

0.11.0 Jul 15, 2021

#34 in #slack

Download history 74/week @ 2024-03-15 25/week @ 2024-03-22 48/week @ 2024-03-29 18/week @ 2024-04-05 6/week @ 2024-04-12 15/week @ 2024-04-19 25/week @ 2024-04-26 11/week @ 2024-05-03 14/week @ 2024-05-10 17/week @ 2024-05-17 23/week @ 2024-05-24 12/week @ 2024-05-31 24/week @ 2024-06-07 93/week @ 2024-06-14 125/week @ 2024-06-21 40/week @ 2024-06-28

283 downloads per month
Used in swook

MIT/Apache

39KB
826 lines

slack-hook3

crates.io MIT licensed Apache licensed

A rust crate for sending messages to Slack via webhooks. A modernized fork of the slack-hook2 crate. Happy to merge things back in the future.

Slack is a messaging platform for team collaboration.

Upgrading? See the CHANGELOG.

Requires rust 1.35 or newer.

Usage

Add this to your Cargo.toml:

[dependencies]
slack-hook3 = "0.11"

Add the crate to your existing project:

use slack_hook3::{Slack, PayloadBuilder};

fn main() {
    let slack = Slack::new("https://hooks.slack.com/services/abc/123/45z").unwrap();
    let p = PayloadBuilder::new()
      .text("test message")
      .channel("#testing")
      .username("My Bot")
      .icon_emoji(":chart_with_upwards_trend:")
      .build()
      .unwrap();

    let res = slack.send(&p);
    match res {
        Ok(()) => println!("ok"),
        Err(x) => println!("ERR: {:?}",x)
    }
}

Attachments

To create a payload with just an attachment:

extern crate slack_hook;
use slack_hook::{PayloadBuilder, AttachmentBuilder};

fn main() {
  let _ = PayloadBuilder::new()
    .attachments(vec![AttachmentBuilder::new("my text").color("#b13d41").build().unwrap()])
    .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:

extern crate slack_hook;
use slack_hook::{PayloadBuilder, SlackTextContent, SlackLink};
use slack_hook::SlackTextContent::{Text, Link};

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

Sending this payload will print 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

~4–19MB
~259K SLoC