Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions types_message_generation_stopped.go
Original file line number Diff line number Diff line change
@@ -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
}
82 changes: 82 additions & 0 deletions types_message_generation_stopped_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
Loading