Slack CLI
repository·main·Indexed 42 days ago
https://github.com/slackapi/slack-cliA command-line tool for managing Slack application lifecycles. It allows developers to call any Slack API method via the `api` command, manage app installations and deletions with the `app` command, and link local projects to existing Slack apps.
What's inside Slack CLI
- The Slack CLI is a command-line tool used to create and manage Slack apps. It is designed to be used alongside the Bolt frameworks (for JavaScript and Python) or the Deno Slack SDK to streamline the app development lifecycle.
Use the Slack CLI
mainThe
slackcommand-line tool is used to create, run, and deploy Slack apps. The general syntax for the CLI is:slack <command> <subcommand> [flags]Common workflows include:
- Authentication:
slack loginto authenticate your account. - Project Setup:
slack createto start a new app orslack initto initialize an existing one. - Development:
slack runto start a local development server. - Deployment:
slack deployto push your app to the Slack Platform. - Documentation:
slack docsto open the official developer documentation.
$ slack login # Log in to your Slack account $ slack create # Create a new Slack app $ slack init # Initialize an existing Slack app $ slack run # Start a local development server $ slack deploy # Deploy to the Slack Platform $ slack docs # Open Slack developer docs- Authentication:
Manage app functions with `slack function`
mainThe
slack functioncommand allows you to inspect, configure, and manage the custom functions included in a Slack app. Functions are logic components used as steps within Workflow Builder. You can use this command to distribute functions to teammates or a workspace and lookup their distribution status.slack function <subcommand> [flags]Overview of Slack CLI Hooks
mainHooks are the mechanism through which the Slack CLI and various SDKs (Deno, Bolt for JavaScript, Bolt for Python, Bolt for Java) communicate. Different CLI commands trigger specific hooks to perform tasks like deploying, running, or checking for updates.
Common CLI commands and their associated hooks:
Command Hook(s) slack deployget-manifest,build,deployslack doctorcheck-update,doctorslack manifest infoget-manifestslack runget-manifest,startslack trigger createget-triggerslack upgradecheck-update,install-updateDiscovering hooks via the `get-hooks` hook
mainTo support various SDKs (like Deno, Bolt, or Python) without hardcoding their specific requirements, the CLI uses a service-discovery pattern centered on the
get-hookshook.How it works
- The project contains a
.slack/hooks.jsonfile. By default, this file contains only theget-hooksdefinition. - When the CLI needs to know what functionality an SDK supports, it invokes the
get-hookshook. - The SDK responds with a JSON object containing all available hook definitions and their default configurations.
Developer Workflow
- Upgrading SDKs: You generally do not need to manually edit
hooks.jsonwhen upgrading your SDK, as the SDK dictates the interface viaget-hooks. - Overriding Hooks: If you need to provide a custom implementation for a specific hook, you can override that specific section within your
hooks.jsonfile.
# Example structure of .slack/hooks.json { "get-hooks": { "command": "path/to/your/sdk-hook-script" } }- The project contains a
Understand the Admin Approval flow for Enterprise Grid apps
mainBy default, Admin Approval of Apps is enabled for all Slack Enterprise Grid organizations. When you attempt to install an app via the CLI, the following flow occurs:
- Request: The CLI prompts you to request access. A request containing the app's OAuth scopes and outgoing domains is sent to the Org Admin.
- Notification: Slackbot confirms the request was sent to you and notifies the Org Admin.
- Approval: An Org Admin approves the request.
- Completion: Slackbot notifies you of the approval. You can then attempt the installation again, and it should succeed.
If your organization has disabled Admin Approval, the first installation attempt will succeed immediately.
How the CLI resolves hook commands
mainWhen the CLI needs to execute a hook, it follows this resolution priority:
hooks.jsonOverride: If the hook key exists in the localhooks.jsonfile, the CLI uses that command directly and stops.- SDK Response: If the hook is not in
hooks.json, the CLI looks for the hook in thehooksobject returned by theget-hookscommand. If found, it executes that command. - Error: If the hook is found in neither location, the CLI returns an
sdk_hook_not_founderror.
How hooks facilitate CLI-SDK communication
mainThe Slack CLI and application SDKs communicate through a system of hooks defined in a project-level
hooks.jsonfile (typically located in the.slackdirectory).Mental Model
- CLI Role: The CLI acts as the orchestrator. When specific events occur (e.g., generating a manifest, bundling code, or handling local development events), the CLI spawns a separate process to execute a hook script.
- SDK Role: The SDK implements these scripts. This allows the CLI to remain language-agnostic, delegating complex, project-specific tasks to the SDK.
- Execution: The CLI communicates with the hook process via
STDIN(passing JSON objects) and command-line flags. It receives responses via the process'sSTDOUT. - Success/Failure: The CLI relies on the hook process's exit code. An exit code of
0signals success; any other exit code signals failure, causing the CLI to surface ansdk_hook_invocation_failederror.
CLI-SDK protocol negotiation
mainThe rules for how the CLI and SDK communicate (how data is shaped and how diagnostic logs are separated from responses) are negotiated during the initial
get-hookshandshake using theprotocol-versionfield.Supported Protocol Versions
v1: The base and default protocol.v2: Implementsmessage boundaries. This version allows the SDK to clearly delineate actual hook responses from diagnostic data or logging sent toSTDOUT.
If protocol negotiation fails or the SDK does not adhere to the negotiated rules, the CLI will automatically fall back to the default protocol.
Best practices for service tokens
mainBecause service tokens are tied to the developer who requested them, it is recommended to create a dedicated "Slack service account" within your workspace. Using a service account instead of an individual user account:
- Reduces security risks if a developer's token is compromised.
- Prevents service interruptions when an individual leaves the organization.
Getting started with the Slack CLI
mainTo begin developing Slack apps with the CLI, follow these steps:
- Install the CLI: Follow the platform-specific installation guide for MacOS / Linux or Windows.
- Authorize your workspace: Follow the Authorization guide to connect the CLI to your Slack workspace.
- Follow a Quickstart: For a guided path using Bolt frameworks, use the quickstart guide.
Install the Slack CLI on Windows via Automated Installation
mainThe easiest way to install the Slack CLI on Windows is to run the automated installer from Windows PowerShell. This process installs the CLI and configures the command in your path.
Note: PowerShell is a required prerequisite. If you do not have PowerShell 7 installed, you must install it first.
irm https://downloads.slack-edge.com/slack-cli/install-windows.ps1 | iex