shep

Messaging Remote Control — Local Setup

This guide walks you through running the Commands.com Gateway locally and pairing a Telegram bot with Shep end-to-end.

Architecture

Telegram bot webhook
        │
        ▼
cloudflared / ngrok  ──►  Gateway (localhost:8080)  ◄──ws──  Shep daemon
                                                              (shep _serve)

Prerequisites

1. Run the Gateway

The Gateway is a separate project — the .env.example below belongs to that repository, not to Shep. (Shep itself has no .env file: its settings live in SQLite and are edited with shep settings ….)

git clone https://github.com/Commands-com/gateway.git
cd gateway
cp .env.example .env
cat >> .env <<EOF
JWT_SIGNING_KEY=$(openssl rand -base64 48)
AUTH_MODE=demo
OAUTH_DEFAULT_CLIENT_ID=commands-desktop-public
REDIRECT_ALLOWLIST=http://localhost:61696/callback,urn:ietf:wg:oauth:2.0:oob
EOF
go run ./cmd/server

Verify:

curl http://localhost:8080/healthz
# {"status":"ok"}

Open http://localhost:8080/console to see devices, sessions, and integration routes.

2. Expose the Gateway publicly

Pick one. Both support WebSocket upgrades, which is required because the Gateway’s built-in console uses them.

cloudflared (no signup)

cloudflared tunnel --url http://localhost:8080
# => Your quick tunnel: https://random-slug.trycloudflare.com

ngrok

ngrok http 8080
# => https://abcd-1234.ngrok-free.app

Export the URL for the next steps:

export SHEP_GATEWAY_PUBLIC_URL=https://random-slug.trycloudflare.com

3. Export your Telegram bot token

Shep needs the bot token to make outbound sendMessage calls (the Gateway does not proxy outbound traffic).

export SHEP_TELEGRAM_BOT_TOKEN=123456:ABCDEFG-your-token-here

_serve reads this from process.env when constructing the messaging service — see container.ts.

4. Pair Shep with Telegram

Start the web UI so you can use the pairing dialog (the CLI wizard works identically):

pnpm dev:web      # http://localhost:3000
# or
shep ui           # http://localhost:4050

Navigate to Settings → Messaging Remote Control:

  1. Flip Enable messaging on
  2. Gateway URL: http://localhost:8080 (not the public URL — the daemon connects to the Gateway on localhost)
  3. Click Pair device on Telegram

A dialog opens showing:

Because the daemon sees the Gateway on localhost but Telegram needs a public URL, rewrite the webhook URL host to your tunnel domain:

Copy the route_id and route_token out of the pairing dialog and build the URL yourself:

# From the pairing dialog's webhook URL
ROUTE_ID=...
ROUTE_TOKEN=...
WEBHOOK_URL="${SHEP_GATEWAY_PUBLIC_URL}/integrations/${ROUTE_ID}/${ROUTE_TOKEN}"

Set the Telegram webhook:

curl -X POST "https://api.telegram.org/bot${SHEP_TELEGRAM_BOT_TOKEN}/setWebhook" \
  -d "url=${WEBHOOK_URL}"
# {"ok":true,"result":true,"description":"Webhook was set"}

Verify:

curl -s "https://api.telegram.org/bot${SHEP_TELEGRAM_BOT_TOKEN}/getWebhookInfo" | jq

5. Start the daemon

shep _serve
# or during development:
pnpm dev:cli _serve

pnpm dev:web does not start the messaging service by default — it opens a persistent WebSocket tunnel to the gateway, which is not something every developer wants running on every dev-server start. Opt in explicitly:

SHEP_ENABLE_MESSAGING=1 pnpm dev:web

If messaging is not configured yet, the dev server logs SHEP_ENABLE_MESSAGING=1 but messaging is not configured yet — pair a platform in Settings first and carries on.

The daemon:

  1. Resolves IGatewayClient and calls fetchAccessToken (demo mode uses the commands-desktop-public client, no secret required).
  2. Constructs MessagingService with the fetched token + your bot token.
  3. Opens the WebSocket tunnel with Authorization: Bearer <token> on the upgrade headers.
  4. Sends tunnel.activate for each paired platform’s route.
  5. Begins listening for tunnel.request frames.

6. Pair from your phone

In your Telegram bot chat, send the 6-digit code:

/pair 482913

The daemon’s MessagingService.handleTunnelRequest:

  1. Receives the tunnel.request frame
  2. Parses the body as a Telegram Update
  3. Matches /pair <code> via parsePairCommand
  4. Looks up pendingPairingCode for Telegram in settings
  5. Calls ConfirmMessagingPairingUseCase → sets paired: true, stores your chat ID
  6. Replies: “Paired with Shep. You can now send commands like /status, /list, or /help.”

The web UI will show the row flip to Paired with your chat ID on the next render.

7. Drive Shep from your phone

/list                    — list features
/status 42               — status of feature 42
/new add dark mode       — create a new feature
/approve 42              — approve an agent run
/chat 42                 — start an interactive agent relay for feature 42
<any text while relay>   — forwarded to the agent as a user message
/end                     — end the active relay
/help                    — command reference

Replies come back through sendMessage directly to your chat.

Interactive chat relay

/chat <feature_id> attaches your Telegram chat to a feature’s interactive session. While a relay is active:

The daemon will start a new session if none exists, and queue messages if the session is still booting. You can drive an entire conversation from Telegram without touching the web UI.

Bot token storage

The daemon needs your Telegram bot token to call sendMessage. You have two options:

  1. Settings UI (recommended for real use) — after pairing, a Bot API token field appears below the Telegram row. It is stored in the settings row of Shep’s SQLite database (~/.shep/data, column messaging_telegram_bot_token) and loaded by DI on daemon start. Note that it is stored as plaintext in that column — protect the file accordingly.
  2. Environment variable (quick dev) — export SHEP_TELEGRAM_BOT_TOKEN before running shep _serve. Settings takes precedence when both are set (messagingConfig.telegram?.botToken ?? process.env.SHEP_TELEGRAM_BOT_TOKEN).

The same pattern applies to the CLI wizard: after /confirm pairing, it prompts for the bot token and stores it in settings via UpdateSettingsUseCase.

Troubleshooting

Tunnel refuses the upgrade with 401. The daemon’s OAuth token fetch failed. Check that OAUTH_DEFAULT_CLIENT_ID=commands-desktop-public is set on the Gateway and that gatewayClientId in Shep settings matches (or is unset — it defaults to the same value).

/pair <code> gets “Invalid or expired pairing code”. Codes expire after 10 minutes. Start a new pairing from the UI.

Webhook POSTs return 503. Either the daemon isn’t running, the tunnel is disconnected, or the route hasn’t been activated yet. Check adapter.isRouteActivated(routeId) — in logs you’ll see tunnel.activate followed by tunnel.activate.result ok:true when healthy.

Notifications don’t arrive in Telegram. Check that SHEP_TELEGRAM_BOT_TOKEN is set in the environment the daemon runs in — this is separate from the Gateway token. The TelegramMessageSender will silently no-op if the bot token is missing.

Limitations