Merge pull request #1 from lukaszraczylo/feat/typed-allowed-updates

feat(api): type AllowedUpdates as []UpdateType for compile-time typo …
This commit is contained in:
2026-05-09 20:53:45 +01:00
committed by GitHub
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
@@ -835,6 +835,20 @@ func goFieldX(plan *enumPlan, enumParent, overrideParent string, f spec.Field) s
if name := fieldTypeOverride(overrideParent, 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 {