From d29e60be28cf73a56c75e2560f631d59d5b8e39b Mon Sep 17 00:00:00 2001 From: Paul Larsen Date: Tue, 18 Jun 2024 13:28:34 +0200 Subject: [PATCH] Regenerate library for bot API 7.5 --- gen_methods.go | 68 ++++++- gen_types.go | 516 ++++++++++++++++++++++++++++++++++++++++++++++++- spec_commit | 2 +- 3 files changed, 580 insertions(+), 6 deletions(-) diff --git a/gen_methods.go b/gen_methods.go index 18107fd..8264121 100755 --- a/gen_methods.go +++ b/gen_methods.go @@ -1299,6 +1299,8 @@ func (bot *Bot) EditGeneralForumTopic(chatId int64, name string, opts *EditGener // EditMessageCaptionOpts is the set of optional fields for Bot.EditMessageCaption. type EditMessageCaptionOpts struct { + // Unique identifier of the business connection on behalf of which the message to be edited was sent + BusinessConnectionId string // Required if inline_message_id is not specified. Unique identifier for the target chat ChatId int64 // Required if inline_message_id is not specified. Identifier of the message to edit @@ -1321,11 +1323,12 @@ type EditMessageCaptionOpts struct { // EditMessageCaption (https://core.telegram.org/bots/api#editmessagecaption) // -// Use this method to edit captions of messages. On success, if the edited message is not an inline message, the edited Message is returned, otherwise True is returned. +// Use this method to edit captions of messages. On success, if the edited message is not an inline message, the edited Message is returned, otherwise True is returned. Note that business messages that were not sent by the bot and do not contain an inline keyboard can only be edited within 48 hours from the time they were sent. // - opts (type EditMessageCaptionOpts): All optional parameters. func (bot *Bot) EditMessageCaption(opts *EditMessageCaptionOpts) (*Message, bool, error) { v := map[string]string{} if opts != nil { + v["business_connection_id"] = opts.BusinessConnectionId if opts.ChatId != 0 { v["chat_id"] = strconv.FormatInt(opts.ChatId, 10) } @@ -1374,6 +1377,8 @@ func (bot *Bot) EditMessageCaption(opts *EditMessageCaptionOpts) (*Message, bool // EditMessageLiveLocationOpts is the set of optional fields for Bot.EditMessageLiveLocation. type EditMessageLiveLocationOpts struct { + // Unique identifier of the business connection on behalf of which the message to be edited was sent + BusinessConnectionId string // Required if inline_message_id is not specified. Unique identifier for the target chat ChatId int64 // Required if inline_message_id is not specified. Identifier of the message to edit @@ -1405,6 +1410,7 @@ func (bot *Bot) EditMessageLiveLocation(latitude float64, longitude float64, opt v["latitude"] = strconv.FormatFloat(latitude, 'f', -1, 64) v["longitude"] = strconv.FormatFloat(longitude, 'f', -1, 64) if opts != nil { + v["business_connection_id"] = opts.BusinessConnectionId if opts.ChatId != 0 { v["chat_id"] = strconv.FormatInt(opts.ChatId, 10) } @@ -1455,6 +1461,8 @@ func (bot *Bot) EditMessageLiveLocation(latitude float64, longitude float64, opt // EditMessageMediaOpts is the set of optional fields for Bot.EditMessageMedia. type EditMessageMediaOpts struct { + // Unique identifier of the business connection on behalf of which the message to be edited was sent + BusinessConnectionId string // Required if inline_message_id is not specified. Unique identifier for the target chat ChatId int64 // Required if inline_message_id is not specified. Identifier of the message to edit @@ -1469,7 +1477,7 @@ type EditMessageMediaOpts struct { // EditMessageMedia (https://core.telegram.org/bots/api#editmessagemedia) // -// Use this method to edit animation, audio, document, photo, or video messages. If a message is part of a message album, then it can be edited only to an audio for audio albums, only to a document for document albums and to a photo or a video otherwise. When an inline message is edited, a new file can't be uploaded; use a previously uploaded file via its file_id or specify a URL. On success, if the edited message is not an inline message, the edited Message is returned, otherwise True is returned. +// Use this method to edit animation, audio, document, photo, or video messages. If a message is part of a message album, then it can be edited only to an audio for audio albums, only to a document for document albums and to a photo or a video otherwise. When an inline message is edited, a new file can't be uploaded; use a previously uploaded file via its file_id or specify a URL. On success, if the edited message is not an inline message, the edited Message is returned, otherwise True is returned. Note that business messages that were not sent by the bot and do not contain an inline keyboard can only be edited within 48 hours from the time they were sent. // - media (type InputMedia): A JSON-serialized object for a new media content of the message // - opts (type EditMessageMediaOpts): All optional parameters. func (bot *Bot) EditMessageMedia(media InputMedia, opts *EditMessageMediaOpts) (*Message, bool, error) { @@ -1481,6 +1489,7 @@ func (bot *Bot) EditMessageMedia(media InputMedia, opts *EditMessageMediaOpts) ( } v["media"] = string(inputBs) if opts != nil { + v["business_connection_id"] = opts.BusinessConnectionId if opts.ChatId != 0 { v["chat_id"] = strconv.FormatInt(opts.ChatId, 10) } @@ -1519,6 +1528,8 @@ func (bot *Bot) EditMessageMedia(media InputMedia, opts *EditMessageMediaOpts) ( // EditMessageReplyMarkupOpts is the set of optional fields for Bot.EditMessageReplyMarkup. type EditMessageReplyMarkupOpts struct { + // Unique identifier of the business connection on behalf of which the message to be edited was sent + BusinessConnectionId string // Required if inline_message_id is not specified. Unique identifier for the target chat ChatId int64 // Required if inline_message_id is not specified. Identifier of the message to edit @@ -1533,11 +1544,12 @@ type EditMessageReplyMarkupOpts struct { // EditMessageReplyMarkup (https://core.telegram.org/bots/api#editmessagereplymarkup) // -// Use this method to edit only the reply markup of messages. On success, if the edited message is not an inline message, the edited Message is returned, otherwise True is returned. +// Use this method to edit only the reply markup of messages. On success, if the edited message is not an inline message, the edited Message is returned, otherwise True is returned. Note that business messages that were not sent by the bot and do not contain an inline keyboard can only be edited within 48 hours from the time they were sent. // - opts (type EditMessageReplyMarkupOpts): All optional parameters. func (bot *Bot) EditMessageReplyMarkup(opts *EditMessageReplyMarkupOpts) (*Message, bool, error) { v := map[string]string{} if opts != nil { + v["business_connection_id"] = opts.BusinessConnectionId if opts.ChatId != 0 { v["chat_id"] = strconv.FormatInt(opts.ChatId, 10) } @@ -1576,6 +1588,8 @@ func (bot *Bot) EditMessageReplyMarkup(opts *EditMessageReplyMarkupOpts) (*Messa // EditMessageTextOpts is the set of optional fields for Bot.EditMessageText. type EditMessageTextOpts struct { + // Unique identifier of the business connection on behalf of which the message to be edited was sent + BusinessConnectionId string // Required if inline_message_id is not specified. Unique identifier for the target chat ChatId int64 // Required if inline_message_id is not specified. Identifier of the message to edit @@ -1596,13 +1610,14 @@ type EditMessageTextOpts struct { // EditMessageText (https://core.telegram.org/bots/api#editmessagetext) // -// Use this method to edit text and game messages. On success, if the edited message is not an inline message, the edited Message is returned, otherwise True is returned. +// Use this method to edit text and game messages. On success, if the edited message is not an inline message, the edited Message is returned, otherwise True is returned. Note that business messages that were not sent by the bot and do not contain an inline keyboard can only be edited within 48 hours from the time they were sent. // - text (type string): New text of the message, 1-4096 characters after entities parsing // - opts (type EditMessageTextOpts): All optional parameters. func (bot *Bot) EditMessageText(text string, opts *EditMessageTextOpts) (*Message, bool, error) { v := map[string]string{} v["text"] = text if opts != nil { + v["business_connection_id"] = opts.BusinessConnectionId if opts.ChatId != 0 { v["chat_id"] = strconv.FormatInt(opts.ChatId, 10) } @@ -2289,6 +2304,45 @@ func (bot *Bot) GetMyShortDescription(opts *GetMyShortDescriptionOpts) (*BotShor return &b, json.Unmarshal(r, &b) } +// GetStarTransactionsOpts is the set of optional fields for Bot.GetStarTransactions. +type GetStarTransactionsOpts struct { + // Number of transactions to skip in the response + Offset int64 + // The maximum number of transactions to be retrieved. Values between 1-100 are accepted. Defaults to 100. + Limit int64 + // RequestOpts are an additional optional field to configure timeouts for individual requests + RequestOpts *RequestOpts +} + +// GetStarTransactions (https://core.telegram.org/bots/api#getstartransactions) +// +// Returns the bot's Telegram Star transactions in chronological order. On success, returns a StarTransactions object. +// - opts (type GetStarTransactionsOpts): All optional parameters. +func (bot *Bot) GetStarTransactions(opts *GetStarTransactionsOpts) (*StarTransactions, error) { + v := map[string]string{} + if opts != nil { + if opts.Offset != 0 { + v["offset"] = strconv.FormatInt(opts.Offset, 10) + } + if opts.Limit != 0 { + v["limit"] = strconv.FormatInt(opts.Limit, 10) + } + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.Request("getStarTransactions", v, nil, reqOpts) + if err != nil { + return nil, err + } + + var s StarTransactions + return &s, json.Unmarshal(r, &s) +} + // GetStickerSetOpts is the set of optional fields for Bot.GetStickerSet. type GetStickerSetOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests @@ -5686,6 +5740,8 @@ func (bot *Bot) SetWebhook(url string, opts *SetWebhookOpts) (bool, error) { // StopMessageLiveLocationOpts is the set of optional fields for Bot.StopMessageLiveLocation. type StopMessageLiveLocationOpts struct { + // Unique identifier of the business connection on behalf of which the message to be edited was sent + BusinessConnectionId string // Required if inline_message_id is not specified. Unique identifier for the target chat ChatId int64 // Required if inline_message_id is not specified. Identifier of the message with live location to stop @@ -5705,6 +5761,7 @@ type StopMessageLiveLocationOpts struct { func (bot *Bot) StopMessageLiveLocation(opts *StopMessageLiveLocationOpts) (*Message, bool, error) { v := map[string]string{} if opts != nil { + v["business_connection_id"] = opts.BusinessConnectionId if opts.ChatId != 0 { v["chat_id"] = strconv.FormatInt(opts.ChatId, 10) } @@ -5743,6 +5800,8 @@ func (bot *Bot) StopMessageLiveLocation(opts *StopMessageLiveLocationOpts) (*Mes // StopPollOpts is the set of optional fields for Bot.StopPoll. type StopPollOpts struct { + // Unique identifier of the business connection on behalf of which the message to be edited was sent + BusinessConnectionId string // A JSON-serialized object for a new message inline keyboard. ReplyMarkup InlineKeyboardMarkup // RequestOpts are an additional optional field to configure timeouts for individual requests @@ -5760,6 +5819,7 @@ func (bot *Bot) StopPoll(chatId int64, messageId int64, opts *StopPollOpts) (*Po v["chat_id"] = strconv.FormatInt(chatId, 10) v["message_id"] = strconv.FormatInt(messageId, 10) if opts != nil { + v["business_connection_id"] = opts.BusinessConnectionId bs, err := json.Marshal(opts.ReplyMarkup) if err != nil { return nil, fmt.Errorf("failed to marshal field reply_markup: %w", err) diff --git a/gen_types.go b/gen_types.go index 507bda0..9c2de73 100755 --- a/gen_types.go +++ b/gen_types.go @@ -2921,7 +2921,7 @@ type InlineKeyboardButton struct { Text string `json:"text"` // Optional. HTTP or tg:// URL to be opened when the button is pressed. Links tg://user?id= can be used to mention a user by their identifier without using a username, if this is allowed by their privacy settings. Url string `json:"url,omitempty"` - // Optional. Data to be sent in a callback query to the bot when button is pressed, 1-64 bytes. Not supported for messages sent on behalf of a Telegram Business account. + // Optional. Data to be sent in a callback query to the bot when the button is pressed, 1-64 bytes CallbackData string `json:"callback_data,omitempty"` // Optional. Description of the Web App that will be launched when the user presses the button. The Web App will be able to send an arbitrary message on behalf of the user using the method answerWebAppQuery. Available only in private chats between a user and the bot. Not supported for messages sent on behalf of a Telegram Business account. WebApp *WebAppInfo `json:"web_app,omitempty"` @@ -7509,6 +7509,225 @@ type ResponseParameters struct { RetryAfter int64 `json:"retry_after,omitempty"` } +// RevenueWithdrawalState (https://core.telegram.org/bots/api#revenuewithdrawalstate) +// +// This object describes the state of a revenue withdrawal operation. Currently, it can be one of +// - RevenueWithdrawalStatePending +// - RevenueWithdrawalStateSucceeded +// - RevenueWithdrawalStateFailed +type RevenueWithdrawalState interface { + GetType() string + // MergeRevenueWithdrawalState returns a MergedRevenueWithdrawalState struct to simplify working with complex telegram types in a non-generic world. + MergeRevenueWithdrawalState() MergedRevenueWithdrawalState + // revenueWithdrawalState exists to avoid external types implementing this interface. + revenueWithdrawalState() +} + +// Ensure that all subtypes correctly implement the parent interface. +var ( + _ RevenueWithdrawalState = RevenueWithdrawalStatePending{} + _ RevenueWithdrawalState = RevenueWithdrawalStateSucceeded{} + _ RevenueWithdrawalState = RevenueWithdrawalStateFailed{} +) + +// MergedRevenueWithdrawalState is a helper type to simplify interactions with the various RevenueWithdrawalState subtypes. +type MergedRevenueWithdrawalState struct { + // Type of the state + Type string `json:"type"` + // Optional. Date the withdrawal was completed in Unix time (Only for succeeded) + Date int64 `json:"date,omitempty"` + // Optional. An HTTPS URL that can be used to see transaction details (Only for succeeded) + Url string `json:"url,omitempty"` +} + +// GetType is a helper method to easily access the common fields of an interface. +func (v MergedRevenueWithdrawalState) GetType() string { + return v.Type +} + +// MergedRevenueWithdrawalState.revenueWithdrawalState is a dummy method to avoid interface implementation. +func (v MergedRevenueWithdrawalState) revenueWithdrawalState() {} + +// MergeRevenueWithdrawalState returns a MergedRevenueWithdrawalState struct to simplify working with types in a non-generic world. +func (v MergedRevenueWithdrawalState) MergeRevenueWithdrawalState() MergedRevenueWithdrawalState { + return v +} + +// unmarshalRevenueWithdrawalStateArray is a JSON unmarshalling helper which allows unmarshalling an array of interfaces +// using unmarshalRevenueWithdrawalState. +func unmarshalRevenueWithdrawalStateArray(d json.RawMessage) ([]RevenueWithdrawalState, error) { + if len(d) == 0 { + return nil, nil + } + + var ds []json.RawMessage + err := json.Unmarshal(d, &ds) + if err != nil { + return nil, fmt.Errorf("failed to unmarshal initial RevenueWithdrawalState JSON into an array: %w", err) + } + + var vs []RevenueWithdrawalState + for idx, d := range ds { + v, err := unmarshalRevenueWithdrawalState(d) + if err != nil { + return nil, fmt.Errorf("failed to unmarshal RevenueWithdrawalState on array item %d: %w", idx, err) + } + vs = append(vs, v) + } + + return vs, nil +} + +// unmarshalRevenueWithdrawalState is a JSON unmarshal helper to marshal the right structs into a RevenueWithdrawalState interface +// based on the Type field. +func unmarshalRevenueWithdrawalState(d json.RawMessage) (RevenueWithdrawalState, error) { + if len(d) == 0 { + return nil, nil + } + + t := struct { + Type string + }{} + err := json.Unmarshal(d, &t) + if err != nil { + return nil, fmt.Errorf("failed to unmarshal RevenueWithdrawalState for constant field 'Type': %w", err) + } + + switch t.Type { + case "pending": + s := RevenueWithdrawalStatePending{} + err := json.Unmarshal(d, &s) + if err != nil { + return nil, fmt.Errorf("failed to unmarshal RevenueWithdrawalState for value 'pending': %w", err) + } + return s, nil + + case "succeeded": + s := RevenueWithdrawalStateSucceeded{} + err := json.Unmarshal(d, &s) + if err != nil { + return nil, fmt.Errorf("failed to unmarshal RevenueWithdrawalState for value 'succeeded': %w", err) + } + return s, nil + + case "failed": + s := RevenueWithdrawalStateFailed{} + err := json.Unmarshal(d, &s) + if err != nil { + return nil, fmt.Errorf("failed to unmarshal RevenueWithdrawalState for value 'failed': %w", err) + } + return s, nil + + } + return nil, fmt.Errorf("unknown interface for RevenueWithdrawalState with Type %v", t.Type) +} + +// RevenueWithdrawalStateFailed (https://core.telegram.org/bots/api#revenuewithdrawalstatefailed) +// +// The withdrawal failed and the transaction was refunded. +type RevenueWithdrawalStateFailed struct{} + +// GetType is a helper method to easily access the common fields of an interface. +func (v RevenueWithdrawalStateFailed) GetType() string { + return "failed" +} + +// MergeRevenueWithdrawalState returns a MergedRevenueWithdrawalState struct to simplify working with types in a non-generic world. +func (v RevenueWithdrawalStateFailed) MergeRevenueWithdrawalState() MergedRevenueWithdrawalState { + return MergedRevenueWithdrawalState{ + Type: "failed", + } +} + +// MarshalJSON is a custom JSON marshaller to allow for enforcing the Type value. +func (v RevenueWithdrawalStateFailed) MarshalJSON() ([]byte, error) { + type alias RevenueWithdrawalStateFailed + a := struct { + Type string `json:"type"` + alias + }{ + Type: "failed", + alias: (alias)(v), + } + return json.Marshal(a) +} + +// RevenueWithdrawalStateFailed.revenueWithdrawalState is a dummy method to avoid interface implementation. +func (v RevenueWithdrawalStateFailed) revenueWithdrawalState() {} + +// RevenueWithdrawalStatePending (https://core.telegram.org/bots/api#revenuewithdrawalstatepending) +// +// The withdrawal is in progress. +type RevenueWithdrawalStatePending struct{} + +// GetType is a helper method to easily access the common fields of an interface. +func (v RevenueWithdrawalStatePending) GetType() string { + return "pending" +} + +// MergeRevenueWithdrawalState returns a MergedRevenueWithdrawalState struct to simplify working with types in a non-generic world. +func (v RevenueWithdrawalStatePending) MergeRevenueWithdrawalState() MergedRevenueWithdrawalState { + return MergedRevenueWithdrawalState{ + Type: "pending", + } +} + +// MarshalJSON is a custom JSON marshaller to allow for enforcing the Type value. +func (v RevenueWithdrawalStatePending) MarshalJSON() ([]byte, error) { + type alias RevenueWithdrawalStatePending + a := struct { + Type string `json:"type"` + alias + }{ + Type: "pending", + alias: (alias)(v), + } + return json.Marshal(a) +} + +// RevenueWithdrawalStatePending.revenueWithdrawalState is a dummy method to avoid interface implementation. +func (v RevenueWithdrawalStatePending) revenueWithdrawalState() {} + +// RevenueWithdrawalStateSucceeded (https://core.telegram.org/bots/api#revenuewithdrawalstatesucceeded) +// +// The withdrawal succeeded. +type RevenueWithdrawalStateSucceeded struct { + // Date the withdrawal was completed in Unix time + Date int64 `json:"date"` + // An HTTPS URL that can be used to see transaction details + Url string `json:"url"` +} + +// GetType is a helper method to easily access the common fields of an interface. +func (v RevenueWithdrawalStateSucceeded) GetType() string { + return "succeeded" +} + +// MergeRevenueWithdrawalState returns a MergedRevenueWithdrawalState struct to simplify working with types in a non-generic world. +func (v RevenueWithdrawalStateSucceeded) MergeRevenueWithdrawalState() MergedRevenueWithdrawalState { + return MergedRevenueWithdrawalState{ + Type: "succeeded", + Date: v.Date, + Url: v.Url, + } +} + +// MarshalJSON is a custom JSON marshaller to allow for enforcing the Type value. +func (v RevenueWithdrawalStateSucceeded) MarshalJSON() ([]byte, error) { + type alias RevenueWithdrawalStateSucceeded + a := struct { + Type string `json:"type"` + alias + }{ + Type: "succeeded", + alias: (alias)(v), + } + return json.Marshal(a) +} + +// RevenueWithdrawalStateSucceeded.revenueWithdrawalState is a dummy method to avoid interface implementation. +func (v RevenueWithdrawalStateSucceeded) revenueWithdrawalState() {} + // SentWebAppMessage (https://core.telegram.org/bots/api#sentwebappmessage) // // Describes an inline message sent by a Web App on behalf of a user. @@ -7577,6 +7796,61 @@ type ShippingQuery struct { ShippingAddress ShippingAddress `json:"shipping_address"` } +// StarTransaction (https://core.telegram.org/bots/api#startransaction) +// +// Describes a Telegram Star transaction. +type StarTransaction struct { + // Unique identifier of the transaction. Coincides with the identifer of the original transaction for refund transactions. Coincides with SuccessfulPayment.telegram_payment_charge_id for successful incoming payments from users. + Id string `json:"id"` + // Number of Telegram Stars transferred by the transaction + Amount int64 `json:"amount"` + // Date the transaction was created in Unix time + Date int64 `json:"date"` + // Optional. Source of an incoming transaction (e.g., a user purchasing goods or services, Fragment refunding a failed withdrawal). Only for incoming transactions + Source TransactionPartner `json:"source,omitempty"` + // Optional. Receiver of an outgoing transaction (e.g., a user for a purchase refund, Fragment for a withdrawal). Only for outgoing transactions + Receiver TransactionPartner `json:"receiver,omitempty"` +} + +// UnmarshalJSON is a custom JSON unmarshaller to use the helpers which allow for unmarshalling structs into interfaces. +func (v *StarTransaction) UnmarshalJSON(b []byte) error { + // All fields in StarTransaction, with interface fields as json.RawMessage + type tmp struct { + Id string `json:"id"` + Amount int64 `json:"amount"` + Date int64 `json:"date"` + Source json.RawMessage `json:"source"` + Receiver json.RawMessage `json:"receiver"` + } + t := tmp{} + err := json.Unmarshal(b, &t) + if err != nil { + return fmt.Errorf("failed to unmarshal StarTransaction JSON into tmp struct: %w", err) + } + + v.Id = t.Id + v.Amount = t.Amount + v.Date = t.Date + v.Source, err = unmarshalTransactionPartner(t.Source) + if err != nil { + return fmt.Errorf("failed to unmarshal custom JSON field Source: %w", err) + } + v.Receiver, err = unmarshalTransactionPartner(t.Receiver) + if err != nil { + return fmt.Errorf("failed to unmarshal custom JSON field Receiver: %w", err) + } + + return nil +} + +// StarTransactions (https://core.telegram.org/bots/api#startransactions) +// +// Contains a list of Telegram Star transactions. +type StarTransactions struct { + // The list of transactions + Transactions []StarTransaction `json:"transactions,omitempty"` +} + // Sticker (https://core.telegram.org/bots/api#sticker) // // This object represents a sticker. @@ -7689,6 +7963,246 @@ type TextQuote struct { IsManual bool `json:"is_manual,omitempty"` } +// TransactionPartner (https://core.telegram.org/bots/api#transactionpartner) +// +// This object describes the source of a transaction, or its recipient for outgoing transactions. Currently, it can be one of +// - TransactionPartnerFragment +// - TransactionPartnerUser +// - TransactionPartnerOther +type TransactionPartner interface { + GetType() string + // MergeTransactionPartner returns a MergedTransactionPartner struct to simplify working with complex telegram types in a non-generic world. + MergeTransactionPartner() MergedTransactionPartner + // transactionPartner exists to avoid external types implementing this interface. + transactionPartner() +} + +// Ensure that all subtypes correctly implement the parent interface. +var ( + _ TransactionPartner = TransactionPartnerFragment{} + _ TransactionPartner = TransactionPartnerUser{} + _ TransactionPartner = TransactionPartnerOther{} +) + +// MergedTransactionPartner is a helper type to simplify interactions with the various TransactionPartner subtypes. +type MergedTransactionPartner struct { + // Type of the transaction partner + Type string `json:"type"` + // Optional. State of the transaction if the transaction is outgoing (Only for fragment) + WithdrawalState RevenueWithdrawalState `json:"withdrawal_state,omitempty"` + // Optional. Information about the user (Only for user) + User *User `json:"user,omitempty"` +} + +// GetType is a helper method to easily access the common fields of an interface. +func (v MergedTransactionPartner) GetType() string { + return v.Type +} + +// MergedTransactionPartner.transactionPartner is a dummy method to avoid interface implementation. +func (v MergedTransactionPartner) transactionPartner() {} + +// MergeTransactionPartner returns a MergedTransactionPartner struct to simplify working with types in a non-generic world. +func (v MergedTransactionPartner) MergeTransactionPartner() MergedTransactionPartner { + return v +} + +// unmarshalTransactionPartnerArray is a JSON unmarshalling helper which allows unmarshalling an array of interfaces +// using unmarshalTransactionPartner. +func unmarshalTransactionPartnerArray(d json.RawMessage) ([]TransactionPartner, error) { + if len(d) == 0 { + return nil, nil + } + + var ds []json.RawMessage + err := json.Unmarshal(d, &ds) + if err != nil { + return nil, fmt.Errorf("failed to unmarshal initial TransactionPartner JSON into an array: %w", err) + } + + var vs []TransactionPartner + for idx, d := range ds { + v, err := unmarshalTransactionPartner(d) + if err != nil { + return nil, fmt.Errorf("failed to unmarshal TransactionPartner on array item %d: %w", idx, err) + } + vs = append(vs, v) + } + + return vs, nil +} + +// unmarshalTransactionPartner is a JSON unmarshal helper to marshal the right structs into a TransactionPartner interface +// based on the Type field. +func unmarshalTransactionPartner(d json.RawMessage) (TransactionPartner, error) { + if len(d) == 0 { + return nil, nil + } + + t := struct { + Type string + }{} + err := json.Unmarshal(d, &t) + if err != nil { + return nil, fmt.Errorf("failed to unmarshal TransactionPartner for constant field 'Type': %w", err) + } + + switch t.Type { + case "fragment": + s := TransactionPartnerFragment{} + err := json.Unmarshal(d, &s) + if err != nil { + return nil, fmt.Errorf("failed to unmarshal TransactionPartner for value 'fragment': %w", err) + } + return s, nil + + case "user": + s := TransactionPartnerUser{} + err := json.Unmarshal(d, &s) + if err != nil { + return nil, fmt.Errorf("failed to unmarshal TransactionPartner for value 'user': %w", err) + } + return s, nil + + case "other": + s := TransactionPartnerOther{} + err := json.Unmarshal(d, &s) + if err != nil { + return nil, fmt.Errorf("failed to unmarshal TransactionPartner for value 'other': %w", err) + } + return s, nil + + } + return nil, fmt.Errorf("unknown interface for TransactionPartner with Type %v", t.Type) +} + +// TransactionPartnerFragment (https://core.telegram.org/bots/api#transactionpartnerfragment) +// +// Describes a withdrawal transaction with Fragment. +type TransactionPartnerFragment struct { + // Optional. State of the transaction if the transaction is outgoing + WithdrawalState RevenueWithdrawalState `json:"withdrawal_state,omitempty"` +} + +// UnmarshalJSON is a custom JSON unmarshaller to use the helpers which allow for unmarshalling structs into interfaces. +func (v *TransactionPartnerFragment) UnmarshalJSON(b []byte) error { + // All fields in TransactionPartnerFragment, with interface fields as json.RawMessage + type tmp struct { + WithdrawalState json.RawMessage `json:"withdrawal_state"` + } + t := tmp{} + err := json.Unmarshal(b, &t) + if err != nil { + return fmt.Errorf("failed to unmarshal TransactionPartnerFragment JSON into tmp struct: %w", err) + } + + v.WithdrawalState, err = unmarshalRevenueWithdrawalState(t.WithdrawalState) + if err != nil { + return fmt.Errorf("failed to unmarshal custom JSON field WithdrawalState: %w", err) + } + + return nil +} + +// GetType is a helper method to easily access the common fields of an interface. +func (v TransactionPartnerFragment) GetType() string { + return "fragment" +} + +// MergeTransactionPartner returns a MergedTransactionPartner struct to simplify working with types in a non-generic world. +func (v TransactionPartnerFragment) MergeTransactionPartner() MergedTransactionPartner { + return MergedTransactionPartner{ + Type: "fragment", + WithdrawalState: v.WithdrawalState, + } +} + +// MarshalJSON is a custom JSON marshaller to allow for enforcing the Type value. +func (v TransactionPartnerFragment) MarshalJSON() ([]byte, error) { + type alias TransactionPartnerFragment + a := struct { + Type string `json:"type"` + alias + }{ + Type: "fragment", + alias: (alias)(v), + } + return json.Marshal(a) +} + +// TransactionPartnerFragment.transactionPartner is a dummy method to avoid interface implementation. +func (v TransactionPartnerFragment) transactionPartner() {} + +// TransactionPartnerOther (https://core.telegram.org/bots/api#transactionpartnerother) +// +// Describes a transaction with an unknown source or recipient. +type TransactionPartnerOther struct{} + +// GetType is a helper method to easily access the common fields of an interface. +func (v TransactionPartnerOther) GetType() string { + return "other" +} + +// MergeTransactionPartner returns a MergedTransactionPartner struct to simplify working with types in a non-generic world. +func (v TransactionPartnerOther) MergeTransactionPartner() MergedTransactionPartner { + return MergedTransactionPartner{ + Type: "other", + } +} + +// MarshalJSON is a custom JSON marshaller to allow for enforcing the Type value. +func (v TransactionPartnerOther) MarshalJSON() ([]byte, error) { + type alias TransactionPartnerOther + a := struct { + Type string `json:"type"` + alias + }{ + Type: "other", + alias: (alias)(v), + } + return json.Marshal(a) +} + +// TransactionPartnerOther.transactionPartner is a dummy method to avoid interface implementation. +func (v TransactionPartnerOther) transactionPartner() {} + +// TransactionPartnerUser (https://core.telegram.org/bots/api#transactionpartneruser) +// +// Describes a transaction with a user. +type TransactionPartnerUser struct { + // Information about the user + User User `json:"user"` +} + +// GetType is a helper method to easily access the common fields of an interface. +func (v TransactionPartnerUser) GetType() string { + return "user" +} + +// MergeTransactionPartner returns a MergedTransactionPartner struct to simplify working with types in a non-generic world. +func (v TransactionPartnerUser) MergeTransactionPartner() MergedTransactionPartner { + return MergedTransactionPartner{ + Type: "user", + User: &v.User, + } +} + +// MarshalJSON is a custom JSON marshaller to allow for enforcing the Type value. +func (v TransactionPartnerUser) MarshalJSON() ([]byte, error) { + type alias TransactionPartnerUser + a := struct { + Type string `json:"type"` + alias + }{ + Type: "user", + alias: (alias)(v), + } + return json.Marshal(a) +} + +// TransactionPartnerUser.transactionPartner is a dummy method to avoid interface implementation. +func (v TransactionPartnerUser) transactionPartner() {} + // Update (https://core.telegram.org/bots/api#update) // // This object represents an incoming update. diff --git a/spec_commit b/spec_commit index 1dbd978..fb8ce7c 100644 --- a/spec_commit +++ b/spec_commit @@ -1 +1 @@ -1bafab9a42d4907d8166bcd079ad8f84315ff419 \ No newline at end of file +3cc848238f1a40bd66b4e83583f00e0feeb66467 \ No newline at end of file