diff --git a/data_types.go b/data_types.go index c557e8a..12820f4 100644 --- a/data_types.go +++ b/data_types.go @@ -2,6 +2,7 @@ package groupme import ( + "encoding/json" "regexp" "time" ) @@ -23,6 +24,20 @@ func (id ID) String() string { return string(id) } +// UnmarshalJSON also accepts numeric IDs, which GroupMe relays unchanged from +// clients that send them. +func (id *ID) UnmarshalJSON(data []byte) error { + if len(data) > 0 && data[0] != '"' { + var num json.Number + if err := json.Unmarshal(data, &num); err != nil { + return err + } + *id = ID(num) + return nil + } + return json.Unmarshal(data, (*string)(id)) +} + // Timestamp is the number of seconds since the UNIX epoch type Timestamp uint64 diff --git a/real_time.go b/real_time.go index 93c5fba..886d596 100644 --- a/real_time.go +++ b/real_time.go @@ -119,6 +119,12 @@ func NewPushSubscription(context context.Context) *PushSubscription { return &r } +func (r *PushSubscription) reportError(err error) { + for _, h := range r.handlers { + h.HandleError(err) + } +} + func (r *PushSubscription) AddHandler(h Handler) { r.handlers = append(r.handlers, h) } diff --git a/real_time_handler.go b/real_time_handler.go index 5ef20ac..507cd97 100644 --- a/real_time_handler.go +++ b/real_time_handler.go @@ -2,6 +2,7 @@ package groupme import ( "encoding/json" + "fmt" "strconv" ) @@ -59,7 +60,10 @@ func init() { return } var out Message - if json.Unmarshal(b, &out) != nil || out.ID == "" { + if err := json.Unmarshal(b, &out); err != nil { + r.reportError(fmt.Errorf("failed to decode GroupMe push message: %w", err)) + return + } else if out.ID == "" { return } if out.ConversationID == "" {