feat(api): type AllowedUpdates as []UpdateType for compile-time typo safety

Both SetWebhookParams.AllowedUpdates and GetUpdatesParams.AllowedUpdates
(plus the WebhookInfo.AllowedUpdates field on the response side) were
typed []string, forcing callers to cast every typed constant:

  AllowedUpdates: []string{
      string(api.UpdateMessage),
      string(api.UpdateMyChatMember),
      ...
  }

Switch to []UpdateType so the same call site is typo-safe end-to-end:

  AllowedUpdates: []UpdateType{
      api.UpdateMessage,
      api.UpdateMyChatMember,
      ...
  }

Wire format is unchanged — UpdateType is type UpdateType string, marshals
identically as JSON strings. The MultipartFields()/runtime.go encoding
paths likewise continue to work via json.Marshal on the typed slice.

Implementation note: api/methods.gen.go and api/types.gen.go are
generated by cmd/genapi from internal/spec/api.json, where the field is
described as Array of String. The Telegram docs do not enumerate the
allowed_updates values inline, so the scraper cannot synthesise the
enum and UpdateType lives hand-curated in api/enums.go (see existing
doc comment there). The retype is therefore done as a small pinned
override inside cmd/genapi/emitter.go's goField — keyed on the wire
field name allowed_updates with elem type string — so the change
survives a future regeneration of the .gen.go files. transport/longpoll
drops the now-unnecessary []string conversion.

Backward incompatibility: callers passing untyped []string variables
will need to convert; callers using untyped string literals inside the
slice ARE fine because Go's untyped-literal rule auto-converts.
This commit is contained in:
2026-05-09 20:20:30 +01:00
parent 6f9b29ea0c
commit 62c76e7e4e
4 changed files with 18 additions and 8 deletions
+2 -2
View File
@@ -27,7 +27,7 @@ type GetUpdatesParams struct {
// Timeout in seconds for long polling. Defaults to 0, i.e. usual short polling. Should be positive, short polling should be used for testing purposes only.
Timeout *int64 `json:"timeout,omitempty"`
// A JSON-serialized list of the update types you want your bot to receive. For example, specify ["message", "edited_channel_post", "callback_query"] to only receive updates of these types. See Update for a complete list of available update types. Specify an empty list to receive all update types except chat_member, message_reaction, and message_reaction_count (default). If not specified, the previous setting will be used.Please note that this parameter doesn't affect updates created before the call to getUpdates, so unwanted updates may be received for a short period of time.
AllowedUpdates []string `json:"allowed_updates,omitempty"`
AllowedUpdates []UpdateType `json:"allowed_updates,omitempty"`
}
// GetUpdates calls the getUpdates Telegram Bot API method.
@@ -54,7 +54,7 @@ type SetWebhookParams struct {
// The maximum allowed number of simultaneous HTTPS connections to the webhook for update delivery, 1-100. Defaults to 40. Use lower values to limit the load on your bot's server, and higher values to increase your bot's throughput.
MaxConnections *int64 `json:"max_connections,omitempty"`
// A JSON-serialized list of the update types you want your bot to receive. For example, specify ["message", "edited_channel_post", "callback_query"] to only receive updates of these types. See Update for a complete list of available update types. Specify an empty list to receive all update types except chat_member, message_reaction, and message_reaction_count (default). If not specified, the previous setting will be used.Please note that this parameter doesn't affect updates created before the call to the setWebhook, so unwanted updates may be received for a short period of time.
AllowedUpdates []string `json:"allowed_updates,omitempty"`
AllowedUpdates []UpdateType `json:"allowed_updates,omitempty"`
// Pass True to drop all pending updates
DropPendingUpdates *bool `json:"drop_pending_updates,omitempty"`
// A secret token to be sent in a header “X-Telegram-Bot-Api-Secret-Token” in every webhook request, 1-256 characters. Only characters A-Z, a-z, 0-9, _ and - are allowed. The header is useful to ensure that the request comes from a webhook set by you.
+1 -1
View File
@@ -91,7 +91,7 @@ type WebhookInfo struct {
// Optional. The maximum allowed number of simultaneous HTTPS connections to the webhook for update delivery
MaxConnections *int64 `json:"max_connections,omitempty"`
// Optional. A list of update types the bot is subscribed to. Defaults to all update types except chat_member
AllowedUpdates []string `json:"allowed_updates,omitempty"`
AllowedUpdates []UpdateType `json:"allowed_updates,omitempty"`
}
// This object represents a Telegram user or bot.
+14
View File
@@ -781,6 +781,20 @@ func goField(plan *enumPlan, parent string, f spec.Field) string {
if name := plan.FieldEnum(parent, f.Name); name != "" {
return fmt.Sprintf("%s %s %s", f.Name, name, tag)
}
// Pinned companion-enum retype: allowed_updates is an Array of String
// in the upstream spec, but the Go API exposes a hand-curated
// UpdateType (api/enums.go) since the values are not enumerated
// inline by Telegram. Retype []string → []UpdateType wherever the
// wire field is allowed_updates so callers can pass typed constants
// (api.UpdateMessage, ...) without string casts. Wire format is
// unchanged: UpdateType is a typed string, marshals identically.
if f.JSONName == "allowed_updates" &&
f.Type.Kind == spec.KindArray &&
f.Type.ElemType != nil &&
f.Type.ElemType.Kind == spec.KindPrimitive &&
f.Type.ElemType.Name == "string" {
return fmt.Sprintf("%s []UpdateType %s", f.Name, tag)
}
return fmt.Sprintf("%s %s %s", f.Name, goType(f.Type, !f.Required), tag)
}
+1 -5
View File
@@ -72,11 +72,7 @@ func (p *LongPoller) Run(ctx context.Context) error {
params.Timeout = &to
}
if len(p.AllowedTypes) > 0 {
allowed := make([]string, len(p.AllowedTypes))
for i, t := range p.AllowedTypes {
allowed[i] = string(t)
}
params.AllowedUpdates = allowed
params.AllowedUpdates = p.AllowedTypes
}
ups, err := api.GetUpdates(ctx, p.Bot, params)
if err != nil {