# Slack

Slack access uses Socket Mode so users can mention the app, DM it, or continue in a thread, with replies sent back to the original channel or thread; direct local Slack connection is enabled unless `SLACK_CHANNEL_DIRECT_ENABLED=false`.

## Usage Modes

| Mode | Trigger | Notes |
| --- | --- | --- |
| Channel mention | Mention the app in a channel and enter a question | Good for team status checks, alert handling, and control commands. |
| App DM | Send a direct message to the app | Good for personal remote status queries and control. |
| Thread reply | Continue in the thread where the app replied | Keeps one device investigation or operation in a single Slack thread. |

## Prepare the Slack App

1. Create an app in Slack API Apps.
2. In OAuth & Permissions → Bot Token Scopes, add:
   - `chat:write`
   - `app_mentions:read`
   - `im:history`
3. In Event Subscriptions, subscribe to Bot Events:
   - `app_mention`
   - `message.im`
4. Enable Socket Mode.
5. In Basic Information → App-Level Tokens, create a token with `connections:write` and copy the `xapp-...` token.
6. In OAuth & Permissions, install the app to the workspace and copy the Bot User OAuth Token, usually `xoxb-...`.
7. Copy Signing Secret from Basic Information.

## Configure Device Agent

Open **Settings → Channels → Slack** and fill in:

| Field | `.env` | Description |
| --- | --- | --- |
| Enabled | `SLACK_ENABLED` | Starts the Slack channel |
| Bot Token | `SLACK_BOT_TOKEN` | `xoxb-...` |
| App Token | `SLACK_APP_TOKEN` | `xapp-...` with `connections:write` |
| Signing Secret | `SLACK_SIGNING_SECRET` | Optional Slack app Signing Secret |
| Allow list | `SLACK_ALLOW_FROM` | Optional Slack user ID allow list |

You can also configure it in `.env`:

```bash
SLACK_ENABLED=true
SLACK_BOT_TOKEN=xoxb-your-slack-bot-token
SLACK_APP_TOKEN=xapp-your-slack-app-token
SLACK_SIGNING_SECRET=your-signing-secret
SLACK_ALLOW_FROM=U01234567,U89ABCDEF
SLACK_CHANNEL_DIRECT_ENABLED=true
```

Restart the service after saving.

## Verify Access

1. Confirm the service has restarted.
2. Check logs for `Slack: enabled` and `Channel started (Socket Mode)`.
3. Mention the app in a channel, or send a DM to the app.
4. If the allow list is enabled, make sure the sender Slack user ID is included.

Channel replies are sent to the original thread. DMs reply directly in the DM conversation.

## Checks

- No DM response: subscribe to `message.im` and reinstall the app to the workspace.
- No channel response: mention the app and confirm it has joined the channel.
- Socket Mode does not start: make sure `SLACK_APP_TOKEN` starts with `xapp-...` and has `connections:write`.
- Direct channel does not start: confirm `SLACK_CHANNEL_DIRECT_ENABLED=false` is not set.
