docs: README, compose example, integration test
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
15874f7ecb
commit
45f88378a6
4 files changed
+236
No files matched your search
@@ -0,0 +1,132 @@
|
|||||||
|
# signalbot
|
||||||
|
|
||||||
|
Send and receive Signal text messages from Go, in a few lines.
|
||||||
|
|
||||||
|
signalbot is a small, pure-Go library (no CGO) plus a CLI. It talks HTTP and
|
||||||
|
websocket to a [signal-cli-rest-api](https://github.com/bbernhard/signal-cli-rest-api)
|
||||||
|
container, which runs [signal-cli](https://github.com/AsamK/signal-cli) and
|
||||||
|
handles the Signal protocol.
|
||||||
|
|
||||||
|
> Unofficial client, not affiliated with or endorsed by Signal.
|
||||||
|
|
||||||
|
## How it works
|
||||||
|
|
||||||
|
```
|
||||||
|
┌──────────────── docker compose ────────────────┐
|
||||||
|
│ your app │
|
||||||
|
│ └─ signalbot (Go library) ──HTTP/ws──┐ │
|
||||||
|
│ ▼ │
|
||||||
|
│ signal-api (bbernhard/signal-cli-rest-api) │
|
||||||
|
│ └─ volume: account keys, sessions, groups │
|
||||||
|
└─────────────────────────────────────────────────┘
|
||||||
|
```
|
||||||
|
|
||||||
|
Signal has no bot API and no broadcast channels. A bot is a regular Signal
|
||||||
|
account (use a dedicated number) linked as a **secondary device** of a phone,
|
||||||
|
and a "channel" is an **announcement group**: a group where only admins can
|
||||||
|
send messages, with the bot as an admin.
|
||||||
|
|
||||||
|
## Install
|
||||||
|
|
||||||
|
```sh
|
||||||
|
go get git.siteop.biz/vincentb99/signalbot
|
||||||
|
go install git.siteop.biz/vincentb99/signalbot/cmd/signalbot@latest # CLI
|
||||||
|
```
|
||||||
|
|
||||||
|
Requires Go 1.23+.
|
||||||
|
|
||||||
|
## Quick start
|
||||||
|
|
||||||
|
```go
|
||||||
|
client, err := signalbot.New("http://signal-api:8080")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send to a group ("group.<base64>", see `signalbot groups`),
|
||||||
|
// a phone number (E.164) or an ACI UUID. Plain text only.
|
||||||
|
err = client.Send(ctx, "group.SGVsbG8...", "New release available")
|
||||||
|
|
||||||
|
// Receive incoming text messages until ctx is cancelled.
|
||||||
|
err = client.Listen(ctx, func(ctx context.Context, m signalbot.Message) {
|
||||||
|
// m.From, m.GroupID ("" for direct messages), m.Text, m.Timestamp
|
||||||
|
_ = m.Reply(ctx, "got it") // answers in the same conversation
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
`Listen` reconnects automatically (backoff 1s → 30s) and calls the handler
|
||||||
|
for one message at a time, in arrival order. It ignores everything that is
|
||||||
|
not a text message (receipts, typing, reactions, attachments, messages sent
|
||||||
|
from the bot's own phone). A panic in the handler is recovered and logged.
|
||||||
|
|
||||||
|
Options: `WithNumber` (choose an account when several are linked),
|
||||||
|
`WithHTTPClient`, `WithLogger` (`*slog.Logger`, default `slog.Default()`).
|
||||||
|
|
||||||
|
Errors can be checked with `errors.Is`: `ErrNotLinked`,
|
||||||
|
`ErrMultipleAccounts`, `ErrUnavailable` (unreachable or 5xx),
|
||||||
|
`ErrRateLimited` (429), `ErrEmptyText`. Other API errors are `*APIError`
|
||||||
|
(`StatusCode`, `Message`). `Send` never retries.
|
||||||
|
|
||||||
|
See [`examples/`](examples/) for a complete app and a docker-compose file.
|
||||||
|
|
||||||
|
## Setup
|
||||||
|
|
||||||
|
1. **Bot account.** Get a dedicated number with Signal installed on a phone.
|
||||||
|
On that phone, create the group, make it an announcement group (group
|
||||||
|
settings → *Send messages* → *Only admins*) and add members.
|
||||||
|
2. **Start the service** with port 8080 published on localhost (see
|
||||||
|
[`examples/docker-compose.yml`](examples/docker-compose.yml), uncomment
|
||||||
|
`ports`).
|
||||||
|
3. **Link** the bot: `signalbot link`, then on the bot's phone: Signal →
|
||||||
|
Settings → Linked devices → **+**, and scan the QR code. Without the CLI,
|
||||||
|
you can also open
|
||||||
|
`http://localhost:8080/v1/qrcodelink?device_name=signalbot` in a browser
|
||||||
|
and scan the PNG.
|
||||||
|
4. **Find the group id**: `signalbot groups` (it warns if the bot is not an
|
||||||
|
admin or the group is not admins-only). Test with
|
||||||
|
`signalbot send -to group.… "hello"` and `signalbot listen`.
|
||||||
|
5. **Unpublish the port** and configure your app with the service URL and
|
||||||
|
the group id.
|
||||||
|
|
||||||
|
## Important
|
||||||
|
|
||||||
|
- **Keep `Listen` running, even if you only publish.** Receiving also
|
||||||
|
processes retry receipts (e.g. a member who reinstalled Signal); without
|
||||||
|
it, some members may stop being able to decrypt the bot's group messages.
|
||||||
|
- **Use `JSON_RPC_RECEIVE_MODE=manual`.** With the default (`on-start`),
|
||||||
|
messages that arrive while your app is down are silently lost. With
|
||||||
|
`manual`, they stay queued by Signal and are delivered when `Listen`
|
||||||
|
reconnects.
|
||||||
|
- **Never expose the service publicly**: signal-cli-rest-api has no
|
||||||
|
authentication. Keep it on the compose network.
|
||||||
|
- **Keep the volume.** It holds the account keys; losing it means linking
|
||||||
|
again.
|
||||||
|
- `MODE=json-rpc-native` is fast and light; if it proves unstable, use
|
||||||
|
`MODE=json-rpc` (JVM, more memory).
|
||||||
|
|
||||||
|
## CLI
|
||||||
|
|
||||||
|
```
|
||||||
|
signalbot link [-device-name signalbot] [-timeout 5m]
|
||||||
|
signalbot groups [-number N]
|
||||||
|
signalbot send [-number N] -to <group id | number | uuid> <text...>
|
||||||
|
signalbot listen [-number N]
|
||||||
|
```
|
||||||
|
|
||||||
|
All commands accept `-url` (default `$SIGNALBOT_URL`, else
|
||||||
|
`http://localhost:8080`).
|
||||||
|
|
||||||
|
## Tests
|
||||||
|
|
||||||
|
```sh
|
||||||
|
go test ./...
|
||||||
|
# against a real, linked service:
|
||||||
|
SIGNALBOT_IT_URL=http://localhost:8080 SIGNALBOT_IT_GROUP=group.… go test -run Integration -v
|
||||||
|
```
|
||||||
|
|
||||||
|
## Licence
|
||||||
|
|
||||||
|
signalbot is MIT licensed. The signal-cli-rest-api image it talks to ships
|
||||||
|
components under GPL-3.0 (signal-cli) and AGPL-3.0 (libsignal); signalbot
|
||||||
|
contains none of them and only communicates with the service over the
|
||||||
|
network.
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
# Minimal deployment: the Signal service and an app using signalbot.
|
||||||
|
services:
|
||||||
|
signal-api:
|
||||||
|
image: bbernhard/signal-cli-rest-api:0.101
|
||||||
|
environment:
|
||||||
|
- MODE=json-rpc-native # fallback: MODE=json-rpc if native mode is unstable
|
||||||
|
- JSON_RPC_RECEIVE_MODE=manual # keep messages queued while the app is down
|
||||||
|
volumes:
|
||||||
|
- signal-data:/home/.local/share/signal-cli # account keys: losing it means re-linking
|
||||||
|
restart: unless-stopped
|
||||||
|
# Only while linking (`signalbot link`), then remove: the API has no authentication.
|
||||||
|
# ports:
|
||||||
|
# - "127.0.0.1:8080:8080"
|
||||||
|
|
||||||
|
app:
|
||||||
|
build: .
|
||||||
|
environment:
|
||||||
|
- SIGNAL_API_URL=http://signal-api:8080
|
||||||
|
- SIGNAL_GROUP_ID=group.REPLACE_ME # from `signalbot groups`
|
||||||
|
depends_on:
|
||||||
|
- signal-api
|
||||||
|
restart: unless-stopped
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
signal-data:
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
// Command example announces itself in a group, then answers every incoming
|
||||||
|
// text message with an echo.
|
||||||
|
//
|
||||||
|
// SIGNAL_API_URL=http://localhost:8080 SIGNAL_GROUP_ID=group.… go run ./examples
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
|
||||||
|
"git.siteop.biz/vincentb99/signalbot"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
client, err := signalbot.New(os.Getenv("SIGNAL_API_URL"))
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
if group := os.Getenv("SIGNAL_GROUP_ID"); group != "" {
|
||||||
|
if err := client.Send(ctx, group, "example bot started"); err != nil {
|
||||||
|
log.Printf("announce: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Keep Listen running even if you only publish: it also processes the
|
||||||
|
// retry receipts that keep group members able to decrypt your messages.
|
||||||
|
err = client.Listen(ctx, func(ctx context.Context, m signalbot.Message) {
|
||||||
|
if err := m.Reply(ctx, "echo: "+m.Text); err != nil {
|
||||||
|
log.Printf("reply: %v", err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
if err != nil && !errors.Is(err, context.Canceled) {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
package signalbot
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TestIntegrationPingGroupsSend runs against a real, linked service:
|
||||||
|
//
|
||||||
|
// SIGNALBOT_IT_URL=http://localhost:8080 SIGNALBOT_IT_GROUP=group.… go test -run Integration -v
|
||||||
|
func TestIntegrationPingGroupsSend(t *testing.T) {
|
||||||
|
url, group := os.Getenv("SIGNALBOT_IT_URL"), os.Getenv("SIGNALBOT_IT_GROUP")
|
||||||
|
if url == "" || group == "" {
|
||||||
|
t.Skip("set SIGNALBOT_IT_URL and SIGNALBOT_IT_GROUP to run")
|
||||||
|
}
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
c := mustNew(t, url, WithNumber(os.Getenv("SIGNALBOT_IT_NUMBER")))
|
||||||
|
if err := c.Ping(ctx); err != nil {
|
||||||
|
t.Fatalf("Ping: %v", err)
|
||||||
|
}
|
||||||
|
groups, err := c.Groups(ctx)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Groups: %v", err)
|
||||||
|
}
|
||||||
|
found := false
|
||||||
|
for _, g := range groups {
|
||||||
|
found = found || g.ID == group
|
||||||
|
}
|
||||||
|
if !found {
|
||||||
|
t.Fatalf("group %s not in %+v", group, groups)
|
||||||
|
}
|
||||||
|
if err := c.Send(ctx, group, "signalbot integration test "+time.Now().Format(time.RFC3339)); err != nil {
|
||||||
|
t.Fatalf("Send: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user