From 7ccd067fe19eb0800541597dd274b054ff49ce58 Mon Sep 17 00:00:00 2001 From: darxnet Date: Mon, 16 Nov 2026 12:41:50 +0100 Subject: [PATCH] fix: decode draft_id sent as a string in stopped_message_generation The Bot API server serializes MessageGenerationStopped.draft_id as a JSON string although it is documented as an Integer (tdlib/telegram-bot-api#945, Client.cpp uses td::to_string). The update failed to decode, was skipped as UndecodableUpdateError and HandleOnStoppedMessageGeneration never ran, so a bot kept generating after the user pressed Stop. draft_id now accepts both a number and a numeric string; DraftID stays int64. Co-Authored-By: Claude Opus 5.5 --- types_message_generation_stopped.go | 53 +++++++++++++++ types_message_generation_stopped_test.go | 82 ++++++++++++++++++++++++ 2 files changed, 135 insertions(+) create mode 100644 types_message_generation_stopped.go create mode 100644 types_message_generation_stopped_test.go diff --git a/types_message_generation_stopped.go b/types_message_generation_stopped.go new file mode 100644 index 0000000..7428ed2 --- /dev/null +++ b/types_message_generation_stopped.go @@ -0,0 +1,53 @@ +package gogram + +import ( + "encoding/json" + "fmt" + "strconv" +) + +// UnmarshalJSON implements [json.Unmarshaler]. The Bot API server sends +// draft_id as a JSON string although it is documented as an Integer +// (tdlib/telegram-bot-api#945), so both forms are accepted; without this +// the whole stopped_message_generation update fails to decode and is +// skipped. +func (r *MessageGenerationStopped) UnmarshalJSON(data []byte) error { + type plain MessageGenerationStopped + + var v struct { + *plain + + DraftID json.RawMessage `json:"draft_id"` + } + + v.plain = (*plain)(r) + + if err := json.Unmarshal(data, &v); err != nil { + return err + } + + r.DraftID = 0 + + raw := v.DraftID + if len(raw) == 0 || string(raw) == "null" { + return nil + } + + if raw[0] == '"' { + var s string + if err := json.Unmarshal(raw, &s); err != nil { + return err + } + + raw = []byte(s) + } + + id, err := strconv.ParseInt(string(raw), 10, 64) + if err != nil { + return fmt.Errorf("gogram: draft_id %s: %w", v.DraftID, err) + } + + r.DraftID = id + + return nil +} diff --git a/types_message_generation_stopped_test.go b/types_message_generation_stopped_test.go new file mode 100644 index 0000000..69f5d52 --- /dev/null +++ b/types_message_generation_stopped_test.go @@ -0,0 +1,82 @@ +package gogram_test + +import ( + "encoding/json" + "testing" + + "github.com/darxnet/gogram" +) + +func TestMessageGenerationStopped_DraftID(t *testing.T) { + t.Parallel() + + for raw, want := range map[string]int64{ + `"42"`: 42, // what the Bot API server sends (tdlib/telegram-bot-api#945) + `42`: 42, // what the documentation promises + `"-7"`: -7, + `"9223372036854775807"`: 9223372036854775807, + `null`: 0, + } { + var update gogram.Update + + data := `{"update_id":1,"stopped_message_generation":{"chat":{"id":5,"type":"private"},` + + `"message_thread_id":3,"draft_id":` + raw + `}}` + if err := json.Unmarshal([]byte(data), &update); err != nil { + t.Fatalf("draft_id %s: %v", raw, err) + } + + got := update.StoppedMessageGeneration + if got == nil || got.DraftID != want || got.Chat.ID != 5 || got.MessageThreadID != 3 { + t.Fatalf("draft_id %s: got %+v, want DraftID %d", raw, got, want) + } + } + + var stopped gogram.MessageGenerationStopped + if err := json.Unmarshal([]byte(`{"chat":{"id":5,"type":"private"}}`), &stopped); err != nil || stopped.DraftID != 0 { + t.Fatalf("missing draft_id: %+v, %v", stopped, err) + } + + for _, raw := range []string{`"x"`, `""`, `"4.2"`, `4.2`, `true`, `"9223372036854775808"`} { + data := `{"chat":{"id":5,"type":"private"},"draft_id":` + raw + `}` + if err := json.Unmarshal([]byte(data), &stopped); err == nil { + t.Fatalf("draft_id %s: no error", raw) + } + } + + // Marshalling keeps the documented number. + out, err := json.Marshal(&gogram.MessageGenerationStopped{DraftID: 42}) + if err != nil { + t.Fatal(err) + } + + var back map[string]any + if err := json.Unmarshal(out, &back); err != nil || back["draft_id"] != float64(42) { + t.Fatalf("marshal = %s", out) + } +} + +func TestStoppedMessageGeneration_StringDraftIDReachesHandler(t *testing.T) { + t.Parallel() + + _, client := newRecorderClient(t) + + var draftID int64 + + router := gogram.NewRouter() + router.HandleOnStoppedMessageGeneration(func(_ *gogram.Context, m *gogram.MessageGenerationStopped) error { + draftID = m.DraftID + return nil + }) + + var update gogram.Update + if err := json.Unmarshal([]byte(`{"update_id":1,"stopped_message_generation":`+ + `{"chat":{"id":5,"type":"private"},"draft_id":"42"}}`), &update); err != nil { + t.Fatal(err) + } + + router.Process(gogram.NewTestContext(t.Context(), client, &update)) + + if draftID != 42 { + t.Fatalf("handler got draft_id %d, want 42", draftID) + } +}