mirror of
https://github.com/lukaszraczylo/go-telegram.git
synced 2026-06-05 22:43:59 +00:00
3c04d7b0b1
The Telegram docs describe many string fields and parameters with
phrases like "can be ..., or ...", "must be one of ...", or "always X",
yet the generated Go API surface used raw `string` for every one of
them. Callers had to write magic strings or `string(api.ChatTypePrivate)`
to satisfy the field type. This change makes those fields typed Go
string enums emitted from the IR, so the IDE autocompletes valid values
and breaking-value drift surfaces at compile time.
Pipeline changes:
- internal/spec/ir.go: Field gains EnumValues []string. Empty for non-
enum fields; otherwise the wire-level values in doc order, deduped.
- cmd/scrape/enums.go: extractEnumValues recognises the curly-quoted
patterns Telegram uses ("can be either", "currently can be", "one
of", "must be", "always X") and rejects free-text quoted refs (e.g.
"Can be available only for X") via a tight gap check between the
trigger phrase and the first quoted value. parse_mode parameters
get the canonical Markdown / MarkdownV2 / HTML triple injected
because Telegram links to a separate formatting-options section
instead of listing values inline.
- cmd/genapi/enums.go: planEnums groups fields by sorted value-tuple,
picks a canonical Go enum name (most-common candidate, parent-
prefixed beats plain, shortest beats longer, alphabetical for
determinism), resolves cross-group name collisions by parent prefix.
- cmd/genapi/emitter.go + templates: goField rewrites the field type
to the planned enum name; multipartFieldEntry casts typed enum
values back to string when composing the wire map; enums.tmpl now
iterates the planned enums instead of hardcoding four hand-curated
ones; sentinelForField produces typed-constant test fixtures.
- api/enums.gen.go: regenerated from the live IR. 66 enum types, 155
constants. ParseMode, ChatType, MessageEntityType, ChatMember /
MessageOrigin / PaidMedia / Background / StoryAreaType / Reaction /
TransactionPartner / PassportElement variant Status & Type fields
are now typed.
- api/enums.go: hand-coded UpdateType (used by transport.LongPoller).
The Telegram docs do not enumerate Update payload kinds inline, so
the codegen pipeline cannot synthesise this enum.
- api/types.gen.go, api/methods.gen.go, api/methods_gen_test.go: 137
field declarations rewritten string -> typed enum.
- dispatch/, examples/: dropped every string(api.<Const>) cast. The
HasEntity filter now takes api.MessageEntityType; ChatType filter
compares typed values directly. ChatMember discriminator filter
casts variant.Status (typed per variant) to string for comparison.
- internal/spec/api.json, testdata/golden/*: regenerated and
refreshed. make regen-from-fixture is byte-deterministic across
runs.
Renames (no compat shims; v1 pre-public):
- EntityX -> MessageEntityTypeX (e.g. EntityBotCommand -> MessageEntityTypeBotCommand)
- EntityStrike -> MessageEntityTypeStrikethrough (full wire name)
114 lines
3.8 KiB
Go
114 lines
3.8 KiB
Go
// Code generated by cmd/genapi. DO NOT EDIT.
|
|
|
|
//go:build !ignore_autogenerated
|
|
|
|
package api
|
|
|
|
import (
|
|
"context"
|
|
"github.com/goccy/go-json"
|
|
"strconv"
|
|
|
|
"github.com/lukaszraczylo/go-telegram/client"
|
|
)
|
|
|
|
var _ = strconv.Itoa // keep import for multipart helpers
|
|
var _ = json.Marshal // keep import for complex multipart fields
|
|
|
|
// GetMeParams is the parameter set for GetMe.
|
|
//
|
|
// A simple method for testing your bot's authentication token. Requires no parameters. Returns basic information about the bot in form of a User object.
|
|
type GetMeParams struct {
|
|
}
|
|
|
|
// GetMe calls the getMe Telegram Bot API method.
|
|
//
|
|
// A simple method for testing your bot's authentication token. Requires no parameters. Returns basic information about the bot in form of a User object.
|
|
func GetMe(ctx context.Context, b *client.Bot, p *GetMeParams) (*User, error) {
|
|
return client.Call[*GetMeParams, *User](ctx, b, "getMe", p)
|
|
}
|
|
|
|
// SendMessageParams is the parameter set for SendMessage.
|
|
//
|
|
// Use this method to send text messages. On success, the sent Message is returned.
|
|
type SendMessageParams struct {
|
|
// Unique identifier for the target chat.
|
|
ChatID ChatID `json:"chat_id"`
|
|
// Text of the message to be sent.
|
|
Text string `json:"text"`
|
|
// Mode for parsing entities in the message text.
|
|
ParseMode ParseMode `json:"parse_mode,omitempty"`
|
|
}
|
|
|
|
// SendMessage calls the sendMessage Telegram Bot API method.
|
|
//
|
|
// Use this method to send text messages. On success, the sent Message is returned.
|
|
func SendMessage(ctx context.Context, b *client.Bot, p *SendMessageParams) (*Message, error) {
|
|
return client.Call[*SendMessageParams, *Message](ctx, b, "sendMessage", p)
|
|
}
|
|
|
|
// SendDocumentParams is the parameter set for SendDocument.
|
|
//
|
|
// Use this method to send general files. On success, the sent Message is returned.
|
|
type SendDocumentParams struct {
|
|
// Unique identifier for the target chat.
|
|
ChatID int64 `json:"chat_id"`
|
|
// File to send.
|
|
Document *InputFile `json:"document"`
|
|
// Document caption.
|
|
Caption string `json:"caption,omitempty"`
|
|
}
|
|
|
|
// HasFile reports whether a multipart upload is required.
|
|
func (p *SendDocumentParams) HasFile() bool {
|
|
if p.Document != nil && p.Document.IsLocalUpload() {
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
// MultipartFields returns the non-file fields used in the multipart body.
|
|
func (p *SendDocumentParams) MultipartFields() map[string]string {
|
|
out := map[string]string{}
|
|
out["chat_id"] = strconv.FormatInt(p.ChatID, 10)
|
|
if p.Caption != "" {
|
|
out["caption"] = p.Caption
|
|
}
|
|
return out
|
|
}
|
|
|
|
// MultipartFiles returns the file parts.
|
|
func (p *SendDocumentParams) MultipartFiles() []client.MultipartFile {
|
|
var files []client.MultipartFile
|
|
if p.Document != nil && p.Document.IsLocalUpload() {
|
|
name := p.Document.Filename
|
|
if name == "" {
|
|
name = "document"
|
|
}
|
|
files = append(files, client.MultipartFile{FieldName: "document", Filename: name, Reader: p.Document.Reader})
|
|
}
|
|
return files
|
|
}
|
|
|
|
// SendDocument calls the sendDocument Telegram Bot API method.
|
|
//
|
|
// Use this method to send general files. On success, the sent Message is returned.
|
|
func SendDocument(ctx context.Context, b *client.Bot, p *SendDocumentParams) (*Message, error) {
|
|
return client.Call[*SendDocumentParams, *Message](ctx, b, "sendDocument", p)
|
|
}
|
|
|
|
// GetUpdatesParams is the parameter set for GetUpdates.
|
|
//
|
|
// Use this method to receive incoming updates using long polling. Returns an Array of Update objects.
|
|
type GetUpdatesParams struct {
|
|
// Limits the number of updates to be retrieved.
|
|
Limit *int64 `json:"limit,omitempty"`
|
|
}
|
|
|
|
// GetUpdates calls the getUpdates Telegram Bot API method.
|
|
//
|
|
// Use this method to receive incoming updates using long polling. Returns an Array of Update objects.
|
|
func GetUpdates(ctx context.Context, b *client.Bot, p *GetUpdatesParams) ([]Update, error) {
|
|
return client.Call[*GetUpdatesParams, []Update](ctx, b, "getUpdates", p)
|
|
}
|