#slack #webhook #messaging #hook

slack-hook

A rust crate for sending messages to Slack via webhooks

14 releases (8 breaking)

Uses old Rust 2015

0.8.0 Oct 23, 2018
0.6.0 Sep 26, 2017
0.5.0 Jul 24, 2017
0.3.0 Feb 26, 2017
0.0.1 Dec 30, 2014

#1664 in Web programming

Download history 765/week @ 2024-07-22 1360/week @ 2024-07-29 1082/week @ 2024-08-05 1131/week @ 2024-08-12 1255/week @ 2024-08-19 1321/week @ 2024-08-26 1135/week @ 2024-09-02 1479/week @ 2024-09-09 1107/week @ 2024-09-16 1007/week @ 2024-09-23 1120/week @ 2024-09-30 1065/week @ 2024-10-07 1125/week @ 2024-10-14 1009/week @ 2024-10-21 1315/week @ 2024-10-28 1315/week @ 2024-11-04

4,779 downloads per month
Used in 6 crates (5 directly)

MIT/Apache

41KB
882 lines

rust-slack

Travis Build Status 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.

Requires rust 1.17 or newer.

Usage

Add this to your Cargo.toml:

[dependencies]
slack-hook = "0.7"

Add the crate to your existing project:

extern crate slack_hook;
use slack_hook::{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

~22–30MB
~531K SLoC