A fully generated, opinionated API client library for Slack.
One way to interact with the Slack platform is its HTTP RPC-based Web API, a collection of methods requiring OAuth 2.0-based user, bot, or workspace tokens blessed with related OAuth scopes.
name | url |
---|---|
Slack developer relations | https://api.slack.com/support |
This client is generated from the Slack OpenAPI
specs based on API spec version 1.7.0
. This way it will remain
up to date as features are added. The documentation for the crate is generated
along with the code to make this library easy to use.
To install the library, add the following to your Cargo.toml
file.
[dependencies]
slack-chat-api = "0.3.1"
Typical use will require intializing a Client
. This requires
a user agent string and set of credentials.
use slack_chat_api::Client;
let slack = Client::new(
String::from("client-id"),
String::from("client-secret"),
String::from("redirect-uri"),
String::from("token"),
String::from("refresh-token")
);
Alternatively, the library can search for most of the variables required for the client in the environment:
SLACK_CLIENT_ID
SLACK_CLIENT_SECRET
SLACK_REDIRECT_URI
And then you can create a client from the environment.
use slack_chat_api::Client;
let slack = Client::new_from_env(
String::from("token"),
String::from("refresh-token")
);
It is okay to pass empty values for token
and refresh_token
. In
the initial state of the client, you will not know these values.
To start off a fresh client and get a token
and refresh_token
, use the following.
use slack_chat_api::Client;
async fn do_call() {
let mut slack = Client::new_from_env("", "");
// Get the URL to request consent from the user.
// You can optionally pass in scopes. If none are provided, then the
// resulting URL will not have any scopes.
let user_consent_url = slack.user_consent_url(&["some-scope".to_string()]);
// In your redirect URL capture the code sent and our state.
// Send it along to the request for the token.
let code = "thing-from-redirect-url";
let state = "state-from-redirect-url";
let mut access_token = slack.get_access_token(code, state).await.unwrap();
// You can additionally refresh the access token with the following.
// You must have a refresh token to be able to call this function.
access_token = slack.refresh_access_token().await.unwrap();
}