#slack #slash-command #oauth #framework #api-client #blocks #create

slack-framework-rs

slack-framework-rs is the library that the author uses to create the Slack App

2 releases

0.1.1 Oct 1, 2024
0.1.0 Sep 4, 2024

#1118 in Web programming

25 downloads per month

MIT/Apache

2.5MB
9K SLoC

slack-framework-rs

slack-framework-rs is the library that the author uses to create the Slack App.

Since it is for its own use, it currently provides only limited features, but it is strictly omitted from app-specific processing, so I think others can use it as well. However, there are some processes that have not been tested very well, so there may be things that do not work or do not represent. Issues and Merge Requests on GitLab are welcome.

Features

TODO (what I know)

Example

use slack_framework_rs::core as sc;
use slack_framework_rs::server as ss;
use slack_framework_rs::slack_api as ssa;
use slack_framework_rs::types as st;

/// The structure for implementing handler traits.
struct Handler;

#[tokio::main]
async fn main() -> Result<(), st::Error> {
    // Server settings
    let server_config = ss::Config::builder()
        .sock("127.0.0.1:3000".parse().unwrap())
        .build();

    // Slack App settings
    let slack_creds = sc::AppCredentials {
        id: "Client ID".into(),
        secret: "Client Secret".into(),
        signing_secret: "Signing Secret".into(),
        verification_token: "Verification Token".into(),
    };
    let slack_config = sc::Config {
        timestamp_range: 300,
    };
    let slack = Slack::new(slack_creds, slack_config);

    // HTTP Client
    let client = reqwest::Client::new();

    // Starts serving
    Server::new(
        server_config,
        slack,
        client,
        Handler,
        Handler,
        Handler,
    )
    .serve()
    .await
}

Implementations of handlers

/// Implements a process of slash commands.
impl ss::SlashCommandHandlerTrait for Handler {
    async fn handle_slash_command(
        &self,
        _client: &Client,
        body: SlashCommandBody,
    ) -> Result<st::SlashCommandResponse, ResponseError> {
        println!("body: {body:?}");
        Ok(st::SlashCommandResponse::empty().into())
    }
}

/// Implements a process of OAuth.
impl ss::OauthHandlerTrait for Handler {
    async fn handle_oauth(
        &self,
        _client: reqwest::Client,
        body: OauthV2AccessResponse,
    ) -> Result<Option<OauthRedirectResponse>, ResponseError> {
        println!("body: {body:?}");
        Ok(None)
    }

    async fn take_oauth_token_from_team_id(&self, _team_id: &str) -> Result<String, ResponseError> {
        Ok("token".into())
    }

    fn redirect_uri(&self) -> &str {
        Ok("redirect_uri".into())
    }
}

/// Implements to return the home page.
/// The home page should have an `Add to Slack` button.
impl HomeHandlerTrait for Handler {
    async fn handle_home(&self) -> Result<Vec<u8>, ResponseError> {
        const RESPONSE: &str = r#"<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title></title></head><body>ok</body></html>"#;
        Ok(RESPONSE.into())
    }
}

Documents

Dependencies

~14–28MB
~399K SLoC