Slack Morphism for Rust
Slack Morphism is a modern client library for Slack Web/Events API and Block Kit.
Intro and motivation
- Type-safety: All of the models, API and Block Kit support in Slack Morphism is well-typed.
- Easy to use: The library depends only on familiar for Rust developers principles and libraries like Serde, hyper, futures.
- Async: Using latest Rust async/.await language features and libraries, the library provides access to all of the functions in asynchronous manner
Getting Started
Cargo.toml dependencies example:
[]
="0.1"
="0.1"
All imports you need:
use *; // access to network/client functions
use *; // Slack Web API methods (chat, users, views, etc)
use *; // Slack Events API listener (routes) implementation
use *; // common Slack models like SlackUser, etc and macros
use *; // Slack Block Kit models
use *; // Slack Events Models
Slack Web API client
Create a client instance:
use *;
let client = new;
Make Web API methods calls
For most of Slack Web API methods (except for OAuth methods, Incoming Webhooks and event replies) you need a Slack token to make a call. For simple bots you can have it in your config files, or you can obtain workspace tokens using Slack OAuth.
In the example below, we’re using a hardcoded Slack token, but don’t do that for your production bots and apps. You should securely and properly store all of Slack tokens.
use *;
use *;
use *;
async
Pagination support
Some Web API methods defines cursors and pagination, to give you an ability to load a lot of data continually (using batching and continually making many requests).
Examples: conversations.history
, conversations.list
, users.list
, ...
To help with those methods Slack Morphism provides additional a “scroller” implementation, which deal with all scrolling/batching requests for you.
For example for users.list
:
use *;
use *;
use *;
use Duration;
async
Block Kit support
To support Slack Block Kit rich messages and views, the library provides:
- Well-typed models
- Macros to help build blocks, block elements
Let’s take some very simple block example:
Now, let’s look at how it looks with type-safe code using Slack Morphism Blocks macro support:
use *;
use *;
let blocks : = slack_blocks!;
Let’s look at another more complex example for welcoming message:
use *;
use *;
use *;
use *;
async
Look other examples in examples/templates.rs
Events API and OAuth support
The library provides route implementation in SlackClientEventsListener
based on Hyper/Tokio for:
- Push Events
- Interaction Events
- Command Events
- OAuth v2 redirects and client functions
You can chain all of the routes using chain_service_routes_fn
from the library.
Also the library provides Slack events signature verifier (SlackEventSignatureVerifier
)
(which is already integrated in the routes implementation for you).
All you need is provide your client id and secret configuration to route implementation.
Look at the examples/test_server sources for the details.
Limitations
Slack Morphism doesn't provide:
- RTM API (the usage of which is slowly declining in favour of Events API)
- Legacy Web/Events API methods and models (like Slack Message attachments, which should be replaced with Slack Blocks)