Skip to content

Instantly share code, notes, and snippets.

@karagraysen
Created August 28, 2020 19:24
Show Gist options
  • Save karagraysen/819cab95107645715604bdaee2cbc782 to your computer and use it in GitHub Desktop.
Save karagraysen/819cab95107645715604bdaee2cbc782 to your computer and use it in GitHub Desktop.
Slack Slash Command Hello World in Rust using Rocket.
#![feature(proc_macro_hygiene, decl_macro)]
#[macro_use] extern crate rocket;
use rocket::request::Form;
#[derive(FromForm)]
pub struct Parameters {
// Payload fields from "Preparing your app to receive Commands" section in documentation:
// https://api.slack.com/interactivity/slash-commands#app_command_handling.
pub token: String,
pub team_id: String,
pub team_domain: String,
pub channel_id: String,
pub channel_name: String,
pub user_id: String,
pub user_name: String,
pub command: String,
pub text: String,
pub response_url: String,
pub trigger_id: String,
pub api_app_id: String,
}
#[post("/", data = "<input>")]
fn command(input: Form<Parameters>) -> String {
// Unwrap inner object.
let input_inner = input.into_inner();
// Say hello to user.
format!("Hello {}", input_inner.user_name)
}
#[catch(404)]
fn not_found() -> &'static str {
// Catch all in case someone goes to the URL directly.
"You are using this tool incorrectly. Please use it through the command line or Slack."
}
fn main() {
// Initialize Rocket, mount root route and register 404 catcher.
rocket::ignite().mount("/", routes![command]).register(catchers![not_found]).launch();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment