> ## Documentation Index
> Fetch the complete documentation index at: https://docs.nerves-hub.org/llms.txt
> Use this file to discover all available pages before exploring further.

# Create a Slack app for your instance

> Create the Slack app a self-hosted NervesHub instance needs before organizations can connect Slack workspaces, including Socket Mode for an instance Slack cannot reach.

The Slack integration works through a Slack app that belongs to the NervesHub instance. Organizations connect a workspace by installing that app. A self-hosted instance has no Slack app until you create one, and Slack stays hidden on every organization's **Integrations** page until you do.

What an organization admin does afterwards is on the [Slack](https://docs.nerves-hub.org/outbound/slack.md) page.

## Requirements

* NervesHub 2.5.0 or later.
* A Slack workspace to develop the app in, where you can create apps.
* The instance's URL. The examples use `https://your.nerveshub.host`.
* A [`SECRETS_ENCRYPTION_KEY`](https://docs.nerves-hub.org/hosting/feature-prerequisites#secrets-encryption-key). NervesHub encrypts each workspace's bot token with it. In production, a node with `SLACK_CLIENT_ID` set and no key refuses to boot.

## Choose how Slack reaches the instance

Slack sends events, button clicks and slash commands to the instance. There are two ways for them to arrive:

| | Request URLs | Socket Mode |
| - | - | - |
| Use when | Slack can reach the instance over HTTPS. | The instance is behind a VPN or a firewall Slack cannot get through. |
| How it works | Slack posts to URLs under `/integrations/slack/`. | Each web node opens connections to Slack, and Slack sends everything down them. |
| Variable | `SLACK_SIGNING_SECRET` | `SLACK_APP_TOKEN` |

Posting notifications, installing the app and linking accounts work either way. The instance calls Slack to post, and installs and account links go through the person's browser. Buttons, `/nerveshub` commands and link previews need one of the two.

## Create the app with request URLs

**1. Set the secrets encryption key**

Generate a key and set it as `SECRETS_ENCRYPTION_KEY` on every node, if the instance does not have one yet:

```bash theme={null}
openssl rand -base64 32
```

**2. Create the app from a manifest**

Go to [api.slack.com/apps](https://api.slack.com/apps). Choose **Create New App**, then **From a manifest**, and pick a workspace to develop the app in.

Paste the manifest below. Replace `your.nerveshub.host` with your instance's host everywhere it appears.

```json theme={null}
{
  "display_information": {
    "name": "NervesHub",
    "description": "Device notifications from NervesHub"
  },
  "features": {
    "bot_user": {
      "display_name": "NervesHub",
      "always_online": true
    },
    "slash_commands": [
      {
        "command": "/nerveshub",
        "url": "https://your.nerveshub.host/integrations/slack/commands",
        "description": "Check on and act on NervesHub devices",
        "usage_hint": "device | identify | reboot | script <identifier> [script] | link | help",
        "should_escape": false
      }
    ],
    "unfurl_domains": ["your.nerveshub.host"]
  },
  "oauth_config": {
    "redirect_urls": [
      "https://your.nerveshub.host/integrations/slack/oauth/callback",
      "https://your.nerveshub.host/integrations/slack/link/callback"
    ],
    "scopes": {
      "bot": ["chat:write", "chat:write.public", "channels:read", "groups:read", "commands", "links:read", "links:write"]
    }
  },
  "settings": {
    "event_subscriptions": {
      "request_url": "https://your.nerveshub.host/integrations/slack/events",
      "bot_events": ["app_uninstalled", "tokens_revoked", "link_shared"]
    },
    "interactivity": {
      "is_enabled": true,
      "request_url": "https://your.nerveshub.host/integrations/slack/interactivity"
    },
    "org_deploy_enabled": false,
    "socket_mode_enabled": false,
    "token_rotation_enabled": false
  }
}
```

Slack checks the events URL when you save the manifest. The instance must already answer at that URL with `SLACK_SIGNING_SECRET` set, which it cannot do before the app exists. On a first setup, save the manifest without the `event_subscriptions` block, finish the remaining steps, then add the block back.

**3. Turn on distribution**

Under **Manage Distribution**, turn on public distribution. Without it, only the development workspace can install the app.

**4. Set the variables**

From the app's **Basic Information** page, copy the credentials into the instance's environment:

```bash theme={null}
SLACK_CLIENT_ID=...
SLACK_CLIENT_SECRET=...
SLACK_SIGNING_SECRET=...
```

**5. Restart the instance**

Restart every node. Slack now appears on each organization's **Integrations** page.

**6. Add event subscriptions back**

If you saved the manifest without `event_subscriptions`, add the block back now and save. Slack can verify the events URL once the instance is running with the signing secret.

## Create the app with Socket Mode

**1. Set the secrets encryption key**

Generate a key and set it as `SECRETS_ENCRYPTION_KEY` on every node, if the instance does not have one yet:

```bash theme={null}
openssl rand -base64 32
```

**2. Create the app from the Socket Mode manifest**

Go to [api.slack.com/apps](https://api.slack.com/apps). Choose **Create New App**, then **From a manifest**, and pick a workspace to develop the app in.

Paste the manifest below. It turns Socket Mode on and has no request URLs. The redirect URLs stay, because only browsers follow them. Replace `your.nerveshub.host` with your instance's host.

```json theme={null}
{
  "display_information": {
    "name": "NervesHub",
    "description": "Device notifications from NervesHub"
  },
  "features": {
    "bot_user": {
      "display_name": "NervesHub",
      "always_online": true
    },
    "slash_commands": [
      {
        "command": "/nerveshub",
        "description": "Check on and act on NervesHub devices",
        "usage_hint": "device | identify | reboot | script <identifier> [script] | link | help",
        "should_escape": false
      }
    ],
    "unfurl_domains": ["your.nerveshub.host"]
  },
  "oauth_config": {
    "redirect_urls": [
      "https://your.nerveshub.host/integrations/slack/oauth/callback",
      "https://your.nerveshub.host/integrations/slack/link/callback"
    ],
    "scopes": {
      "bot": ["chat:write", "chat:write.public", "channels:read", "groups:read", "commands", "links:read", "links:write"]
    }
  },
  "settings": {
    "event_subscriptions": {
      "bot_events": ["app_uninstalled", "tokens_revoked", "link_shared"]
    },
    "interactivity": {
      "is_enabled": true
    },
    "org_deploy_enabled": false,
    "socket_mode_enabled": true,
    "token_rotation_enabled": false
  }
}
```

**3. Turn on distribution**

Under **Manage Distribution**, turn on public distribution. Without it, only the development workspace can install the app.

**4. Generate an app-level token**

Under **Basic Information**, generate an **App-Level Token** with the `connections:write` scope. It starts with `xapp-`.

**5. Set the variables**

```bash theme={null}
SLACK_CLIENT_ID=...
SLACK_CLIENT_SECRET=...
SLACK_APP_TOKEN=xapp-...
```

You do not need `SLACK_SIGNING_SECRET`. Without it, the instance's `/integrations/slack/` URLs answer 404.

**6. Allow outbound connections to Slack**

Let the instance reach `slack.com` and the WebSocket hosts Slack hands out, such as `wss-primary.slack.com`, over HTTPS.

**7. Restart the instance**

Restart every node. Slack now appears on each organization's **Integrations** page.

Each web node keeps two Socket Mode connections open. Slack replaces every connection every few hours, and the second one carries on while the first reconnects. Slack allows ten connections per app, so an instance can run up to five web nodes in Socket Mode. Nodes started with `NERVES_HUB_APP=device` open none.

## Check that it works

1. Sign in as an organization admin and open the organization's **Integrations** page. An organization with no integrations yet shows a **Slack** card with an **Add to Slack** button. Otherwise, **Add integration** offers **Connect a Slack workspace**.
2. Click **Add to Slack** and approve the install in Slack. Slack sends you back to the **Integrations** page, which lists the workspace.
3. Add a Slack channel and use **Send test message**. The message arrives in the channel.
4. In Slack, run `/nerveshub help`. A reply listing the commands confirms that Slack reaches the instance, by request URL or Socket Mode.

## Variables

Set these on every node. All are read at boot.

| Variable | Description |
| - | - |
| `SLACK_CLIENT_ID` | The app's client ID, from **Basic Information**. |
| `SLACK_CLIENT_SECRET` | The app's client secret. NervesHub uses it to exchange an install's code for the workspace's bot token. |
| `SLACK_SIGNING_SECRET` | The app's signing secret. NervesHub refuses requests to `/integrations/slack/` that do not carry a valid signature. |
| `SLACK_APP_TOKEN` | An app-level token with the `connections:write` scope, for Socket Mode. |
| `SECRETS_ENCRYPTION_KEY` | 32 random bytes, base64 encoded. Encrypts bot tokens before they are stored. |
| `FRIENDLY_PLATFORM_NAME` | Optional. The name your users know the instance by. Slack messages and the Slack pages use it, as in "Open in NervesHub". Defaults to `NervesHub`. |

Slack is on when `SLACK_CLIENT_ID` and `SLACK_CLIENT_SECRET` are set, along with `SLACK_SIGNING_SECRET`, `SLACK_APP_TOKEN`, or both.

> [!WARNING]
>
> Changing `SECRETS_ENCRYPTION_KEY` makes the stored bot tokens unreadable. Every organization would have to connect its workspaces again. The same key also protects [two-factor authentication](https://docs.nerves-hub.org/access/two-factor.md) secrets and Teams and Discord webhook URLs.

## What the app asks for

The app requests these bot scopes when a workspace installs it:

| Scope | Why |
| - | - |
| `chat:write` | Post device notifications to channels. |
| `chat:write.public` | Post to public channels without being invited first. |
| `channels:read` | List public channels to choose from. |
| `groups:read` | List private channels the app has been invited to. |
| `commands` | Slash commands and message actions. |
| `links:read` | Hear about links to this instance posted in Slack. |
| `links:write` | Preview them. |

Linking a Slack account to a NervesHub account uses Sign in with Slack, which asks for the `openid` and `profile` scopes. NervesHub uses them only to read who signed in, and does not keep that token.

The app subscribes to three bot events: `app_uninstalled` and `tokens_revoked`, so NervesHub disconnects a workspace that removes the app, and `link_shared`, for link previews.

## Requests from Slack

With request URLs, Slack calls three paths on the instance:

| Path | Receives |
| - | - |
| `/integrations/slack/events` | Events |
| `/integrations/slack/interactivity` | Button clicks |
| `/integrations/slack/commands` | Slash commands |

Every request there must carry a valid `X-Slack-Signature` for the app's signing secret and be no more than five minutes old. NervesHub answers anything else with 401. When the instance has no Slack app, or has no signing secret because it uses Socket Mode alone, these paths answer 404.

## Limits

* **Unfurl domain.** Register the instance's own host as its unfurl domain. Slack also matches subdomains of a registered domain, so a parent domain shared by several instances would send each instance's app the others' links. When two apps in a workspace register the same domain, Slack sends a link only to the app installed there first.
* **Enterprise Grid.** NervesHub refuses organization-wide installs on Slack Enterprise Grid. Install the app in a single workspace.
* **New scopes need a reinstall.** Slack only grants new scopes when the app is installed again. If you add scopes to an existing app, each connected workspace has to install it again.

## Updating an older app

An app created from an earlier manifest needs these added:

| Feature | Add to the manifest |
| - | - |
| Buttons and account linking | `interactivity`, and the `/integrations/slack/link/callback` redirect URL |
| Slash commands | `slash_commands` |
| Link previews | `unfurl_domains`, the `links:read` and `links:write` scopes, and the `link_shared` event |

## Related pages

* [Slack](https://docs.nerves-hub.org/outbound/slack.md)
* [Slack prerequisites](https://docs.nerves-hub.org/hosting/feature-prerequisites.md#slack)
* [Roles and permissions](https://docs.nerves-hub.org/access/roles.md)
* [Slack setup notes on GitHub](https://github.com/nerves-hub/nerves_hub_web/blob/main/docs/slack.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.