Created
August 28, 2020 19:24
-
-
Save istx25/819cab95107645715604bdaee2cbc782 to your computer and use it in GitHub Desktop.
Slack Slash Command Hello World in Rust using Rocket.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![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