feat: parse incoming messages and reply
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
18d810295b
commit
c7fa13b7c9
2 files changed
+187
No files matched your search
+84
@@ -0,0 +1,84 @@
|
||||
package signalbot
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Message is an incoming text message.
|
||||
type Message struct {
|
||||
// From is the sender's phone number, or its ACI UUID when the number is
|
||||
// not shared.
|
||||
From string
|
||||
// GroupID is the group the message was posted in ("group.<base64>", same
|
||||
// format as Groups and Send), or "" for a direct message.
|
||||
GroupID string
|
||||
Text string
|
||||
Timestamp time.Time
|
||||
|
||||
client *Client
|
||||
}
|
||||
|
||||
// Reply sends text to the conversation the message came from: the group
|
||||
// if GroupID is set, the sender otherwise.
|
||||
func (m Message) Reply(ctx context.Context, text string) error {
|
||||
to := m.GroupID
|
||||
if to == "" {
|
||||
to = m.From
|
||||
}
|
||||
return m.client.Send(ctx, to, text)
|
||||
}
|
||||
|
||||
// envelope is the subset of a signal-cli "receive" notification we use.
|
||||
type envelope struct {
|
||||
Envelope struct {
|
||||
SourceNumber *string `json:"sourceNumber"`
|
||||
SourceUUID string `json:"sourceUuid"`
|
||||
Timestamp int64 `json:"timestamp"`
|
||||
DataMessage *struct {
|
||||
Message *string `json:"message"`
|
||||
GroupInfo *struct {
|
||||
GroupID string `json:"groupId"`
|
||||
} `json:"groupInfo"`
|
||||
} `json:"dataMessage"`
|
||||
} `json:"envelope"`
|
||||
}
|
||||
|
||||
// parseEnvelope extracts a text message from a websocket frame. It reports
|
||||
// false for anything that is not a non-empty text message from someone else.
|
||||
// The returned Message has no client set.
|
||||
func parseEnvelope(data []byte, self Account) (Message, bool) {
|
||||
var e envelope
|
||||
if json.Unmarshal(data, &e) != nil {
|
||||
return Message{}, false
|
||||
}
|
||||
env := e.Envelope
|
||||
dm := env.DataMessage
|
||||
if dm == nil || dm.Message == nil || *dm.Message == "" {
|
||||
return Message{}, false
|
||||
}
|
||||
var number string
|
||||
if env.SourceNumber != nil {
|
||||
number = *env.SourceNumber
|
||||
}
|
||||
if isSelf(self, []string{number, env.SourceUUID}) {
|
||||
return Message{}, false
|
||||
}
|
||||
m := Message{
|
||||
From: number,
|
||||
Text: *dm.Message,
|
||||
Timestamp: time.UnixMilli(env.Timestamp),
|
||||
}
|
||||
if m.From == "" {
|
||||
m.From = env.SourceUUID
|
||||
}
|
||||
if m.From == "" {
|
||||
return Message{}, false
|
||||
}
|
||||
if dm.GroupInfo != nil && dm.GroupInfo.GroupID != "" {
|
||||
m.GroupID = "group." + base64.StdEncoding.EncodeToString([]byte(dm.GroupInfo.GroupID))
|
||||
}
|
||||
return m, true
|
||||
}
|
||||
+103
@@ -0,0 +1,103 @@
|
||||
package signalbot
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
var self = Account{Number: "+33600000000", ACI: "bot-aci"}
|
||||
|
||||
const directEnvelope = `{"envelope":{"source":"+33600000001","sourceNumber":"+33600000001","sourceUuid":"u-1","sourceName":"Alice","sourceDevice":1,"timestamp":1763640000000,"dataMessage":{"timestamp":1763640000000,"message":"hello","expiresInSeconds":0,"viewOnce":false}},"account":"+33600000000"}`
|
||||
|
||||
const groupEnvelope = `{"envelope":{"source":"+33600000001","sourceNumber":"+33600000001","sourceUuid":"u-1","sourceName":"Alice","sourceDevice":1,"timestamp":1763640000000,"dataMessage":{"timestamp":1763640000000,"message":"hi group","groupInfo":{"groupId":"Z3JvdXAtaW50ZXJuYWw=","type":"DELIVER"}}},"account":"+33600000000"}`
|
||||
|
||||
func TestParseDirectMessage(t *testing.T) {
|
||||
m, ok := parseEnvelope([]byte(directEnvelope), self)
|
||||
if !ok {
|
||||
t.Fatal("ok = false")
|
||||
}
|
||||
if m.From != "+33600000001" || m.GroupID != "" || m.Text != "hello" {
|
||||
t.Fatalf("got %+v", m)
|
||||
}
|
||||
if !m.Timestamp.Equal(time.UnixMilli(1763640000000)) {
|
||||
t.Fatalf("Timestamp = %v", m.Timestamp)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseGroupMessage(t *testing.T) {
|
||||
m, ok := parseEnvelope([]byte(groupEnvelope), self)
|
||||
want := "group." + base64.StdEncoding.EncodeToString([]byte("Z3JvdXAtaW50ZXJuYWw="))
|
||||
if !ok || m.GroupID != want || m.Text != "hi group" {
|
||||
t.Fatalf("got %+v, %v; want GroupID %s", m, ok, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseFromUUIDWhenNoNumber(t *testing.T) {
|
||||
env := `{"envelope":{"sourceNumber":null,"sourceUuid":"u-1","timestamp":1,"dataMessage":{"message":"x"}},"account":"+33600000000"}`
|
||||
m, ok := parseEnvelope([]byte(env), self)
|
||||
if !ok || m.From != "u-1" {
|
||||
t.Fatalf("got %+v, %v", m, ok)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseIgnored(t *testing.T) {
|
||||
cases := map[string]string{
|
||||
"receipt": `{"envelope":{"sourceNumber":"+33600000001","timestamp":1,"receiptMessage":{"when":1,"isDelivery":true,"timestamps":[1]}},"account":"+33600000000"}`,
|
||||
"typing": `{"envelope":{"sourceNumber":"+33600000001","timestamp":1,"typingMessage":{"action":"STARTED","timestamp":1}},"account":"+33600000000"}`,
|
||||
"sync": `{"envelope":{"sourceNumber":"+33600000000","timestamp":1,"syncMessage":{"sentMessage":{"message":"from phone","destination":"+33600000001"}}},"account":"+33600000000"}`,
|
||||
"empty": `{"envelope":{"sourceNumber":"+33600000001","timestamp":1,"dataMessage":{"message":""}},"account":"+33600000000"}`,
|
||||
"reaction": `{"envelope":{"sourceNumber":"+33600000001","timestamp":1,"dataMessage":{"message":null,"reaction":{"emoji":"👍","targetSentTimestamp":1}}},"account":"+33600000000"}`,
|
||||
"invalid": `{not json`,
|
||||
}
|
||||
for name, env := range cases {
|
||||
if m, ok := parseEnvelope([]byte(env), self); ok {
|
||||
t.Errorf("%s: ok = true, msg %+v", name, m)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseIgnoresSelf(t *testing.T) {
|
||||
byNumber := `{"envelope":{"sourceNumber":"+33600000000","sourceUuid":"bot-aci","timestamp":1,"dataMessage":{"message":"echo"}},"account":"+33600000000"}`
|
||||
byACI := `{"envelope":{"sourceNumber":"","sourceUuid":"bot-aci","timestamp":1,"dataMessage":{"message":"echo"}},"account":"+33600000000"}`
|
||||
for _, env := range []string{byNumber, byACI} {
|
||||
if _, ok := parseEnvelope([]byte(env), self); ok {
|
||||
t.Errorf("own message accepted: %s", env)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseUnicode(t *testing.T) {
|
||||
env := `{"envelope":{"sourceNumber":"+33600000001","timestamp":1,"dataMessage":{"message":"é 🎬\nx"}},"account":"+33600000000"}`
|
||||
m, ok := parseEnvelope([]byte(env), self)
|
||||
if !ok || m.Text != "é 🎬\nx" {
|
||||
t.Fatalf("got %q, %v", m.Text, ok)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReplyTargets(t *testing.T) {
|
||||
f := newFakeAPI(t)
|
||||
f.accounts(`[{"number":"+33600000000"}]`)
|
||||
got := f.captureSend(t, 201, ``)
|
||||
c := mustNew(t, f.URL)
|
||||
|
||||
for _, env := range []string{groupEnvelope, directEnvelope} {
|
||||
m, ok := parseEnvelope([]byte(env), self)
|
||||
if !ok {
|
||||
t.Fatal("parse failed")
|
||||
}
|
||||
m.client = c
|
||||
if err := m.Reply(context.Background(), "ack"); err != nil {
|
||||
t.Fatalf("Reply: %v", err)
|
||||
}
|
||||
want := m.GroupID
|
||||
if want == "" {
|
||||
want = m.From
|
||||
}
|
||||
rcpt := (*got)["recipients"].([]any)
|
||||
if len(rcpt) != 1 || rcpt[0] != want {
|
||||
t.Errorf("recipients = %v, want [%s]", rcpt, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user