#slack #webhook #messaging #hook #codebase

slack-hook2

A rust crate for sending messages to Slack via webhooks

3 unstable releases

0.10.1 Nov 25, 2022
0.10.0 Mar 5, 2020
0.9.0 Feb 19, 2020

#1156 in Web programming

Download history 51/week @ 2024-08-05 52/week @ 2024-08-12 46/week @ 2024-08-19 163/week @ 2024-08-26 226/week @ 2024-09-02 20/week @ 2024-09-09 230/week @ 2024-09-16 203/week @ 2024-09-23 109/week @ 2024-09-30 375/week @ 2024-10-07 303/week @ 2024-10-14 287/week @ 2024-10-21 284/week @ 2024-10-28 84/week @ 2024-11-04 53/week @ 2024-11-11 141/week @ 2024-11-18

565 downloads per month
Used in 2 crates

MIT/Apache

39KB
826 lines

slack-hook2

crates.io MIT licensed Apache licensed

A rust crate for sending messages to Slack via webhooks. A modernized fork of the slack-hook 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.

NOTE: Slack webhooks are deprecated. Use this crate only for legacy codebases.

Usage

Add this to your Cargo.toml:

[dependencies]
slack-hook2 = "0.10.1"

Add the crate to your existing project:

use slack_hook2::{Slack, PayloadBuilder};

#[tokio::main]
async 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).await;
    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

~5–17MB
~232K SLoC