- handler panic log includes the stack trace and no longer the sender number - error and exception frames from the service are logged at Warn level - Listen looks the account up again after a connection loss (re-link) - document that received but unhandled messages are lost at shutdown Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
121 lines
3.1 KiB
Go
121 lines
3.1 KiB
Go
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
|
|
}
|
|
|
|
// serviceError extracts an error reported by the service on the receive
|
|
// websocket: a top-level "error" (string or {"message"}), or a signal-cli
|
|
// "exception" (e.g. a message that could not be decrypted).
|
|
func serviceError(data []byte) (string, bool) {
|
|
var f struct {
|
|
Error json.RawMessage `json:"error"`
|
|
Exception *struct {
|
|
Message string `json:"message"`
|
|
Type string `json:"type"`
|
|
} `json:"exception"`
|
|
}
|
|
if json.Unmarshal(data, &f) != nil {
|
|
return "", false
|
|
}
|
|
if len(f.Error) > 0 && string(f.Error) != "null" {
|
|
var s string
|
|
if json.Unmarshal(f.Error, &s) == nil {
|
|
return s, true
|
|
}
|
|
var o struct {
|
|
Message string `json:"message"`
|
|
}
|
|
if json.Unmarshal(f.Error, &o) == nil && o.Message != "" {
|
|
return o.Message, true
|
|
}
|
|
return string(f.Error), true
|
|
}
|
|
if f.Exception != nil {
|
|
if f.Exception.Type != "" {
|
|
return f.Exception.Type + ": " + f.Exception.Message, true
|
|
}
|
|
return f.Exception.Message, true
|
|
}
|
|
return "", false
|
|
}
|