This code is a proxy between the clearbot and the node-sonos-http-api library.
It connects out to ClearBot over a native WebSocket (authenticating with a
shared RELAY_TOKEN), receives JSON commands, and proxies them to
node-sonos-http-api. The connection reconnects automatically with exponential
backoff if it drops.
sequenceDiagram
Note over Slack,ClearBot: ClearBot connects out to Slack (Socket Mode)
Slack-->>ClearBot: message event (e.g. "burn")
Note over ClearBot,Sonos Proxy: Sonos Proxy connects out to ClearBot (WebSocket, token auth)
ClearBot-->>Sonos Proxy: { type: play_url, url: burn.mp3 }
Sonos Proxy-->>node-sonos-http-api: GET http://localhost:5005/Office/clip/burn.mp3/20
- NPM
- Node 20–22 (uses the built-in global
WebSocket)
- Set up clearbot
- Put some mp3s in the
static/clipsdirectory (they correspond to the clearbot sounds defined in https://github.com/clearfunction/clearbot/blob/main/src/responses.ts.) - Set up your
.envfile (see.env.example):CLEARBOT_URL— the bot's WebSocket URL (ws://localhost:3000locally,wss://…in production)RELAY_TOKEN— must match the value set onclearbotSONOS_BRIDGE_URL— base URL ofnode-sonos-http-api(e.g.http://localhost:5005)SONOS_ROOMS— the speaker room name(s); comma-separated to play on multiple (e.g.Living Room,Kitchen)
- If you don't have a Sonos speaker, then you can still use the local player... just ensure you've got
USE_LOCAL_SOUNDSset totrue - If you do have a Sonos speaker, then you'll also need the
node-sonos-http-apirunning locally - Ensure your
clearbotis running. It has its own documentation. - Run
npm run dev - Watch for
Connecting to <CLEARBOT_URL>followed byConnected to serveronce it attaches to the bot. If you instead see a reconnect loop with acode 1006close immediately after connecting, theRELAY_TOKEN(or URL) is wrong. - Enjoy!
We set this up with
pm2
to daemonize it on the Mac Mini in our closet. Let's see how it works out for
us!
npm install # installs this app
npm run tsc # compiles to build/app.js
npm install pm2 -g # installs the daemonizer
pm2 start ./build/app.js --name sonos_proxy # assumes you're in this app's folder, starts the daemon
pm2 save # saves the running process as a daemon that will be auto-restarted even after reboots