mirror of
https://github.com/lukaszraczylo/go-telegram.git
synced 2026-07-01 19:05:03 +00:00
feat(api): add api.Ptr helper for optional scalar fields
Telegram's optional int/bool/float fields are pointers so callers can explicitly send false or 0 to override a chat default — distinct from 'absent', which uses the chat default. The pointer construction has been ergonomically painful: photoLimit := int64(5) Limit: &photoLimit api.Ptr[T any](v T) *T collapses that to a single line: Limit: api.Ptr[int64](5) DisableNotification: api.Ptr(true) Pointers stay because the explicit-zero distinction matters for fields like DisableNotification, ProtectContent, and getUpdates.Offset where sending 0 / false explicitly is semantically different from omitting the field.
This commit is contained in:
@@ -141,6 +141,25 @@ Run any example: `TELEGRAM_BOT_TOKEN=xxx go run ./examples/<name>`
|
||||
| | [`polls`](examples/polls) | `sendPoll` and answer tally |
|
||||
| | [`payments`](examples/payments) | Invoice → pre-checkout → success |
|
||||
|
||||
## Optional fields
|
||||
|
||||
Telegram marks many fields as optional. For optional **scalars** (int, bool, float) we use pointers so you can explicitly send `false` or `0` when the wire format needs to override a chat default. The `api.Ptr` helper keeps that ergonomic:
|
||||
|
||||
```go
|
||||
api.SendMessage(ctx, bot, &api.SendMessageParams{
|
||||
ChatID: api.ChatIDFromInt(chatID),
|
||||
Text: "hi",
|
||||
DisableNotification: api.Ptr(true), // type inferred
|
||||
})
|
||||
|
||||
api.GetUserProfilePhotos(ctx, bot, &api.GetUserProfilePhotosParams{
|
||||
UserID: userID,
|
||||
Limit: api.Ptr[int64](5), // explicit type for untyped literals
|
||||
})
|
||||
```
|
||||
|
||||
Optional structs and slices are already nullable in Go — no helper needed.
|
||||
|
||||
## Reference docs
|
||||
|
||||
Full API reference is auto-generated from source comments and lives in [`docs/reference/`](docs/reference/README.md) — browse package by package on GitHub, or read it rendered at [go-telegram.raczylo.com](https://go-telegram.raczylo.com/) and [pkg.go.dev](https://pkg.go.dev/github.com/lukaszraczylo/go-telegram).
|
||||
|
||||
Reference in New Issue
Block a user