Skip to main content

Crate slack_morphism

Crate slack_morphism 

Source
Expand description

§Slack Morphism for Rust

Slack Morphism is a modern client library for Slack Web/Events API and Block Kit.

§Slack Web API client

§Create a client instance:

ⓘ
use slack_morphism::prelude::*;

let client = SlackClient::new(SlackClientHyperConnector::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 slack_morphism::prelude::*;


let client = SlackClient::new(SlackClientHyperConnector::new());

// Create our Slack API token
let token_value: SlackApiTokenValue = "xoxb-89.....".into();
let token: SlackApiToken = SlackApiToken::new(token_value);

// Create a Slack session with this token
// A session is just a lightweight wrapper around your token
// not to specify it all the time for series of calls.
let session = client.open_session(&token);

// Make your first API call (which is `api.test` here)
let test: SlackApiTestResponse = session
        .api_test(&SlackApiTestRequest::new().with_foo("Test".into()))
        .await?;

// Send a simple text message
let post_chat_req =
    SlackApiChatPostMessageRequest::new("#general".into(),
           SlackMessageContent::new().with_text("Hey there!".into())
    );

let post_chat_resp = session.chat_post_message(&post_chat_req).await?;

§Block Kit

Blocks, block elements, and views are modeled as typed structs and enums, with the slack_blocks! macro (and md!/pt! for text objects) to build the block lists they hold:

use slack_morphism::prelude::*;

let show_footer = true;

let blocks: Vec<SlackBlock> = slack_blocks![
    SlackHeaderBlock::new(pt!("Deploy report")),
    SlackSectionBlock::new()
        .with_text(md!("Deployed *{}* to {}", "api", "us-east-1"))
        .with_fields(vec![md!("*Duration:*\n42s"), md!("*Result:*\nsuccess")]),
    SlackActionsBlock::new(slack_blocks![
        SlackBlockButtonElement::new(pt!("Rollback"))
            .with_action_id("rollback".into())
            .with_style(SlackBlockButtonStyle::Danger),
        SlackBlockButtonElement::new(pt!("View details")).with_action_id("details".into()),
    ]),
    SlackRichTextBlock::new(vec![SlackRichTextSection::new(vec![
        "Triggered by ".into(),
        SlackRichTextText::new("the release bot".to_string()).bold().into(),
    ])
    .into()]),
    SlackTableBlock::new(vec![
        vec!["Service".into(), "Status".into()],
        vec!["api".into(), "healthy".into()],
    ]),
    optionally(show_footer => SlackContextBlock::new(vec![md!("Posted automatically")])),
];

let content = SlackMessageContent::new().with_blocks(blocks);

See the blocks::block_kit module docs for a full guide covering text objects, sections, actions and inputs, rich text, tables, templates, and views.

§Events API and OAuth support for Hyper and Axum

The library provides two different ways to work with Slack Events API:

  • Using pure Hyper-based solution
  • Using more high-level solution for axum web framework.

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.

§Socket Mode support

The library provides Socket Mode support additionally Events API leveraging Web-sockets in cases you don’t want/need to expose publicly available HTTP endpoint.

§Docs and examples

Please follow to the official website. Examples available on: github.

Re-exports§

pub use socket_mode::*;

Modules§

api
apps
blocks
comments
errors
events
files
listener
manifest
multipart_form
prelude
socket_mode

Macros§

md
Builds a SlackBlockMarkDownText, converted into the caller’s target type via Into.
pt
Builds a SlackBlockPlainText, converted into the caller’s target type via Into.
slack_block_item
slack_blocks
Builds a Vec<T> of block-kit items from a comma-separated list, in one of two syntaxes that may be freely mixed within a single list:

Structs§

EmailAddress
PhoneNumber
SLACK_HTTP_EMPTY_GET_PARAMS
SlackActionId
SlackActionType
SlackApiCookieValue
SlackApiResponseScrollerState
SlackApiToken
SlackApiTokenInit
Required fields of SlackApiToken; convert with SlackApiToken::from or .into().
SlackApiTokenScope
SlackApiTokenValue
SlackAppContextEntityType
SlackAppCredentials
SlackAppCredentialsInit
Required fields of SlackAppCredentials; convert with SlackAppCredentials::from or .into().
SlackAppId
SlackAppManifest
SlackAppManifestDisplayInformation
SlackAppManifestDisplayInformationInit
Required fields of SlackAppManifestDisplayInformation; convert with SlackAppManifestDisplayInformation::from or .into().
SlackAppManifestFeatures
SlackAppManifestFeaturesAppHome
SlackAppManifestFeaturesAppHomeInit
Required fields of SlackAppManifestFeaturesAppHome; convert with SlackAppManifestFeaturesAppHome::from or .into().
SlackAppManifestFeaturesBotUser
SlackAppManifestFeaturesBotUserInit
Required fields of SlackAppManifestFeaturesBotUser; convert with SlackAppManifestFeaturesBotUser::from or .into().
SlackAppManifestFeaturesInit
Required fields of SlackAppManifestFeatures; convert with SlackAppManifestFeatures::from or .into().
SlackAppManifestFeaturesShortcut
SlackAppManifestFeaturesShortcutInit
Required fields of SlackAppManifestFeaturesShortcut; convert with SlackAppManifestFeaturesShortcut::from or .into().
SlackAppManifestFeaturesSlashCommand
SlackAppManifestFeaturesSlashCommandInit
Required fields of SlackAppManifestFeaturesSlashCommand; convert with SlackAppManifestFeaturesSlashCommand::from or .into().
SlackAppManifestFeaturesWorkflowStep
SlackAppManifestFeaturesWorkflowStepInit
Required fields of SlackAppManifestFeaturesWorkflowStep; convert with SlackAppManifestFeaturesWorkflowStep::from or .into().
SlackAppManifestInit
Required fields of SlackAppManifest; convert with SlackAppManifest::from or .into().
SlackAppManifestMetadata
SlackAppManifestMetadataInit
Required fields of SlackAppManifestMetadata; convert with SlackAppManifestMetadata::from or .into().
SlackAppManifestOAuthConfig
SlackAppManifestOAuthConfigInit
Required fields of SlackAppManifestOAuthConfig; convert with SlackAppManifestOAuthConfig::from or .into().
SlackAppManifestOAuthConfigScopes
SlackAppManifestOAuthConfigScopesInit
Required fields of SlackAppManifestOAuthConfigScopes; convert with SlackAppManifestOAuthConfigScopes::from or .into().
SlackAppManifestSettings
SlackAppManifestSettingsEventSubscriptions
SlackAppManifestSettingsEventSubscriptionsInit
Required fields of SlackAppManifestSettingsEventSubscriptions; convert with SlackAppManifestSettingsEventSubscriptions::from or .into().
SlackAppManifestSettingsInit
Required fields of SlackAppManifestSettings; convert with SlackAppManifestSettings::from or .into().
SlackAppManifestSettingsInteractivity
SlackAppManifestSettingsInteractivityInit
Required fields of SlackAppManifestSettingsInteractivity; convert with SlackAppManifestSettingsInteractivity::from or .into().
SlackAssistantPrompt
SlackAssistantPromptInit
Required fields of SlackAssistantPrompt; convert with SlackAssistantPrompt::from or .into().
SlackAssistantThread
SlackAssistantThreadContext
SlackAssistantThreadContextInit
Required fields of SlackAssistantThreadContext; convert with SlackAssistantThreadContext::from or .into().
SlackAssistantThreadInit
Required fields of SlackAssistantThread; convert with SlackAssistantThread::from or .into().
SlackAvatarHash
SlackBasicChannelInfo
SlackBasicChannelInfoInit
Required fields of SlackBasicChannelInfo; convert with SlackBasicChannelInfo::from or .into().
SlackBasicTeamInfo
SlackBasicTeamInfoInit
Required fields of SlackBasicTeamInfo; convert with SlackBasicTeamInfo::from or .into().
SlackBasicUserInfo
SlackBasicUserInfoInit
Required fields of SlackBasicUserInfo; convert with SlackBasicUserInfo::from or .into().
SlackBotId
SlackBotInfo
SlackBotInfoInit
Required fields of SlackBotInfo; convert with SlackBotInfo::from or .into().
SlackCallbackId
SlackCanvasId
SlackCanvasSectionId
A section inside a canvas, e.g. temp:C:eBa219af721c664422cb90a52fac.
SlackChannelCurrentState
SlackChannelCurrentStateInit
Required fields of SlackChannelCurrentState; convert with SlackChannelCurrentState::from or .into().
SlackChannelDetails
SlackChannelDetailsInit
Required fields of SlackChannelDetails; convert with SlackChannelDetails::from or .into().
SlackChannelFlags
SlackChannelFlagsInit
Required fields of SlackChannelFlags; convert with SlackChannelFlags::from or .into().
SlackChannelId
SlackChannelInfo
SlackChannelInfoInit
Required fields of SlackChannelInfo; convert with SlackChannelInfo::from or .into().
SlackChannelPriority
SlackChannelType
SlackClient
SlackClientApiCallContext
SlackClientHttpApi
SlackClientHttpApiUri
SlackClientHttpSessionApi
SlackClientId
SlackClientMessageId
SlackClientSecret
SlackClientSession
SlackColor
SlackCommandId
SlackConversationId
SlackCursorId
SlackDate
SlackDateTime
SlackEmoji
SlackEmojiName
Represent a Slack custom emoji name without the leading : and trailing :
SlackEnterpriseId
SlackEnterpriseSubteamId
SlackEnterpriseUser
SlackEnterpriseUserInit
Required fields of SlackEnterpriseUser; convert with SlackEnterpriseUser::from or .into().
SlackEnvelopeMessage
SlackEventType
SlackFile
SlackFileCommentId
SlackFileExternalType
SlackFileFlags
SlackFileFlagsInit
Required fields of SlackFileFlags; convert with SlackFileFlags::from or .into().
SlackFileId
SlackFileInit
Required fields of SlackFile; convert with SlackFile::from or .into().
SlackFilePrettyType
SlackFileSnippetType
SlackFileType
SlackFileUploadUrl
SlackHistoryMessage
SlackHistoryMessageInit
Required fields of SlackHistoryMessage; convert with SlackHistoryMessage::from or .into().
SlackIcon
SlackIconImages
SlackIconImagesInit
Required fields of SlackIconImages; convert with SlackIconImages::from or .into().
SlackIconInit
Required fields of SlackIcon; convert with SlackIcon::from or .into().
SlackLocale
SlackMessage
SlackMessageAttachment
SlackMessageAttachmentFieldObject
SlackMessageAttachmentFieldObjectInit
Required fields of SlackMessageAttachmentFieldObject; convert with SlackMessageAttachmentFieldObject::from or .into().
SlackMessageAttachmentId
SlackMessageAttachmentInit
Required fields of SlackMessageAttachment; convert with SlackMessageAttachment::from or .into().
SlackMessageContent
SlackMessageContentInit
Required fields of SlackMessageContent; convert with SlackMessageContent::from or .into().
SlackMessageEdited
SlackMessageEditedInit
Required fields of SlackMessageEdited; convert with SlackMessageEdited::from or .into().
SlackMessageInit
Required fields of SlackMessage; convert with SlackMessage::from or .into().
SlackMessageMetadata
SlackMessageMetadataInit
Required fields of SlackMessageMetadata; convert with SlackMessageMetadata::from or .into().
SlackMessageOrigin
SlackMessageOriginInit
Required fields of SlackMessageOrigin; convert with SlackMessageOrigin::from or .into().
SlackMessageSender
SlackMessageSenderInit
Required fields of SlackMessageSender; convert with SlackMessageSender::from or .into().
SlackMimeType
SlackParentMessageParams
SlackParentMessageParamsInit
Required fields of SlackParentMessageParams; convert with SlackParentMessageParams::from or .into().
SlackPin
SlackPinInit
Required fields of SlackPin; convert with SlackPin::from or .into().
SlackReaction
SlackReactionInit
Required fields of SlackReaction; convert with SlackReaction::from or .into().
SlackReactionName
SlackRelaxedUrl
This type is needed since Slack allowes invalid URLs in some places like Rich sections and we still need to read them on the client side, so we store it as a string but provide convinent messages to convert to Url if possible.
SlackResponseMetadata
SlackResponseMetadataInit
Required fields of SlackResponseMetadata; convert with SlackResponseMetadata::from or .into().
SlackResponseUrl
SlackScheduledMid
SlackSigningSecret
SlackStarsItemChannel
SlackStarsItemChannelInit
Required fields of SlackStarsItemChannel; convert with SlackStarsItemChannel::from or .into().
SlackStarsItemFile
SlackStarsItemFileComment
SlackStarsItemFileCommentInit
Required fields of SlackStarsItemFileComment; convert with SlackStarsItemFileComment::from or .into().
SlackStarsItemFileInit
Required fields of SlackStarsItemFile; convert with SlackStarsItemFile::from or .into().
SlackStarsItemGroup
SlackStarsItemGroupInit
Required fields of SlackStarsItemGroup; convert with SlackStarsItemGroup::from or .into().
SlackStarsItemIm
SlackStarsItemImInit
Required fields of SlackStarsItemIm; convert with SlackStarsItemIm::from or .into().
SlackStarsItemMessage
SlackStarsItemMessageInit
Required fields of SlackStarsItemMessage; convert with SlackStarsItemMessage::from or .into().
SlackTeamId
SlackTeamInfo
SlackTeamInfoInit
Required fields of SlackTeamInfo; convert with SlackTeamInfo::from or .into().
SlackTeamProfile
SlackTeamProfileField
SlackTeamProfileFieldId
SlackTeamProfileFieldInit
Required fields of SlackTeamProfileField; convert with SlackTeamProfileField::from or .into().
SlackTeamProfileInit
Required fields of SlackTeamProfile; convert with SlackTeamProfile::from or .into().
SlackTeamUrl
SlackTriggerId
SlackTs
SlackUnfurlId
SlackUpdatedMessage
SlackUpdatedMessageInit
Required fields of SlackUpdatedMessage; convert with SlackUpdatedMessage::from or .into().
SlackUser
SlackUserFlags
SlackUserFlagsInit
Required fields of SlackUserFlags; convert with SlackUserFlags::from or .into().
SlackUserGroup
SlackUserGroupId
SlackUserGroupInit
Required fields of SlackUserGroup; convert with SlackUserGroup::from or .into().
SlackUserGroupPrefs
SlackUserGroupPrefsInit
Required fields of SlackUserGroupPrefs; convert with SlackUserGroupPrefs::from or .into().
SlackUserId
SlackUserInit
Required fields of SlackUser; convert with SlackUser::from or .into().
SlackUserProfile
SlackUserProfileFieldId
SlackUserProfileFieldValue
SlackUserProfileInit
Required fields of SlackUserProfile; convert with SlackUserProfile::from or .into().
SlackVerificationToken
SlackViewId
SlackWebSocketsUrl

Enums§

SlackApiTokenType
SlackAutoType
SlackConversationType
SlackEmojiRef
SlackHuddleState
SlackMessageResponseType
SlackReactionsItem
SlackShortcutType
SlackStarsItem

Traits§

HasChannelInfo
SlackApiResponseScroller
SlackApiScrollableRequest
SlackApiScrollableResponse
SlackBlocksTemplate
SlackClientHttpConnector
SlackMessageTemplate
SlackTextFormat

Type Aliases§

AnyStdResult
ClientResult
SlackChannelPurposeInfo
SlackChannelTopicInfo
SlackCivilDate
A calendar date without a time zone, as used by the Slack API.
SlackUtcDateTime
An instant in time in UTC, as used by the Slack API.
UserCallbackResult