Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
42 lines
1.0 KiB
Go
42 lines
1.0 KiB
Go
// 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)
|
|
}
|
|
}
|