- Go 100%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
Preserve reply routing, room/self filtering and initial-history suppression. Initialize the client before serving requests, escape visitor HTML, and update transitive security fixes. Exercise SDK send serialization and sync parsing with a mock homeserver. |
||
| .gitignore | ||
| go.mod | ||
| go.sum | ||
| health.go | ||
| health_test.go | ||
| LICENSE | ||
| matrix_test.go | ||
| README.md | ||
| server.go | ||
| server_test.go | ||
| web-msg.env.example | ||
| web-msg.jpg | ||
web-msg
Backend for a website chat widget, bridged to a Matrix room. Visitor messages are posted into the room; the operator answers from any Matrix client and the answer is delivered back to the visitor's browser by polling. The widget front end itself is not part of this repository.
How it works
POST /sendtakes{"session_id": "...", "message": "..."}from the widget and forwards it to the configured room as<session_id>: <message>.GET /poll?session_id=...returns (and clears) any queued replies for that session as a JSON array of{"content", "timestamp"}.GET /healthreports the service name, build commit and start time.
Routing operator messages back to a visitor:
- A Matrix reply (or thread reply) to a forwarded visitor message goes to that visitor.
- A plain message goes to the single visitor who sent something in the last 30 minutes. If there are none or several, nothing is delivered and the bot posts a notice in the room saying so.
State is held in memory only. Sessions idle for 24 hours are dropped. Request bodies are capped at 16 KiB and messages at 8 KiB.
Configuration
Settings come from the environment; see web-msg.env.example:
| Variable | Meaning |
|---|---|
MATRIX_HOMESERVER |
Homeserver base URL |
MATRIX_USER_ID |
The bot account's Matrix user ID |
MATRIX_ROOM_ID |
Room the bot posts visitor messages to |
MATRIX_ACCESS_TOKEN |
Access token for the bot account |
The bot account must already be joined to the room. The service uses mautrix-go for the Matrix client API; encryption and the bridge framework are not enabled. Initial sync history is skipped after restart.
Build and run
Needs Go 1.27 or newer; the module selects Go 1.27.4 for security fixes.
go build
set -a; . ./web-msg.env; set +a
./web-msg
It listens on 127.0.0.1:4999; put a reverse proxy in front of it to expose
/send and /poll to the widget.
Tests: go test ./...
License
MIT. See LICENSE.