This Slack bot is made possible by the Slack crate, a Rust library that makes interfacing with the Slack API within Rust easy(-ier). Without the Slack crate, one would have to use one of the many HTTP libraries for Rust (or even make one! 😱) to make HTTP requests to the various endpoints that the Slack API offers, but this isn't as easy as using the pre-made Slack crate in my opinion. Unfortunately, this comes with the trade-off that the Slack crate only works with Slack's "classic apps," which are deprecated. Therefore, people wanting a write a Slack bot in Rust will have to make a decision: whether they want to make a Slack botusing an easier yet deprectaed method, in which case they should follow these instructions; or whether they want to use one of Rust's HTTP libraries instead to manually make all of the API calls.
Here are the steps I followed for setting up a Slack bot to work with the Slack crate:
- Join the Slack workspace that you want your bot to run in.
- Go to "https://api.slack.com/rtm#classic" and click "Create a classic Slack app".
- Enter a name for your Slack bot and ensure you create it in the workspace that you joined in Step 1.
- On the Basic Information page (see sidebar at left), under "Add features and functionality", click "Bots".
- Click "Add Legacy Bot User".
- Enter a display name and default userame for your bot.
- Click "add".
- Go back to the Basic Information page, under "Features and Functionality", click "Event Subscriptions".
- Turn the switch in the upper right on.
cargo new project_name
.- Make your Cargo.toml look like this:
[package]
name = <whatever you named your project>
description = <however you want to describe this project>
version = "0.1.0" # Doesn't literally have to be 0.1.0 but there also isn't a reason for you to change this (right now)
authors = ["Your Name <youremail@website.com>"]
# See more keys and their definitions...
[features]
actix = [ "actix-web", "acxtix-rt", "serde" ]
[[bin]]
name = "verify"
path = "src/verify.rs"
required-features = [ "actix" ]
[dependencies]
# These versions are probably out of date but I know they work, so I'm hesitant to upgrade.
# You can see for yourself what the latest versions are and try and see if it works with the latest version at crates.io
serde = { version = "1.0.104", features = [ "derive" ], optional = true }
actix-web = { version = "2.0.0", optional = true }
actix-rt = { version = "1.0.0", optional = true }
- Create a file called
verify.rs
in thesrc
directory with the following contents:
//! A "script" used to validate the Slack Events API.
//!
//! Run ngrok with: `ngrok http PORT` (see PORT below)
use std::{io, net::Ipv4Addr};
use actix_web::{web, App, HttpResponse, HttpServer, Responder};
use serde::Deserialize;
/// Which port number the host is bound to
const PORT: u16 = 3152;
#[derive(Deserialize, Debug)]
struct Payload {
token: String,
challenge: String,
r#type: String,
}
#[actix_rt::main]
async fn main() -> io::Result<()> {
// Which public url to connect to
let socket_addr = (Ipv4Addr::LOCALHOST, PORT);
// Let's set up a web server!
HttpServer::new(|| App::new().route("/slack/events", web::post().to(post_handler)))
.bind(socket_addr)?
.run()
.await
}
async fn post_handler(web::Json(response): web::Json<Payload>) -> impl Responder {
println!("{:?}", response);
HttpResponse::Ok().body(response.challenge)
}
- Install ngrok. On macOS with Homebrew you can
brew install --cask ngrok
. ngrok http 3152
.cargo run --bin verify --features=actix
in another terminal window. You will see lots of things compiling.- With both ngrok and the verify program running in the background, go to the terminal window with ngrok running. You will see something like "Forwarding https://<some code>.ngrok.io -> http://localhost:3152". Copy the https://<come code>.ngrok.io URL and go back to the Event Subscriptions web page in your browser. Paste the URL into the text field labelled "Request URL." Then add "/slack/events" to the end of this URL. So overall the URL in the "Request URL" text field should be "https://<some code>.ngrok.io/slack/events." You will see a green "verified" checkmark.
- You can kill ngrok and your verify program.
- Click on "Subscribe to bot events".
- Click "Add Bot User Event".
- Add "app_mention" and "message.channels".
- Click "Save Changes" at the bottom of your screen.
- Go back to the Basic Information Page, under "Install app to your workspace" click "Install App to Workspace".
- Click "allow" .
- To get your API token, on the Install App page (see sidebar at left) copy the string of letters and numbers under "Bot User OAuth Access Token".