From 18cd46296a994ebfeeefd6f4bba2a20db998d850 Mon Sep 17 00:00:00 2001 From: Paul Larsen Date: Wed, 21 Aug 2024 18:17:57 +0100 Subject: [PATCH 1/3] Setup WithContext methods --- bot.go | 9 + gen_methods.go | 4350 +++++++++++++++++++++++++++++++++-- scripts/generate/methods.go | 45 +- 3 files changed, 4172 insertions(+), 232 deletions(-) diff --git a/bot.go b/bot.go index bce9791..7d00af4 100644 --- a/bot.go +++ b/bot.go @@ -1,6 +1,7 @@ package gotgbot import ( + "context" "encoding/json" "errors" "fmt" @@ -100,3 +101,11 @@ func (bot *Bot) Request(method string, params map[string]string, data map[string return bot.BotClient.RequestWithContext(ctx, bot.Token, method, params, data, opts) } + +func (bot *Bot) RequestWithContext(ctx context.Context, method string, params map[string]string, data map[string]FileReader, opts *RequestOpts) (json.RawMessage, error) { + if bot.BotClient == nil { + return nil, ErrNilBotClient + } + + return bot.BotClient.RequestWithContext(ctx, bot.Token, method, params, data, opts) +} diff --git a/gen_methods.go b/gen_methods.go index dea5610..74c1add 100755 --- a/gen_methods.go +++ b/gen_methods.go @@ -4,6 +4,7 @@ package gotgbot import ( + "context" "encoding/json" "fmt" "strconv" @@ -47,6 +48,32 @@ func (bot *Bot) AddStickerToSet(userId int64, name string, sticker InputSticker, return b, json.Unmarshal(r, &b) } +// AddStickerToSetWithContext is the same as AddStickerToSet, but with a context.Context parameter. +func (bot *Bot) AddStickerToSetWithContext(ctx context.Context, userId int64, name string, sticker InputSticker, opts *AddStickerToSetOpts) (bool, error) { + v := map[string]string{} + data := map[string]FileReader{} + v["user_id"] = strconv.FormatInt(userId, 10) + v["name"] = name + inputBs, err := sticker.InputParams("sticker", data) + if err != nil { + return false, fmt.Errorf("failed to marshal field sticker: %w", err) + } + v["sticker"] = string(inputBs) + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "addStickerToSet", v, data, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + // AnswerCallbackQueryOpts is the set of optional fields for Bot.AnswerCallbackQuery. type AnswerCallbackQueryOpts struct { // Text of the notification. If not specified, nothing will be shown to the user, 0-200 characters @@ -92,6 +119,33 @@ func (bot *Bot) AnswerCallbackQuery(callbackQueryId string, opts *AnswerCallback return b, json.Unmarshal(r, &b) } +// AnswerCallbackQueryWithContext is the same as AnswerCallbackQuery, but with a context.Context parameter. +func (bot *Bot) AnswerCallbackQueryWithContext(ctx context.Context, callbackQueryId string, opts *AnswerCallbackQueryOpts) (bool, error) { + v := map[string]string{} + v["callback_query_id"] = callbackQueryId + if opts != nil { + v["text"] = opts.Text + v["show_alert"] = strconv.FormatBool(opts.ShowAlert) + v["url"] = opts.Url + if opts.CacheTime != 0 { + v["cache_time"] = strconv.FormatInt(opts.CacheTime, 10) + } + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "answerCallbackQuery", v, nil, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + // AnswerInlineQueryOpts is the set of optional fields for Bot.AnswerInlineQuery. type AnswerInlineQueryOpts struct { // The maximum amount of time in seconds that the result of the inline query may be cached on the server. Defaults to 300. @@ -152,6 +206,46 @@ func (bot *Bot) AnswerInlineQuery(inlineQueryId string, results []InlineQueryRes return b, json.Unmarshal(r, &b) } +// AnswerInlineQueryWithContext is the same as AnswerInlineQuery, but with a context.Context parameter. +func (bot *Bot) AnswerInlineQueryWithContext(ctx context.Context, inlineQueryId string, results []InlineQueryResult, opts *AnswerInlineQueryOpts) (bool, error) { + v := map[string]string{} + v["inline_query_id"] = inlineQueryId + if results != nil { + bs, err := json.Marshal(results) + if err != nil { + return false, fmt.Errorf("failed to marshal field results: %w", err) + } + v["results"] = string(bs) + } + if opts != nil { + if opts.CacheTime != 0 { + v["cache_time"] = strconv.FormatInt(opts.CacheTime, 10) + } + v["is_personal"] = strconv.FormatBool(opts.IsPersonal) + v["next_offset"] = opts.NextOffset + if opts.Button != nil { + bs, err := json.Marshal(opts.Button) + if err != nil { + return false, fmt.Errorf("failed to marshal field button: %w", err) + } + v["button"] = string(bs) + } + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "answerInlineQuery", v, nil, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + // AnswerPreCheckoutQueryOpts is the set of optional fields for Bot.AnswerPreCheckoutQuery. type AnswerPreCheckoutQueryOpts struct { // Required if ok is False. Error message in human readable form that explains the reason for failure to proceed with the checkout (e.g. "Sorry, somebody just bought the last of our amazing black T-shirts while you were busy filling out your payment details. Please choose a different color or garment!"). Telegram will display this message to the user. @@ -188,6 +282,29 @@ func (bot *Bot) AnswerPreCheckoutQuery(preCheckoutQueryId string, ok bool, opts return b, json.Unmarshal(r, &b) } +// AnswerPreCheckoutQueryWithContext is the same as AnswerPreCheckoutQuery, but with a context.Context parameter. +func (bot *Bot) AnswerPreCheckoutQueryWithContext(ctx context.Context, preCheckoutQueryId string, ok bool, opts *AnswerPreCheckoutQueryOpts) (bool, error) { + v := map[string]string{} + v["pre_checkout_query_id"] = preCheckoutQueryId + v["ok"] = strconv.FormatBool(ok) + if opts != nil { + v["error_message"] = opts.ErrorMessage + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "answerPreCheckoutQuery", v, nil, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + // AnswerShippingQueryOpts is the set of optional fields for Bot.AnswerShippingQuery. type AnswerShippingQueryOpts struct { // Required if ok is True. A JSON-serialized array of available shipping options. @@ -233,6 +350,36 @@ func (bot *Bot) AnswerShippingQuery(shippingQueryId string, ok bool, opts *Answe return b, json.Unmarshal(r, &b) } +// AnswerShippingQueryWithContext is the same as AnswerShippingQuery, but with a context.Context parameter. +func (bot *Bot) AnswerShippingQueryWithContext(ctx context.Context, shippingQueryId string, ok bool, opts *AnswerShippingQueryOpts) (bool, error) { + v := map[string]string{} + v["shipping_query_id"] = shippingQueryId + v["ok"] = strconv.FormatBool(ok) + if opts != nil { + if opts.ShippingOptions != nil { + bs, err := json.Marshal(opts.ShippingOptions) + if err != nil { + return false, fmt.Errorf("failed to marshal field shipping_options: %w", err) + } + v["shipping_options"] = string(bs) + } + v["error_message"] = opts.ErrorMessage + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "answerShippingQuery", v, nil, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + // AnswerWebAppQueryOpts is the set of optional fields for Bot.AnswerWebAppQuery. type AnswerWebAppQueryOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests @@ -268,6 +415,30 @@ func (bot *Bot) AnswerWebAppQuery(webAppQueryId string, result InlineQueryResult return &s, json.Unmarshal(r, &s) } +// AnswerWebAppQueryWithContext is the same as AnswerWebAppQuery, but with a context.Context parameter. +func (bot *Bot) AnswerWebAppQueryWithContext(ctx context.Context, webAppQueryId string, result InlineQueryResult, opts *AnswerWebAppQueryOpts) (*SentWebAppMessage, error) { + v := map[string]string{} + v["web_app_query_id"] = webAppQueryId + bs, err := json.Marshal(result) + if err != nil { + return nil, fmt.Errorf("failed to marshal field result: %w", err) + } + v["result"] = string(bs) + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "answerWebAppQuery", v, nil, reqOpts) + if err != nil { + return nil, err + } + + var s SentWebAppMessage + return &s, json.Unmarshal(r, &s) +} + // ApproveChatJoinRequestOpts is the set of optional fields for Bot.ApproveChatJoinRequest. type ApproveChatJoinRequestOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests @@ -299,6 +470,26 @@ func (bot *Bot) ApproveChatJoinRequest(chatId int64, userId int64, opts *Approve return b, json.Unmarshal(r, &b) } +// ApproveChatJoinRequestWithContext is the same as ApproveChatJoinRequest, but with a context.Context parameter. +func (bot *Bot) ApproveChatJoinRequestWithContext(ctx context.Context, chatId int64, userId int64, opts *ApproveChatJoinRequestOpts) (bool, error) { + v := map[string]string{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + v["user_id"] = strconv.FormatInt(userId, 10) + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "approveChatJoinRequest", v, nil, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + // BanChatMemberOpts is the set of optional fields for Bot.BanChatMember. type BanChatMemberOpts struct { // Date when the user will be unbanned; Unix time. If user is banned for more than 366 days or less than 30 seconds from the current time they are considered to be banned forever. Applied for supergroups and channels only. @@ -340,6 +531,32 @@ func (bot *Bot) BanChatMember(chatId int64, userId int64, opts *BanChatMemberOpt return b, json.Unmarshal(r, &b) } +// BanChatMemberWithContext is the same as BanChatMember, but with a context.Context parameter. +func (bot *Bot) BanChatMemberWithContext(ctx context.Context, chatId int64, userId int64, opts *BanChatMemberOpts) (bool, error) { + v := map[string]string{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + v["user_id"] = strconv.FormatInt(userId, 10) + if opts != nil { + if opts.UntilDate != 0 { + v["until_date"] = strconv.FormatInt(opts.UntilDate, 10) + } + v["revoke_messages"] = strconv.FormatBool(opts.RevokeMessages) + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "banChatMember", v, nil, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + // BanChatSenderChatOpts is the set of optional fields for Bot.BanChatSenderChat. type BanChatSenderChatOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests @@ -371,6 +588,26 @@ func (bot *Bot) BanChatSenderChat(chatId int64, senderChatId int64, opts *BanCha return b, json.Unmarshal(r, &b) } +// BanChatSenderChatWithContext is the same as BanChatSenderChat, but with a context.Context parameter. +func (bot *Bot) BanChatSenderChatWithContext(ctx context.Context, chatId int64, senderChatId int64, opts *BanChatSenderChatOpts) (bool, error) { + v := map[string]string{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + v["sender_chat_id"] = strconv.FormatInt(senderChatId, 10) + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "banChatSenderChat", v, nil, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + // CloseOpts is the set of optional fields for Bot.Close. type CloseOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests @@ -398,6 +635,24 @@ func (bot *Bot) Close(opts *CloseOpts) (bool, error) { return b, json.Unmarshal(r, &b) } +// CloseWithContext is the same as Close, but with a context.Context parameter. +func (bot *Bot) CloseWithContext(ctx context.Context, opts *CloseOpts) (bool, error) { + v := map[string]string{} + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "close", v, nil, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + // CloseForumTopicOpts is the set of optional fields for Bot.CloseForumTopic. type CloseForumTopicOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests @@ -429,6 +684,26 @@ func (bot *Bot) CloseForumTopic(chatId int64, messageThreadId int64, opts *Close return b, json.Unmarshal(r, &b) } +// CloseForumTopicWithContext is the same as CloseForumTopic, but with a context.Context parameter. +func (bot *Bot) CloseForumTopicWithContext(ctx context.Context, chatId int64, messageThreadId int64, opts *CloseForumTopicOpts) (bool, error) { + v := map[string]string{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + v["message_thread_id"] = strconv.FormatInt(messageThreadId, 10) + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "closeForumTopic", v, nil, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + // CloseGeneralForumTopicOpts is the set of optional fields for Bot.CloseGeneralForumTopic. type CloseGeneralForumTopicOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests @@ -458,21 +733,40 @@ func (bot *Bot) CloseGeneralForumTopic(chatId int64, opts *CloseGeneralForumTopi return b, json.Unmarshal(r, &b) } -// CopyMessageOpts is the set of optional fields for Bot.CopyMessage. -type CopyMessageOpts struct { - // Unique identifier for the target message thread (topic) of the forum; for forum supergroups only - MessageThreadId int64 - // New caption for media, 0-1024 characters after entities parsing. If not specified, the original caption is kept - Caption *string - // Mode for parsing entities in the new caption. See formatting options for more details. - ParseMode string - // A JSON-serialized list of special entities that appear in the new caption, which can be specified instead of parse_mode - CaptionEntities []MessageEntity - // Pass True, if the caption must be shown above the message media. Ignored if a new caption isn't specified. - ShowCaptionAboveMedia bool - // Sends the message silently. Users will receive a notification with no sound. - DisableNotification bool - // Protects the contents of the sent message from forwarding and saving +// CloseGeneralForumTopicWithContext is the same as CloseGeneralForumTopic, but with a context.Context parameter. +func (bot *Bot) CloseGeneralForumTopicWithContext(ctx context.Context, chatId int64, opts *CloseGeneralForumTopicOpts) (bool, error) { + v := map[string]string{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "closeGeneralForumTopic", v, nil, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + +// CopyMessageOpts is the set of optional fields for Bot.CopyMessage. +type CopyMessageOpts struct { + // Unique identifier for the target message thread (topic) of the forum; for forum supergroups only + MessageThreadId int64 + // New caption for media, 0-1024 characters after entities parsing. If not specified, the original caption is kept + Caption *string + // Mode for parsing entities in the new caption. See formatting options for more details. + ParseMode string + // A JSON-serialized list of special entities that appear in the new caption, which can be specified instead of parse_mode + CaptionEntities []MessageEntity + // Pass True, if the caption must be shown above the message media. Ignored if a new caption isn't specified. + ShowCaptionAboveMedia bool + // Sends the message silently. Users will receive a notification with no sound. + DisableNotification bool + // Protects the contents of the sent message from forwarding and saving ProtectContent bool // Description of the message to reply to ReplyParameters *ReplyParameters @@ -542,6 +836,60 @@ func (bot *Bot) CopyMessage(chatId int64, fromChatId int64, messageId int64, opt return &m, json.Unmarshal(r, &m) } +// CopyMessageWithContext is the same as CopyMessage, but with a context.Context parameter. +func (bot *Bot) CopyMessageWithContext(ctx context.Context, chatId int64, fromChatId int64, messageId int64, opts *CopyMessageOpts) (*MessageId, error) { + v := map[string]string{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + v["from_chat_id"] = strconv.FormatInt(fromChatId, 10) + v["message_id"] = strconv.FormatInt(messageId, 10) + if opts != nil { + if opts.MessageThreadId != 0 { + v["message_thread_id"] = strconv.FormatInt(opts.MessageThreadId, 10) + } + if opts.Caption != nil { + v["caption"] = *opts.Caption + } + v["parse_mode"] = opts.ParseMode + if opts.CaptionEntities != nil { + bs, err := json.Marshal(opts.CaptionEntities) + if err != nil { + return nil, fmt.Errorf("failed to marshal field caption_entities: %w", err) + } + v["caption_entities"] = string(bs) + } + v["show_caption_above_media"] = strconv.FormatBool(opts.ShowCaptionAboveMedia) + v["disable_notification"] = strconv.FormatBool(opts.DisableNotification) + v["protect_content"] = strconv.FormatBool(opts.ProtectContent) + if opts.ReplyParameters != nil { + bs, err := json.Marshal(opts.ReplyParameters) + if err != nil { + return nil, fmt.Errorf("failed to marshal field reply_parameters: %w", err) + } + v["reply_parameters"] = string(bs) + } + if opts.ReplyMarkup != nil { + bs, err := json.Marshal(opts.ReplyMarkup) + if err != nil { + return nil, fmt.Errorf("failed to marshal field reply_markup: %w", err) + } + v["reply_markup"] = string(bs) + } + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "copyMessage", v, nil, reqOpts) + if err != nil { + return nil, err + } + + var m MessageId + return &m, json.Unmarshal(r, &m) +} + // CopyMessagesOpts is the set of optional fields for Bot.CopyMessages. type CopyMessagesOpts struct { // Unique identifier for the target message thread (topic) of the forum; for forum supergroups only @@ -597,6 +945,41 @@ func (bot *Bot) CopyMessages(chatId int64, fromChatId int64, messageIds []int64, return m, json.Unmarshal(r, &m) } +// CopyMessagesWithContext is the same as CopyMessages, but with a context.Context parameter. +func (bot *Bot) CopyMessagesWithContext(ctx context.Context, chatId int64, fromChatId int64, messageIds []int64, opts *CopyMessagesOpts) ([]MessageId, error) { + v := map[string]string{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + v["from_chat_id"] = strconv.FormatInt(fromChatId, 10) + if messageIds != nil { + bs, err := json.Marshal(messageIds) + if err != nil { + return nil, fmt.Errorf("failed to marshal field message_ids: %w", err) + } + v["message_ids"] = string(bs) + } + if opts != nil { + if opts.MessageThreadId != 0 { + v["message_thread_id"] = strconv.FormatInt(opts.MessageThreadId, 10) + } + v["disable_notification"] = strconv.FormatBool(opts.DisableNotification) + v["protect_content"] = strconv.FormatBool(opts.ProtectContent) + v["remove_caption"] = strconv.FormatBool(opts.RemoveCaption) + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "copyMessages", v, nil, reqOpts) + if err != nil { + return nil, err + } + + var m []MessageId + return m, json.Unmarshal(r, &m) +} + // CreateChatInviteLinkOpts is the set of optional fields for Bot.CreateChatInviteLink. type CreateChatInviteLinkOpts struct { // Invite link name; 0-32 characters @@ -644,6 +1027,35 @@ func (bot *Bot) CreateChatInviteLink(chatId int64, opts *CreateChatInviteLinkOpt return &c, json.Unmarshal(r, &c) } +// CreateChatInviteLinkWithContext is the same as CreateChatInviteLink, but with a context.Context parameter. +func (bot *Bot) CreateChatInviteLinkWithContext(ctx context.Context, chatId int64, opts *CreateChatInviteLinkOpts) (*ChatInviteLink, error) { + v := map[string]string{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + if opts != nil { + v["name"] = opts.Name + if opts.ExpireDate != 0 { + v["expire_date"] = strconv.FormatInt(opts.ExpireDate, 10) + } + if opts.MemberLimit != 0 { + v["member_limit"] = strconv.FormatInt(opts.MemberLimit, 10) + } + v["creates_join_request"] = strconv.FormatBool(opts.CreatesJoinRequest) + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "createChatInviteLink", v, nil, reqOpts) + if err != nil { + return nil, err + } + + var c ChatInviteLink + return &c, json.Unmarshal(r, &c) +} + // CreateChatSubscriptionInviteLinkOpts is the set of optional fields for Bot.CreateChatSubscriptionInviteLink. type CreateChatSubscriptionInviteLinkOpts struct { // Invite link name; 0-32 characters @@ -682,6 +1094,30 @@ func (bot *Bot) CreateChatSubscriptionInviteLink(chatId int64, subscriptionPerio return &c, json.Unmarshal(r, &c) } +// CreateChatSubscriptionInviteLinkWithContext is the same as CreateChatSubscriptionInviteLink, but with a context.Context parameter. +func (bot *Bot) CreateChatSubscriptionInviteLinkWithContext(ctx context.Context, chatId int64, subscriptionPeriod int64, subscriptionPrice int64, opts *CreateChatSubscriptionInviteLinkOpts) (*ChatInviteLink, error) { + v := map[string]string{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + v["subscription_period"] = strconv.FormatInt(subscriptionPeriod, 10) + v["subscription_price"] = strconv.FormatInt(subscriptionPrice, 10) + if opts != nil { + v["name"] = opts.Name + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "createChatSubscriptionInviteLink", v, nil, reqOpts) + if err != nil { + return nil, err + } + + var c ChatInviteLink + return &c, json.Unmarshal(r, &c) +} + // CreateForumTopicOpts is the set of optional fields for Bot.CreateForumTopic. type CreateForumTopicOpts struct { // Color of the topic icon in RGB format. Currently, must be one of 7322096 (0x6FB9F0), 16766590 (0xFFD67E), 13338331 (0xCB86DB), 9367192 (0x8EEE98), 16749490 (0xFF93B2), or 16478047 (0xFB6F5F) @@ -723,6 +1159,32 @@ func (bot *Bot) CreateForumTopic(chatId int64, name string, opts *CreateForumTop return &f, json.Unmarshal(r, &f) } +// CreateForumTopicWithContext is the same as CreateForumTopic, but with a context.Context parameter. +func (bot *Bot) CreateForumTopicWithContext(ctx context.Context, chatId int64, name string, opts *CreateForumTopicOpts) (*ForumTopic, error) { + v := map[string]string{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + v["name"] = name + if opts != nil { + if opts.IconColor != 0 { + v["icon_color"] = strconv.FormatInt(opts.IconColor, 10) + } + v["icon_custom_emoji_id"] = opts.IconCustomEmojiId + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "createForumTopic", v, nil, reqOpts) + if err != nil { + return nil, err + } + + var f ForumTopic + return &f, json.Unmarshal(r, &f) +} + // CreateInvoiceLinkOpts is the set of optional fields for Bot.CreateInvoiceLink. type CreateInvoiceLinkOpts struct { // Payment provider token, obtained via @BotFather. Pass an empty string for payments in Telegram Stars. @@ -827,6 +1289,66 @@ func (bot *Bot) CreateInvoiceLink(title string, description string, payload stri return s, json.Unmarshal(r, &s) } +// CreateInvoiceLinkWithContext is the same as CreateInvoiceLink, but with a context.Context parameter. +func (bot *Bot) CreateInvoiceLinkWithContext(ctx context.Context, title string, description string, payload string, currency string, prices []LabeledPrice, opts *CreateInvoiceLinkOpts) (string, error) { + v := map[string]string{} + v["title"] = title + v["description"] = description + v["payload"] = payload + v["currency"] = currency + if prices != nil { + bs, err := json.Marshal(prices) + if err != nil { + return "", fmt.Errorf("failed to marshal field prices: %w", err) + } + v["prices"] = string(bs) + } + if opts != nil { + v["provider_token"] = opts.ProviderToken + if opts.MaxTipAmount != 0 { + v["max_tip_amount"] = strconv.FormatInt(opts.MaxTipAmount, 10) + } + if opts.SuggestedTipAmounts != nil { + bs, err := json.Marshal(opts.SuggestedTipAmounts) + if err != nil { + return "", fmt.Errorf("failed to marshal field suggested_tip_amounts: %w", err) + } + v["suggested_tip_amounts"] = string(bs) + } + v["provider_data"] = opts.ProviderData + v["photo_url"] = opts.PhotoUrl + if opts.PhotoSize != 0 { + v["photo_size"] = strconv.FormatInt(opts.PhotoSize, 10) + } + if opts.PhotoWidth != 0 { + v["photo_width"] = strconv.FormatInt(opts.PhotoWidth, 10) + } + if opts.PhotoHeight != 0 { + v["photo_height"] = strconv.FormatInt(opts.PhotoHeight, 10) + } + v["need_name"] = strconv.FormatBool(opts.NeedName) + v["need_phone_number"] = strconv.FormatBool(opts.NeedPhoneNumber) + v["need_email"] = strconv.FormatBool(opts.NeedEmail) + v["need_shipping_address"] = strconv.FormatBool(opts.NeedShippingAddress) + v["send_phone_number_to_provider"] = strconv.FormatBool(opts.SendPhoneNumberToProvider) + v["send_email_to_provider"] = strconv.FormatBool(opts.SendEmailToProvider) + v["is_flexible"] = strconv.FormatBool(opts.IsFlexible) + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "createInvoiceLink", v, nil, reqOpts) + if err != nil { + return "", err + } + + var s string + return s, json.Unmarshal(r, &s) +} + // CreateNewStickerSetOpts is the set of optional fields for Bot.CreateNewStickerSet. type CreateNewStickerSetOpts struct { // Type of stickers in the set, pass "regular", "mask", or "custom_emoji". By default, a regular sticker set is created. @@ -885,21 +1407,62 @@ func (bot *Bot) CreateNewStickerSet(userId int64, name string, title string, sti return b, json.Unmarshal(r, &b) } -// DeclineChatJoinRequestOpts is the set of optional fields for Bot.DeclineChatJoinRequest. -type DeclineChatJoinRequestOpts struct { - // RequestOpts are an additional optional field to configure timeouts for individual requests - RequestOpts *RequestOpts -} - -// DeclineChatJoinRequest (https://core.telegram.org/bots/api#declinechatjoinrequest) -// -// Use this method to decline a chat join request. The bot must be an administrator in the chat for this to work and must have the can_invite_users administrator right. Returns True on success. -// - chatId (type int64): Unique identifier for the target chat -// - userId (type int64): Unique identifier of the target user -// - opts (type DeclineChatJoinRequestOpts): All optional parameters. -func (bot *Bot) DeclineChatJoinRequest(chatId int64, userId int64, opts *DeclineChatJoinRequestOpts) (bool, error) { +// CreateNewStickerSetWithContext is the same as CreateNewStickerSet, but with a context.Context parameter. +func (bot *Bot) CreateNewStickerSetWithContext(ctx context.Context, userId int64, name string, title string, stickers []InputSticker, opts *CreateNewStickerSetOpts) (bool, error) { v := map[string]string{} - v["chat_id"] = strconv.FormatInt(chatId, 10) + data := map[string]FileReader{} + v["user_id"] = strconv.FormatInt(userId, 10) + v["name"] = name + v["title"] = title + if stickers != nil { + var rawList []json.RawMessage + for idx, im := range stickers { + inputBs, err := im.InputParams("stickers"+strconv.Itoa(idx), data) + if err != nil { + return false, fmt.Errorf("failed to marshal list item %d for field stickers: %w", idx, err) + } + rawList = append(rawList, inputBs) + } + bs, err := json.Marshal(rawList) + if err != nil { + return false, fmt.Errorf("failed to marshal raw json list for field: stickers %w", err) + } + v["stickers"] = string(bs) + } + if opts != nil { + v["sticker_type"] = opts.StickerType + v["needs_repainting"] = strconv.FormatBool(opts.NeedsRepainting) + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "createNewStickerSet", v, data, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + +// DeclineChatJoinRequestOpts is the set of optional fields for Bot.DeclineChatJoinRequest. +type DeclineChatJoinRequestOpts struct { + // RequestOpts are an additional optional field to configure timeouts for individual requests + RequestOpts *RequestOpts +} + +// DeclineChatJoinRequest (https://core.telegram.org/bots/api#declinechatjoinrequest) +// +// Use this method to decline a chat join request. The bot must be an administrator in the chat for this to work and must have the can_invite_users administrator right. Returns True on success. +// - chatId (type int64): Unique identifier for the target chat +// - userId (type int64): Unique identifier of the target user +// - opts (type DeclineChatJoinRequestOpts): All optional parameters. +func (bot *Bot) DeclineChatJoinRequest(chatId int64, userId int64, opts *DeclineChatJoinRequestOpts) (bool, error) { + v := map[string]string{} + v["chat_id"] = strconv.FormatInt(chatId, 10) v["user_id"] = strconv.FormatInt(userId, 10) var reqOpts *RequestOpts @@ -916,6 +1479,26 @@ func (bot *Bot) DeclineChatJoinRequest(chatId int64, userId int64, opts *Decline return b, json.Unmarshal(r, &b) } +// DeclineChatJoinRequestWithContext is the same as DeclineChatJoinRequest, but with a context.Context parameter. +func (bot *Bot) DeclineChatJoinRequestWithContext(ctx context.Context, chatId int64, userId int64, opts *DeclineChatJoinRequestOpts) (bool, error) { + v := map[string]string{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + v["user_id"] = strconv.FormatInt(userId, 10) + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "declineChatJoinRequest", v, nil, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + // DeleteChatPhotoOpts is the set of optional fields for Bot.DeleteChatPhoto. type DeleteChatPhotoOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests @@ -945,6 +1528,25 @@ func (bot *Bot) DeleteChatPhoto(chatId int64, opts *DeleteChatPhotoOpts) (bool, return b, json.Unmarshal(r, &b) } +// DeleteChatPhotoWithContext is the same as DeleteChatPhoto, but with a context.Context parameter. +func (bot *Bot) DeleteChatPhotoWithContext(ctx context.Context, chatId int64, opts *DeleteChatPhotoOpts) (bool, error) { + v := map[string]string{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "deleteChatPhoto", v, nil, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + // DeleteChatStickerSetOpts is the set of optional fields for Bot.DeleteChatStickerSet. type DeleteChatStickerSetOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests @@ -974,6 +1576,25 @@ func (bot *Bot) DeleteChatStickerSet(chatId int64, opts *DeleteChatStickerSetOpt return b, json.Unmarshal(r, &b) } +// DeleteChatStickerSetWithContext is the same as DeleteChatStickerSet, but with a context.Context parameter. +func (bot *Bot) DeleteChatStickerSetWithContext(ctx context.Context, chatId int64, opts *DeleteChatStickerSetOpts) (bool, error) { + v := map[string]string{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "deleteChatStickerSet", v, nil, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + // DeleteForumTopicOpts is the set of optional fields for Bot.DeleteForumTopic. type DeleteForumTopicOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests @@ -1005,6 +1626,26 @@ func (bot *Bot) DeleteForumTopic(chatId int64, messageThreadId int64, opts *Dele return b, json.Unmarshal(r, &b) } +// DeleteForumTopicWithContext is the same as DeleteForumTopic, but with a context.Context parameter. +func (bot *Bot) DeleteForumTopicWithContext(ctx context.Context, chatId int64, messageThreadId int64, opts *DeleteForumTopicOpts) (bool, error) { + v := map[string]string{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + v["message_thread_id"] = strconv.FormatInt(messageThreadId, 10) + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "deleteForumTopic", v, nil, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + // DeleteMessageOpts is the set of optional fields for Bot.DeleteMessage. type DeleteMessageOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests @@ -1046,6 +1687,26 @@ func (bot *Bot) DeleteMessage(chatId int64, messageId int64, opts *DeleteMessage return b, json.Unmarshal(r, &b) } +// DeleteMessageWithContext is the same as DeleteMessage, but with a context.Context parameter. +func (bot *Bot) DeleteMessageWithContext(ctx context.Context, chatId int64, messageId int64, opts *DeleteMessageOpts) (bool, error) { + v := map[string]string{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + v["message_id"] = strconv.FormatInt(messageId, 10) + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "deleteMessage", v, nil, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + // DeleteMessagesOpts is the set of optional fields for Bot.DeleteMessages. type DeleteMessagesOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests @@ -1083,6 +1744,32 @@ func (bot *Bot) DeleteMessages(chatId int64, messageIds []int64, opts *DeleteMes return b, json.Unmarshal(r, &b) } +// DeleteMessagesWithContext is the same as DeleteMessages, but with a context.Context parameter. +func (bot *Bot) DeleteMessagesWithContext(ctx context.Context, chatId int64, messageIds []int64, opts *DeleteMessagesOpts) (bool, error) { + v := map[string]string{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + if messageIds != nil { + bs, err := json.Marshal(messageIds) + if err != nil { + return false, fmt.Errorf("failed to marshal field message_ids: %w", err) + } + v["message_ids"] = string(bs) + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "deleteMessages", v, nil, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + // DeleteMyCommandsOpts is the set of optional fields for Bot.DeleteMyCommands. type DeleteMyCommandsOpts struct { // A JSON-serialized object, describing scope of users for which the commands are relevant. Defaults to BotCommandScopeDefault. @@ -1122,6 +1809,32 @@ func (bot *Bot) DeleteMyCommands(opts *DeleteMyCommandsOpts) (bool, error) { return b, json.Unmarshal(r, &b) } +// DeleteMyCommandsWithContext is the same as DeleteMyCommands, but with a context.Context parameter. +func (bot *Bot) DeleteMyCommandsWithContext(ctx context.Context, opts *DeleteMyCommandsOpts) (bool, error) { + v := map[string]string{} + if opts != nil { + bs, err := json.Marshal(opts.Scope) + if err != nil { + return false, fmt.Errorf("failed to marshal field scope: %w", err) + } + v["scope"] = string(bs) + v["language_code"] = opts.LanguageCode + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "deleteMyCommands", v, nil, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + // DeleteStickerFromSetOpts is the set of optional fields for Bot.DeleteStickerFromSet. type DeleteStickerFromSetOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests @@ -1151,6 +1864,25 @@ func (bot *Bot) DeleteStickerFromSet(sticker string, opts *DeleteStickerFromSetO return b, json.Unmarshal(r, &b) } +// DeleteStickerFromSetWithContext is the same as DeleteStickerFromSet, but with a context.Context parameter. +func (bot *Bot) DeleteStickerFromSetWithContext(ctx context.Context, sticker string, opts *DeleteStickerFromSetOpts) (bool, error) { + v := map[string]string{} + v["sticker"] = sticker + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "deleteStickerFromSet", v, nil, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + // DeleteStickerSetOpts is the set of optional fields for Bot.DeleteStickerSet. type DeleteStickerSetOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests @@ -1180,6 +1912,25 @@ func (bot *Bot) DeleteStickerSet(name string, opts *DeleteStickerSetOpts) (bool, return b, json.Unmarshal(r, &b) } +// DeleteStickerSetWithContext is the same as DeleteStickerSet, but with a context.Context parameter. +func (bot *Bot) DeleteStickerSetWithContext(ctx context.Context, name string, opts *DeleteStickerSetOpts) (bool, error) { + v := map[string]string{} + v["name"] = name + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "deleteStickerSet", v, nil, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + // DeleteWebhookOpts is the set of optional fields for Bot.DeleteWebhook. type DeleteWebhookOpts struct { // Pass True to drop all pending updates @@ -1212,6 +1963,27 @@ func (bot *Bot) DeleteWebhook(opts *DeleteWebhookOpts) (bool, error) { return b, json.Unmarshal(r, &b) } +// DeleteWebhookWithContext is the same as DeleteWebhook, but with a context.Context parameter. +func (bot *Bot) DeleteWebhookWithContext(ctx context.Context, opts *DeleteWebhookOpts) (bool, error) { + v := map[string]string{} + if opts != nil { + v["drop_pending_updates"] = strconv.FormatBool(opts.DropPendingUpdates) + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "deleteWebhook", v, nil, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + // EditChatInviteLinkOpts is the set of optional fields for Bot.EditChatInviteLink. type EditChatInviteLinkOpts struct { // Invite link name; 0-32 characters @@ -1261,26 +2033,20 @@ func (bot *Bot) EditChatInviteLink(chatId int64, inviteLink string, opts *EditCh return &c, json.Unmarshal(r, &c) } -// EditChatSubscriptionInviteLinkOpts is the set of optional fields for Bot.EditChatSubscriptionInviteLink. -type EditChatSubscriptionInviteLinkOpts struct { - // Invite link name; 0-32 characters - Name string - // RequestOpts are an additional optional field to configure timeouts for individual requests - RequestOpts *RequestOpts -} - -// EditChatSubscriptionInviteLink (https://core.telegram.org/bots/api#editchatsubscriptioninvitelink) -// -// Use this method to edit a subscription invite link created by the bot. The bot must have the can_invite_users administrator rights. Returns the edited invite link as a ChatInviteLink object. -// - chatId (type int64): Unique identifier for the target chat -// - inviteLink (type string): The invite link to edit -// - opts (type EditChatSubscriptionInviteLinkOpts): All optional parameters. -func (bot *Bot) EditChatSubscriptionInviteLink(chatId int64, inviteLink string, opts *EditChatSubscriptionInviteLinkOpts) (*ChatInviteLink, error) { +// EditChatInviteLinkWithContext is the same as EditChatInviteLink, but with a context.Context parameter. +func (bot *Bot) EditChatInviteLinkWithContext(ctx context.Context, chatId int64, inviteLink string, opts *EditChatInviteLinkOpts) (*ChatInviteLink, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) v["invite_link"] = inviteLink if opts != nil { v["name"] = opts.Name + if opts.ExpireDate != 0 { + v["expire_date"] = strconv.FormatInt(opts.ExpireDate, 10) + } + if opts.MemberLimit != 0 { + v["member_limit"] = strconv.FormatInt(opts.MemberLimit, 10) + } + v["creates_join_request"] = strconv.FormatBool(opts.CreatesJoinRequest) } var reqOpts *RequestOpts @@ -1288,7 +2054,7 @@ func (bot *Bot) EditChatSubscriptionInviteLink(chatId int64, inviteLink string, reqOpts = opts.RequestOpts } - r, err := bot.Request("editChatSubscriptionInviteLink", v, nil, reqOpts) + r, err := bot.RequestWithContext(ctx, "editChatInviteLink", v, nil, reqOpts) if err != nil { return nil, err } @@ -1297,9 +2063,68 @@ func (bot *Bot) EditChatSubscriptionInviteLink(chatId int64, inviteLink string, return &c, json.Unmarshal(r, &c) } -// EditForumTopicOpts is the set of optional fields for Bot.EditForumTopic. -type EditForumTopicOpts struct { - // New topic name, 0-128 characters. If not specified or empty, the current name of the topic will be kept +// EditChatSubscriptionInviteLinkOpts is the set of optional fields for Bot.EditChatSubscriptionInviteLink. +type EditChatSubscriptionInviteLinkOpts struct { + // Invite link name; 0-32 characters + Name string + // RequestOpts are an additional optional field to configure timeouts for individual requests + RequestOpts *RequestOpts +} + +// EditChatSubscriptionInviteLink (https://core.telegram.org/bots/api#editchatsubscriptioninvitelink) +// +// Use this method to edit a subscription invite link created by the bot. The bot must have the can_invite_users administrator rights. Returns the edited invite link as a ChatInviteLink object. +// - chatId (type int64): Unique identifier for the target chat +// - inviteLink (type string): The invite link to edit +// - opts (type EditChatSubscriptionInviteLinkOpts): All optional parameters. +func (bot *Bot) EditChatSubscriptionInviteLink(chatId int64, inviteLink string, opts *EditChatSubscriptionInviteLinkOpts) (*ChatInviteLink, error) { + v := map[string]string{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + v["invite_link"] = inviteLink + if opts != nil { + v["name"] = opts.Name + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.Request("editChatSubscriptionInviteLink", v, nil, reqOpts) + if err != nil { + return nil, err + } + + var c ChatInviteLink + return &c, json.Unmarshal(r, &c) +} + +// EditChatSubscriptionInviteLinkWithContext is the same as EditChatSubscriptionInviteLink, but with a context.Context parameter. +func (bot *Bot) EditChatSubscriptionInviteLinkWithContext(ctx context.Context, chatId int64, inviteLink string, opts *EditChatSubscriptionInviteLinkOpts) (*ChatInviteLink, error) { + v := map[string]string{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + v["invite_link"] = inviteLink + if opts != nil { + v["name"] = opts.Name + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "editChatSubscriptionInviteLink", v, nil, reqOpts) + if err != nil { + return nil, err + } + + var c ChatInviteLink + return &c, json.Unmarshal(r, &c) +} + +// EditForumTopicOpts is the set of optional fields for Bot.EditForumTopic. +type EditForumTopicOpts struct { + // New topic name, 0-128 characters. If not specified or empty, the current name of the topic will be kept Name string // New unique identifier of the custom emoji shown as the topic icon. Use getForumTopicIconStickers to get all allowed custom emoji identifiers. Pass an empty string to remove the icon. If not specified, the current icon will be kept IconCustomEmojiId *string @@ -1338,6 +2163,32 @@ func (bot *Bot) EditForumTopic(chatId int64, messageThreadId int64, opts *EditFo return b, json.Unmarshal(r, &b) } +// EditForumTopicWithContext is the same as EditForumTopic, but with a context.Context parameter. +func (bot *Bot) EditForumTopicWithContext(ctx context.Context, chatId int64, messageThreadId int64, opts *EditForumTopicOpts) (bool, error) { + v := map[string]string{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + v["message_thread_id"] = strconv.FormatInt(messageThreadId, 10) + if opts != nil { + v["name"] = opts.Name + if opts.IconCustomEmojiId != nil { + v["icon_custom_emoji_id"] = *opts.IconCustomEmojiId + } + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "editForumTopic", v, nil, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + // EditGeneralForumTopicOpts is the set of optional fields for Bot.EditGeneralForumTopic. type EditGeneralForumTopicOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests @@ -1369,6 +2220,26 @@ func (bot *Bot) EditGeneralForumTopic(chatId int64, name string, opts *EditGener return b, json.Unmarshal(r, &b) } +// EditGeneralForumTopicWithContext is the same as EditGeneralForumTopic, but with a context.Context parameter. +func (bot *Bot) EditGeneralForumTopicWithContext(ctx context.Context, chatId int64, name string, opts *EditGeneralForumTopicOpts) (bool, error) { + v := map[string]string{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + v["name"] = name + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "editGeneralForumTopic", v, nil, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + // 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 @@ -1447,6 +2318,57 @@ func (bot *Bot) EditMessageCaption(opts *EditMessageCaptionOpts) (*Message, bool } +// EditMessageCaptionWithContext is the same as EditMessageCaption, but with a context.Context parameter. +func (bot *Bot) EditMessageCaptionWithContext(ctx context.Context, 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) + } + if opts.MessageId != 0 { + v["message_id"] = strconv.FormatInt(opts.MessageId, 10) + } + v["inline_message_id"] = opts.InlineMessageId + v["caption"] = opts.Caption + v["parse_mode"] = opts.ParseMode + if opts.CaptionEntities != nil { + bs, err := json.Marshal(opts.CaptionEntities) + if err != nil { + return nil, false, fmt.Errorf("failed to marshal field caption_entities: %w", err) + } + v["caption_entities"] = string(bs) + } + v["show_caption_above_media"] = strconv.FormatBool(opts.ShowCaptionAboveMedia) + bs, err := json.Marshal(opts.ReplyMarkup) + if err != nil { + return nil, false, fmt.Errorf("failed to marshal field reply_markup: %w", err) + } + v["reply_markup"] = string(bs) + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "editMessageCaption", v, nil, reqOpts) + if err != nil { + return nil, false, err + } + + var m Message + if err := json.Unmarshal(r, &m); err != nil { + var b bool + if err := json.Unmarshal(r, &b); err != nil { + return nil, false, err + } + return nil, b, nil + } + return &m, true, nil + +} + // 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 @@ -1531,6 +2453,61 @@ func (bot *Bot) EditMessageLiveLocation(latitude float64, longitude float64, opt } +// EditMessageLiveLocationWithContext is the same as EditMessageLiveLocation, but with a context.Context parameter. +func (bot *Bot) EditMessageLiveLocationWithContext(ctx context.Context, latitude float64, longitude float64, opts *EditMessageLiveLocationOpts) (*Message, bool, error) { + v := map[string]string{} + 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) + } + if opts.MessageId != 0 { + v["message_id"] = strconv.FormatInt(opts.MessageId, 10) + } + v["inline_message_id"] = opts.InlineMessageId + if opts.LivePeriod != nil { + v["live_period"] = strconv.FormatInt(*opts.LivePeriod, 10) + } + if opts.HorizontalAccuracy != 0.0 { + v["horizontal_accuracy"] = strconv.FormatFloat(opts.HorizontalAccuracy, 'f', -1, 64) + } + if opts.Heading != 0 { + v["heading"] = strconv.FormatInt(opts.Heading, 10) + } + if opts.ProximityAlertRadius != 0 { + v["proximity_alert_radius"] = strconv.FormatInt(opts.ProximityAlertRadius, 10) + } + bs, err := json.Marshal(opts.ReplyMarkup) + if err != nil { + return nil, false, fmt.Errorf("failed to marshal field reply_markup: %w", err) + } + v["reply_markup"] = string(bs) + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "editMessageLiveLocation", v, nil, reqOpts) + if err != nil { + return nil, false, err + } + + var m Message + if err := json.Unmarshal(r, &m); err != nil { + var b bool + if err := json.Unmarshal(r, &b); err != nil { + return nil, false, err + } + return nil, b, nil + } + return &m, true, nil + +} + // 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 @@ -1598,28 +2575,15 @@ 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 - MessageId int64 - // Required if chat_id and message_id are not specified. Identifier of the inline message - InlineMessageId string - // A JSON-serialized object for an inline keyboard. - ReplyMarkup InlineKeyboardMarkup - // RequestOpts are an additional optional field to configure timeouts for individual requests - RequestOpts *RequestOpts -} - -// 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. 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) { +// EditMessageMediaWithContext is the same as EditMessageMedia, but with a context.Context parameter. +func (bot *Bot) EditMessageMediaWithContext(ctx context.Context, media InputMedia, opts *EditMessageMediaOpts) (*Message, bool, error) { v := map[string]string{} + data := map[string]FileReader{} + inputBs, err := media.InputParams("media", data) + if err != nil { + return nil, false, fmt.Errorf("failed to marshal field media: %w", err) + } + v["media"] = string(inputBs) if opts != nil { v["business_connection_id"] = opts.BusinessConnectionId if opts.ChatId != 0 { @@ -1641,7 +2605,7 @@ func (bot *Bot) EditMessageReplyMarkup(opts *EditMessageReplyMarkupOpts) (*Messa reqOpts = opts.RequestOpts } - r, err := bot.Request("editMessageReplyMarkup", v, nil, reqOpts) + r, err := bot.RequestWithContext(ctx, "editMessageMedia", v, data, reqOpts) if err != nil { return nil, false, err } @@ -1658,8 +2622,8 @@ func (bot *Bot) EditMessageReplyMarkup(opts *EditMessageReplyMarkupOpts) (*Messa } -// EditMessageTextOpts is the set of optional fields for Bot.EditMessageText. -type EditMessageTextOpts struct { +// 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 @@ -1668,26 +2632,18 @@ type EditMessageTextOpts struct { MessageId int64 // Required if chat_id and message_id are not specified. Identifier of the inline message InlineMessageId string - // Mode for parsing entities in the message text. See formatting options for more details. - ParseMode string - // A JSON-serialized list of special entities that appear in message text, which can be specified instead of parse_mode - Entities []MessageEntity - // Link preview generation options for the message - LinkPreviewOptions *LinkPreviewOptions // A JSON-serialized object for an inline keyboard. ReplyMarkup InlineKeyboardMarkup // RequestOpts are an additional optional field to configure timeouts for individual requests RequestOpts *RequestOpts } -// EditMessageText (https://core.telegram.org/bots/api#editmessagetext) +// EditMessageReplyMarkup (https://core.telegram.org/bots/api#editmessagereplymarkup) // -// 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) { +// 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{} - v["text"] = text if opts != nil { v["business_connection_id"] = opts.BusinessConnectionId if opts.ChatId != 0 { @@ -1697,15 +2653,124 @@ func (bot *Bot) EditMessageText(text string, opts *EditMessageTextOpts) (*Messag v["message_id"] = strconv.FormatInt(opts.MessageId, 10) } v["inline_message_id"] = opts.InlineMessageId - v["parse_mode"] = opts.ParseMode - if opts.Entities != nil { - bs, err := json.Marshal(opts.Entities) - if err != nil { - return nil, false, fmt.Errorf("failed to marshal field entities: %w", err) - } - v["entities"] = string(bs) - } - if opts.LinkPreviewOptions != nil { + bs, err := json.Marshal(opts.ReplyMarkup) + if err != nil { + return nil, false, fmt.Errorf("failed to marshal field reply_markup: %w", err) + } + v["reply_markup"] = string(bs) + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.Request("editMessageReplyMarkup", v, nil, reqOpts) + if err != nil { + return nil, false, err + } + + var m Message + if err := json.Unmarshal(r, &m); err != nil { + var b bool + if err := json.Unmarshal(r, &b); err != nil { + return nil, false, err + } + return nil, b, nil + } + return &m, true, nil + +} + +// EditMessageReplyMarkupWithContext is the same as EditMessageReplyMarkup, but with a context.Context parameter. +func (bot *Bot) EditMessageReplyMarkupWithContext(ctx context.Context, 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) + } + if opts.MessageId != 0 { + v["message_id"] = strconv.FormatInt(opts.MessageId, 10) + } + v["inline_message_id"] = opts.InlineMessageId + bs, err := json.Marshal(opts.ReplyMarkup) + if err != nil { + return nil, false, fmt.Errorf("failed to marshal field reply_markup: %w", err) + } + v["reply_markup"] = string(bs) + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "editMessageReplyMarkup", v, nil, reqOpts) + if err != nil { + return nil, false, err + } + + var m Message + if err := json.Unmarshal(r, &m); err != nil { + var b bool + if err := json.Unmarshal(r, &b); err != nil { + return nil, false, err + } + return nil, b, nil + } + return &m, true, nil + +} + +// 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 + MessageId int64 + // Required if chat_id and message_id are not specified. Identifier of the inline message + InlineMessageId string + // Mode for parsing entities in the message text. See formatting options for more details. + ParseMode string + // A JSON-serialized list of special entities that appear in message text, which can be specified instead of parse_mode + Entities []MessageEntity + // Link preview generation options for the message + LinkPreviewOptions *LinkPreviewOptions + // A JSON-serialized object for an inline keyboard. + ReplyMarkup InlineKeyboardMarkup + // RequestOpts are an additional optional field to configure timeouts for individual requests + RequestOpts *RequestOpts +} + +// 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. 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) + } + if opts.MessageId != 0 { + v["message_id"] = strconv.FormatInt(opts.MessageId, 10) + } + v["inline_message_id"] = opts.InlineMessageId + v["parse_mode"] = opts.ParseMode + if opts.Entities != nil { + bs, err := json.Marshal(opts.Entities) + if err != nil { + return nil, false, fmt.Errorf("failed to marshal field entities: %w", err) + } + v["entities"] = string(bs) + } + if opts.LinkPreviewOptions != nil { bs, err := json.Marshal(opts.LinkPreviewOptions) if err != nil { return nil, false, fmt.Errorf("failed to marshal field link_preview_options: %w", err) @@ -1741,6 +2806,63 @@ func (bot *Bot) EditMessageText(text string, opts *EditMessageTextOpts) (*Messag } +// EditMessageTextWithContext is the same as EditMessageText, but with a context.Context parameter. +func (bot *Bot) EditMessageTextWithContext(ctx context.Context, 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) + } + if opts.MessageId != 0 { + v["message_id"] = strconv.FormatInt(opts.MessageId, 10) + } + v["inline_message_id"] = opts.InlineMessageId + v["parse_mode"] = opts.ParseMode + if opts.Entities != nil { + bs, err := json.Marshal(opts.Entities) + if err != nil { + return nil, false, fmt.Errorf("failed to marshal field entities: %w", err) + } + v["entities"] = string(bs) + } + if opts.LinkPreviewOptions != nil { + bs, err := json.Marshal(opts.LinkPreviewOptions) + if err != nil { + return nil, false, fmt.Errorf("failed to marshal field link_preview_options: %w", err) + } + v["link_preview_options"] = string(bs) + } + bs, err := json.Marshal(opts.ReplyMarkup) + if err != nil { + return nil, false, fmt.Errorf("failed to marshal field reply_markup: %w", err) + } + v["reply_markup"] = string(bs) + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "editMessageText", v, nil, reqOpts) + if err != nil { + return nil, false, err + } + + var m Message + if err := json.Unmarshal(r, &m); err != nil { + var b bool + if err := json.Unmarshal(r, &b); err != nil { + return nil, false, err + } + return nil, b, nil + } + return &m, true, nil + +} + // ExportChatInviteLinkOpts is the set of optional fields for Bot.ExportChatInviteLink. type ExportChatInviteLinkOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests @@ -1770,6 +2892,25 @@ func (bot *Bot) ExportChatInviteLink(chatId int64, opts *ExportChatInviteLinkOpt return s, json.Unmarshal(r, &s) } +// ExportChatInviteLinkWithContext is the same as ExportChatInviteLink, but with a context.Context parameter. +func (bot *Bot) ExportChatInviteLinkWithContext(ctx context.Context, chatId int64, opts *ExportChatInviteLinkOpts) (string, error) { + v := map[string]string{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "exportChatInviteLink", v, nil, reqOpts) + if err != nil { + return "", err + } + + var s string + return s, json.Unmarshal(r, &s) +} + // ForwardMessageOpts is the set of optional fields for Bot.ForwardMessage. type ForwardMessageOpts struct { // Unique identifier for the target message thread (topic) of the forum; for forum supergroups only @@ -1816,6 +2957,34 @@ func (bot *Bot) ForwardMessage(chatId int64, fromChatId int64, messageId int64, return &m, json.Unmarshal(r, &m) } +// ForwardMessageWithContext is the same as ForwardMessage, but with a context.Context parameter. +func (bot *Bot) ForwardMessageWithContext(ctx context.Context, chatId int64, fromChatId int64, messageId int64, opts *ForwardMessageOpts) (*Message, error) { + v := map[string]string{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + v["from_chat_id"] = strconv.FormatInt(fromChatId, 10) + v["message_id"] = strconv.FormatInt(messageId, 10) + if opts != nil { + if opts.MessageThreadId != 0 { + v["message_thread_id"] = strconv.FormatInt(opts.MessageThreadId, 10) + } + v["disable_notification"] = strconv.FormatBool(opts.DisableNotification) + v["protect_content"] = strconv.FormatBool(opts.ProtectContent) + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "forwardMessage", v, nil, reqOpts) + if err != nil { + return nil, err + } + + var m Message + return &m, json.Unmarshal(r, &m) +} + // ForwardMessagesOpts is the set of optional fields for Bot.ForwardMessages. type ForwardMessagesOpts struct { // Unique identifier for the target message thread (topic) of the forum; for forum supergroups only @@ -1868,6 +3037,40 @@ func (bot *Bot) ForwardMessages(chatId int64, fromChatId int64, messageIds []int return m, json.Unmarshal(r, &m) } +// ForwardMessagesWithContext is the same as ForwardMessages, but with a context.Context parameter. +func (bot *Bot) ForwardMessagesWithContext(ctx context.Context, chatId int64, fromChatId int64, messageIds []int64, opts *ForwardMessagesOpts) ([]MessageId, error) { + v := map[string]string{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + v["from_chat_id"] = strconv.FormatInt(fromChatId, 10) + if messageIds != nil { + bs, err := json.Marshal(messageIds) + if err != nil { + return nil, fmt.Errorf("failed to marshal field message_ids: %w", err) + } + v["message_ids"] = string(bs) + } + if opts != nil { + if opts.MessageThreadId != 0 { + v["message_thread_id"] = strconv.FormatInt(opts.MessageThreadId, 10) + } + v["disable_notification"] = strconv.FormatBool(opts.DisableNotification) + v["protect_content"] = strconv.FormatBool(opts.ProtectContent) + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "forwardMessages", v, nil, reqOpts) + if err != nil { + return nil, err + } + + var m []MessageId + return m, json.Unmarshal(r, &m) +} + // GetBusinessConnectionOpts is the set of optional fields for Bot.GetBusinessConnection. type GetBusinessConnectionOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests @@ -1897,6 +3100,25 @@ func (bot *Bot) GetBusinessConnection(businessConnectionId string, opts *GetBusi return &b, json.Unmarshal(r, &b) } +// GetBusinessConnectionWithContext is the same as GetBusinessConnection, but with a context.Context parameter. +func (bot *Bot) GetBusinessConnectionWithContext(ctx context.Context, businessConnectionId string, opts *GetBusinessConnectionOpts) (*BusinessConnection, error) { + v := map[string]string{} + v["business_connection_id"] = businessConnectionId + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "getBusinessConnection", v, nil, reqOpts) + if err != nil { + return nil, err + } + + var b BusinessConnection + return &b, json.Unmarshal(r, &b) +} + // GetChatOpts is the set of optional fields for Bot.GetChat. type GetChatOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests @@ -1926,6 +3148,25 @@ func (bot *Bot) GetChat(chatId int64, opts *GetChatOpts) (*ChatFullInfo, error) return &c, json.Unmarshal(r, &c) } +// GetChatWithContext is the same as GetChat, but with a context.Context parameter. +func (bot *Bot) GetChatWithContext(ctx context.Context, chatId int64, opts *GetChatOpts) (*ChatFullInfo, error) { + v := map[string]string{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "getChat", v, nil, reqOpts) + if err != nil { + return nil, err + } + + var c ChatFullInfo + return &c, json.Unmarshal(r, &c) +} + // GetChatAdministratorsOpts is the set of optional fields for Bot.GetChatAdministrators. type GetChatAdministratorsOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests @@ -1954,6 +3195,24 @@ func (bot *Bot) GetChatAdministrators(chatId int64, opts *GetChatAdministratorsO return unmarshalChatMemberArray(r) } +// GetChatAdministratorsWithContext is the same as GetChatAdministrators, but with a context.Context parameter. +func (bot *Bot) GetChatAdministratorsWithContext(ctx context.Context, chatId int64, opts *GetChatAdministratorsOpts) ([]ChatMember, error) { + v := map[string]string{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "getChatAdministrators", v, nil, reqOpts) + if err != nil { + return nil, err + } + + return unmarshalChatMemberArray(r) +} + // GetChatMemberOpts is the set of optional fields for Bot.GetChatMember. type GetChatMemberOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests @@ -1984,6 +3243,25 @@ func (bot *Bot) GetChatMember(chatId int64, userId int64, opts *GetChatMemberOpt return unmarshalChatMember(r) } +// GetChatMemberWithContext is the same as GetChatMember, but with a context.Context parameter. +func (bot *Bot) GetChatMemberWithContext(ctx context.Context, chatId int64, userId int64, opts *GetChatMemberOpts) (ChatMember, error) { + v := map[string]string{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + v["user_id"] = strconv.FormatInt(userId, 10) + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "getChatMember", v, nil, reqOpts) + if err != nil { + return nil, err + } + + return unmarshalChatMember(r) +} + // GetChatMemberCountOpts is the set of optional fields for Bot.GetChatMemberCount. type GetChatMemberCountOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests @@ -2013,6 +3291,25 @@ func (bot *Bot) GetChatMemberCount(chatId int64, opts *GetChatMemberCountOpts) ( return i, json.Unmarshal(r, &i) } +// GetChatMemberCountWithContext is the same as GetChatMemberCount, but with a context.Context parameter. +func (bot *Bot) GetChatMemberCountWithContext(ctx context.Context, chatId int64, opts *GetChatMemberCountOpts) (int64, error) { + v := map[string]string{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "getChatMemberCount", v, nil, reqOpts) + if err != nil { + return 0, err + } + + var i int64 + return i, json.Unmarshal(r, &i) +} + // GetChatMenuButtonOpts is the set of optional fields for Bot.GetChatMenuButton. type GetChatMenuButtonOpts struct { // Unique identifier for the target private chat. If not specified, default bot's menu button will be returned @@ -2046,6 +3343,28 @@ func (bot *Bot) GetChatMenuButton(opts *GetChatMenuButtonOpts) (MenuButton, erro return unmarshalMenuButton(r) } +// GetChatMenuButtonWithContext is the same as GetChatMenuButton, but with a context.Context parameter. +func (bot *Bot) GetChatMenuButtonWithContext(ctx context.Context, opts *GetChatMenuButtonOpts) (MenuButton, error) { + v := map[string]string{} + if opts != nil { + if opts.ChatId != nil { + v["chat_id"] = strconv.FormatInt(*opts.ChatId, 10) + } + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "getChatMenuButton", v, nil, reqOpts) + if err != nil { + return nil, err + } + + return unmarshalMenuButton(r) +} + // GetCustomEmojiStickersOpts is the set of optional fields for Bot.GetCustomEmojiStickers. type GetCustomEmojiStickersOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests @@ -2081,19 +3400,63 @@ func (bot *Bot) GetCustomEmojiStickers(customEmojiIds []string, opts *GetCustomE return s, json.Unmarshal(r, &s) } -// GetFileOpts is the set of optional fields for Bot.GetFile. -type GetFileOpts struct { - // RequestOpts are an additional optional field to configure timeouts for individual requests - RequestOpts *RequestOpts +// GetCustomEmojiStickersWithContext is the same as GetCustomEmojiStickers, but with a context.Context parameter. +func (bot *Bot) GetCustomEmojiStickersWithContext(ctx context.Context, customEmojiIds []string, opts *GetCustomEmojiStickersOpts) ([]Sticker, error) { + v := map[string]string{} + if customEmojiIds != nil { + bs, err := json.Marshal(customEmojiIds) + if err != nil { + return nil, fmt.Errorf("failed to marshal field custom_emoji_ids: %w", err) + } + v["custom_emoji_ids"] = string(bs) + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "getCustomEmojiStickers", v, nil, reqOpts) + if err != nil { + return nil, err + } + + var s []Sticker + return s, json.Unmarshal(r, &s) +} + +// GetFileOpts is the set of optional fields for Bot.GetFile. +type GetFileOpts struct { + // RequestOpts are an additional optional field to configure timeouts for individual requests + RequestOpts *RequestOpts +} + +// GetFile (https://core.telegram.org/bots/api#getfile) +// +// Use this method to get basic information about a file and prepare it for downloading. For the moment, bots can download files of up to 20MB in size. On success, a File object is returned. The file can then be downloaded via the link https://api.telegram.org/file/bot/, where is taken from the response. It is guaranteed that the link will be valid for at least 1 hour. When the link expires, a new one can be requested by calling getFile again. +// Note: This function may not preserve the original file name and MIME type. You should save the file's MIME type and name (if available) when the File object is received. +// - fileId (type string): File identifier to get information about +// - opts (type GetFileOpts): All optional parameters. +func (bot *Bot) GetFile(fileId string, opts *GetFileOpts) (*File, error) { + v := map[string]string{} + v["file_id"] = fileId + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.Request("getFile", v, nil, reqOpts) + if err != nil { + return nil, err + } + + var f File + return &f, json.Unmarshal(r, &f) } -// GetFile (https://core.telegram.org/bots/api#getfile) -// -// Use this method to get basic information about a file and prepare it for downloading. For the moment, bots can download files of up to 20MB in size. On success, a File object is returned. The file can then be downloaded via the link https://api.telegram.org/file/bot/, where is taken from the response. It is guaranteed that the link will be valid for at least 1 hour. When the link expires, a new one can be requested by calling getFile again. -// Note: This function may not preserve the original file name and MIME type. You should save the file's MIME type and name (if available) when the File object is received. -// - fileId (type string): File identifier to get information about -// - opts (type GetFileOpts): All optional parameters. -func (bot *Bot) GetFile(fileId string, opts *GetFileOpts) (*File, error) { +// GetFileWithContext is the same as GetFile, but with a context.Context parameter. +func (bot *Bot) GetFileWithContext(ctx context.Context, fileId string, opts *GetFileOpts) (*File, error) { v := map[string]string{} v["file_id"] = fileId @@ -2102,7 +3465,7 @@ func (bot *Bot) GetFile(fileId string, opts *GetFileOpts) (*File, error) { reqOpts = opts.RequestOpts } - r, err := bot.Request("getFile", v, nil, reqOpts) + r, err := bot.RequestWithContext(ctx, "getFile", v, nil, reqOpts) if err != nil { return nil, err } @@ -2138,6 +3501,24 @@ func (bot *Bot) GetForumTopicIconStickers(opts *GetForumTopicIconStickersOpts) ( return s, json.Unmarshal(r, &s) } +// GetForumTopicIconStickersWithContext is the same as GetForumTopicIconStickers, but with a context.Context parameter. +func (bot *Bot) GetForumTopicIconStickersWithContext(ctx context.Context, opts *GetForumTopicIconStickersOpts) ([]Sticker, error) { + v := map[string]string{} + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "getForumTopicIconStickers", v, nil, reqOpts) + if err != nil { + return nil, err + } + + var s []Sticker + return s, json.Unmarshal(r, &s) +} + // GetGameHighScoresOpts is the set of optional fields for Bot.GetGameHighScores. type GetGameHighScoresOpts struct { // Required if inline_message_id is not specified. Unique identifier for the target chat @@ -2182,6 +3563,34 @@ func (bot *Bot) GetGameHighScores(userId int64, opts *GetGameHighScoresOpts) ([] return g, json.Unmarshal(r, &g) } +// GetGameHighScoresWithContext is the same as GetGameHighScores, but with a context.Context parameter. +func (bot *Bot) GetGameHighScoresWithContext(ctx context.Context, userId int64, opts *GetGameHighScoresOpts) ([]GameHighScore, error) { + v := map[string]string{} + v["user_id"] = strconv.FormatInt(userId, 10) + if opts != nil { + if opts.ChatId != 0 { + v["chat_id"] = strconv.FormatInt(opts.ChatId, 10) + } + if opts.MessageId != 0 { + v["message_id"] = strconv.FormatInt(opts.MessageId, 10) + } + v["inline_message_id"] = opts.InlineMessageId + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "getGameHighScores", v, nil, reqOpts) + if err != nil { + return nil, err + } + + var g []GameHighScore + return g, json.Unmarshal(r, &g) +} + // GetMeOpts is the set of optional fields for Bot.GetMe. type GetMeOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests @@ -2209,6 +3618,24 @@ func (bot *Bot) GetMe(opts *GetMeOpts) (*User, error) { return &u, json.Unmarshal(r, &u) } +// GetMeWithContext is the same as GetMe, but with a context.Context parameter. +func (bot *Bot) GetMeWithContext(ctx context.Context, opts *GetMeOpts) (*User, error) { + v := map[string]string{} + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "getMe", v, nil, reqOpts) + if err != nil { + return nil, err + } + + var u User + return &u, json.Unmarshal(r, &u) +} + // GetMyCommandsOpts is the set of optional fields for Bot.GetMyCommands. type GetMyCommandsOpts struct { // A JSON-serialized object, describing scope of users. Defaults to BotCommandScopeDefault. @@ -2248,6 +3675,32 @@ func (bot *Bot) GetMyCommands(opts *GetMyCommandsOpts) ([]BotCommand, error) { return b, json.Unmarshal(r, &b) } +// GetMyCommandsWithContext is the same as GetMyCommands, but with a context.Context parameter. +func (bot *Bot) GetMyCommandsWithContext(ctx context.Context, opts *GetMyCommandsOpts) ([]BotCommand, error) { + v := map[string]string{} + if opts != nil { + bs, err := json.Marshal(opts.Scope) + if err != nil { + return nil, fmt.Errorf("failed to marshal field scope: %w", err) + } + v["scope"] = string(bs) + v["language_code"] = opts.LanguageCode + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "getMyCommands", v, nil, reqOpts) + if err != nil { + return nil, err + } + + var b []BotCommand + return b, json.Unmarshal(r, &b) +} + // GetMyDefaultAdministratorRightsOpts is the set of optional fields for Bot.GetMyDefaultAdministratorRights. type GetMyDefaultAdministratorRightsOpts struct { // Pass True to get default administrator rights of the bot in channels. Otherwise, default administrator rights of the bot for groups and supergroups will be returned. @@ -2280,6 +3733,27 @@ func (bot *Bot) GetMyDefaultAdministratorRights(opts *GetMyDefaultAdministratorR return &c, json.Unmarshal(r, &c) } +// GetMyDefaultAdministratorRightsWithContext is the same as GetMyDefaultAdministratorRights, but with a context.Context parameter. +func (bot *Bot) GetMyDefaultAdministratorRightsWithContext(ctx context.Context, opts *GetMyDefaultAdministratorRightsOpts) (*ChatAdministratorRights, error) { + v := map[string]string{} + if opts != nil { + v["for_channels"] = strconv.FormatBool(opts.ForChannels) + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "getMyDefaultAdministratorRights", v, nil, reqOpts) + if err != nil { + return nil, err + } + + var c ChatAdministratorRights + return &c, json.Unmarshal(r, &c) +} + // GetMyDescriptionOpts is the set of optional fields for Bot.GetMyDescription. type GetMyDescriptionOpts struct { // A two-letter ISO 639-1 language code or an empty string @@ -2312,6 +3786,27 @@ func (bot *Bot) GetMyDescription(opts *GetMyDescriptionOpts) (*BotDescription, e return &b, json.Unmarshal(r, &b) } +// GetMyDescriptionWithContext is the same as GetMyDescription, but with a context.Context parameter. +func (bot *Bot) GetMyDescriptionWithContext(ctx context.Context, opts *GetMyDescriptionOpts) (*BotDescription, error) { + v := map[string]string{} + if opts != nil { + v["language_code"] = opts.LanguageCode + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "getMyDescription", v, nil, reqOpts) + if err != nil { + return nil, err + } + + var b BotDescription + return &b, json.Unmarshal(r, &b) +} + // GetMyNameOpts is the set of optional fields for Bot.GetMyName. type GetMyNameOpts struct { // A two-letter ISO 639-1 language code or an empty string @@ -2344,6 +3839,27 @@ func (bot *Bot) GetMyName(opts *GetMyNameOpts) (*BotName, error) { return &b, json.Unmarshal(r, &b) } +// GetMyNameWithContext is the same as GetMyName, but with a context.Context parameter. +func (bot *Bot) GetMyNameWithContext(ctx context.Context, opts *GetMyNameOpts) (*BotName, error) { + v := map[string]string{} + if opts != nil { + v["language_code"] = opts.LanguageCode + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "getMyName", v, nil, reqOpts) + if err != nil { + return nil, err + } + + var b BotName + return &b, json.Unmarshal(r, &b) +} + // GetMyShortDescriptionOpts is the set of optional fields for Bot.GetMyShortDescription. type GetMyShortDescriptionOpts struct { // A two-letter ISO 639-1 language code or an empty string @@ -2376,6 +3892,27 @@ func (bot *Bot) GetMyShortDescription(opts *GetMyShortDescriptionOpts) (*BotShor return &b, json.Unmarshal(r, &b) } +// GetMyShortDescriptionWithContext is the same as GetMyShortDescription, but with a context.Context parameter. +func (bot *Bot) GetMyShortDescriptionWithContext(ctx context.Context, opts *GetMyShortDescriptionOpts) (*BotShortDescription, error) { + v := map[string]string{} + if opts != nil { + v["language_code"] = opts.LanguageCode + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "getMyShortDescription", v, nil, reqOpts) + if err != nil { + return nil, err + } + + var b BotShortDescription + 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 @@ -2415,6 +3952,32 @@ func (bot *Bot) GetStarTransactions(opts *GetStarTransactionsOpts) (*StarTransac return &s, json.Unmarshal(r, &s) } +// GetStarTransactionsWithContext is the same as GetStarTransactions, but with a context.Context parameter. +func (bot *Bot) GetStarTransactionsWithContext(ctx context.Context, 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.RequestWithContext(ctx, "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 @@ -2444,6 +4007,25 @@ func (bot *Bot) GetStickerSet(name string, opts *GetStickerSetOpts) (*StickerSet return &s, json.Unmarshal(r, &s) } +// GetStickerSetWithContext is the same as GetStickerSet, but with a context.Context parameter. +func (bot *Bot) GetStickerSetWithContext(ctx context.Context, name string, opts *GetStickerSetOpts) (*StickerSet, error) { + v := map[string]string{} + v["name"] = name + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "getStickerSet", v, nil, reqOpts) + if err != nil { + return nil, err + } + + var s StickerSet + return &s, json.Unmarshal(r, &s) +} + // GetUpdatesOpts is the set of optional fields for Bot.GetUpdates. type GetUpdatesOpts struct { // Identifier of the first update to be returned. Must be greater by one than the highest among the identifiers of previously received updates. By default, updates starting with the earliest unconfirmed update are returned. An update is considered confirmed as soon as getUpdates is called with an offset higher than its update_id. The negative offset can be specified to retrieve updates starting from -offset update from the end of the updates queue. All previous updates will be forgotten. @@ -2497,19 +4079,75 @@ func (bot *Bot) GetUpdates(opts *GetUpdatesOpts) ([]Update, error) { return u, json.Unmarshal(r, &u) } +// GetUpdatesWithContext is the same as GetUpdates, but with a context.Context parameter. +func (bot *Bot) GetUpdatesWithContext(ctx context.Context, opts *GetUpdatesOpts) ([]Update, 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) + } + if opts.Timeout != 0 { + v["timeout"] = strconv.FormatInt(opts.Timeout, 10) + } + if opts.AllowedUpdates != nil { + bs, err := json.Marshal(opts.AllowedUpdates) + if err != nil { + return nil, fmt.Errorf("failed to marshal field allowed_updates: %w", err) + } + v["allowed_updates"] = string(bs) + } + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "getUpdates", v, nil, reqOpts) + if err != nil { + return nil, err + } + + var u []Update + return u, json.Unmarshal(r, &u) +} + // GetUserChatBoostsOpts is the set of optional fields for Bot.GetUserChatBoosts. type GetUserChatBoostsOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests RequestOpts *RequestOpts } -// GetUserChatBoosts (https://core.telegram.org/bots/api#getuserchatboosts) -// -// Use this method to get the list of boosts added to a chat by a user. Requires administrator rights in the chat. Returns a UserChatBoosts object. -// - chatId (type int64): Unique identifier for the chat -// - userId (type int64): Unique identifier of the target user -// - opts (type GetUserChatBoostsOpts): All optional parameters. -func (bot *Bot) GetUserChatBoosts(chatId int64, userId int64, opts *GetUserChatBoostsOpts) (*UserChatBoosts, error) { +// GetUserChatBoosts (https://core.telegram.org/bots/api#getuserchatboosts) +// +// Use this method to get the list of boosts added to a chat by a user. Requires administrator rights in the chat. Returns a UserChatBoosts object. +// - chatId (type int64): Unique identifier for the chat +// - userId (type int64): Unique identifier of the target user +// - opts (type GetUserChatBoostsOpts): All optional parameters. +func (bot *Bot) GetUserChatBoosts(chatId int64, userId int64, opts *GetUserChatBoostsOpts) (*UserChatBoosts, error) { + v := map[string]string{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + v["user_id"] = strconv.FormatInt(userId, 10) + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.Request("getUserChatBoosts", v, nil, reqOpts) + if err != nil { + return nil, err + } + + var u UserChatBoosts + return &u, json.Unmarshal(r, &u) +} + +// GetUserChatBoostsWithContext is the same as GetUserChatBoosts, but with a context.Context parameter. +func (bot *Bot) GetUserChatBoostsWithContext(ctx context.Context, chatId int64, userId int64, opts *GetUserChatBoostsOpts) (*UserChatBoosts, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) v["user_id"] = strconv.FormatInt(userId, 10) @@ -2519,7 +4157,7 @@ func (bot *Bot) GetUserChatBoosts(chatId int64, userId int64, opts *GetUserChatB reqOpts = opts.RequestOpts } - r, err := bot.Request("getUserChatBoosts", v, nil, reqOpts) + r, err := bot.RequestWithContext(ctx, "getUserChatBoosts", v, nil, reqOpts) if err != nil { return nil, err } @@ -2569,6 +4207,33 @@ func (bot *Bot) GetUserProfilePhotos(userId int64, opts *GetUserProfilePhotosOpt return &u, json.Unmarshal(r, &u) } +// GetUserProfilePhotosWithContext is the same as GetUserProfilePhotos, but with a context.Context parameter. +func (bot *Bot) GetUserProfilePhotosWithContext(ctx context.Context, userId int64, opts *GetUserProfilePhotosOpts) (*UserProfilePhotos, error) { + v := map[string]string{} + v["user_id"] = strconv.FormatInt(userId, 10) + 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.RequestWithContext(ctx, "getUserProfilePhotos", v, nil, reqOpts) + if err != nil { + return nil, err + } + + var u UserProfilePhotos + return &u, json.Unmarshal(r, &u) +} + // GetWebhookInfoOpts is the set of optional fields for Bot.GetWebhookInfo. type GetWebhookInfoOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests @@ -2596,6 +4261,24 @@ func (bot *Bot) GetWebhookInfo(opts *GetWebhookInfoOpts) (*WebhookInfo, error) { return &w, json.Unmarshal(r, &w) } +// GetWebhookInfoWithContext is the same as GetWebhookInfo, but with a context.Context parameter. +func (bot *Bot) GetWebhookInfoWithContext(ctx context.Context, opts *GetWebhookInfoOpts) (*WebhookInfo, error) { + v := map[string]string{} + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "getWebhookInfo", v, nil, reqOpts) + if err != nil { + return nil, err + } + + var w WebhookInfo + return &w, json.Unmarshal(r, &w) +} + // HideGeneralForumTopicOpts is the set of optional fields for Bot.HideGeneralForumTopic. type HideGeneralForumTopicOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests @@ -2625,6 +4308,25 @@ func (bot *Bot) HideGeneralForumTopic(chatId int64, opts *HideGeneralForumTopicO return b, json.Unmarshal(r, &b) } +// HideGeneralForumTopicWithContext is the same as HideGeneralForumTopic, but with a context.Context parameter. +func (bot *Bot) HideGeneralForumTopicWithContext(ctx context.Context, chatId int64, opts *HideGeneralForumTopicOpts) (bool, error) { + v := map[string]string{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "hideGeneralForumTopic", v, nil, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + // LeaveChatOpts is the set of optional fields for Bot.LeaveChat. type LeaveChatOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests @@ -2654,6 +4356,25 @@ func (bot *Bot) LeaveChat(chatId int64, opts *LeaveChatOpts) (bool, error) { return b, json.Unmarshal(r, &b) } +// LeaveChatWithContext is the same as LeaveChat, but with a context.Context parameter. +func (bot *Bot) LeaveChatWithContext(ctx context.Context, chatId int64, opts *LeaveChatOpts) (bool, error) { + v := map[string]string{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "leaveChat", v, nil, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + // LogOutOpts is the set of optional fields for Bot.LogOut. type LogOutOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests @@ -2681,6 +4402,24 @@ func (bot *Bot) LogOut(opts *LogOutOpts) (bool, error) { return b, json.Unmarshal(r, &b) } +// LogOutWithContext is the same as LogOut, but with a context.Context parameter. +func (bot *Bot) LogOutWithContext(ctx context.Context, opts *LogOutOpts) (bool, error) { + v := map[string]string{} + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "logOut", v, nil, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + // PinChatMessageOpts is the set of optional fields for Bot.PinChatMessage. type PinChatMessageOpts struct { // Unique identifier of the business connection on behalf of which the message will be pinned @@ -2720,6 +4459,30 @@ func (bot *Bot) PinChatMessage(chatId int64, messageId int64, opts *PinChatMessa return b, json.Unmarshal(r, &b) } +// PinChatMessageWithContext is the same as PinChatMessage, but with a context.Context parameter. +func (bot *Bot) PinChatMessageWithContext(ctx context.Context, chatId int64, messageId int64, opts *PinChatMessageOpts) (bool, error) { + v := map[string]string{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + v["message_id"] = strconv.FormatInt(messageId, 10) + if opts != nil { + v["business_connection_id"] = opts.BusinessConnectionId + v["disable_notification"] = strconv.FormatBool(opts.DisableNotification) + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "pinChatMessage", v, nil, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + // PromoteChatMemberOpts is the set of optional fields for Bot.PromoteChatMember. type PromoteChatMemberOpts struct { // Pass True if the administrator's presence in the chat is hidden @@ -2798,6 +4561,43 @@ func (bot *Bot) PromoteChatMember(chatId int64, userId int64, opts *PromoteChatM return b, json.Unmarshal(r, &b) } +// PromoteChatMemberWithContext is the same as PromoteChatMember, but with a context.Context parameter. +func (bot *Bot) PromoteChatMemberWithContext(ctx context.Context, chatId int64, userId int64, opts *PromoteChatMemberOpts) (bool, error) { + v := map[string]string{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + v["user_id"] = strconv.FormatInt(userId, 10) + if opts != nil { + v["is_anonymous"] = strconv.FormatBool(opts.IsAnonymous) + v["can_manage_chat"] = strconv.FormatBool(opts.CanManageChat) + v["can_delete_messages"] = strconv.FormatBool(opts.CanDeleteMessages) + v["can_manage_video_chats"] = strconv.FormatBool(opts.CanManageVideoChats) + v["can_restrict_members"] = strconv.FormatBool(opts.CanRestrictMembers) + v["can_promote_members"] = strconv.FormatBool(opts.CanPromoteMembers) + v["can_change_info"] = strconv.FormatBool(opts.CanChangeInfo) + v["can_invite_users"] = strconv.FormatBool(opts.CanInviteUsers) + v["can_post_stories"] = strconv.FormatBool(opts.CanPostStories) + v["can_edit_stories"] = strconv.FormatBool(opts.CanEditStories) + v["can_delete_stories"] = strconv.FormatBool(opts.CanDeleteStories) + v["can_post_messages"] = strconv.FormatBool(opts.CanPostMessages) + v["can_edit_messages"] = strconv.FormatBool(opts.CanEditMessages) + v["can_pin_messages"] = strconv.FormatBool(opts.CanPinMessages) + v["can_manage_topics"] = strconv.FormatBool(opts.CanManageTopics) + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "promoteChatMember", v, nil, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + // RefundStarPaymentOpts is the set of optional fields for Bot.RefundStarPayment. type RefundStarPaymentOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests @@ -2829,6 +4629,26 @@ func (bot *Bot) RefundStarPayment(userId int64, telegramPaymentChargeId string, return b, json.Unmarshal(r, &b) } +// RefundStarPaymentWithContext is the same as RefundStarPayment, but with a context.Context parameter. +func (bot *Bot) RefundStarPaymentWithContext(ctx context.Context, userId int64, telegramPaymentChargeId string, opts *RefundStarPaymentOpts) (bool, error) { + v := map[string]string{} + v["user_id"] = strconv.FormatInt(userId, 10) + v["telegram_payment_charge_id"] = telegramPaymentChargeId + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "refundStarPayment", v, nil, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + // ReopenForumTopicOpts is the set of optional fields for Bot.ReopenForumTopic. type ReopenForumTopicOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests @@ -2860,6 +4680,26 @@ func (bot *Bot) ReopenForumTopic(chatId int64, messageThreadId int64, opts *Reop return b, json.Unmarshal(r, &b) } +// ReopenForumTopicWithContext is the same as ReopenForumTopic, but with a context.Context parameter. +func (bot *Bot) ReopenForumTopicWithContext(ctx context.Context, chatId int64, messageThreadId int64, opts *ReopenForumTopicOpts) (bool, error) { + v := map[string]string{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + v["message_thread_id"] = strconv.FormatInt(messageThreadId, 10) + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "reopenForumTopic", v, nil, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + // ReopenGeneralForumTopicOpts is the set of optional fields for Bot.ReopenGeneralForumTopic. type ReopenGeneralForumTopicOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests @@ -2889,6 +4729,25 @@ func (bot *Bot) ReopenGeneralForumTopic(chatId int64, opts *ReopenGeneralForumTo return b, json.Unmarshal(r, &b) } +// ReopenGeneralForumTopicWithContext is the same as ReopenGeneralForumTopic, but with a context.Context parameter. +func (bot *Bot) ReopenGeneralForumTopicWithContext(ctx context.Context, chatId int64, opts *ReopenGeneralForumTopicOpts) (bool, error) { + v := map[string]string{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "reopenGeneralForumTopic", v, nil, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + // ReplaceStickerInSetOpts is the set of optional fields for Bot.ReplaceStickerInSet. type ReplaceStickerInSetOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests @@ -2929,6 +4788,33 @@ func (bot *Bot) ReplaceStickerInSet(userId int64, name string, oldSticker string return b, json.Unmarshal(r, &b) } +// ReplaceStickerInSetWithContext is the same as ReplaceStickerInSet, but with a context.Context parameter. +func (bot *Bot) ReplaceStickerInSetWithContext(ctx context.Context, userId int64, name string, oldSticker string, sticker InputSticker, opts *ReplaceStickerInSetOpts) (bool, error) { + v := map[string]string{} + data := map[string]FileReader{} + v["user_id"] = strconv.FormatInt(userId, 10) + v["name"] = name + v["old_sticker"] = oldSticker + inputBs, err := sticker.InputParams("sticker", data) + if err != nil { + return false, fmt.Errorf("failed to marshal field sticker: %w", err) + } + v["sticker"] = string(inputBs) + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "replaceStickerInSet", v, data, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + // RestrictChatMemberOpts is the set of optional fields for Bot.RestrictChatMember. type RestrictChatMemberOpts struct { // Pass True if chat permissions are set independently. Otherwise, the can_send_other_messages and can_add_web_page_previews permissions will imply the can_send_messages, can_send_audios, can_send_documents, can_send_photos, can_send_videos, can_send_video_notes, and can_send_voice_notes permissions; the can_send_polls permission will imply the can_send_messages permission. @@ -2976,6 +4862,37 @@ func (bot *Bot) RestrictChatMember(chatId int64, userId int64, permissions ChatP return b, json.Unmarshal(r, &b) } +// RestrictChatMemberWithContext is the same as RestrictChatMember, but with a context.Context parameter. +func (bot *Bot) RestrictChatMemberWithContext(ctx context.Context, chatId int64, userId int64, permissions ChatPermissions, opts *RestrictChatMemberOpts) (bool, error) { + v := map[string]string{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + v["user_id"] = strconv.FormatInt(userId, 10) + bs, err := json.Marshal(permissions) + if err != nil { + return false, fmt.Errorf("failed to marshal field permissions: %w", err) + } + v["permissions"] = string(bs) + if opts != nil { + v["use_independent_chat_permissions"] = strconv.FormatBool(opts.UseIndependentChatPermissions) + if opts.UntilDate != 0 { + v["until_date"] = strconv.FormatInt(opts.UntilDate, 10) + } + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "restrictChatMember", v, nil, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + // RevokeChatInviteLinkOpts is the set of optional fields for Bot.RevokeChatInviteLink. type RevokeChatInviteLinkOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests @@ -3007,6 +4924,26 @@ func (bot *Bot) RevokeChatInviteLink(chatId int64, inviteLink string, opts *Revo return &c, json.Unmarshal(r, &c) } +// RevokeChatInviteLinkWithContext is the same as RevokeChatInviteLink, but with a context.Context parameter. +func (bot *Bot) RevokeChatInviteLinkWithContext(ctx context.Context, chatId int64, inviteLink string, opts *RevokeChatInviteLinkOpts) (*ChatInviteLink, error) { + v := map[string]string{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + v["invite_link"] = inviteLink + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "revokeChatInviteLink", v, nil, reqOpts) + if err != nil { + return nil, err + } + + var c ChatInviteLink + return &c, json.Unmarshal(r, &c) +} + // SendAnimationOpts is the set of optional fields for Bot.SendAnimation. type SendAnimationOpts struct { // Unique identifier of the business connection on behalf of which the message will be sent @@ -3127,6 +5064,83 @@ func (bot *Bot) SendAnimation(chatId int64, animation InputFileOrString, opts *S return &m, json.Unmarshal(r, &m) } +// SendAnimationWithContext is the same as SendAnimation, but with a context.Context parameter. +func (bot *Bot) SendAnimationWithContext(ctx context.Context, chatId int64, animation InputFileOrString, opts *SendAnimationOpts) (*Message, error) { + v := map[string]string{} + data := map[string]FileReader{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + if animation != nil { + err := animation.Attach("animation", data) + if err != nil { + return nil, fmt.Errorf("failed to attach 'animation' input file: %w", err) + } + v["animation"] = animation.getValue() + } + if opts != nil { + v["business_connection_id"] = opts.BusinessConnectionId + if opts.MessageThreadId != 0 { + v["message_thread_id"] = strconv.FormatInt(opts.MessageThreadId, 10) + } + if opts.Duration != 0 { + v["duration"] = strconv.FormatInt(opts.Duration, 10) + } + if opts.Width != 0 { + v["width"] = strconv.FormatInt(opts.Width, 10) + } + if opts.Height != 0 { + v["height"] = strconv.FormatInt(opts.Height, 10) + } + if opts.Thumbnail != nil { + err := opts.Thumbnail.Attach("thumbnail", data) + if err != nil { + return nil, fmt.Errorf("failed to attach 'thumbnail' input file: %w", err) + } + v["thumbnail"] = opts.Thumbnail.getValue() + } + v["caption"] = opts.Caption + v["parse_mode"] = opts.ParseMode + if opts.CaptionEntities != nil { + bs, err := json.Marshal(opts.CaptionEntities) + if err != nil { + return nil, fmt.Errorf("failed to marshal field caption_entities: %w", err) + } + v["caption_entities"] = string(bs) + } + v["show_caption_above_media"] = strconv.FormatBool(opts.ShowCaptionAboveMedia) + v["has_spoiler"] = strconv.FormatBool(opts.HasSpoiler) + v["disable_notification"] = strconv.FormatBool(opts.DisableNotification) + v["protect_content"] = strconv.FormatBool(opts.ProtectContent) + v["message_effect_id"] = opts.MessageEffectId + if opts.ReplyParameters != nil { + bs, err := json.Marshal(opts.ReplyParameters) + if err != nil { + return nil, fmt.Errorf("failed to marshal field reply_parameters: %w", err) + } + v["reply_parameters"] = string(bs) + } + if opts.ReplyMarkup != nil { + bs, err := json.Marshal(opts.ReplyMarkup) + if err != nil { + return nil, fmt.Errorf("failed to marshal field reply_markup: %w", err) + } + v["reply_markup"] = string(bs) + } + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "sendAnimation", v, data, reqOpts) + if err != nil { + return nil, err + } + + var m Message + return &m, json.Unmarshal(r, &m) +} + // SendAudioOpts is the set of optional fields for Bot.SendAudio. type SendAudioOpts struct { // Unique identifier of the business connection on behalf of which the message will be sent @@ -3238,6 +5252,77 @@ func (bot *Bot) SendAudio(chatId int64, audio InputFileOrString, opts *SendAudio return &m, json.Unmarshal(r, &m) } +// SendAudioWithContext is the same as SendAudio, but with a context.Context parameter. +func (bot *Bot) SendAudioWithContext(ctx context.Context, chatId int64, audio InputFileOrString, opts *SendAudioOpts) (*Message, error) { + v := map[string]string{} + data := map[string]FileReader{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + if audio != nil { + err := audio.Attach("audio", data) + if err != nil { + return nil, fmt.Errorf("failed to attach 'audio' input file: %w", err) + } + v["audio"] = audio.getValue() + } + if opts != nil { + v["business_connection_id"] = opts.BusinessConnectionId + if opts.MessageThreadId != 0 { + v["message_thread_id"] = strconv.FormatInt(opts.MessageThreadId, 10) + } + v["caption"] = opts.Caption + v["parse_mode"] = opts.ParseMode + if opts.CaptionEntities != nil { + bs, err := json.Marshal(opts.CaptionEntities) + if err != nil { + return nil, fmt.Errorf("failed to marshal field caption_entities: %w", err) + } + v["caption_entities"] = string(bs) + } + if opts.Duration != 0 { + v["duration"] = strconv.FormatInt(opts.Duration, 10) + } + v["performer"] = opts.Performer + v["title"] = opts.Title + if opts.Thumbnail != nil { + err := opts.Thumbnail.Attach("thumbnail", data) + if err != nil { + return nil, fmt.Errorf("failed to attach 'thumbnail' input file: %w", err) + } + v["thumbnail"] = opts.Thumbnail.getValue() + } + v["disable_notification"] = strconv.FormatBool(opts.DisableNotification) + v["protect_content"] = strconv.FormatBool(opts.ProtectContent) + v["message_effect_id"] = opts.MessageEffectId + if opts.ReplyParameters != nil { + bs, err := json.Marshal(opts.ReplyParameters) + if err != nil { + return nil, fmt.Errorf("failed to marshal field reply_parameters: %w", err) + } + v["reply_parameters"] = string(bs) + } + if opts.ReplyMarkup != nil { + bs, err := json.Marshal(opts.ReplyMarkup) + if err != nil { + return nil, fmt.Errorf("failed to marshal field reply_markup: %w", err) + } + v["reply_markup"] = string(bs) + } + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "sendAudio", v, data, reqOpts) + if err != nil { + return nil, err + } + + var m Message + return &m, json.Unmarshal(r, &m) +} + // SendChatActionOpts is the set of optional fields for Bot.SendChatAction. type SendChatActionOpts struct { // Unique identifier of the business connection on behalf of which the action will be sent @@ -3280,6 +5365,32 @@ func (bot *Bot) SendChatAction(chatId int64, action string, opts *SendChatAction return b, json.Unmarshal(r, &b) } +// SendChatActionWithContext is the same as SendChatAction, but with a context.Context parameter. +func (bot *Bot) SendChatActionWithContext(ctx context.Context, chatId int64, action string, opts *SendChatActionOpts) (bool, error) { + v := map[string]string{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + v["action"] = action + if opts != nil { + v["business_connection_id"] = opts.BusinessConnectionId + if opts.MessageThreadId != 0 { + v["message_thread_id"] = strconv.FormatInt(opts.MessageThreadId, 10) + } + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "sendChatAction", v, nil, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + // SendContactOpts is the set of optional fields for Bot.SendContact. type SendContactOpts struct { // Unique identifier of the business connection on behalf of which the message will be sent @@ -3356,6 +5467,52 @@ func (bot *Bot) SendContact(chatId int64, phoneNumber string, firstName string, return &m, json.Unmarshal(r, &m) } +// SendContactWithContext is the same as SendContact, but with a context.Context parameter. +func (bot *Bot) SendContactWithContext(ctx context.Context, chatId int64, phoneNumber string, firstName string, opts *SendContactOpts) (*Message, error) { + v := map[string]string{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + v["phone_number"] = phoneNumber + v["first_name"] = firstName + if opts != nil { + v["business_connection_id"] = opts.BusinessConnectionId + if opts.MessageThreadId != 0 { + v["message_thread_id"] = strconv.FormatInt(opts.MessageThreadId, 10) + } + v["last_name"] = opts.LastName + v["vcard"] = opts.Vcard + v["disable_notification"] = strconv.FormatBool(opts.DisableNotification) + v["protect_content"] = strconv.FormatBool(opts.ProtectContent) + v["message_effect_id"] = opts.MessageEffectId + if opts.ReplyParameters != nil { + bs, err := json.Marshal(opts.ReplyParameters) + if err != nil { + return nil, fmt.Errorf("failed to marshal field reply_parameters: %w", err) + } + v["reply_parameters"] = string(bs) + } + if opts.ReplyMarkup != nil { + bs, err := json.Marshal(opts.ReplyMarkup) + if err != nil { + return nil, fmt.Errorf("failed to marshal field reply_markup: %w", err) + } + v["reply_markup"] = string(bs) + } + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "sendContact", v, nil, reqOpts) + if err != nil { + return nil, err + } + + var m Message + return &m, json.Unmarshal(r, &m) +} + // SendDiceOpts is the set of optional fields for Bot.SendDice. type SendDiceOpts struct { // Unique identifier of the business connection on behalf of which the message will be sent @@ -3378,12 +5535,55 @@ type SendDiceOpts struct { RequestOpts *RequestOpts } -// SendDice (https://core.telegram.org/bots/api#senddice) -// -// Use this method to send an animated emoji that will display a random value. On success, the sent Message is returned. -// - chatId (type int64): Unique identifier for the target chat -// - opts (type SendDiceOpts): All optional parameters. -func (bot *Bot) SendDice(chatId int64, opts *SendDiceOpts) (*Message, error) { +// SendDice (https://core.telegram.org/bots/api#senddice) +// +// Use this method to send an animated emoji that will display a random value. On success, the sent Message is returned. +// - chatId (type int64): Unique identifier for the target chat +// - opts (type SendDiceOpts): All optional parameters. +func (bot *Bot) SendDice(chatId int64, opts *SendDiceOpts) (*Message, error) { + v := map[string]string{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + if opts != nil { + v["business_connection_id"] = opts.BusinessConnectionId + if opts.MessageThreadId != 0 { + v["message_thread_id"] = strconv.FormatInt(opts.MessageThreadId, 10) + } + v["emoji"] = opts.Emoji + v["disable_notification"] = strconv.FormatBool(opts.DisableNotification) + v["protect_content"] = strconv.FormatBool(opts.ProtectContent) + v["message_effect_id"] = opts.MessageEffectId + if opts.ReplyParameters != nil { + bs, err := json.Marshal(opts.ReplyParameters) + if err != nil { + return nil, fmt.Errorf("failed to marshal field reply_parameters: %w", err) + } + v["reply_parameters"] = string(bs) + } + if opts.ReplyMarkup != nil { + bs, err := json.Marshal(opts.ReplyMarkup) + if err != nil { + return nil, fmt.Errorf("failed to marshal field reply_markup: %w", err) + } + v["reply_markup"] = string(bs) + } + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.Request("sendDice", v, nil, reqOpts) + if err != nil { + return nil, err + } + + var m Message + return &m, json.Unmarshal(r, &m) +} + +// SendDiceWithContext is the same as SendDice, but with a context.Context parameter. +func (bot *Bot) SendDiceWithContext(ctx context.Context, chatId int64, opts *SendDiceOpts) (*Message, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) if opts != nil { @@ -3416,7 +5616,7 @@ func (bot *Bot) SendDice(chatId int64, opts *SendDiceOpts) (*Message, error) { reqOpts = opts.RequestOpts } - r, err := bot.Request("sendDice", v, nil, reqOpts) + r, err := bot.RequestWithContext(ctx, "sendDice", v, nil, reqOpts) if err != nil { return nil, err } @@ -3527,6 +5727,73 @@ func (bot *Bot) SendDocument(chatId int64, document InputFileOrString, opts *Sen return &m, json.Unmarshal(r, &m) } +// SendDocumentWithContext is the same as SendDocument, but with a context.Context parameter. +func (bot *Bot) SendDocumentWithContext(ctx context.Context, chatId int64, document InputFileOrString, opts *SendDocumentOpts) (*Message, error) { + v := map[string]string{} + data := map[string]FileReader{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + if document != nil { + err := document.Attach("document", data) + if err != nil { + return nil, fmt.Errorf("failed to attach 'document' input file: %w", err) + } + v["document"] = document.getValue() + } + if opts != nil { + v["business_connection_id"] = opts.BusinessConnectionId + if opts.MessageThreadId != 0 { + v["message_thread_id"] = strconv.FormatInt(opts.MessageThreadId, 10) + } + if opts.Thumbnail != nil { + err := opts.Thumbnail.Attach("thumbnail", data) + if err != nil { + return nil, fmt.Errorf("failed to attach 'thumbnail' input file: %w", err) + } + v["thumbnail"] = opts.Thumbnail.getValue() + } + v["caption"] = opts.Caption + v["parse_mode"] = opts.ParseMode + if opts.CaptionEntities != nil { + bs, err := json.Marshal(opts.CaptionEntities) + if err != nil { + return nil, fmt.Errorf("failed to marshal field caption_entities: %w", err) + } + v["caption_entities"] = string(bs) + } + v["disable_content_type_detection"] = strconv.FormatBool(opts.DisableContentTypeDetection) + v["disable_notification"] = strconv.FormatBool(opts.DisableNotification) + v["protect_content"] = strconv.FormatBool(opts.ProtectContent) + v["message_effect_id"] = opts.MessageEffectId + if opts.ReplyParameters != nil { + bs, err := json.Marshal(opts.ReplyParameters) + if err != nil { + return nil, fmt.Errorf("failed to marshal field reply_parameters: %w", err) + } + v["reply_parameters"] = string(bs) + } + if opts.ReplyMarkup != nil { + bs, err := json.Marshal(opts.ReplyMarkup) + if err != nil { + return nil, fmt.Errorf("failed to marshal field reply_markup: %w", err) + } + v["reply_markup"] = string(bs) + } + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "sendDocument", v, data, reqOpts) + if err != nil { + return nil, err + } + + var m Message + return &m, json.Unmarshal(r, &m) +} + // SendGameOpts is the set of optional fields for Bot.SendGame. type SendGameOpts struct { // Unique identifier of the business connection on behalf of which the message will be sent @@ -3593,6 +5860,47 @@ func (bot *Bot) SendGame(chatId int64, gameShortName string, opts *SendGameOpts) return &m, json.Unmarshal(r, &m) } +// SendGameWithContext is the same as SendGame, but with a context.Context parameter. +func (bot *Bot) SendGameWithContext(ctx context.Context, chatId int64, gameShortName string, opts *SendGameOpts) (*Message, error) { + v := map[string]string{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + v["game_short_name"] = gameShortName + if opts != nil { + v["business_connection_id"] = opts.BusinessConnectionId + if opts.MessageThreadId != 0 { + v["message_thread_id"] = strconv.FormatInt(opts.MessageThreadId, 10) + } + v["disable_notification"] = strconv.FormatBool(opts.DisableNotification) + v["protect_content"] = strconv.FormatBool(opts.ProtectContent) + v["message_effect_id"] = opts.MessageEffectId + if opts.ReplyParameters != nil { + bs, err := json.Marshal(opts.ReplyParameters) + if err != nil { + return nil, fmt.Errorf("failed to marshal field reply_parameters: %w", err) + } + v["reply_parameters"] = string(bs) + } + bs, err := json.Marshal(opts.ReplyMarkup) + if err != nil { + return nil, fmt.Errorf("failed to marshal field reply_markup: %w", err) + } + v["reply_markup"] = string(bs) + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "sendGame", v, nil, reqOpts) + if err != nil { + return nil, err + } + + var m Message + return &m, json.Unmarshal(r, &m) +} + // SendInvoiceOpts is the set of optional fields for Bot.SendInvoice. type SendInvoiceOpts struct { // Unique identifier for the target message thread (topic) of the forum; for forum supergroups only @@ -3732,6 +6040,86 @@ func (bot *Bot) SendInvoice(chatId int64, title string, description string, payl return &m, json.Unmarshal(r, &m) } +// SendInvoiceWithContext is the same as SendInvoice, but with a context.Context parameter. +func (bot *Bot) SendInvoiceWithContext(ctx context.Context, chatId int64, title string, description string, payload string, currency string, prices []LabeledPrice, opts *SendInvoiceOpts) (*Message, error) { + v := map[string]string{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + v["title"] = title + v["description"] = description + v["payload"] = payload + v["currency"] = currency + if prices != nil { + bs, err := json.Marshal(prices) + if err != nil { + return nil, fmt.Errorf("failed to marshal field prices: %w", err) + } + v["prices"] = string(bs) + } + if opts != nil { + if opts.MessageThreadId != 0 { + v["message_thread_id"] = strconv.FormatInt(opts.MessageThreadId, 10) + } + v["provider_token"] = opts.ProviderToken + if opts.MaxTipAmount != 0 { + v["max_tip_amount"] = strconv.FormatInt(opts.MaxTipAmount, 10) + } + if opts.SuggestedTipAmounts != nil { + bs, err := json.Marshal(opts.SuggestedTipAmounts) + if err != nil { + return nil, fmt.Errorf("failed to marshal field suggested_tip_amounts: %w", err) + } + v["suggested_tip_amounts"] = string(bs) + } + v["start_parameter"] = opts.StartParameter + v["provider_data"] = opts.ProviderData + v["photo_url"] = opts.PhotoUrl + if opts.PhotoSize != 0 { + v["photo_size"] = strconv.FormatInt(opts.PhotoSize, 10) + } + if opts.PhotoWidth != 0 { + v["photo_width"] = strconv.FormatInt(opts.PhotoWidth, 10) + } + if opts.PhotoHeight != 0 { + v["photo_height"] = strconv.FormatInt(opts.PhotoHeight, 10) + } + v["need_name"] = strconv.FormatBool(opts.NeedName) + v["need_phone_number"] = strconv.FormatBool(opts.NeedPhoneNumber) + v["need_email"] = strconv.FormatBool(opts.NeedEmail) + v["need_shipping_address"] = strconv.FormatBool(opts.NeedShippingAddress) + v["send_phone_number_to_provider"] = strconv.FormatBool(opts.SendPhoneNumberToProvider) + v["send_email_to_provider"] = strconv.FormatBool(opts.SendEmailToProvider) + v["is_flexible"] = strconv.FormatBool(opts.IsFlexible) + v["disable_notification"] = strconv.FormatBool(opts.DisableNotification) + v["protect_content"] = strconv.FormatBool(opts.ProtectContent) + v["message_effect_id"] = opts.MessageEffectId + if opts.ReplyParameters != nil { + bs, err := json.Marshal(opts.ReplyParameters) + if err != nil { + return nil, fmt.Errorf("failed to marshal field reply_parameters: %w", err) + } + v["reply_parameters"] = string(bs) + } + bs, err := json.Marshal(opts.ReplyMarkup) + if err != nil { + return nil, fmt.Errorf("failed to marshal field reply_markup: %w", err) + } + v["reply_markup"] = string(bs) + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "sendInvoice", v, nil, reqOpts) + if err != nil { + return nil, err + } + + var m Message + return &m, json.Unmarshal(r, &m) +} + // SendLocationOpts is the set of optional fields for Bot.SendLocation. type SendLocationOpts struct { // Unique identifier of the business connection on behalf of which the message will be sent @@ -3813,7 +6201,63 @@ func (bot *Bot) SendLocation(chatId int64, latitude float64, longitude float64, reqOpts = opts.RequestOpts } - r, err := bot.Request("sendLocation", v, nil, reqOpts) + r, err := bot.Request("sendLocation", v, nil, reqOpts) + if err != nil { + return nil, err + } + + var m Message + return &m, json.Unmarshal(r, &m) +} + +// SendLocationWithContext is the same as SendLocation, but with a context.Context parameter. +func (bot *Bot) SendLocationWithContext(ctx context.Context, chatId int64, latitude float64, longitude float64, opts *SendLocationOpts) (*Message, error) { + v := map[string]string{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + 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.MessageThreadId != 0 { + v["message_thread_id"] = strconv.FormatInt(opts.MessageThreadId, 10) + } + if opts.HorizontalAccuracy != 0.0 { + v["horizontal_accuracy"] = strconv.FormatFloat(opts.HorizontalAccuracy, 'f', -1, 64) + } + if opts.LivePeriod != 0 { + v["live_period"] = strconv.FormatInt(opts.LivePeriod, 10) + } + if opts.Heading != 0 { + v["heading"] = strconv.FormatInt(opts.Heading, 10) + } + if opts.ProximityAlertRadius != 0 { + v["proximity_alert_radius"] = strconv.FormatInt(opts.ProximityAlertRadius, 10) + } + v["disable_notification"] = strconv.FormatBool(opts.DisableNotification) + v["protect_content"] = strconv.FormatBool(opts.ProtectContent) + v["message_effect_id"] = opts.MessageEffectId + if opts.ReplyParameters != nil { + bs, err := json.Marshal(opts.ReplyParameters) + if err != nil { + return nil, fmt.Errorf("failed to marshal field reply_parameters: %w", err) + } + v["reply_parameters"] = string(bs) + } + if opts.ReplyMarkup != nil { + bs, err := json.Marshal(opts.ReplyMarkup) + if err != nil { + return nil, fmt.Errorf("failed to marshal field reply_markup: %w", err) + } + v["reply_markup"] = string(bs) + } + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "sendLocation", v, nil, reqOpts) if err != nil { return nil, err } @@ -3896,6 +6340,57 @@ func (bot *Bot) SendMediaGroup(chatId int64, media []InputMedia, opts *SendMedia return m, json.Unmarshal(r, &m) } +// SendMediaGroupWithContext is the same as SendMediaGroup, but with a context.Context parameter. +func (bot *Bot) SendMediaGroupWithContext(ctx context.Context, chatId int64, media []InputMedia, opts *SendMediaGroupOpts) ([]Message, error) { + v := map[string]string{} + data := map[string]FileReader{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + if media != nil { + var rawList []json.RawMessage + for idx, im := range media { + inputBs, err := im.InputParams("media"+strconv.Itoa(idx), data) + if err != nil { + return nil, fmt.Errorf("failed to marshal list item %d for field media: %w", idx, err) + } + rawList = append(rawList, inputBs) + } + bs, err := json.Marshal(rawList) + if err != nil { + return nil, fmt.Errorf("failed to marshal raw json list for field: media %w", err) + } + v["media"] = string(bs) + } + if opts != nil { + v["business_connection_id"] = opts.BusinessConnectionId + if opts.MessageThreadId != 0 { + v["message_thread_id"] = strconv.FormatInt(opts.MessageThreadId, 10) + } + v["disable_notification"] = strconv.FormatBool(opts.DisableNotification) + v["protect_content"] = strconv.FormatBool(opts.ProtectContent) + v["message_effect_id"] = opts.MessageEffectId + if opts.ReplyParameters != nil { + bs, err := json.Marshal(opts.ReplyParameters) + if err != nil { + return nil, fmt.Errorf("failed to marshal field reply_parameters: %w", err) + } + v["reply_parameters"] = string(bs) + } + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "sendMediaGroup", v, data, reqOpts) + if err != nil { + return nil, err + } + + var m []Message + return m, json.Unmarshal(r, &m) +} + // SendMessageOpts is the set of optional fields for Bot.SendMessage. type SendMessageOpts struct { // Unique identifier of the business connection on behalf of which the message will be sent @@ -3985,6 +6480,64 @@ func (bot *Bot) SendMessage(chatId int64, text string, opts *SendMessageOpts) (* return &m, json.Unmarshal(r, &m) } +// SendMessageWithContext is the same as SendMessage, but with a context.Context parameter. +func (bot *Bot) SendMessageWithContext(ctx context.Context, chatId int64, text string, opts *SendMessageOpts) (*Message, error) { + v := map[string]string{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + v["text"] = text + if opts != nil { + v["business_connection_id"] = opts.BusinessConnectionId + if opts.MessageThreadId != 0 { + v["message_thread_id"] = strconv.FormatInt(opts.MessageThreadId, 10) + } + v["parse_mode"] = opts.ParseMode + if opts.Entities != nil { + bs, err := json.Marshal(opts.Entities) + if err != nil { + return nil, fmt.Errorf("failed to marshal field entities: %w", err) + } + v["entities"] = string(bs) + } + if opts.LinkPreviewOptions != nil { + bs, err := json.Marshal(opts.LinkPreviewOptions) + if err != nil { + return nil, fmt.Errorf("failed to marshal field link_preview_options: %w", err) + } + v["link_preview_options"] = string(bs) + } + v["disable_notification"] = strconv.FormatBool(opts.DisableNotification) + v["protect_content"] = strconv.FormatBool(opts.ProtectContent) + v["message_effect_id"] = opts.MessageEffectId + if opts.ReplyParameters != nil { + bs, err := json.Marshal(opts.ReplyParameters) + if err != nil { + return nil, fmt.Errorf("failed to marshal field reply_parameters: %w", err) + } + v["reply_parameters"] = string(bs) + } + if opts.ReplyMarkup != nil { + bs, err := json.Marshal(opts.ReplyMarkup) + if err != nil { + return nil, fmt.Errorf("failed to marshal field reply_markup: %w", err) + } + v["reply_markup"] = string(bs) + } + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "sendMessage", v, nil, reqOpts) + if err != nil { + return nil, err + } + + var m Message + return &m, json.Unmarshal(r, &m) +} + // SendPaidMediaOpts is the set of optional fields for Bot.SendPaidMedia. type SendPaidMediaOpts struct { // Unique identifier of the business connection on behalf of which the message will be sent @@ -4071,7 +6624,168 @@ func (bot *Bot) SendPaidMedia(chatId int64, starCount int64, media []InputPaidMe reqOpts = opts.RequestOpts } - r, err := bot.Request("sendPaidMedia", v, data, reqOpts) + r, err := bot.Request("sendPaidMedia", v, data, reqOpts) + if err != nil { + return nil, err + } + + var m Message + return &m, json.Unmarshal(r, &m) +} + +// SendPaidMediaWithContext is the same as SendPaidMedia, but with a context.Context parameter. +func (bot *Bot) SendPaidMediaWithContext(ctx context.Context, chatId int64, starCount int64, media []InputPaidMedia, opts *SendPaidMediaOpts) (*Message, error) { + v := map[string]string{} + data := map[string]FileReader{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + v["star_count"] = strconv.FormatInt(starCount, 10) + if media != nil { + var rawList []json.RawMessage + for idx, im := range media { + inputBs, err := im.InputParams("media"+strconv.Itoa(idx), data) + if err != nil { + return nil, fmt.Errorf("failed to marshal list item %d for field media: %w", idx, err) + } + rawList = append(rawList, inputBs) + } + bs, err := json.Marshal(rawList) + if err != nil { + return nil, fmt.Errorf("failed to marshal raw json list for field: media %w", err) + } + v["media"] = string(bs) + } + if opts != nil { + v["business_connection_id"] = opts.BusinessConnectionId + v["caption"] = opts.Caption + v["parse_mode"] = opts.ParseMode + if opts.CaptionEntities != nil { + bs, err := json.Marshal(opts.CaptionEntities) + if err != nil { + return nil, fmt.Errorf("failed to marshal field caption_entities: %w", err) + } + v["caption_entities"] = string(bs) + } + v["show_caption_above_media"] = strconv.FormatBool(opts.ShowCaptionAboveMedia) + v["disable_notification"] = strconv.FormatBool(opts.DisableNotification) + v["protect_content"] = strconv.FormatBool(opts.ProtectContent) + if opts.ReplyParameters != nil { + bs, err := json.Marshal(opts.ReplyParameters) + if err != nil { + return nil, fmt.Errorf("failed to marshal field reply_parameters: %w", err) + } + v["reply_parameters"] = string(bs) + } + if opts.ReplyMarkup != nil { + bs, err := json.Marshal(opts.ReplyMarkup) + if err != nil { + return nil, fmt.Errorf("failed to marshal field reply_markup: %w", err) + } + v["reply_markup"] = string(bs) + } + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "sendPaidMedia", v, data, reqOpts) + if err != nil { + return nil, err + } + + var m Message + return &m, json.Unmarshal(r, &m) +} + +// SendPhotoOpts is the set of optional fields for Bot.SendPhoto. +type SendPhotoOpts struct { + // Unique identifier of the business connection on behalf of which the message will be sent + BusinessConnectionId string + // Unique identifier for the target message thread (topic) of the forum; for forum supergroups only + MessageThreadId int64 + // Photo caption (may also be used when resending photos by file_id), 0-1024 characters after entities parsing + Caption string + // Mode for parsing entities in the photo caption. See formatting options for more details. + ParseMode string + // A JSON-serialized list of special entities that appear in the caption, which can be specified instead of parse_mode + CaptionEntities []MessageEntity + // Pass True, if the caption must be shown above the message media + ShowCaptionAboveMedia bool + // Pass True if the photo needs to be covered with a spoiler animation + HasSpoiler bool + // Sends the message silently. Users will receive a notification with no sound. + DisableNotification bool + // Protects the contents of the sent message from forwarding and saving + ProtectContent bool + // Unique identifier of the message effect to be added to the message; for private chats only + MessageEffectId string + // Description of the message to reply to + ReplyParameters *ReplyParameters + // Additional interface options. A JSON-serialized object for an inline keyboard, custom reply keyboard, instructions to remove a reply keyboard or to force a reply from the user + ReplyMarkup ReplyMarkup + // RequestOpts are an additional optional field to configure timeouts for individual requests + RequestOpts *RequestOpts +} + +// SendPhoto (https://core.telegram.org/bots/api#sendphoto) +// +// Use this method to send photos. On success, the sent Message is returned. +// - chatId (type int64): Unique identifier for the target chat +// - photo (type InputFileOrString): Photo to send. Pass a file_id as String to send a photo that exists on the Telegram servers (recommended), pass an HTTP URL as a String for Telegram to get a photo from the Internet, or upload a new photo using multipart/form-data. The photo must be at most 10 MB in size. The photo's width and height must not exceed 10000 in total. Width and height ratio must be at most 20. More information on Sending Files: https://core.telegram.org/bots/api#sending-files +// - opts (type SendPhotoOpts): All optional parameters. +func (bot *Bot) SendPhoto(chatId int64, photo InputFileOrString, opts *SendPhotoOpts) (*Message, error) { + v := map[string]string{} + data := map[string]FileReader{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + if photo != nil { + err := photo.Attach("photo", data) + if err != nil { + return nil, fmt.Errorf("failed to attach 'photo' input file: %w", err) + } + v["photo"] = photo.getValue() + } + if opts != nil { + v["business_connection_id"] = opts.BusinessConnectionId + if opts.MessageThreadId != 0 { + v["message_thread_id"] = strconv.FormatInt(opts.MessageThreadId, 10) + } + v["caption"] = opts.Caption + v["parse_mode"] = opts.ParseMode + if opts.CaptionEntities != nil { + bs, err := json.Marshal(opts.CaptionEntities) + if err != nil { + return nil, fmt.Errorf("failed to marshal field caption_entities: %w", err) + } + v["caption_entities"] = string(bs) + } + v["show_caption_above_media"] = strconv.FormatBool(opts.ShowCaptionAboveMedia) + v["has_spoiler"] = strconv.FormatBool(opts.HasSpoiler) + v["disable_notification"] = strconv.FormatBool(opts.DisableNotification) + v["protect_content"] = strconv.FormatBool(opts.ProtectContent) + v["message_effect_id"] = opts.MessageEffectId + if opts.ReplyParameters != nil { + bs, err := json.Marshal(opts.ReplyParameters) + if err != nil { + return nil, fmt.Errorf("failed to marshal field reply_parameters: %w", err) + } + v["reply_parameters"] = string(bs) + } + if opts.ReplyMarkup != nil { + bs, err := json.Marshal(opts.ReplyMarkup) + if err != nil { + return nil, fmt.Errorf("failed to marshal field reply_markup: %w", err) + } + v["reply_markup"] = string(bs) + } + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.Request("sendPhoto", v, data, reqOpts) if err != nil { return nil, err } @@ -4080,43 +6794,8 @@ func (bot *Bot) SendPaidMedia(chatId int64, starCount int64, media []InputPaidMe return &m, json.Unmarshal(r, &m) } -// SendPhotoOpts is the set of optional fields for Bot.SendPhoto. -type SendPhotoOpts struct { - // Unique identifier of the business connection on behalf of which the message will be sent - BusinessConnectionId string - // Unique identifier for the target message thread (topic) of the forum; for forum supergroups only - MessageThreadId int64 - // Photo caption (may also be used when resending photos by file_id), 0-1024 characters after entities parsing - Caption string - // Mode for parsing entities in the photo caption. See formatting options for more details. - ParseMode string - // A JSON-serialized list of special entities that appear in the caption, which can be specified instead of parse_mode - CaptionEntities []MessageEntity - // Pass True, if the caption must be shown above the message media - ShowCaptionAboveMedia bool - // Pass True if the photo needs to be covered with a spoiler animation - HasSpoiler bool - // Sends the message silently. Users will receive a notification with no sound. - DisableNotification bool - // Protects the contents of the sent message from forwarding and saving - ProtectContent bool - // Unique identifier of the message effect to be added to the message; for private chats only - MessageEffectId string - // Description of the message to reply to - ReplyParameters *ReplyParameters - // Additional interface options. A JSON-serialized object for an inline keyboard, custom reply keyboard, instructions to remove a reply keyboard or to force a reply from the user - ReplyMarkup ReplyMarkup - // RequestOpts are an additional optional field to configure timeouts for individual requests - RequestOpts *RequestOpts -} - -// SendPhoto (https://core.telegram.org/bots/api#sendphoto) -// -// Use this method to send photos. On success, the sent Message is returned. -// - chatId (type int64): Unique identifier for the target chat -// - photo (type InputFileOrString): Photo to send. Pass a file_id as String to send a photo that exists on the Telegram servers (recommended), pass an HTTP URL as a String for Telegram to get a photo from the Internet, or upload a new photo using multipart/form-data. The photo must be at most 10 MB in size. The photo's width and height must not exceed 10000 in total. Width and height ratio must be at most 20. More information on Sending Files: https://core.telegram.org/bots/api#sending-files -// - opts (type SendPhotoOpts): All optional parameters. -func (bot *Bot) SendPhoto(chatId int64, photo InputFileOrString, opts *SendPhotoOpts) (*Message, error) { +// SendPhotoWithContext is the same as SendPhoto, but with a context.Context parameter. +func (bot *Bot) SendPhotoWithContext(ctx context.Context, chatId int64, photo InputFileOrString, opts *SendPhotoOpts) (*Message, error) { v := map[string]string{} data := map[string]FileReader{} v["chat_id"] = strconv.FormatInt(chatId, 10) @@ -4167,7 +6846,7 @@ func (bot *Bot) SendPhoto(chatId int64, photo InputFileOrString, opts *SendPhoto reqOpts = opts.RequestOpts } - r, err := bot.Request("sendPhoto", v, data, reqOpts) + r, err := bot.RequestWithContext(ctx, "sendPhoto", v, data, reqOpts) if err != nil { return nil, err } @@ -4307,6 +6986,87 @@ func (bot *Bot) SendPoll(chatId int64, question string, options []InputPollOptio return &m, json.Unmarshal(r, &m) } +// SendPollWithContext is the same as SendPoll, but with a context.Context parameter. +func (bot *Bot) SendPollWithContext(ctx context.Context, chatId int64, question string, options []InputPollOption, opts *SendPollOpts) (*Message, error) { + v := map[string]string{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + v["question"] = question + if options != nil { + bs, err := json.Marshal(options) + if err != nil { + return nil, fmt.Errorf("failed to marshal field options: %w", err) + } + v["options"] = string(bs) + } + if opts != nil { + v["business_connection_id"] = opts.BusinessConnectionId + if opts.MessageThreadId != 0 { + v["message_thread_id"] = strconv.FormatInt(opts.MessageThreadId, 10) + } + v["question_parse_mode"] = opts.QuestionParseMode + if opts.QuestionEntities != nil { + bs, err := json.Marshal(opts.QuestionEntities) + if err != nil { + return nil, fmt.Errorf("failed to marshal field question_entities: %w", err) + } + v["question_entities"] = string(bs) + } + v["is_anonymous"] = strconv.FormatBool(opts.IsAnonymous) + v["type"] = opts.Type + v["allows_multiple_answers"] = strconv.FormatBool(opts.AllowsMultipleAnswers) + if opts.Type == "quiz" { + // correct_option_id should always be set when the type is "quiz" - it doesnt need to be set for type "regular". + v["correct_option_id"] = strconv.FormatInt(opts.CorrectOptionId, 10) + } + v["explanation"] = opts.Explanation + v["explanation_parse_mode"] = opts.ExplanationParseMode + if opts.ExplanationEntities != nil { + bs, err := json.Marshal(opts.ExplanationEntities) + if err != nil { + return nil, fmt.Errorf("failed to marshal field explanation_entities: %w", err) + } + v["explanation_entities"] = string(bs) + } + if opts.OpenPeriod != 0 { + v["open_period"] = strconv.FormatInt(opts.OpenPeriod, 10) + } + if opts.CloseDate != 0 { + v["close_date"] = strconv.FormatInt(opts.CloseDate, 10) + } + v["is_closed"] = strconv.FormatBool(opts.IsClosed) + v["disable_notification"] = strconv.FormatBool(opts.DisableNotification) + v["protect_content"] = strconv.FormatBool(opts.ProtectContent) + v["message_effect_id"] = opts.MessageEffectId + if opts.ReplyParameters != nil { + bs, err := json.Marshal(opts.ReplyParameters) + if err != nil { + return nil, fmt.Errorf("failed to marshal field reply_parameters: %w", err) + } + v["reply_parameters"] = string(bs) + } + if opts.ReplyMarkup != nil { + bs, err := json.Marshal(opts.ReplyMarkup) + if err != nil { + return nil, fmt.Errorf("failed to marshal field reply_markup: %w", err) + } + v["reply_markup"] = string(bs) + } + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "sendPoll", v, nil, reqOpts) + if err != nil { + return nil, err + } + + var m Message + return &m, json.Unmarshal(r, &m) +} + // SendStickerOpts is the set of optional fields for Bot.SendSticker. type SendStickerOpts struct { // Unique identifier of the business connection on behalf of which the message will be sent @@ -4376,7 +7136,144 @@ func (bot *Bot) SendSticker(chatId int64, sticker InputFileOrString, opts *SendS reqOpts = opts.RequestOpts } - r, err := bot.Request("sendSticker", v, data, reqOpts) + r, err := bot.Request("sendSticker", v, data, reqOpts) + if err != nil { + return nil, err + } + + var m Message + return &m, json.Unmarshal(r, &m) +} + +// SendStickerWithContext is the same as SendSticker, but with a context.Context parameter. +func (bot *Bot) SendStickerWithContext(ctx context.Context, chatId int64, sticker InputFileOrString, opts *SendStickerOpts) (*Message, error) { + v := map[string]string{} + data := map[string]FileReader{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + if sticker != nil { + err := sticker.Attach("sticker", data) + if err != nil { + return nil, fmt.Errorf("failed to attach 'sticker' input file: %w", err) + } + v["sticker"] = sticker.getValue() + } + if opts != nil { + v["business_connection_id"] = opts.BusinessConnectionId + if opts.MessageThreadId != 0 { + v["message_thread_id"] = strconv.FormatInt(opts.MessageThreadId, 10) + } + v["emoji"] = opts.Emoji + v["disable_notification"] = strconv.FormatBool(opts.DisableNotification) + v["protect_content"] = strconv.FormatBool(opts.ProtectContent) + v["message_effect_id"] = opts.MessageEffectId + if opts.ReplyParameters != nil { + bs, err := json.Marshal(opts.ReplyParameters) + if err != nil { + return nil, fmt.Errorf("failed to marshal field reply_parameters: %w", err) + } + v["reply_parameters"] = string(bs) + } + if opts.ReplyMarkup != nil { + bs, err := json.Marshal(opts.ReplyMarkup) + if err != nil { + return nil, fmt.Errorf("failed to marshal field reply_markup: %w", err) + } + v["reply_markup"] = string(bs) + } + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "sendSticker", v, data, reqOpts) + if err != nil { + return nil, err + } + + var m Message + return &m, json.Unmarshal(r, &m) +} + +// SendVenueOpts is the set of optional fields for Bot.SendVenue. +type SendVenueOpts struct { + // Unique identifier of the business connection on behalf of which the message will be sent + BusinessConnectionId string + // Unique identifier for the target message thread (topic) of the forum; for forum supergroups only + MessageThreadId int64 + // Foursquare identifier of the venue + FoursquareId string + // Foursquare type of the venue, if known. (For example, "arts_entertainment/default", "arts_entertainment/aquarium" or "food/icecream".) + FoursquareType string + // Google Places identifier of the venue + GooglePlaceId string + // Google Places type of the venue. (See supported types.) + GooglePlaceType string + // Sends the message silently. Users will receive a notification with no sound. + DisableNotification bool + // Protects the contents of the sent message from forwarding and saving + ProtectContent bool + // Unique identifier of the message effect to be added to the message; for private chats only + MessageEffectId string + // Description of the message to reply to + ReplyParameters *ReplyParameters + // Additional interface options. A JSON-serialized object for an inline keyboard, custom reply keyboard, instructions to remove a reply keyboard or to force a reply from the user + ReplyMarkup ReplyMarkup + // RequestOpts are an additional optional field to configure timeouts for individual requests + RequestOpts *RequestOpts +} + +// SendVenue (https://core.telegram.org/bots/api#sendvenue) +// +// Use this method to send information about a venue. On success, the sent Message is returned. +// - chatId (type int64): Unique identifier for the target chat +// - latitude (type float64): Latitude of the venue +// - longitude (type float64): Longitude of the venue +// - title (type string): Name of the venue +// - address (type string): Address of the venue +// - opts (type SendVenueOpts): All optional parameters. +func (bot *Bot) SendVenue(chatId int64, latitude float64, longitude float64, title string, address string, opts *SendVenueOpts) (*Message, error) { + v := map[string]string{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + v["latitude"] = strconv.FormatFloat(latitude, 'f', -1, 64) + v["longitude"] = strconv.FormatFloat(longitude, 'f', -1, 64) + v["title"] = title + v["address"] = address + if opts != nil { + v["business_connection_id"] = opts.BusinessConnectionId + if opts.MessageThreadId != 0 { + v["message_thread_id"] = strconv.FormatInt(opts.MessageThreadId, 10) + } + v["foursquare_id"] = opts.FoursquareId + v["foursquare_type"] = opts.FoursquareType + v["google_place_id"] = opts.GooglePlaceId + v["google_place_type"] = opts.GooglePlaceType + v["disable_notification"] = strconv.FormatBool(opts.DisableNotification) + v["protect_content"] = strconv.FormatBool(opts.ProtectContent) + v["message_effect_id"] = opts.MessageEffectId + if opts.ReplyParameters != nil { + bs, err := json.Marshal(opts.ReplyParameters) + if err != nil { + return nil, fmt.Errorf("failed to marshal field reply_parameters: %w", err) + } + v["reply_parameters"] = string(bs) + } + if opts.ReplyMarkup != nil { + bs, err := json.Marshal(opts.ReplyMarkup) + if err != nil { + return nil, fmt.Errorf("failed to marshal field reply_markup: %w", err) + } + v["reply_markup"] = string(bs) + } + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.Request("sendVenue", v, nil, reqOpts) if err != nil { return nil, err } @@ -4385,44 +7282,8 @@ func (bot *Bot) SendSticker(chatId int64, sticker InputFileOrString, opts *SendS return &m, json.Unmarshal(r, &m) } -// SendVenueOpts is the set of optional fields for Bot.SendVenue. -type SendVenueOpts struct { - // Unique identifier of the business connection on behalf of which the message will be sent - BusinessConnectionId string - // Unique identifier for the target message thread (topic) of the forum; for forum supergroups only - MessageThreadId int64 - // Foursquare identifier of the venue - FoursquareId string - // Foursquare type of the venue, if known. (For example, "arts_entertainment/default", "arts_entertainment/aquarium" or "food/icecream".) - FoursquareType string - // Google Places identifier of the venue - GooglePlaceId string - // Google Places type of the venue. (See supported types.) - GooglePlaceType string - // Sends the message silently. Users will receive a notification with no sound. - DisableNotification bool - // Protects the contents of the sent message from forwarding and saving - ProtectContent bool - // Unique identifier of the message effect to be added to the message; for private chats only - MessageEffectId string - // Description of the message to reply to - ReplyParameters *ReplyParameters - // Additional interface options. A JSON-serialized object for an inline keyboard, custom reply keyboard, instructions to remove a reply keyboard or to force a reply from the user - ReplyMarkup ReplyMarkup - // RequestOpts are an additional optional field to configure timeouts for individual requests - RequestOpts *RequestOpts -} - -// SendVenue (https://core.telegram.org/bots/api#sendvenue) -// -// Use this method to send information about a venue. On success, the sent Message is returned. -// - chatId (type int64): Unique identifier for the target chat -// - latitude (type float64): Latitude of the venue -// - longitude (type float64): Longitude of the venue -// - title (type string): Name of the venue -// - address (type string): Address of the venue -// - opts (type SendVenueOpts): All optional parameters. -func (bot *Bot) SendVenue(chatId int64, latitude float64, longitude float64, title string, address string, opts *SendVenueOpts) (*Message, error) { +// SendVenueWithContext is the same as SendVenue, but with a context.Context parameter. +func (bot *Bot) SendVenueWithContext(ctx context.Context, chatId int64, latitude float64, longitude float64, title string, address string, opts *SendVenueOpts) (*Message, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) v["latitude"] = strconv.FormatFloat(latitude, 'f', -1, 64) @@ -4462,7 +7323,7 @@ func (bot *Bot) SendVenue(chatId int64, latitude float64, longitude float64, tit reqOpts = opts.RequestOpts } - r, err := bot.Request("sendVenue", v, nil, reqOpts) + r, err := bot.RequestWithContext(ctx, "sendVenue", v, nil, reqOpts) if err != nil { return nil, err } @@ -4594,6 +7455,84 @@ func (bot *Bot) SendVideo(chatId int64, video InputFileOrString, opts *SendVideo return &m, json.Unmarshal(r, &m) } +// SendVideoWithContext is the same as SendVideo, but with a context.Context parameter. +func (bot *Bot) SendVideoWithContext(ctx context.Context, chatId int64, video InputFileOrString, opts *SendVideoOpts) (*Message, error) { + v := map[string]string{} + data := map[string]FileReader{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + if video != nil { + err := video.Attach("video", data) + if err != nil { + return nil, fmt.Errorf("failed to attach 'video' input file: %w", err) + } + v["video"] = video.getValue() + } + if opts != nil { + v["business_connection_id"] = opts.BusinessConnectionId + if opts.MessageThreadId != 0 { + v["message_thread_id"] = strconv.FormatInt(opts.MessageThreadId, 10) + } + if opts.Duration != 0 { + v["duration"] = strconv.FormatInt(opts.Duration, 10) + } + if opts.Width != 0 { + v["width"] = strconv.FormatInt(opts.Width, 10) + } + if opts.Height != 0 { + v["height"] = strconv.FormatInt(opts.Height, 10) + } + if opts.Thumbnail != nil { + err := opts.Thumbnail.Attach("thumbnail", data) + if err != nil { + return nil, fmt.Errorf("failed to attach 'thumbnail' input file: %w", err) + } + v["thumbnail"] = opts.Thumbnail.getValue() + } + v["caption"] = opts.Caption + v["parse_mode"] = opts.ParseMode + if opts.CaptionEntities != nil { + bs, err := json.Marshal(opts.CaptionEntities) + if err != nil { + return nil, fmt.Errorf("failed to marshal field caption_entities: %w", err) + } + v["caption_entities"] = string(bs) + } + v["show_caption_above_media"] = strconv.FormatBool(opts.ShowCaptionAboveMedia) + v["has_spoiler"] = strconv.FormatBool(opts.HasSpoiler) + v["supports_streaming"] = strconv.FormatBool(opts.SupportsStreaming) + v["disable_notification"] = strconv.FormatBool(opts.DisableNotification) + v["protect_content"] = strconv.FormatBool(opts.ProtectContent) + v["message_effect_id"] = opts.MessageEffectId + if opts.ReplyParameters != nil { + bs, err := json.Marshal(opts.ReplyParameters) + if err != nil { + return nil, fmt.Errorf("failed to marshal field reply_parameters: %w", err) + } + v["reply_parameters"] = string(bs) + } + if opts.ReplyMarkup != nil { + bs, err := json.Marshal(opts.ReplyMarkup) + if err != nil { + return nil, fmt.Errorf("failed to marshal field reply_markup: %w", err) + } + v["reply_markup"] = string(bs) + } + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "sendVideo", v, data, reqOpts) + if err != nil { + return nil, err + } + + var m Message + return &m, json.Unmarshal(r, &m) +} + // SendVideoNoteOpts is the set of optional fields for Bot.SendVideoNote. type SendVideoNoteOpts struct { // Unique identifier of the business connection on behalf of which the message will be sent @@ -4688,6 +7627,69 @@ func (bot *Bot) SendVideoNote(chatId int64, videoNote InputFileOrString, opts *S return &m, json.Unmarshal(r, &m) } +// SendVideoNoteWithContext is the same as SendVideoNote, but with a context.Context parameter. +func (bot *Bot) SendVideoNoteWithContext(ctx context.Context, chatId int64, videoNote InputFileOrString, opts *SendVideoNoteOpts) (*Message, error) { + v := map[string]string{} + data := map[string]FileReader{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + if videoNote != nil { + err := videoNote.Attach("video_note", data) + if err != nil { + return nil, fmt.Errorf("failed to attach 'video_note' input file: %w", err) + } + v["video_note"] = videoNote.getValue() + } + if opts != nil { + v["business_connection_id"] = opts.BusinessConnectionId + if opts.MessageThreadId != 0 { + v["message_thread_id"] = strconv.FormatInt(opts.MessageThreadId, 10) + } + if opts.Duration != 0 { + v["duration"] = strconv.FormatInt(opts.Duration, 10) + } + if opts.Length != 0 { + v["length"] = strconv.FormatInt(opts.Length, 10) + } + if opts.Thumbnail != nil { + err := opts.Thumbnail.Attach("thumbnail", data) + if err != nil { + return nil, fmt.Errorf("failed to attach 'thumbnail' input file: %w", err) + } + v["thumbnail"] = opts.Thumbnail.getValue() + } + v["disable_notification"] = strconv.FormatBool(opts.DisableNotification) + v["protect_content"] = strconv.FormatBool(opts.ProtectContent) + v["message_effect_id"] = opts.MessageEffectId + if opts.ReplyParameters != nil { + bs, err := json.Marshal(opts.ReplyParameters) + if err != nil { + return nil, fmt.Errorf("failed to marshal field reply_parameters: %w", err) + } + v["reply_parameters"] = string(bs) + } + if opts.ReplyMarkup != nil { + bs, err := json.Marshal(opts.ReplyMarkup) + if err != nil { + return nil, fmt.Errorf("failed to marshal field reply_markup: %w", err) + } + v["reply_markup"] = string(bs) + } + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "sendVideoNote", v, data, reqOpts) + if err != nil { + return nil, err + } + + var m Message + return &m, json.Unmarshal(r, &m) +} + // SendVoiceOpts is the set of optional fields for Bot.SendVoice. type SendVoiceOpts struct { // Unique identifier of the business connection on behalf of which the message will be sent @@ -4783,6 +7785,68 @@ func (bot *Bot) SendVoice(chatId int64, voice InputFileOrString, opts *SendVoice return &m, json.Unmarshal(r, &m) } +// SendVoiceWithContext is the same as SendVoice, but with a context.Context parameter. +func (bot *Bot) SendVoiceWithContext(ctx context.Context, chatId int64, voice InputFileOrString, opts *SendVoiceOpts) (*Message, error) { + v := map[string]string{} + data := map[string]FileReader{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + if voice != nil { + err := voice.Attach("voice", data) + if err != nil { + return nil, fmt.Errorf("failed to attach 'voice' input file: %w", err) + } + v["voice"] = voice.getValue() + } + if opts != nil { + v["business_connection_id"] = opts.BusinessConnectionId + if opts.MessageThreadId != 0 { + v["message_thread_id"] = strconv.FormatInt(opts.MessageThreadId, 10) + } + v["caption"] = opts.Caption + v["parse_mode"] = opts.ParseMode + if opts.CaptionEntities != nil { + bs, err := json.Marshal(opts.CaptionEntities) + if err != nil { + return nil, fmt.Errorf("failed to marshal field caption_entities: %w", err) + } + v["caption_entities"] = string(bs) + } + if opts.Duration != 0 { + v["duration"] = strconv.FormatInt(opts.Duration, 10) + } + v["disable_notification"] = strconv.FormatBool(opts.DisableNotification) + v["protect_content"] = strconv.FormatBool(opts.ProtectContent) + v["message_effect_id"] = opts.MessageEffectId + if opts.ReplyParameters != nil { + bs, err := json.Marshal(opts.ReplyParameters) + if err != nil { + return nil, fmt.Errorf("failed to marshal field reply_parameters: %w", err) + } + v["reply_parameters"] = string(bs) + } + if opts.ReplyMarkup != nil { + bs, err := json.Marshal(opts.ReplyMarkup) + if err != nil { + return nil, fmt.Errorf("failed to marshal field reply_markup: %w", err) + } + v["reply_markup"] = string(bs) + } + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "sendVoice", v, data, reqOpts) + if err != nil { + return nil, err + } + + var m Message + return &m, json.Unmarshal(r, &m) +} + // SetChatAdministratorCustomTitleOpts is the set of optional fields for Bot.SetChatAdministratorCustomTitle. type SetChatAdministratorCustomTitleOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests @@ -4807,7 +7871,28 @@ func (bot *Bot) SetChatAdministratorCustomTitle(chatId int64, userId int64, cust reqOpts = opts.RequestOpts } - r, err := bot.Request("setChatAdministratorCustomTitle", v, nil, reqOpts) + r, err := bot.Request("setChatAdministratorCustomTitle", v, nil, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + +// SetChatAdministratorCustomTitleWithContext is the same as SetChatAdministratorCustomTitle, but with a context.Context parameter. +func (bot *Bot) SetChatAdministratorCustomTitleWithContext(ctx context.Context, chatId int64, userId int64, customTitle string, opts *SetChatAdministratorCustomTitleOpts) (bool, error) { + v := map[string]string{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + v["user_id"] = strconv.FormatInt(userId, 10) + v["custom_title"] = customTitle + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "setChatAdministratorCustomTitle", v, nil, reqOpts) if err != nil { return false, err } @@ -4850,6 +7935,28 @@ func (bot *Bot) SetChatDescription(chatId int64, opts *SetChatDescriptionOpts) ( return b, json.Unmarshal(r, &b) } +// SetChatDescriptionWithContext is the same as SetChatDescription, but with a context.Context parameter. +func (bot *Bot) SetChatDescriptionWithContext(ctx context.Context, chatId int64, opts *SetChatDescriptionOpts) (bool, error) { + v := map[string]string{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + if opts != nil { + v["description"] = opts.Description + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "setChatDescription", v, nil, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + // SetChatMenuButtonOpts is the set of optional fields for Bot.SetChatMenuButton. type SetChatMenuButtonOpts struct { // Unique identifier for the target private chat. If not specified, default bot's menu button will be changed @@ -4891,6 +7998,34 @@ func (bot *Bot) SetChatMenuButton(opts *SetChatMenuButtonOpts) (bool, error) { return b, json.Unmarshal(r, &b) } +// SetChatMenuButtonWithContext is the same as SetChatMenuButton, but with a context.Context parameter. +func (bot *Bot) SetChatMenuButtonWithContext(ctx context.Context, opts *SetChatMenuButtonOpts) (bool, error) { + v := map[string]string{} + if opts != nil { + if opts.ChatId != nil { + v["chat_id"] = strconv.FormatInt(*opts.ChatId, 10) + } + bs, err := json.Marshal(opts.MenuButton) + if err != nil { + return false, fmt.Errorf("failed to marshal field menu_button: %w", err) + } + v["menu_button"] = string(bs) + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "setChatMenuButton", v, nil, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + // SetChatPermissionsOpts is the set of optional fields for Bot.SetChatPermissions. type SetChatPermissionsOpts struct { // Pass True if chat permissions are set independently. Otherwise, the can_send_other_messages and can_add_web_page_previews permissions will imply the can_send_messages, can_send_audios, can_send_documents, can_send_photos, can_send_videos, can_send_video_notes, and can_send_voice_notes permissions; the can_send_polls permission will imply the can_send_messages permission. @@ -4931,6 +8066,33 @@ func (bot *Bot) SetChatPermissions(chatId int64, permissions ChatPermissions, op return b, json.Unmarshal(r, &b) } +// SetChatPermissionsWithContext is the same as SetChatPermissions, but with a context.Context parameter. +func (bot *Bot) SetChatPermissionsWithContext(ctx context.Context, chatId int64, permissions ChatPermissions, opts *SetChatPermissionsOpts) (bool, error) { + v := map[string]string{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + bs, err := json.Marshal(permissions) + if err != nil { + return false, fmt.Errorf("failed to marshal field permissions: %w", err) + } + v["permissions"] = string(bs) + if opts != nil { + v["use_independent_chat_permissions"] = strconv.FormatBool(opts.UseIndependentChatPermissions) + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "setChatPermissions", v, nil, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + // SetChatPhotoOpts is the set of optional fields for Bot.SetChatPhoto. type SetChatPhotoOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests @@ -4969,6 +8131,33 @@ func (bot *Bot) SetChatPhoto(chatId int64, photo InputFile, opts *SetChatPhotoOp return b, json.Unmarshal(r, &b) } +// SetChatPhotoWithContext is the same as SetChatPhoto, but with a context.Context parameter. +func (bot *Bot) SetChatPhotoWithContext(ctx context.Context, chatId int64, photo InputFile, opts *SetChatPhotoOpts) (bool, error) { + v := map[string]string{} + data := map[string]FileReader{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + if photo != nil { + err := photo.Attach("photo", data) + if err != nil { + return false, fmt.Errorf("failed to attach 'photo' input file: %w", err) + } + v["photo"] = photo.getValue() + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "setChatPhoto", v, data, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + // SetChatStickerSetOpts is the set of optional fields for Bot.SetChatStickerSet. type SetChatStickerSetOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests @@ -5000,6 +8189,26 @@ func (bot *Bot) SetChatStickerSet(chatId int64, stickerSetName string, opts *Set return b, json.Unmarshal(r, &b) } +// SetChatStickerSetWithContext is the same as SetChatStickerSet, but with a context.Context parameter. +func (bot *Bot) SetChatStickerSetWithContext(ctx context.Context, chatId int64, stickerSetName string, opts *SetChatStickerSetOpts) (bool, error) { + v := map[string]string{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + v["sticker_set_name"] = stickerSetName + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "setChatStickerSet", v, nil, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + // SetChatTitleOpts is the set of optional fields for Bot.SetChatTitle. type SetChatTitleOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests @@ -5031,6 +8240,26 @@ func (bot *Bot) SetChatTitle(chatId int64, title string, opts *SetChatTitleOpts) return b, json.Unmarshal(r, &b) } +// SetChatTitleWithContext is the same as SetChatTitle, but with a context.Context parameter. +func (bot *Bot) SetChatTitleWithContext(ctx context.Context, chatId int64, title string, opts *SetChatTitleOpts) (bool, error) { + v := map[string]string{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + v["title"] = title + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "setChatTitle", v, nil, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + // SetCustomEmojiStickerSetThumbnailOpts is the set of optional fields for Bot.SetCustomEmojiStickerSetThumbnail. type SetCustomEmojiStickerSetThumbnailOpts struct { // Custom emoji identifier of a sticker from the sticker set; pass an empty string to drop the thumbnail and use the first sticker as the thumbnail. @@ -5065,6 +8294,28 @@ func (bot *Bot) SetCustomEmojiStickerSetThumbnail(name string, opts *SetCustomEm return b, json.Unmarshal(r, &b) } +// SetCustomEmojiStickerSetThumbnailWithContext is the same as SetCustomEmojiStickerSetThumbnail, but with a context.Context parameter. +func (bot *Bot) SetCustomEmojiStickerSetThumbnailWithContext(ctx context.Context, name string, opts *SetCustomEmojiStickerSetThumbnailOpts) (bool, error) { + v := map[string]string{} + v["name"] = name + if opts != nil { + v["custom_emoji_id"] = opts.CustomEmojiId + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "setCustomEmojiStickerSetThumbnail", v, nil, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + // SetGameScoreOpts is the set of optional fields for Bot.SetGameScore. type SetGameScoreOpts struct { // Pass True if the high score is allowed to decrease. This can be useful when fixing mistakes or banning cheaters @@ -5125,6 +8376,45 @@ func (bot *Bot) SetGameScore(userId int64, score int64, opts *SetGameScoreOpts) } +// SetGameScoreWithContext is the same as SetGameScore, but with a context.Context parameter. +func (bot *Bot) SetGameScoreWithContext(ctx context.Context, userId int64, score int64, opts *SetGameScoreOpts) (*Message, bool, error) { + v := map[string]string{} + v["user_id"] = strconv.FormatInt(userId, 10) + v["score"] = strconv.FormatInt(score, 10) + if opts != nil { + v["force"] = strconv.FormatBool(opts.Force) + v["disable_edit_message"] = strconv.FormatBool(opts.DisableEditMessage) + if opts.ChatId != 0 { + v["chat_id"] = strconv.FormatInt(opts.ChatId, 10) + } + if opts.MessageId != 0 { + v["message_id"] = strconv.FormatInt(opts.MessageId, 10) + } + v["inline_message_id"] = opts.InlineMessageId + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "setGameScore", v, nil, reqOpts) + if err != nil { + return nil, false, err + } + + var m Message + if err := json.Unmarshal(r, &m); err != nil { + var b bool + if err := json.Unmarshal(r, &b); err != nil { + return nil, false, err + } + return nil, b, nil + } + return &m, true, nil + +} + // SetMessageReactionOpts is the set of optional fields for Bot.SetMessageReaction. type SetMessageReactionOpts struct { // A JSON-serialized list of reaction types to set on the message. Currently, as non-premium users, bots can set up to one reaction per message. A custom emoji reaction can be used if it is either already present on the message or explicitly allowed by chat administrators. Paid reactions can't be used by bots. @@ -5161,7 +8451,84 @@ func (bot *Bot) SetMessageReaction(chatId int64, messageId int64, opts *SetMessa reqOpts = opts.RequestOpts } - r, err := bot.Request("setMessageReaction", v, nil, reqOpts) + r, err := bot.Request("setMessageReaction", v, nil, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + +// SetMessageReactionWithContext is the same as SetMessageReaction, but with a context.Context parameter. +func (bot *Bot) SetMessageReactionWithContext(ctx context.Context, chatId int64, messageId int64, opts *SetMessageReactionOpts) (bool, error) { + v := map[string]string{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + v["message_id"] = strconv.FormatInt(messageId, 10) + if opts != nil { + if opts.Reaction != nil { + bs, err := json.Marshal(opts.Reaction) + if err != nil { + return false, fmt.Errorf("failed to marshal field reaction: %w", err) + } + v["reaction"] = string(bs) + } + v["is_big"] = strconv.FormatBool(opts.IsBig) + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "setMessageReaction", v, nil, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + +// SetMyCommandsOpts is the set of optional fields for Bot.SetMyCommands. +type SetMyCommandsOpts struct { + // A JSON-serialized object, describing scope of users for which the commands are relevant. Defaults to BotCommandScopeDefault. + Scope BotCommandScope + // A two-letter ISO 639-1 language code. If empty, commands will be applied to all users from the given scope, for whose language there are no dedicated commands + LanguageCode string + // RequestOpts are an additional optional field to configure timeouts for individual requests + RequestOpts *RequestOpts +} + +// SetMyCommands (https://core.telegram.org/bots/api#setmycommands) +// +// Use this method to change the list of the bot's commands. See this manual for more details about bot commands. Returns True on success. +// - commands (type []BotCommand): A JSON-serialized list of bot commands to be set as the list of the bot's commands. At most 100 commands can be specified. +// - opts (type SetMyCommandsOpts): All optional parameters. +func (bot *Bot) SetMyCommands(commands []BotCommand, opts *SetMyCommandsOpts) (bool, error) { + v := map[string]string{} + if commands != nil { + bs, err := json.Marshal(commands) + if err != nil { + return false, fmt.Errorf("failed to marshal field commands: %w", err) + } + v["commands"] = string(bs) + } + if opts != nil { + bs, err := json.Marshal(opts.Scope) + if err != nil { + return false, fmt.Errorf("failed to marshal field scope: %w", err) + } + v["scope"] = string(bs) + v["language_code"] = opts.LanguageCode + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.Request("setMyCommands", v, nil, reqOpts) if err != nil { return false, err } @@ -5170,22 +8537,8 @@ func (bot *Bot) SetMessageReaction(chatId int64, messageId int64, opts *SetMessa return b, json.Unmarshal(r, &b) } -// SetMyCommandsOpts is the set of optional fields for Bot.SetMyCommands. -type SetMyCommandsOpts struct { - // A JSON-serialized object, describing scope of users for which the commands are relevant. Defaults to BotCommandScopeDefault. - Scope BotCommandScope - // A two-letter ISO 639-1 language code. If empty, commands will be applied to all users from the given scope, for whose language there are no dedicated commands - LanguageCode string - // RequestOpts are an additional optional field to configure timeouts for individual requests - RequestOpts *RequestOpts -} - -// SetMyCommands (https://core.telegram.org/bots/api#setmycommands) -// -// Use this method to change the list of the bot's commands. See this manual for more details about bot commands. Returns True on success. -// - commands (type []BotCommand): A JSON-serialized list of bot commands to be set as the list of the bot's commands. At most 100 commands can be specified. -// - opts (type SetMyCommandsOpts): All optional parameters. -func (bot *Bot) SetMyCommands(commands []BotCommand, opts *SetMyCommandsOpts) (bool, error) { +// SetMyCommandsWithContext is the same as SetMyCommands, but with a context.Context parameter. +func (bot *Bot) SetMyCommandsWithContext(ctx context.Context, commands []BotCommand, opts *SetMyCommandsOpts) (bool, error) { v := map[string]string{} if commands != nil { bs, err := json.Marshal(commands) @@ -5208,7 +8561,7 @@ func (bot *Bot) SetMyCommands(commands []BotCommand, opts *SetMyCommandsOpts) (b reqOpts = opts.RequestOpts } - r, err := bot.Request("setMyCommands", v, nil, reqOpts) + r, err := bot.RequestWithContext(ctx, "setMyCommands", v, nil, reqOpts) if err != nil { return false, err } @@ -5258,6 +8611,34 @@ func (bot *Bot) SetMyDefaultAdministratorRights(opts *SetMyDefaultAdministratorR return b, json.Unmarshal(r, &b) } +// SetMyDefaultAdministratorRightsWithContext is the same as SetMyDefaultAdministratorRights, but with a context.Context parameter. +func (bot *Bot) SetMyDefaultAdministratorRightsWithContext(ctx context.Context, opts *SetMyDefaultAdministratorRightsOpts) (bool, error) { + v := map[string]string{} + if opts != nil { + if opts.Rights != nil { + bs, err := json.Marshal(opts.Rights) + if err != nil { + return false, fmt.Errorf("failed to marshal field rights: %w", err) + } + v["rights"] = string(bs) + } + v["for_channels"] = strconv.FormatBool(opts.ForChannels) + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "setMyDefaultAdministratorRights", v, nil, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + // SetMyDescriptionOpts is the set of optional fields for Bot.SetMyDescription. type SetMyDescriptionOpts struct { // New bot description; 0-512 characters. Pass an empty string to remove the dedicated description for the given language. @@ -5293,6 +8674,28 @@ func (bot *Bot) SetMyDescription(opts *SetMyDescriptionOpts) (bool, error) { return b, json.Unmarshal(r, &b) } +// SetMyDescriptionWithContext is the same as SetMyDescription, but with a context.Context parameter. +func (bot *Bot) SetMyDescriptionWithContext(ctx context.Context, opts *SetMyDescriptionOpts) (bool, error) { + v := map[string]string{} + if opts != nil { + v["description"] = opts.Description + v["language_code"] = opts.LanguageCode + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "setMyDescription", v, nil, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + // SetMyNameOpts is the set of optional fields for Bot.SetMyName. type SetMyNameOpts struct { // New bot name; 0-64 characters. Pass an empty string to remove the dedicated name for the given language. @@ -5328,6 +8731,28 @@ func (bot *Bot) SetMyName(opts *SetMyNameOpts) (bool, error) { return b, json.Unmarshal(r, &b) } +// SetMyNameWithContext is the same as SetMyName, but with a context.Context parameter. +func (bot *Bot) SetMyNameWithContext(ctx context.Context, opts *SetMyNameOpts) (bool, error) { + v := map[string]string{} + if opts != nil { + v["name"] = opts.Name + v["language_code"] = opts.LanguageCode + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "setMyName", v, nil, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + // SetMyShortDescriptionOpts is the set of optional fields for Bot.SetMyShortDescription. type SetMyShortDescriptionOpts struct { // New short description for the bot; 0-120 characters. Pass an empty string to remove the dedicated short description for the given language. @@ -5363,6 +8788,28 @@ func (bot *Bot) SetMyShortDescription(opts *SetMyShortDescriptionOpts) (bool, er return b, json.Unmarshal(r, &b) } +// SetMyShortDescriptionWithContext is the same as SetMyShortDescription, but with a context.Context parameter. +func (bot *Bot) SetMyShortDescriptionWithContext(ctx context.Context, opts *SetMyShortDescriptionOpts) (bool, error) { + v := map[string]string{} + if opts != nil { + v["short_description"] = opts.ShortDescription + v["language_code"] = opts.LanguageCode + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "setMyShortDescription", v, nil, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + // SetPassportDataErrorsOpts is the set of optional fields for Bot.SetPassportDataErrors. type SetPassportDataErrorsOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests @@ -5401,6 +8848,32 @@ func (bot *Bot) SetPassportDataErrors(userId int64, errors []PassportElementErro return b, json.Unmarshal(r, &b) } +// SetPassportDataErrorsWithContext is the same as SetPassportDataErrors, but with a context.Context parameter. +func (bot *Bot) SetPassportDataErrorsWithContext(ctx context.Context, userId int64, errors []PassportElementError, opts *SetPassportDataErrorsOpts) (bool, error) { + v := map[string]string{} + v["user_id"] = strconv.FormatInt(userId, 10) + if errors != nil { + bs, err := json.Marshal(errors) + if err != nil { + return false, fmt.Errorf("failed to marshal field errors: %w", err) + } + v["errors"] = string(bs) + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "setPassportDataErrors", v, nil, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + // SetStickerEmojiListOpts is the set of optional fields for Bot.SetStickerEmojiList. type SetStickerEmojiListOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests @@ -5438,6 +8911,32 @@ func (bot *Bot) SetStickerEmojiList(sticker string, emojiList []string, opts *Se return b, json.Unmarshal(r, &b) } +// SetStickerEmojiListWithContext is the same as SetStickerEmojiList, but with a context.Context parameter. +func (bot *Bot) SetStickerEmojiListWithContext(ctx context.Context, sticker string, emojiList []string, opts *SetStickerEmojiListOpts) (bool, error) { + v := map[string]string{} + v["sticker"] = sticker + if emojiList != nil { + bs, err := json.Marshal(emojiList) + if err != nil { + return false, fmt.Errorf("failed to marshal field emoji_list: %w", err) + } + v["emoji_list"] = string(bs) + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "setStickerEmojiList", v, nil, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + // SetStickerKeywordsOpts is the set of optional fields for Bot.SetStickerKeywords. type SetStickerKeywordsOpts struct { // A JSON-serialized list of 0-20 search keywords for the sticker with total length of up to 64 characters @@ -5478,6 +8977,34 @@ func (bot *Bot) SetStickerKeywords(sticker string, opts *SetStickerKeywordsOpts) return b, json.Unmarshal(r, &b) } +// SetStickerKeywordsWithContext is the same as SetStickerKeywords, but with a context.Context parameter. +func (bot *Bot) SetStickerKeywordsWithContext(ctx context.Context, sticker string, opts *SetStickerKeywordsOpts) (bool, error) { + v := map[string]string{} + v["sticker"] = sticker + if opts != nil { + if opts.Keywords != nil { + bs, err := json.Marshal(opts.Keywords) + if err != nil { + return false, fmt.Errorf("failed to marshal field keywords: %w", err) + } + v["keywords"] = string(bs) + } + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "setStickerKeywords", v, nil, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + // SetStickerMaskPositionOpts is the set of optional fields for Bot.SetStickerMaskPosition. type SetStickerMaskPositionOpts struct { // A JSON-serialized object with the position where the mask should be placed on faces. Omit the parameter to remove the mask position. @@ -5518,6 +9045,34 @@ func (bot *Bot) SetStickerMaskPosition(sticker string, opts *SetStickerMaskPosit return b, json.Unmarshal(r, &b) } +// SetStickerMaskPositionWithContext is the same as SetStickerMaskPosition, but with a context.Context parameter. +func (bot *Bot) SetStickerMaskPositionWithContext(ctx context.Context, sticker string, opts *SetStickerMaskPositionOpts) (bool, error) { + v := map[string]string{} + v["sticker"] = sticker + if opts != nil { + if opts.MaskPosition != nil { + bs, err := json.Marshal(opts.MaskPosition) + if err != nil { + return false, fmt.Errorf("failed to marshal field mask_position: %w", err) + } + v["mask_position"] = string(bs) + } + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "setStickerMaskPosition", v, nil, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + // SetStickerPositionInSetOpts is the set of optional fields for Bot.SetStickerPositionInSet. type SetStickerPositionInSetOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests @@ -5549,6 +9104,26 @@ func (bot *Bot) SetStickerPositionInSet(sticker string, position int64, opts *Se return b, json.Unmarshal(r, &b) } +// SetStickerPositionInSetWithContext is the same as SetStickerPositionInSet, but with a context.Context parameter. +func (bot *Bot) SetStickerPositionInSetWithContext(ctx context.Context, sticker string, position int64, opts *SetStickerPositionInSetOpts) (bool, error) { + v := map[string]string{} + v["sticker"] = sticker + v["position"] = strconv.FormatInt(position, 10) + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "setStickerPositionInSet", v, nil, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + // SetStickerSetThumbnailOpts is the set of optional fields for Bot.SetStickerSetThumbnail. type SetStickerSetThumbnailOpts struct { // A .WEBP or .PNG image with the thumbnail, must be up to 128 kilobytes in size and have a width and height of exactly 100px, or a .TGS animation with a thumbnail up to 32 kilobytes in size (see https://core.telegram.org/stickers#animation-requirements for animated sticker technical requirements), or a WEBM video with the thumbnail up to 32 kilobytes in size; see https://core.telegram.org/stickers#video-requirements for video sticker technical requirements. Pass a file_id as a String to send a file that already exists on the Telegram servers, pass an HTTP URL as a String for Telegram to get a file from the Internet, or upload a new one using multipart/form-data. More information on Sending Files: https://core.telegram.org/bots/api#sending-files. Animated and video sticker set thumbnails can't be uploaded via HTTP URL. If omitted, then the thumbnail is dropped and the first sticker is used as the thumbnail. @@ -5594,6 +9169,37 @@ func (bot *Bot) SetStickerSetThumbnail(name string, userId int64, format string, return b, json.Unmarshal(r, &b) } +// SetStickerSetThumbnailWithContext is the same as SetStickerSetThumbnail, but with a context.Context parameter. +func (bot *Bot) SetStickerSetThumbnailWithContext(ctx context.Context, name string, userId int64, format string, opts *SetStickerSetThumbnailOpts) (bool, error) { + v := map[string]string{} + data := map[string]FileReader{} + v["name"] = name + v["user_id"] = strconv.FormatInt(userId, 10) + v["format"] = format + if opts != nil { + if opts.Thumbnail != nil { + err := opts.Thumbnail.Attach("thumbnail", data) + if err != nil { + return false, fmt.Errorf("failed to attach 'thumbnail' input file: %w", err) + } + v["thumbnail"] = opts.Thumbnail.getValue() + } + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "setStickerSetThumbnail", v, data, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + // SetStickerSetTitleOpts is the set of optional fields for Bot.SetStickerSetTitle. type SetStickerSetTitleOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests @@ -5616,7 +9222,27 @@ func (bot *Bot) SetStickerSetTitle(name string, title string, opts *SetStickerSe reqOpts = opts.RequestOpts } - r, err := bot.Request("setStickerSetTitle", v, nil, reqOpts) + r, err := bot.Request("setStickerSetTitle", v, nil, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + +// SetStickerSetTitleWithContext is the same as SetStickerSetTitle, but with a context.Context parameter. +func (bot *Bot) SetStickerSetTitleWithContext(ctx context.Context, name string, title string, opts *SetStickerSetTitleOpts) (bool, error) { + v := map[string]string{} + v["name"] = name + v["title"] = title + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "setStickerSetTitle", v, nil, reqOpts) if err != nil { return false, err } @@ -5690,6 +9316,48 @@ func (bot *Bot) SetWebhook(url string, opts *SetWebhookOpts) (bool, error) { return b, json.Unmarshal(r, &b) } +// SetWebhookWithContext is the same as SetWebhook, but with a context.Context parameter. +func (bot *Bot) SetWebhookWithContext(ctx context.Context, url string, opts *SetWebhookOpts) (bool, error) { + v := map[string]string{} + data := map[string]FileReader{} + v["url"] = url + if opts != nil { + if opts.Certificate != nil { + err := opts.Certificate.Attach("certificate", data) + if err != nil { + return false, fmt.Errorf("failed to attach 'certificate' input file: %w", err) + } + v["certificate"] = opts.Certificate.getValue() + } + v["ip_address"] = opts.IpAddress + if opts.MaxConnections != 0 { + v["max_connections"] = strconv.FormatInt(opts.MaxConnections, 10) + } + if opts.AllowedUpdates != nil { + bs, err := json.Marshal(opts.AllowedUpdates) + if err != nil { + return false, fmt.Errorf("failed to marshal field allowed_updates: %w", err) + } + v["allowed_updates"] = string(bs) + } + v["drop_pending_updates"] = strconv.FormatBool(opts.DropPendingUpdates) + v["secret_token"] = opts.SecretToken + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "setWebhook", v, data, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + // 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 @@ -5750,6 +9418,47 @@ func (bot *Bot) StopMessageLiveLocation(opts *StopMessageLiveLocationOpts) (*Mes } +// StopMessageLiveLocationWithContext is the same as StopMessageLiveLocation, but with a context.Context parameter. +func (bot *Bot) StopMessageLiveLocationWithContext(ctx context.Context, 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) + } + if opts.MessageId != 0 { + v["message_id"] = strconv.FormatInt(opts.MessageId, 10) + } + v["inline_message_id"] = opts.InlineMessageId + bs, err := json.Marshal(opts.ReplyMarkup) + if err != nil { + return nil, false, fmt.Errorf("failed to marshal field reply_markup: %w", err) + } + v["reply_markup"] = string(bs) + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "stopMessageLiveLocation", v, nil, reqOpts) + if err != nil { + return nil, false, err + } + + var m Message + if err := json.Unmarshal(r, &m); err != nil { + var b bool + if err := json.Unmarshal(r, &b); err != nil { + return nil, false, err + } + return nil, b, nil + } + return &m, true, nil + +} + // 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 @@ -5793,6 +9502,34 @@ func (bot *Bot) StopPoll(chatId int64, messageId int64, opts *StopPollOpts) (*Po return &p, json.Unmarshal(r, &p) } +// StopPollWithContext is the same as StopPoll, but with a context.Context parameter. +func (bot *Bot) StopPollWithContext(ctx context.Context, chatId int64, messageId int64, opts *StopPollOpts) (*Poll, error) { + v := map[string]string{} + 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) + } + v["reply_markup"] = string(bs) + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "stopPoll", v, nil, reqOpts) + if err != nil { + return nil, err + } + + var p Poll + return &p, json.Unmarshal(r, &p) +} + // UnbanChatMemberOpts is the set of optional fields for Bot.UnbanChatMember. type UnbanChatMemberOpts struct { // Do nothing if the user is not banned @@ -5829,6 +9566,29 @@ func (bot *Bot) UnbanChatMember(chatId int64, userId int64, opts *UnbanChatMembe return b, json.Unmarshal(r, &b) } +// UnbanChatMemberWithContext is the same as UnbanChatMember, but with a context.Context parameter. +func (bot *Bot) UnbanChatMemberWithContext(ctx context.Context, chatId int64, userId int64, opts *UnbanChatMemberOpts) (bool, error) { + v := map[string]string{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + v["user_id"] = strconv.FormatInt(userId, 10) + if opts != nil { + v["only_if_banned"] = strconv.FormatBool(opts.OnlyIfBanned) + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "unbanChatMember", v, nil, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + // UnbanChatSenderChatOpts is the set of optional fields for Bot.UnbanChatSenderChat. type UnbanChatSenderChatOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests @@ -5860,6 +9620,26 @@ func (bot *Bot) UnbanChatSenderChat(chatId int64, senderChatId int64, opts *Unba return b, json.Unmarshal(r, &b) } +// UnbanChatSenderChatWithContext is the same as UnbanChatSenderChat, but with a context.Context parameter. +func (bot *Bot) UnbanChatSenderChatWithContext(ctx context.Context, chatId int64, senderChatId int64, opts *UnbanChatSenderChatOpts) (bool, error) { + v := map[string]string{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + v["sender_chat_id"] = strconv.FormatInt(senderChatId, 10) + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "unbanChatSenderChat", v, nil, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + // UnhideGeneralForumTopicOpts is the set of optional fields for Bot.UnhideGeneralForumTopic. type UnhideGeneralForumTopicOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests @@ -5889,6 +9669,25 @@ func (bot *Bot) UnhideGeneralForumTopic(chatId int64, opts *UnhideGeneralForumTo return b, json.Unmarshal(r, &b) } +// UnhideGeneralForumTopicWithContext is the same as UnhideGeneralForumTopic, but with a context.Context parameter. +func (bot *Bot) UnhideGeneralForumTopicWithContext(ctx context.Context, chatId int64, opts *UnhideGeneralForumTopicOpts) (bool, error) { + v := map[string]string{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "unhideGeneralForumTopic", v, nil, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + // UnpinAllChatMessagesOpts is the set of optional fields for Bot.UnpinAllChatMessages. type UnpinAllChatMessagesOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests @@ -5918,6 +9717,25 @@ func (bot *Bot) UnpinAllChatMessages(chatId int64, opts *UnpinAllChatMessagesOpt return b, json.Unmarshal(r, &b) } +// UnpinAllChatMessagesWithContext is the same as UnpinAllChatMessages, but with a context.Context parameter. +func (bot *Bot) UnpinAllChatMessagesWithContext(ctx context.Context, chatId int64, opts *UnpinAllChatMessagesOpts) (bool, error) { + v := map[string]string{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "unpinAllChatMessages", v, nil, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + // UnpinAllForumTopicMessagesOpts is the set of optional fields for Bot.UnpinAllForumTopicMessages. type UnpinAllForumTopicMessagesOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests @@ -5949,6 +9767,26 @@ func (bot *Bot) UnpinAllForumTopicMessages(chatId int64, messageThreadId int64, return b, json.Unmarshal(r, &b) } +// UnpinAllForumTopicMessagesWithContext is the same as UnpinAllForumTopicMessages, but with a context.Context parameter. +func (bot *Bot) UnpinAllForumTopicMessagesWithContext(ctx context.Context, chatId int64, messageThreadId int64, opts *UnpinAllForumTopicMessagesOpts) (bool, error) { + v := map[string]string{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + v["message_thread_id"] = strconv.FormatInt(messageThreadId, 10) + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "unpinAllForumTopicMessages", v, nil, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + // UnpinAllGeneralForumTopicMessagesOpts is the set of optional fields for Bot.UnpinAllGeneralForumTopicMessages. type UnpinAllGeneralForumTopicMessagesOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests @@ -5978,6 +9816,25 @@ func (bot *Bot) UnpinAllGeneralForumTopicMessages(chatId int64, opts *UnpinAllGe return b, json.Unmarshal(r, &b) } +// UnpinAllGeneralForumTopicMessagesWithContext is the same as UnpinAllGeneralForumTopicMessages, but with a context.Context parameter. +func (bot *Bot) UnpinAllGeneralForumTopicMessagesWithContext(ctx context.Context, chatId int64, opts *UnpinAllGeneralForumTopicMessagesOpts) (bool, error) { + v := map[string]string{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "unpinAllGeneralForumTopicMessages", v, nil, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + // UnpinChatMessageOpts is the set of optional fields for Bot.UnpinChatMessage. type UnpinChatMessageOpts struct { // Unique identifier of the business connection on behalf of which the message will be unpinned @@ -6017,6 +9874,31 @@ func (bot *Bot) UnpinChatMessage(chatId int64, opts *UnpinChatMessageOpts) (bool return b, json.Unmarshal(r, &b) } +// UnpinChatMessageWithContext is the same as UnpinChatMessage, but with a context.Context parameter. +func (bot *Bot) UnpinChatMessageWithContext(ctx context.Context, chatId int64, opts *UnpinChatMessageOpts) (bool, error) { + v := map[string]string{} + v["chat_id"] = strconv.FormatInt(chatId, 10) + if opts != nil { + v["business_connection_id"] = opts.BusinessConnectionId + if opts.MessageId != nil { + v["message_id"] = strconv.FormatInt(*opts.MessageId, 10) + } + } + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "unpinChatMessage", v, nil, reqOpts) + if err != nil { + return false, err + } + + var b bool + return b, json.Unmarshal(r, &b) +} + // UploadStickerFileOpts is the set of optional fields for Bot.UploadStickerFile. type UploadStickerFileOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests @@ -6056,3 +9938,31 @@ func (bot *Bot) UploadStickerFile(userId int64, sticker InputFile, stickerFormat var f File return &f, json.Unmarshal(r, &f) } + +// UploadStickerFileWithContext is the same as UploadStickerFile, but with a context.Context parameter. +func (bot *Bot) UploadStickerFileWithContext(ctx context.Context, userId int64, sticker InputFile, stickerFormat string, opts *UploadStickerFileOpts) (*File, error) { + v := map[string]string{} + data := map[string]FileReader{} + v["user_id"] = strconv.FormatInt(userId, 10) + if sticker != nil { + err := sticker.Attach("sticker", data) + if err != nil { + return nil, fmt.Errorf("failed to attach 'sticker' input file: %w", err) + } + v["sticker"] = sticker.getValue() + } + v["sticker_format"] = stickerFormat + + var reqOpts *RequestOpts + if opts != nil { + reqOpts = opts.RequestOpts + } + + r, err := bot.RequestWithContext(ctx, "uploadStickerFile", v, data, reqOpts) + if err != nil { + return nil, err + } + + var f File + return &f, json.Unmarshal(r, &f) +} diff --git a/scripts/generate/methods.go b/scripts/generate/methods.go index 01aadee..f49c4cc 100644 --- a/scripts/generate/methods.go +++ b/scripts/generate/methods.go @@ -24,6 +24,7 @@ import ( "encoding/json" "fmt" "strconv" + "context" ) `) @@ -44,7 +45,7 @@ import ( func generateMethodDef(d APIDescription, tgMethod MethodDescription) (string, error) { method := strings.Builder{} - methodSignature, retTypes, optionalsStruct, err := generateMethodSignature(d, tgMethod) + args, retTypes, optionalsStruct, err := generateMethodSignature(d, tgMethod) if err != nil { return "", err } @@ -74,7 +75,20 @@ func generateMethodDef(d APIDescription, tgMethod MethodDescription) (string, er return "", fmt.Errorf("failed to generate return values: %w", err) } + methodSignature := strings.Title(tgMethod.Name) + "(" + strings.Join(args, ", ") + ") (" + strings.Join(retTypes, ", ") + ", error)" method.WriteString(desc) + method.WriteString(genMethodSig(methodSignature, valueGen, hasData, false, tgMethod, defaultRetVals, returnGen)) + + methodSignatureWithCtx := strings.Title(tgMethod.Name) + "WithContext(ctx context.Context, " + strings.Join(args, ", ") + ") (" + strings.Join(retTypes, ", ") + ", error)" + method.WriteString(fmt.Sprintf("\n// %sWithContext is the same as %s, but with a context.Context parameter.", strings.Title(tgMethod.Name), strings.Title(tgMethod.Name))) + method.WriteString(genMethodSig(methodSignatureWithCtx, valueGen, hasData, true, tgMethod, defaultRetVals, returnGen)) + + return method.String(), nil +} + +func genMethodSig(methodSignature string, valueGen string, hasData bool, withContext bool, tgMethod MethodDescription, defaultRetVals string, returnGen string) string { + method := strings.Builder{} + method.WriteString("\nfunc (bot *Bot) " + methodSignature + " {") method.WriteString("\n v := map[string]string{}") method.WriteString(valueGen) @@ -88,9 +102,17 @@ func generateMethodDef(d APIDescription, tgMethod MethodDescription) (string, er // If sending data, we need to do it over POST if hasData { - method.WriteString("\nr, err := bot.Request(\"" + tgMethod.Name + "\", v, data, reqOpts)") + if withContext { + method.WriteString("\nr, err := bot.RequestWithContext(ctx, \"" + tgMethod.Name + "\", v, data, reqOpts)") + } else { + method.WriteString("\nr, err := bot.Request(\"" + tgMethod.Name + "\", v, data, reqOpts)") + } } else { - method.WriteString("\nr, err := bot.Request(\"" + tgMethod.Name + "\", v, nil, reqOpts)") + if withContext { + method.WriteString("\nr, err := bot.RequestWithContext(ctx, \"" + tgMethod.Name + "\", v, nil, reqOpts)") + } else { + method.WriteString("\nr, err := bot.Request(\"" + tgMethod.Name + "\", v, nil, reqOpts)") + } } method.WriteString("\n if err != nil {") @@ -101,22 +123,21 @@ func generateMethodDef(d APIDescription, tgMethod MethodDescription) (string, er method.WriteString(returnGen) method.WriteString("\n}") - return method.String(), nil + return method.String() } -func generateMethodSignature(d APIDescription, tgMethod MethodDescription) (string, []string, string, error) { +func generateMethodSignature(d APIDescription, tgMethod MethodDescription) ([]string, []string, string, error) { retTypes, err := tgMethod.GetReturnTypes(d) if err != nil { - return "", nil, "", fmt.Errorf("failed to get return for %s: %w", tgMethod.Name, err) + return nil, nil, "", fmt.Errorf("failed to get return for %s: %w", tgMethod.Name, err) } args, optionalsStruct, err := tgMethod.getArgs(d) if err != nil { - return "", nil, "", fmt.Errorf("failed to get args for method %s: %w", tgMethod.Name, err) + return nil, nil, "", fmt.Errorf("failed to get args for method %s: %w", tgMethod.Name, err) } - methodSignature := strings.Title(tgMethod.Name) + "(" + args + ") (" + strings.Join(retTypes, ", ") + ", error)" - return methodSignature, retTypes, optionalsStruct, nil + return args, retTypes, optionalsStruct, nil } func returnValues(d APIDescription, retTypes []string) (string, error) { @@ -358,14 +379,14 @@ func getRetVarName(retType string) string { return strings.ToLower(retType[:1]) } -func (m MethodDescription) getArgs(d APIDescription) (string, string, error) { +func (m MethodDescription) getArgs(d APIDescription) ([]string, string, error) { var requiredArgs []string optionals := strings.Builder{} for _, f := range m.Fields { fieldType, err := f.getPreferredType(d) if err != nil { - return "", "", fmt.Errorf("failed to get preferred type: %w", err) + return nil, "", fmt.Errorf("failed to get preferred type: %w", err) } if f.Required { @@ -388,7 +409,7 @@ func (m MethodDescription) getArgs(d APIDescription) (string, string, error) { requiredArgs = append(requiredArgs, fmt.Sprintf("opts *%s", optionalsName)) - return strings.Join(requiredArgs, ", "), optionalsStructBuilder.String(), nil + return requiredArgs, optionalsStructBuilder.String(), nil } type readerBranchesData struct { From 4d32b7a640b3ddf80caa8926530465ff4bb2674f Mon Sep 17 00:00:00 2001 From: Paul Larsen Date: Fri, 23 Aug 2024 11:48:50 +0100 Subject: [PATCH 2/3] Cleanup and remove duplicate code for proper context handling --- bot.go | 16 +- gen_methods.go | 4231 ++++------------------------------- request.go | 25 +- scripts/generate/methods.go | 121 +- 4 files changed, 570 insertions(+), 3823 deletions(-) diff --git a/bot.go b/bot.go index 7d00af4..340847f 100644 --- a/bot.go +++ b/bot.go @@ -92,20 +92,16 @@ func (bot *Bot) UseMiddleware(mw func(client BotClient) BotClient) *Bot { var ErrNilBotClient = errors.New("nil BotClient") func (bot *Bot) Request(method string, params map[string]string, data map[string]FileReader, opts *RequestOpts) (json.RawMessage, error) { - if bot.BotClient == nil { - return nil, ErrNilBotClient - } - - ctx, cancel := bot.BotClient.TimeoutContext(opts) - defer cancel() - - return bot.BotClient.RequestWithContext(ctx, bot.Token, method, params, data, opts) + return bot.RequestWithContext(context.Background(), method, params, data, opts) } -func (bot *Bot) RequestWithContext(ctx context.Context, method string, params map[string]string, data map[string]FileReader, opts *RequestOpts) (json.RawMessage, error) { +func (bot *Bot) RequestWithContext(parentCtx context.Context, method string, params map[string]string, data map[string]FileReader, opts *RequestOpts) (json.RawMessage, error) { if bot.BotClient == nil { return nil, ErrNilBotClient } - return bot.BotClient.RequestWithContext(ctx, bot.Token, method, params, data, opts) + childCtx, cancel := bot.BotClient.TimeoutContext(parentCtx, opts) + defer cancel() + + return bot.BotClient.RequestWithContext(childCtx, bot.Token, method, params, data, opts) } diff --git a/gen_methods.go b/gen_methods.go index 74c1add..638ad9f 100755 --- a/gen_methods.go +++ b/gen_methods.go @@ -10,7 +10,7 @@ import ( "strconv" ) -// AddStickerToSetOpts is the set of optional fields for Bot.AddStickerToSet. +// AddStickerToSetOpts is the set of optional fields for Bot.AddStickerToSet and Bot.AddStickerToSetWithContext. type AddStickerToSetOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests RequestOpts *RequestOpts @@ -24,31 +24,10 @@ type AddStickerToSetOpts struct { // - sticker (type InputSticker): A JSON-serialized object with information about the added sticker. If exactly the same sticker had already been added to the set, then the set isn't changed. // - opts (type AddStickerToSetOpts): All optional parameters. func (bot *Bot) AddStickerToSet(userId int64, name string, sticker InputSticker, opts *AddStickerToSetOpts) (bool, error) { - v := map[string]string{} - data := map[string]FileReader{} - v["user_id"] = strconv.FormatInt(userId, 10) - v["name"] = name - inputBs, err := sticker.InputParams("sticker", data) - if err != nil { - return false, fmt.Errorf("failed to marshal field sticker: %w", err) - } - v["sticker"] = string(inputBs) - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("addStickerToSet", v, data, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) + return bot.AddStickerToSetWithContext(context.Background(), userId, name, sticker, opts) } -// AddStickerToSetWithContext is the same as AddStickerToSet, but with a context.Context parameter. +// AddStickerToSetWithContext is the same as Bot.AddStickerToSet, but with a context.Context parameter func (bot *Bot) AddStickerToSetWithContext(ctx context.Context, userId int64, name string, sticker InputSticker, opts *AddStickerToSetOpts) (bool, error) { v := map[string]string{} data := map[string]FileReader{} @@ -74,7 +53,7 @@ func (bot *Bot) AddStickerToSetWithContext(ctx context.Context, userId int64, na return b, json.Unmarshal(r, &b) } -// AnswerCallbackQueryOpts is the set of optional fields for Bot.AnswerCallbackQuery. +// AnswerCallbackQueryOpts is the set of optional fields for Bot.AnswerCallbackQuery and Bot.AnswerCallbackQueryWithContext. type AnswerCallbackQueryOpts struct { // Text of the notification. If not specified, nothing will be shown to the user, 0-200 characters Text string @@ -94,32 +73,10 @@ type AnswerCallbackQueryOpts struct { // - callbackQueryId (type string): Unique identifier for the query to be answered // - opts (type AnswerCallbackQueryOpts): All optional parameters. func (bot *Bot) AnswerCallbackQuery(callbackQueryId string, opts *AnswerCallbackQueryOpts) (bool, error) { - v := map[string]string{} - v["callback_query_id"] = callbackQueryId - if opts != nil { - v["text"] = opts.Text - v["show_alert"] = strconv.FormatBool(opts.ShowAlert) - v["url"] = opts.Url - if opts.CacheTime != 0 { - v["cache_time"] = strconv.FormatInt(opts.CacheTime, 10) - } - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("answerCallbackQuery", v, nil, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) + return bot.AnswerCallbackQueryWithContext(context.Background(), callbackQueryId, opts) } -// AnswerCallbackQueryWithContext is the same as AnswerCallbackQuery, but with a context.Context parameter. +// AnswerCallbackQueryWithContext is the same as Bot.AnswerCallbackQuery, but with a context.Context parameter func (bot *Bot) AnswerCallbackQueryWithContext(ctx context.Context, callbackQueryId string, opts *AnswerCallbackQueryOpts) (bool, error) { v := map[string]string{} v["callback_query_id"] = callbackQueryId @@ -146,7 +103,7 @@ func (bot *Bot) AnswerCallbackQueryWithContext(ctx context.Context, callbackQuer return b, json.Unmarshal(r, &b) } -// AnswerInlineQueryOpts is the set of optional fields for Bot.AnswerInlineQuery. +// AnswerInlineQueryOpts is the set of optional fields for Bot.AnswerInlineQuery and Bot.AnswerInlineQueryWithContext. type AnswerInlineQueryOpts struct { // The maximum amount of time in seconds that the result of the inline query may be cached on the server. Defaults to 300. CacheTime int64 @@ -168,45 +125,10 @@ type AnswerInlineQueryOpts struct { // - results (type []InlineQueryResult): A JSON-serialized array of results for the inline query // - opts (type AnswerInlineQueryOpts): All optional parameters. func (bot *Bot) AnswerInlineQuery(inlineQueryId string, results []InlineQueryResult, opts *AnswerInlineQueryOpts) (bool, error) { - v := map[string]string{} - v["inline_query_id"] = inlineQueryId - if results != nil { - bs, err := json.Marshal(results) - if err != nil { - return false, fmt.Errorf("failed to marshal field results: %w", err) - } - v["results"] = string(bs) - } - if opts != nil { - if opts.CacheTime != 0 { - v["cache_time"] = strconv.FormatInt(opts.CacheTime, 10) - } - v["is_personal"] = strconv.FormatBool(opts.IsPersonal) - v["next_offset"] = opts.NextOffset - if opts.Button != nil { - bs, err := json.Marshal(opts.Button) - if err != nil { - return false, fmt.Errorf("failed to marshal field button: %w", err) - } - v["button"] = string(bs) - } - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("answerInlineQuery", v, nil, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) + return bot.AnswerInlineQueryWithContext(context.Background(), inlineQueryId, results, opts) } -// AnswerInlineQueryWithContext is the same as AnswerInlineQuery, but with a context.Context parameter. +// AnswerInlineQueryWithContext is the same as Bot.AnswerInlineQuery, but with a context.Context parameter func (bot *Bot) AnswerInlineQueryWithContext(ctx context.Context, inlineQueryId string, results []InlineQueryResult, opts *AnswerInlineQueryOpts) (bool, error) { v := map[string]string{} v["inline_query_id"] = inlineQueryId @@ -246,7 +168,7 @@ func (bot *Bot) AnswerInlineQueryWithContext(ctx context.Context, inlineQueryId return b, json.Unmarshal(r, &b) } -// AnswerPreCheckoutQueryOpts is the set of optional fields for Bot.AnswerPreCheckoutQuery. +// AnswerPreCheckoutQueryOpts is the set of optional fields for Bot.AnswerPreCheckoutQuery and Bot.AnswerPreCheckoutQueryWithContext. type AnswerPreCheckoutQueryOpts struct { // Required if ok is False. Error message in human readable form that explains the reason for failure to proceed with the checkout (e.g. "Sorry, somebody just bought the last of our amazing black T-shirts while you were busy filling out your payment details. Please choose a different color or garment!"). Telegram will display this message to the user. ErrorMessage string @@ -261,28 +183,10 @@ type AnswerPreCheckoutQueryOpts struct { // - ok (type bool): Specify True if everything is alright (goods are available, etc.) and the bot is ready to proceed with the order. Use False if there are any problems. // - opts (type AnswerPreCheckoutQueryOpts): All optional parameters. func (bot *Bot) AnswerPreCheckoutQuery(preCheckoutQueryId string, ok bool, opts *AnswerPreCheckoutQueryOpts) (bool, error) { - v := map[string]string{} - v["pre_checkout_query_id"] = preCheckoutQueryId - v["ok"] = strconv.FormatBool(ok) - if opts != nil { - v["error_message"] = opts.ErrorMessage - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("answerPreCheckoutQuery", v, nil, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) + return bot.AnswerPreCheckoutQueryWithContext(context.Background(), preCheckoutQueryId, ok, opts) } -// AnswerPreCheckoutQueryWithContext is the same as AnswerPreCheckoutQuery, but with a context.Context parameter. +// AnswerPreCheckoutQueryWithContext is the same as Bot.AnswerPreCheckoutQuery, but with a context.Context parameter func (bot *Bot) AnswerPreCheckoutQueryWithContext(ctx context.Context, preCheckoutQueryId string, ok bool, opts *AnswerPreCheckoutQueryOpts) (bool, error) { v := map[string]string{} v["pre_checkout_query_id"] = preCheckoutQueryId @@ -305,7 +209,7 @@ func (bot *Bot) AnswerPreCheckoutQueryWithContext(ctx context.Context, preChecko return b, json.Unmarshal(r, &b) } -// AnswerShippingQueryOpts is the set of optional fields for Bot.AnswerShippingQuery. +// AnswerShippingQueryOpts is the set of optional fields for Bot.AnswerShippingQuery and Bot.AnswerShippingQueryWithContext. type AnswerShippingQueryOpts struct { // Required if ok is True. A JSON-serialized array of available shipping options. ShippingOptions []ShippingOption @@ -322,35 +226,10 @@ type AnswerShippingQueryOpts struct { // - ok (type bool): Pass True if delivery to the specified address is possible and False if there are any problems (for example, if delivery to the specified address is not possible) // - opts (type AnswerShippingQueryOpts): All optional parameters. func (bot *Bot) AnswerShippingQuery(shippingQueryId string, ok bool, opts *AnswerShippingQueryOpts) (bool, error) { - v := map[string]string{} - v["shipping_query_id"] = shippingQueryId - v["ok"] = strconv.FormatBool(ok) - if opts != nil { - if opts.ShippingOptions != nil { - bs, err := json.Marshal(opts.ShippingOptions) - if err != nil { - return false, fmt.Errorf("failed to marshal field shipping_options: %w", err) - } - v["shipping_options"] = string(bs) - } - v["error_message"] = opts.ErrorMessage - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("answerShippingQuery", v, nil, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) + return bot.AnswerShippingQueryWithContext(context.Background(), shippingQueryId, ok, opts) } -// AnswerShippingQueryWithContext is the same as AnswerShippingQuery, but with a context.Context parameter. +// AnswerShippingQueryWithContext is the same as Bot.AnswerShippingQuery, but with a context.Context parameter func (bot *Bot) AnswerShippingQueryWithContext(ctx context.Context, shippingQueryId string, ok bool, opts *AnswerShippingQueryOpts) (bool, error) { v := map[string]string{} v["shipping_query_id"] = shippingQueryId @@ -380,7 +259,7 @@ func (bot *Bot) AnswerShippingQueryWithContext(ctx context.Context, shippingQuer return b, json.Unmarshal(r, &b) } -// AnswerWebAppQueryOpts is the set of optional fields for Bot.AnswerWebAppQuery. +// AnswerWebAppQueryOpts is the set of optional fields for Bot.AnswerWebAppQuery and Bot.AnswerWebAppQueryWithContext. type AnswerWebAppQueryOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests RequestOpts *RequestOpts @@ -393,29 +272,10 @@ type AnswerWebAppQueryOpts struct { // - result (type InlineQueryResult): A JSON-serialized object describing the message to be sent // - opts (type AnswerWebAppQueryOpts): All optional parameters. func (bot *Bot) AnswerWebAppQuery(webAppQueryId string, result InlineQueryResult, opts *AnswerWebAppQueryOpts) (*SentWebAppMessage, error) { - v := map[string]string{} - v["web_app_query_id"] = webAppQueryId - bs, err := json.Marshal(result) - if err != nil { - return nil, fmt.Errorf("failed to marshal field result: %w", err) - } - v["result"] = string(bs) - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("answerWebAppQuery", v, nil, reqOpts) - if err != nil { - return nil, err - } - - var s SentWebAppMessage - return &s, json.Unmarshal(r, &s) + return bot.AnswerWebAppQueryWithContext(context.Background(), webAppQueryId, result, opts) } -// AnswerWebAppQueryWithContext is the same as AnswerWebAppQuery, but with a context.Context parameter. +// AnswerWebAppQueryWithContext is the same as Bot.AnswerWebAppQuery, but with a context.Context parameter func (bot *Bot) AnswerWebAppQueryWithContext(ctx context.Context, webAppQueryId string, result InlineQueryResult, opts *AnswerWebAppQueryOpts) (*SentWebAppMessage, error) { v := map[string]string{} v["web_app_query_id"] = webAppQueryId @@ -439,7 +299,7 @@ func (bot *Bot) AnswerWebAppQueryWithContext(ctx context.Context, webAppQueryId return &s, json.Unmarshal(r, &s) } -// ApproveChatJoinRequestOpts is the set of optional fields for Bot.ApproveChatJoinRequest. +// ApproveChatJoinRequestOpts is the set of optional fields for Bot.ApproveChatJoinRequest and Bot.ApproveChatJoinRequestWithContext. type ApproveChatJoinRequestOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests RequestOpts *RequestOpts @@ -452,25 +312,10 @@ type ApproveChatJoinRequestOpts struct { // - userId (type int64): Unique identifier of the target user // - opts (type ApproveChatJoinRequestOpts): All optional parameters. func (bot *Bot) ApproveChatJoinRequest(chatId int64, userId int64, opts *ApproveChatJoinRequestOpts) (bool, error) { - v := map[string]string{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - v["user_id"] = strconv.FormatInt(userId, 10) - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("approveChatJoinRequest", v, nil, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) + return bot.ApproveChatJoinRequestWithContext(context.Background(), chatId, userId, opts) } -// ApproveChatJoinRequestWithContext is the same as ApproveChatJoinRequest, but with a context.Context parameter. +// ApproveChatJoinRequestWithContext is the same as Bot.ApproveChatJoinRequest, but with a context.Context parameter func (bot *Bot) ApproveChatJoinRequestWithContext(ctx context.Context, chatId int64, userId int64, opts *ApproveChatJoinRequestOpts) (bool, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) @@ -490,7 +335,7 @@ func (bot *Bot) ApproveChatJoinRequestWithContext(ctx context.Context, chatId in return b, json.Unmarshal(r, &b) } -// BanChatMemberOpts is the set of optional fields for Bot.BanChatMember. +// BanChatMemberOpts is the set of optional fields for Bot.BanChatMember and Bot.BanChatMemberWithContext. type BanChatMemberOpts struct { // Date when the user will be unbanned; Unix time. If user is banned for more than 366 days or less than 30 seconds from the current time they are considered to be banned forever. Applied for supergroups and channels only. UntilDate int64 @@ -507,31 +352,10 @@ type BanChatMemberOpts struct { // - userId (type int64): Unique identifier of the target user // - opts (type BanChatMemberOpts): All optional parameters. func (bot *Bot) BanChatMember(chatId int64, userId int64, opts *BanChatMemberOpts) (bool, error) { - v := map[string]string{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - v["user_id"] = strconv.FormatInt(userId, 10) - if opts != nil { - if opts.UntilDate != 0 { - v["until_date"] = strconv.FormatInt(opts.UntilDate, 10) - } - v["revoke_messages"] = strconv.FormatBool(opts.RevokeMessages) - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("banChatMember", v, nil, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) + return bot.BanChatMemberWithContext(context.Background(), chatId, userId, opts) } -// BanChatMemberWithContext is the same as BanChatMember, but with a context.Context parameter. +// BanChatMemberWithContext is the same as Bot.BanChatMember, but with a context.Context parameter func (bot *Bot) BanChatMemberWithContext(ctx context.Context, chatId int64, userId int64, opts *BanChatMemberOpts) (bool, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) @@ -557,7 +381,7 @@ func (bot *Bot) BanChatMemberWithContext(ctx context.Context, chatId int64, user return b, json.Unmarshal(r, &b) } -// BanChatSenderChatOpts is the set of optional fields for Bot.BanChatSenderChat. +// BanChatSenderChatOpts is the set of optional fields for Bot.BanChatSenderChat and Bot.BanChatSenderChatWithContext. type BanChatSenderChatOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests RequestOpts *RequestOpts @@ -570,25 +394,10 @@ type BanChatSenderChatOpts struct { // - senderChatId (type int64): Unique identifier of the target sender chat // - opts (type BanChatSenderChatOpts): All optional parameters. func (bot *Bot) BanChatSenderChat(chatId int64, senderChatId int64, opts *BanChatSenderChatOpts) (bool, error) { - v := map[string]string{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - v["sender_chat_id"] = strconv.FormatInt(senderChatId, 10) - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("banChatSenderChat", v, nil, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) + return bot.BanChatSenderChatWithContext(context.Background(), chatId, senderChatId, opts) } -// BanChatSenderChatWithContext is the same as BanChatSenderChat, but with a context.Context parameter. +// BanChatSenderChatWithContext is the same as Bot.BanChatSenderChat, but with a context.Context parameter func (bot *Bot) BanChatSenderChatWithContext(ctx context.Context, chatId int64, senderChatId int64, opts *BanChatSenderChatOpts) (bool, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) @@ -608,7 +417,7 @@ func (bot *Bot) BanChatSenderChatWithContext(ctx context.Context, chatId int64, return b, json.Unmarshal(r, &b) } -// CloseOpts is the set of optional fields for Bot.Close. +// CloseOpts is the set of optional fields for Bot.Close and Bot.CloseWithContext. type CloseOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests RequestOpts *RequestOpts @@ -619,23 +428,10 @@ type CloseOpts struct { // Use this method to close the bot instance before moving it from one local server to another. You need to delete the webhook before calling this method to ensure that the bot isn't launched again after server restart. The method will return error 429 in the first 10 minutes after the bot is launched. Returns True on success. Requires no parameters. // - opts (type CloseOpts): All optional parameters. func (bot *Bot) Close(opts *CloseOpts) (bool, error) { - v := map[string]string{} - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("close", v, nil, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) + return bot.CloseWithContext(context.Background(), opts) } -// CloseWithContext is the same as Close, but with a context.Context parameter. +// CloseWithContext is the same as Bot.Close, but with a context.Context parameter func (bot *Bot) CloseWithContext(ctx context.Context, opts *CloseOpts) (bool, error) { v := map[string]string{} @@ -653,7 +449,7 @@ func (bot *Bot) CloseWithContext(ctx context.Context, opts *CloseOpts) (bool, er return b, json.Unmarshal(r, &b) } -// CloseForumTopicOpts is the set of optional fields for Bot.CloseForumTopic. +// CloseForumTopicOpts is the set of optional fields for Bot.CloseForumTopic and Bot.CloseForumTopicWithContext. type CloseForumTopicOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests RequestOpts *RequestOpts @@ -666,25 +462,10 @@ type CloseForumTopicOpts struct { // - messageThreadId (type int64): Unique identifier for the target message thread of the forum topic // - opts (type CloseForumTopicOpts): All optional parameters. func (bot *Bot) CloseForumTopic(chatId int64, messageThreadId int64, opts *CloseForumTopicOpts) (bool, error) { - v := map[string]string{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - v["message_thread_id"] = strconv.FormatInt(messageThreadId, 10) - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("closeForumTopic", v, nil, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) + return bot.CloseForumTopicWithContext(context.Background(), chatId, messageThreadId, opts) } -// CloseForumTopicWithContext is the same as CloseForumTopic, but with a context.Context parameter. +// CloseForumTopicWithContext is the same as Bot.CloseForumTopic, but with a context.Context parameter func (bot *Bot) CloseForumTopicWithContext(ctx context.Context, chatId int64, messageThreadId int64, opts *CloseForumTopicOpts) (bool, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) @@ -704,7 +485,7 @@ func (bot *Bot) CloseForumTopicWithContext(ctx context.Context, chatId int64, me return b, json.Unmarshal(r, &b) } -// CloseGeneralForumTopicOpts is the set of optional fields for Bot.CloseGeneralForumTopic. +// CloseGeneralForumTopicOpts is the set of optional fields for Bot.CloseGeneralForumTopic and Bot.CloseGeneralForumTopicWithContext. type CloseGeneralForumTopicOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests RequestOpts *RequestOpts @@ -716,24 +497,10 @@ type CloseGeneralForumTopicOpts struct { // - chatId (type int64): Unique identifier for the target chat // - opts (type CloseGeneralForumTopicOpts): All optional parameters. func (bot *Bot) CloseGeneralForumTopic(chatId int64, opts *CloseGeneralForumTopicOpts) (bool, error) { - v := map[string]string{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("closeGeneralForumTopic", v, nil, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) + return bot.CloseGeneralForumTopicWithContext(context.Background(), chatId, opts) } -// CloseGeneralForumTopicWithContext is the same as CloseGeneralForumTopic, but with a context.Context parameter. +// CloseGeneralForumTopicWithContext is the same as Bot.CloseGeneralForumTopic, but with a context.Context parameter func (bot *Bot) CloseGeneralForumTopicWithContext(ctx context.Context, chatId int64, opts *CloseGeneralForumTopicOpts) (bool, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) @@ -752,7 +519,7 @@ func (bot *Bot) CloseGeneralForumTopicWithContext(ctx context.Context, chatId in return b, json.Unmarshal(r, &b) } -// CopyMessageOpts is the set of optional fields for Bot.CopyMessage. +// CopyMessageOpts is the set of optional fields for Bot.CopyMessage and Bot.CopyMessageWithContext. type CopyMessageOpts struct { // Unique identifier for the target message thread (topic) of the forum; for forum supergroups only MessageThreadId int64 @@ -784,6 +551,11 @@ type CopyMessageOpts struct { // - messageId (type int64): Message identifier in the chat specified in from_chat_id // - opts (type CopyMessageOpts): All optional parameters. func (bot *Bot) CopyMessage(chatId int64, fromChatId int64, messageId int64, opts *CopyMessageOpts) (*MessageId, error) { + return bot.CopyMessageWithContext(context.Background(), chatId, fromChatId, messageId, opts) +} + +// CopyMessageWithContext is the same as Bot.CopyMessage, but with a context.Context parameter +func (bot *Bot) CopyMessageWithContext(ctx context.Context, chatId int64, fromChatId int64, messageId int64, opts *CopyMessageOpts) (*MessageId, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) v["from_chat_id"] = strconv.FormatInt(fromChatId, 10) @@ -827,7 +599,7 @@ func (bot *Bot) CopyMessage(chatId int64, fromChatId int64, messageId int64, opt reqOpts = opts.RequestOpts } - r, err := bot.Request("copyMessage", v, nil, reqOpts) + r, err := bot.RequestWithContext(ctx, "copyMessage", v, nil, reqOpts) if err != nil { return nil, err } @@ -836,61 +608,7 @@ func (bot *Bot) CopyMessage(chatId int64, fromChatId int64, messageId int64, opt return &m, json.Unmarshal(r, &m) } -// CopyMessageWithContext is the same as CopyMessage, but with a context.Context parameter. -func (bot *Bot) CopyMessageWithContext(ctx context.Context, chatId int64, fromChatId int64, messageId int64, opts *CopyMessageOpts) (*MessageId, error) { - v := map[string]string{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - v["from_chat_id"] = strconv.FormatInt(fromChatId, 10) - v["message_id"] = strconv.FormatInt(messageId, 10) - if opts != nil { - if opts.MessageThreadId != 0 { - v["message_thread_id"] = strconv.FormatInt(opts.MessageThreadId, 10) - } - if opts.Caption != nil { - v["caption"] = *opts.Caption - } - v["parse_mode"] = opts.ParseMode - if opts.CaptionEntities != nil { - bs, err := json.Marshal(opts.CaptionEntities) - if err != nil { - return nil, fmt.Errorf("failed to marshal field caption_entities: %w", err) - } - v["caption_entities"] = string(bs) - } - v["show_caption_above_media"] = strconv.FormatBool(opts.ShowCaptionAboveMedia) - v["disable_notification"] = strconv.FormatBool(opts.DisableNotification) - v["protect_content"] = strconv.FormatBool(opts.ProtectContent) - if opts.ReplyParameters != nil { - bs, err := json.Marshal(opts.ReplyParameters) - if err != nil { - return nil, fmt.Errorf("failed to marshal field reply_parameters: %w", err) - } - v["reply_parameters"] = string(bs) - } - if opts.ReplyMarkup != nil { - bs, err := json.Marshal(opts.ReplyMarkup) - if err != nil { - return nil, fmt.Errorf("failed to marshal field reply_markup: %w", err) - } - v["reply_markup"] = string(bs) - } - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.RequestWithContext(ctx, "copyMessage", v, nil, reqOpts) - if err != nil { - return nil, err - } - - var m MessageId - return &m, json.Unmarshal(r, &m) -} - -// CopyMessagesOpts is the set of optional fields for Bot.CopyMessages. +// CopyMessagesOpts is the set of optional fields for Bot.CopyMessages and Bot.CopyMessagesWithContext. type CopyMessagesOpts struct { // Unique identifier for the target message thread (topic) of the forum; for forum supergroups only MessageThreadId int64 @@ -912,40 +630,10 @@ type CopyMessagesOpts struct { // - messageIds (type []int64): A JSON-serialized list of 1-100 identifiers of messages in the chat from_chat_id to copy. The identifiers must be specified in a strictly increasing order. // - opts (type CopyMessagesOpts): All optional parameters. func (bot *Bot) CopyMessages(chatId int64, fromChatId int64, messageIds []int64, opts *CopyMessagesOpts) ([]MessageId, error) { - v := map[string]string{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - v["from_chat_id"] = strconv.FormatInt(fromChatId, 10) - if messageIds != nil { - bs, err := json.Marshal(messageIds) - if err != nil { - return nil, fmt.Errorf("failed to marshal field message_ids: %w", err) - } - v["message_ids"] = string(bs) - } - if opts != nil { - if opts.MessageThreadId != 0 { - v["message_thread_id"] = strconv.FormatInt(opts.MessageThreadId, 10) - } - v["disable_notification"] = strconv.FormatBool(opts.DisableNotification) - v["protect_content"] = strconv.FormatBool(opts.ProtectContent) - v["remove_caption"] = strconv.FormatBool(opts.RemoveCaption) - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("copyMessages", v, nil, reqOpts) - if err != nil { - return nil, err - } - - var m []MessageId - return m, json.Unmarshal(r, &m) + return bot.CopyMessagesWithContext(context.Background(), chatId, fromChatId, messageIds, opts) } -// CopyMessagesWithContext is the same as CopyMessages, but with a context.Context parameter. +// CopyMessagesWithContext is the same as Bot.CopyMessages, but with a context.Context parameter func (bot *Bot) CopyMessagesWithContext(ctx context.Context, chatId int64, fromChatId int64, messageIds []int64, opts *CopyMessagesOpts) ([]MessageId, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) @@ -980,7 +668,7 @@ func (bot *Bot) CopyMessagesWithContext(ctx context.Context, chatId int64, fromC return m, json.Unmarshal(r, &m) } -// CreateChatInviteLinkOpts is the set of optional fields for Bot.CreateChatInviteLink. +// CreateChatInviteLinkOpts is the set of optional fields for Bot.CreateChatInviteLink and Bot.CreateChatInviteLinkWithContext. type CreateChatInviteLinkOpts struct { // Invite link name; 0-32 characters Name string @@ -1000,34 +688,10 @@ type CreateChatInviteLinkOpts struct { // - chatId (type int64): Unique identifier for the target chat // - opts (type CreateChatInviteLinkOpts): All optional parameters. func (bot *Bot) CreateChatInviteLink(chatId int64, opts *CreateChatInviteLinkOpts) (*ChatInviteLink, error) { - v := map[string]string{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - if opts != nil { - v["name"] = opts.Name - if opts.ExpireDate != 0 { - v["expire_date"] = strconv.FormatInt(opts.ExpireDate, 10) - } - if opts.MemberLimit != 0 { - v["member_limit"] = strconv.FormatInt(opts.MemberLimit, 10) - } - v["creates_join_request"] = strconv.FormatBool(opts.CreatesJoinRequest) - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("createChatInviteLink", v, nil, reqOpts) - if err != nil { - return nil, err - } - - var c ChatInviteLink - return &c, json.Unmarshal(r, &c) + return bot.CreateChatInviteLinkWithContext(context.Background(), chatId, opts) } -// CreateChatInviteLinkWithContext is the same as CreateChatInviteLink, but with a context.Context parameter. +// CreateChatInviteLinkWithContext is the same as Bot.CreateChatInviteLink, but with a context.Context parameter func (bot *Bot) CreateChatInviteLinkWithContext(ctx context.Context, chatId int64, opts *CreateChatInviteLinkOpts) (*ChatInviteLink, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) @@ -1056,7 +720,7 @@ func (bot *Bot) CreateChatInviteLinkWithContext(ctx context.Context, chatId int6 return &c, json.Unmarshal(r, &c) } -// CreateChatSubscriptionInviteLinkOpts is the set of optional fields for Bot.CreateChatSubscriptionInviteLink. +// CreateChatSubscriptionInviteLinkOpts is the set of optional fields for Bot.CreateChatSubscriptionInviteLink and Bot.CreateChatSubscriptionInviteLinkWithContext. type CreateChatSubscriptionInviteLinkOpts struct { // Invite link name; 0-32 characters Name string @@ -1072,29 +736,10 @@ type CreateChatSubscriptionInviteLinkOpts struct { // - subscriptionPrice (type int64): The amount of Telegram Stars a user must pay initially and after each subsequent subscription period to be a member of the chat; 1-2500 // - opts (type CreateChatSubscriptionInviteLinkOpts): All optional parameters. func (bot *Bot) CreateChatSubscriptionInviteLink(chatId int64, subscriptionPeriod int64, subscriptionPrice int64, opts *CreateChatSubscriptionInviteLinkOpts) (*ChatInviteLink, error) { - v := map[string]string{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - v["subscription_period"] = strconv.FormatInt(subscriptionPeriod, 10) - v["subscription_price"] = strconv.FormatInt(subscriptionPrice, 10) - if opts != nil { - v["name"] = opts.Name - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("createChatSubscriptionInviteLink", v, nil, reqOpts) - if err != nil { - return nil, err - } - - var c ChatInviteLink - return &c, json.Unmarshal(r, &c) + return bot.CreateChatSubscriptionInviteLinkWithContext(context.Background(), chatId, subscriptionPeriod, subscriptionPrice, opts) } -// CreateChatSubscriptionInviteLinkWithContext is the same as CreateChatSubscriptionInviteLink, but with a context.Context parameter. +// CreateChatSubscriptionInviteLinkWithContext is the same as Bot.CreateChatSubscriptionInviteLink, but with a context.Context parameter func (bot *Bot) CreateChatSubscriptionInviteLinkWithContext(ctx context.Context, chatId int64, subscriptionPeriod int64, subscriptionPrice int64, opts *CreateChatSubscriptionInviteLinkOpts) (*ChatInviteLink, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) @@ -1118,7 +763,7 @@ func (bot *Bot) CreateChatSubscriptionInviteLinkWithContext(ctx context.Context, return &c, json.Unmarshal(r, &c) } -// CreateForumTopicOpts is the set of optional fields for Bot.CreateForumTopic. +// CreateForumTopicOpts is the set of optional fields for Bot.CreateForumTopic and Bot.CreateForumTopicWithContext. type CreateForumTopicOpts struct { // Color of the topic icon in RGB format. Currently, must be one of 7322096 (0x6FB9F0), 16766590 (0xFFD67E), 13338331 (0xCB86DB), 9367192 (0x8EEE98), 16749490 (0xFF93B2), or 16478047 (0xFB6F5F) IconColor int64 @@ -1135,31 +780,10 @@ type CreateForumTopicOpts struct { // - name (type string): Topic name, 1-128 characters // - opts (type CreateForumTopicOpts): All optional parameters. func (bot *Bot) CreateForumTopic(chatId int64, name string, opts *CreateForumTopicOpts) (*ForumTopic, error) { - v := map[string]string{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - v["name"] = name - if opts != nil { - if opts.IconColor != 0 { - v["icon_color"] = strconv.FormatInt(opts.IconColor, 10) - } - v["icon_custom_emoji_id"] = opts.IconCustomEmojiId - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("createForumTopic", v, nil, reqOpts) - if err != nil { - return nil, err - } - - var f ForumTopic - return &f, json.Unmarshal(r, &f) + return bot.CreateForumTopicWithContext(context.Background(), chatId, name, opts) } -// CreateForumTopicWithContext is the same as CreateForumTopic, but with a context.Context parameter. +// CreateForumTopicWithContext is the same as Bot.CreateForumTopic, but with a context.Context parameter func (bot *Bot) CreateForumTopicWithContext(ctx context.Context, chatId int64, name string, opts *CreateForumTopicOpts) (*ForumTopic, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) @@ -1185,7 +809,7 @@ func (bot *Bot) CreateForumTopicWithContext(ctx context.Context, chatId int64, n return &f, json.Unmarshal(r, &f) } -// CreateInvoiceLinkOpts is the set of optional fields for Bot.CreateInvoiceLink. +// CreateInvoiceLinkOpts is the set of optional fields for Bot.CreateInvoiceLink and Bot.CreateInvoiceLinkWithContext. type CreateInvoiceLinkOpts struct { // Payment provider token, obtained via @BotFather. Pass an empty string for payments in Telegram Stars. ProviderToken string @@ -1231,65 +855,10 @@ type CreateInvoiceLinkOpts struct { // - prices (type []LabeledPrice): Price breakdown, a JSON-serialized list of components (e.g. product price, tax, discount, delivery cost, delivery tax, bonus, etc.). Must contain exactly one item for payments in Telegram Stars. // - opts (type CreateInvoiceLinkOpts): All optional parameters. func (bot *Bot) CreateInvoiceLink(title string, description string, payload string, currency string, prices []LabeledPrice, opts *CreateInvoiceLinkOpts) (string, error) { - v := map[string]string{} - v["title"] = title - v["description"] = description - v["payload"] = payload - v["currency"] = currency - if prices != nil { - bs, err := json.Marshal(prices) - if err != nil { - return "", fmt.Errorf("failed to marshal field prices: %w", err) - } - v["prices"] = string(bs) - } - if opts != nil { - v["provider_token"] = opts.ProviderToken - if opts.MaxTipAmount != 0 { - v["max_tip_amount"] = strconv.FormatInt(opts.MaxTipAmount, 10) - } - if opts.SuggestedTipAmounts != nil { - bs, err := json.Marshal(opts.SuggestedTipAmounts) - if err != nil { - return "", fmt.Errorf("failed to marshal field suggested_tip_amounts: %w", err) - } - v["suggested_tip_amounts"] = string(bs) - } - v["provider_data"] = opts.ProviderData - v["photo_url"] = opts.PhotoUrl - if opts.PhotoSize != 0 { - v["photo_size"] = strconv.FormatInt(opts.PhotoSize, 10) - } - if opts.PhotoWidth != 0 { - v["photo_width"] = strconv.FormatInt(opts.PhotoWidth, 10) - } - if opts.PhotoHeight != 0 { - v["photo_height"] = strconv.FormatInt(opts.PhotoHeight, 10) - } - v["need_name"] = strconv.FormatBool(opts.NeedName) - v["need_phone_number"] = strconv.FormatBool(opts.NeedPhoneNumber) - v["need_email"] = strconv.FormatBool(opts.NeedEmail) - v["need_shipping_address"] = strconv.FormatBool(opts.NeedShippingAddress) - v["send_phone_number_to_provider"] = strconv.FormatBool(opts.SendPhoneNumberToProvider) - v["send_email_to_provider"] = strconv.FormatBool(opts.SendEmailToProvider) - v["is_flexible"] = strconv.FormatBool(opts.IsFlexible) - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("createInvoiceLink", v, nil, reqOpts) - if err != nil { - return "", err - } - - var s string - return s, json.Unmarshal(r, &s) + return bot.CreateInvoiceLinkWithContext(context.Background(), title, description, payload, currency, prices, opts) } -// CreateInvoiceLinkWithContext is the same as CreateInvoiceLink, but with a context.Context parameter. +// CreateInvoiceLinkWithContext is the same as Bot.CreateInvoiceLink, but with a context.Context parameter func (bot *Bot) CreateInvoiceLinkWithContext(ctx context.Context, title string, description string, payload string, currency string, prices []LabeledPrice, opts *CreateInvoiceLinkOpts) (string, error) { v := map[string]string{} v["title"] = title @@ -1349,7 +918,7 @@ func (bot *Bot) CreateInvoiceLinkWithContext(ctx context.Context, title string, return s, json.Unmarshal(r, &s) } -// CreateNewStickerSetOpts is the set of optional fields for Bot.CreateNewStickerSet. +// CreateNewStickerSetOpts is the set of optional fields for Bot.CreateNewStickerSet and Bot.CreateNewStickerSetWithContext. type CreateNewStickerSetOpts struct { // Type of stickers in the set, pass "regular", "mask", or "custom_emoji". By default, a regular sticker set is created. StickerType string @@ -1368,46 +937,10 @@ type CreateNewStickerSetOpts struct { // - stickers (type []InputSticker): A JSON-serialized list of 1-50 initial stickers to be added to the sticker set // - opts (type CreateNewStickerSetOpts): All optional parameters. func (bot *Bot) CreateNewStickerSet(userId int64, name string, title string, stickers []InputSticker, opts *CreateNewStickerSetOpts) (bool, error) { - v := map[string]string{} - data := map[string]FileReader{} - v["user_id"] = strconv.FormatInt(userId, 10) - v["name"] = name - v["title"] = title - if stickers != nil { - var rawList []json.RawMessage - for idx, im := range stickers { - inputBs, err := im.InputParams("stickers"+strconv.Itoa(idx), data) - if err != nil { - return false, fmt.Errorf("failed to marshal list item %d for field stickers: %w", idx, err) - } - rawList = append(rawList, inputBs) - } - bs, err := json.Marshal(rawList) - if err != nil { - return false, fmt.Errorf("failed to marshal raw json list for field: stickers %w", err) - } - v["stickers"] = string(bs) - } - if opts != nil { - v["sticker_type"] = opts.StickerType - v["needs_repainting"] = strconv.FormatBool(opts.NeedsRepainting) - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("createNewStickerSet", v, data, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) + return bot.CreateNewStickerSetWithContext(context.Background(), userId, name, title, stickers, opts) } -// CreateNewStickerSetWithContext is the same as CreateNewStickerSet, but with a context.Context parameter. +// CreateNewStickerSetWithContext is the same as Bot.CreateNewStickerSet, but with a context.Context parameter func (bot *Bot) CreateNewStickerSetWithContext(ctx context.Context, userId int64, name string, title string, stickers []InputSticker, opts *CreateNewStickerSetOpts) (bool, error) { v := map[string]string{} data := map[string]FileReader{} @@ -1448,7 +981,7 @@ func (bot *Bot) CreateNewStickerSetWithContext(ctx context.Context, userId int64 return b, json.Unmarshal(r, &b) } -// DeclineChatJoinRequestOpts is the set of optional fields for Bot.DeclineChatJoinRequest. +// DeclineChatJoinRequestOpts is the set of optional fields for Bot.DeclineChatJoinRequest and Bot.DeclineChatJoinRequestWithContext. type DeclineChatJoinRequestOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests RequestOpts *RequestOpts @@ -1461,25 +994,10 @@ type DeclineChatJoinRequestOpts struct { // - userId (type int64): Unique identifier of the target user // - opts (type DeclineChatJoinRequestOpts): All optional parameters. func (bot *Bot) DeclineChatJoinRequest(chatId int64, userId int64, opts *DeclineChatJoinRequestOpts) (bool, error) { - v := map[string]string{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - v["user_id"] = strconv.FormatInt(userId, 10) - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("declineChatJoinRequest", v, nil, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) + return bot.DeclineChatJoinRequestWithContext(context.Background(), chatId, userId, opts) } -// DeclineChatJoinRequestWithContext is the same as DeclineChatJoinRequest, but with a context.Context parameter. +// DeclineChatJoinRequestWithContext is the same as Bot.DeclineChatJoinRequest, but with a context.Context parameter func (bot *Bot) DeclineChatJoinRequestWithContext(ctx context.Context, chatId int64, userId int64, opts *DeclineChatJoinRequestOpts) (bool, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) @@ -1499,7 +1017,7 @@ func (bot *Bot) DeclineChatJoinRequestWithContext(ctx context.Context, chatId in return b, json.Unmarshal(r, &b) } -// DeleteChatPhotoOpts is the set of optional fields for Bot.DeleteChatPhoto. +// DeleteChatPhotoOpts is the set of optional fields for Bot.DeleteChatPhoto and Bot.DeleteChatPhotoWithContext. type DeleteChatPhotoOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests RequestOpts *RequestOpts @@ -1511,24 +1029,10 @@ type DeleteChatPhotoOpts struct { // - chatId (type int64): Unique identifier for the target chat // - opts (type DeleteChatPhotoOpts): All optional parameters. func (bot *Bot) DeleteChatPhoto(chatId int64, opts *DeleteChatPhotoOpts) (bool, error) { - v := map[string]string{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("deleteChatPhoto", v, nil, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) + return bot.DeleteChatPhotoWithContext(context.Background(), chatId, opts) } -// DeleteChatPhotoWithContext is the same as DeleteChatPhoto, but with a context.Context parameter. +// DeleteChatPhotoWithContext is the same as Bot.DeleteChatPhoto, but with a context.Context parameter func (bot *Bot) DeleteChatPhotoWithContext(ctx context.Context, chatId int64, opts *DeleteChatPhotoOpts) (bool, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) @@ -1547,7 +1051,7 @@ func (bot *Bot) DeleteChatPhotoWithContext(ctx context.Context, chatId int64, op return b, json.Unmarshal(r, &b) } -// DeleteChatStickerSetOpts is the set of optional fields for Bot.DeleteChatStickerSet. +// DeleteChatStickerSetOpts is the set of optional fields for Bot.DeleteChatStickerSet and Bot.DeleteChatStickerSetWithContext. type DeleteChatStickerSetOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests RequestOpts *RequestOpts @@ -1559,6 +1063,11 @@ type DeleteChatStickerSetOpts struct { // - chatId (type int64): Unique identifier for the target chat // - opts (type DeleteChatStickerSetOpts): All optional parameters. func (bot *Bot) DeleteChatStickerSet(chatId int64, opts *DeleteChatStickerSetOpts) (bool, error) { + return bot.DeleteChatStickerSetWithContext(context.Background(), chatId, opts) +} + +// DeleteChatStickerSetWithContext is the same as Bot.DeleteChatStickerSet, but with a context.Context parameter +func (bot *Bot) DeleteChatStickerSetWithContext(ctx context.Context, chatId int64, opts *DeleteChatStickerSetOpts) (bool, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) @@ -1567,7 +1076,7 @@ func (bot *Bot) DeleteChatStickerSet(chatId int64, opts *DeleteChatStickerSetOpt reqOpts = opts.RequestOpts } - r, err := bot.Request("deleteChatStickerSet", v, nil, reqOpts) + r, err := bot.RequestWithContext(ctx, "deleteChatStickerSet", v, nil, reqOpts) if err != nil { return false, err } @@ -1576,26 +1085,7 @@ func (bot *Bot) DeleteChatStickerSet(chatId int64, opts *DeleteChatStickerSetOpt return b, json.Unmarshal(r, &b) } -// DeleteChatStickerSetWithContext is the same as DeleteChatStickerSet, but with a context.Context parameter. -func (bot *Bot) DeleteChatStickerSetWithContext(ctx context.Context, chatId int64, opts *DeleteChatStickerSetOpts) (bool, error) { - v := map[string]string{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.RequestWithContext(ctx, "deleteChatStickerSet", v, nil, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) -} - -// DeleteForumTopicOpts is the set of optional fields for Bot.DeleteForumTopic. +// DeleteForumTopicOpts is the set of optional fields for Bot.DeleteForumTopic and Bot.DeleteForumTopicWithContext. type DeleteForumTopicOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests RequestOpts *RequestOpts @@ -1608,25 +1098,10 @@ type DeleteForumTopicOpts struct { // - messageThreadId (type int64): Unique identifier for the target message thread of the forum topic // - opts (type DeleteForumTopicOpts): All optional parameters. func (bot *Bot) DeleteForumTopic(chatId int64, messageThreadId int64, opts *DeleteForumTopicOpts) (bool, error) { - v := map[string]string{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - v["message_thread_id"] = strconv.FormatInt(messageThreadId, 10) - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("deleteForumTopic", v, nil, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) + return bot.DeleteForumTopicWithContext(context.Background(), chatId, messageThreadId, opts) } -// DeleteForumTopicWithContext is the same as DeleteForumTopic, but with a context.Context parameter. +// DeleteForumTopicWithContext is the same as Bot.DeleteForumTopic, but with a context.Context parameter func (bot *Bot) DeleteForumTopicWithContext(ctx context.Context, chatId int64, messageThreadId int64, opts *DeleteForumTopicOpts) (bool, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) @@ -1646,7 +1121,7 @@ func (bot *Bot) DeleteForumTopicWithContext(ctx context.Context, chatId int64, m return b, json.Unmarshal(r, &b) } -// DeleteMessageOpts is the set of optional fields for Bot.DeleteMessage. +// DeleteMessageOpts is the set of optional fields for Bot.DeleteMessage and Bot.DeleteMessageWithContext. type DeleteMessageOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests RequestOpts *RequestOpts @@ -1669,25 +1144,10 @@ type DeleteMessageOpts struct { // - messageId (type int64): Identifier of the message to delete // - opts (type DeleteMessageOpts): All optional parameters. func (bot *Bot) DeleteMessage(chatId int64, messageId int64, opts *DeleteMessageOpts) (bool, error) { - v := map[string]string{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - v["message_id"] = strconv.FormatInt(messageId, 10) - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("deleteMessage", v, nil, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) + return bot.DeleteMessageWithContext(context.Background(), chatId, messageId, opts) } -// DeleteMessageWithContext is the same as DeleteMessage, but with a context.Context parameter. +// DeleteMessageWithContext is the same as Bot.DeleteMessage, but with a context.Context parameter func (bot *Bot) DeleteMessageWithContext(ctx context.Context, chatId int64, messageId int64, opts *DeleteMessageOpts) (bool, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) @@ -1707,7 +1167,7 @@ func (bot *Bot) DeleteMessageWithContext(ctx context.Context, chatId int64, mess return b, json.Unmarshal(r, &b) } -// DeleteMessagesOpts is the set of optional fields for Bot.DeleteMessages. +// DeleteMessagesOpts is the set of optional fields for Bot.DeleteMessages and Bot.DeleteMessagesWithContext. type DeleteMessagesOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests RequestOpts *RequestOpts @@ -1720,31 +1180,10 @@ type DeleteMessagesOpts struct { // - messageIds (type []int64): A JSON-serialized list of 1-100 identifiers of messages to delete. See deleteMessage for limitations on which messages can be deleted // - opts (type DeleteMessagesOpts): All optional parameters. func (bot *Bot) DeleteMessages(chatId int64, messageIds []int64, opts *DeleteMessagesOpts) (bool, error) { - v := map[string]string{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - if messageIds != nil { - bs, err := json.Marshal(messageIds) - if err != nil { - return false, fmt.Errorf("failed to marshal field message_ids: %w", err) - } - v["message_ids"] = string(bs) - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("deleteMessages", v, nil, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) + return bot.DeleteMessagesWithContext(context.Background(), chatId, messageIds, opts) } -// DeleteMessagesWithContext is the same as DeleteMessages, but with a context.Context parameter. +// DeleteMessagesWithContext is the same as Bot.DeleteMessages, but with a context.Context parameter func (bot *Bot) DeleteMessagesWithContext(ctx context.Context, chatId int64, messageIds []int64, opts *DeleteMessagesOpts) (bool, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) @@ -1770,7 +1209,7 @@ func (bot *Bot) DeleteMessagesWithContext(ctx context.Context, chatId int64, mes return b, json.Unmarshal(r, &b) } -// DeleteMyCommandsOpts is the set of optional fields for Bot.DeleteMyCommands. +// DeleteMyCommandsOpts is the set of optional fields for Bot.DeleteMyCommands and Bot.DeleteMyCommandsWithContext. type DeleteMyCommandsOpts struct { // A JSON-serialized object, describing scope of users for which the commands are relevant. Defaults to BotCommandScopeDefault. Scope BotCommandScope @@ -1785,31 +1224,10 @@ type DeleteMyCommandsOpts struct { // Use this method to delete the list of the bot's commands for the given scope and user language. After deletion, higher level commands will be shown to affected users. Returns True on success. // - opts (type DeleteMyCommandsOpts): All optional parameters. func (bot *Bot) DeleteMyCommands(opts *DeleteMyCommandsOpts) (bool, error) { - v := map[string]string{} - if opts != nil { - bs, err := json.Marshal(opts.Scope) - if err != nil { - return false, fmt.Errorf("failed to marshal field scope: %w", err) - } - v["scope"] = string(bs) - v["language_code"] = opts.LanguageCode - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("deleteMyCommands", v, nil, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) + return bot.DeleteMyCommandsWithContext(context.Background(), opts) } -// DeleteMyCommandsWithContext is the same as DeleteMyCommands, but with a context.Context parameter. +// DeleteMyCommandsWithContext is the same as Bot.DeleteMyCommands, but with a context.Context parameter func (bot *Bot) DeleteMyCommandsWithContext(ctx context.Context, opts *DeleteMyCommandsOpts) (bool, error) { v := map[string]string{} if opts != nil { @@ -1835,7 +1253,7 @@ func (bot *Bot) DeleteMyCommandsWithContext(ctx context.Context, opts *DeleteMyC return b, json.Unmarshal(r, &b) } -// DeleteStickerFromSetOpts is the set of optional fields for Bot.DeleteStickerFromSet. +// DeleteStickerFromSetOpts is the set of optional fields for Bot.DeleteStickerFromSet and Bot.DeleteStickerFromSetWithContext. type DeleteStickerFromSetOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests RequestOpts *RequestOpts @@ -1847,24 +1265,10 @@ type DeleteStickerFromSetOpts struct { // - sticker (type string): File identifier of the sticker // - opts (type DeleteStickerFromSetOpts): All optional parameters. func (bot *Bot) DeleteStickerFromSet(sticker string, opts *DeleteStickerFromSetOpts) (bool, error) { - v := map[string]string{} - v["sticker"] = sticker - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("deleteStickerFromSet", v, nil, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) + return bot.DeleteStickerFromSetWithContext(context.Background(), sticker, opts) } -// DeleteStickerFromSetWithContext is the same as DeleteStickerFromSet, but with a context.Context parameter. +// DeleteStickerFromSetWithContext is the same as Bot.DeleteStickerFromSet, but with a context.Context parameter func (bot *Bot) DeleteStickerFromSetWithContext(ctx context.Context, sticker string, opts *DeleteStickerFromSetOpts) (bool, error) { v := map[string]string{} v["sticker"] = sticker @@ -1883,7 +1287,7 @@ func (bot *Bot) DeleteStickerFromSetWithContext(ctx context.Context, sticker str return b, json.Unmarshal(r, &b) } -// DeleteStickerSetOpts is the set of optional fields for Bot.DeleteStickerSet. +// DeleteStickerSetOpts is the set of optional fields for Bot.DeleteStickerSet and Bot.DeleteStickerSetWithContext. type DeleteStickerSetOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests RequestOpts *RequestOpts @@ -1895,24 +1299,10 @@ type DeleteStickerSetOpts struct { // - name (type string): Sticker set name // - opts (type DeleteStickerSetOpts): All optional parameters. func (bot *Bot) DeleteStickerSet(name string, opts *DeleteStickerSetOpts) (bool, error) { - v := map[string]string{} - v["name"] = name - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("deleteStickerSet", v, nil, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) + return bot.DeleteStickerSetWithContext(context.Background(), name, opts) } -// DeleteStickerSetWithContext is the same as DeleteStickerSet, but with a context.Context parameter. +// DeleteStickerSetWithContext is the same as Bot.DeleteStickerSet, but with a context.Context parameter func (bot *Bot) DeleteStickerSetWithContext(ctx context.Context, name string, opts *DeleteStickerSetOpts) (bool, error) { v := map[string]string{} v["name"] = name @@ -1931,7 +1321,7 @@ func (bot *Bot) DeleteStickerSetWithContext(ctx context.Context, name string, op return b, json.Unmarshal(r, &b) } -// DeleteWebhookOpts is the set of optional fields for Bot.DeleteWebhook. +// DeleteWebhookOpts is the set of optional fields for Bot.DeleteWebhook and Bot.DeleteWebhookWithContext. type DeleteWebhookOpts struct { // Pass True to drop all pending updates DropPendingUpdates bool @@ -1944,26 +1334,10 @@ type DeleteWebhookOpts struct { // Use this method to remove webhook integration if you decide to switch back to getUpdates. Returns True on success. // - opts (type DeleteWebhookOpts): All optional parameters. func (bot *Bot) DeleteWebhook(opts *DeleteWebhookOpts) (bool, error) { - v := map[string]string{} - if opts != nil { - v["drop_pending_updates"] = strconv.FormatBool(opts.DropPendingUpdates) - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("deleteWebhook", v, nil, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) + return bot.DeleteWebhookWithContext(context.Background(), opts) } -// DeleteWebhookWithContext is the same as DeleteWebhook, but with a context.Context parameter. +// DeleteWebhookWithContext is the same as Bot.DeleteWebhook, but with a context.Context parameter func (bot *Bot) DeleteWebhookWithContext(ctx context.Context, opts *DeleteWebhookOpts) (bool, error) { v := map[string]string{} if opts != nil { @@ -1984,7 +1358,7 @@ func (bot *Bot) DeleteWebhookWithContext(ctx context.Context, opts *DeleteWebhoo return b, json.Unmarshal(r, &b) } -// EditChatInviteLinkOpts is the set of optional fields for Bot.EditChatInviteLink. +// EditChatInviteLinkOpts is the set of optional fields for Bot.EditChatInviteLink and Bot.EditChatInviteLinkWithContext. type EditChatInviteLinkOpts struct { // Invite link name; 0-32 characters Name string @@ -2005,35 +1379,10 @@ type EditChatInviteLinkOpts struct { // - inviteLink (type string): The invite link to edit // - opts (type EditChatInviteLinkOpts): All optional parameters. func (bot *Bot) EditChatInviteLink(chatId int64, inviteLink string, opts *EditChatInviteLinkOpts) (*ChatInviteLink, error) { - v := map[string]string{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - v["invite_link"] = inviteLink - if opts != nil { - v["name"] = opts.Name - if opts.ExpireDate != 0 { - v["expire_date"] = strconv.FormatInt(opts.ExpireDate, 10) - } - if opts.MemberLimit != 0 { - v["member_limit"] = strconv.FormatInt(opts.MemberLimit, 10) - } - v["creates_join_request"] = strconv.FormatBool(opts.CreatesJoinRequest) - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("editChatInviteLink", v, nil, reqOpts) - if err != nil { - return nil, err - } - - var c ChatInviteLink - return &c, json.Unmarshal(r, &c) + return bot.EditChatInviteLinkWithContext(context.Background(), chatId, inviteLink, opts) } -// EditChatInviteLinkWithContext is the same as EditChatInviteLink, but with a context.Context parameter. +// EditChatInviteLinkWithContext is the same as Bot.EditChatInviteLink, but with a context.Context parameter func (bot *Bot) EditChatInviteLinkWithContext(ctx context.Context, chatId int64, inviteLink string, opts *EditChatInviteLinkOpts) (*ChatInviteLink, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) @@ -2063,7 +1412,7 @@ func (bot *Bot) EditChatInviteLinkWithContext(ctx context.Context, chatId int64, return &c, json.Unmarshal(r, &c) } -// EditChatSubscriptionInviteLinkOpts is the set of optional fields for Bot.EditChatSubscriptionInviteLink. +// EditChatSubscriptionInviteLinkOpts is the set of optional fields for Bot.EditChatSubscriptionInviteLink and Bot.EditChatSubscriptionInviteLinkWithContext. type EditChatSubscriptionInviteLinkOpts struct { // Invite link name; 0-32 characters Name string @@ -2078,28 +1427,10 @@ type EditChatSubscriptionInviteLinkOpts struct { // - inviteLink (type string): The invite link to edit // - opts (type EditChatSubscriptionInviteLinkOpts): All optional parameters. func (bot *Bot) EditChatSubscriptionInviteLink(chatId int64, inviteLink string, opts *EditChatSubscriptionInviteLinkOpts) (*ChatInviteLink, error) { - v := map[string]string{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - v["invite_link"] = inviteLink - if opts != nil { - v["name"] = opts.Name - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("editChatSubscriptionInviteLink", v, nil, reqOpts) - if err != nil { - return nil, err - } - - var c ChatInviteLink - return &c, json.Unmarshal(r, &c) + return bot.EditChatSubscriptionInviteLinkWithContext(context.Background(), chatId, inviteLink, opts) } -// EditChatSubscriptionInviteLinkWithContext is the same as EditChatSubscriptionInviteLink, but with a context.Context parameter. +// EditChatSubscriptionInviteLinkWithContext is the same as Bot.EditChatSubscriptionInviteLink, but with a context.Context parameter func (bot *Bot) EditChatSubscriptionInviteLinkWithContext(ctx context.Context, chatId int64, inviteLink string, opts *EditChatSubscriptionInviteLinkOpts) (*ChatInviteLink, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) @@ -2122,7 +1453,7 @@ func (bot *Bot) EditChatSubscriptionInviteLinkWithContext(ctx context.Context, c return &c, json.Unmarshal(r, &c) } -// EditForumTopicOpts is the set of optional fields for Bot.EditForumTopic. +// EditForumTopicOpts is the set of optional fields for Bot.EditForumTopic and Bot.EditForumTopicWithContext. type EditForumTopicOpts struct { // New topic name, 0-128 characters. If not specified or empty, the current name of the topic will be kept Name string @@ -2139,31 +1470,10 @@ type EditForumTopicOpts struct { // - messageThreadId (type int64): Unique identifier for the target message thread of the forum topic // - opts (type EditForumTopicOpts): All optional parameters. func (bot *Bot) EditForumTopic(chatId int64, messageThreadId int64, opts *EditForumTopicOpts) (bool, error) { - v := map[string]string{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - v["message_thread_id"] = strconv.FormatInt(messageThreadId, 10) - if opts != nil { - v["name"] = opts.Name - if opts.IconCustomEmojiId != nil { - v["icon_custom_emoji_id"] = *opts.IconCustomEmojiId - } - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("editForumTopic", v, nil, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) + return bot.EditForumTopicWithContext(context.Background(), chatId, messageThreadId, opts) } -// EditForumTopicWithContext is the same as EditForumTopic, but with a context.Context parameter. +// EditForumTopicWithContext is the same as Bot.EditForumTopic, but with a context.Context parameter func (bot *Bot) EditForumTopicWithContext(ctx context.Context, chatId int64, messageThreadId int64, opts *EditForumTopicOpts) (bool, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) @@ -2189,7 +1499,7 @@ func (bot *Bot) EditForumTopicWithContext(ctx context.Context, chatId int64, mes return b, json.Unmarshal(r, &b) } -// EditGeneralForumTopicOpts is the set of optional fields for Bot.EditGeneralForumTopic. +// EditGeneralForumTopicOpts is the set of optional fields for Bot.EditGeneralForumTopic and Bot.EditGeneralForumTopicWithContext. type EditGeneralForumTopicOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests RequestOpts *RequestOpts @@ -2202,25 +1512,10 @@ type EditGeneralForumTopicOpts struct { // - name (type string): New topic name, 1-128 characters // - opts (type EditGeneralForumTopicOpts): All optional parameters. func (bot *Bot) EditGeneralForumTopic(chatId int64, name string, opts *EditGeneralForumTopicOpts) (bool, error) { - v := map[string]string{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - v["name"] = name - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("editGeneralForumTopic", v, nil, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) + return bot.EditGeneralForumTopicWithContext(context.Background(), chatId, name, opts) } -// EditGeneralForumTopicWithContext is the same as EditGeneralForumTopic, but with a context.Context parameter. +// EditGeneralForumTopicWithContext is the same as Bot.EditGeneralForumTopic, but with a context.Context parameter func (bot *Bot) EditGeneralForumTopicWithContext(ctx context.Context, chatId int64, name string, opts *EditGeneralForumTopicOpts) (bool, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) @@ -2240,7 +1535,7 @@ func (bot *Bot) EditGeneralForumTopicWithContext(ctx context.Context, chatId int return b, json.Unmarshal(r, &b) } -// EditMessageCaptionOpts is the set of optional fields for Bot.EditMessageCaption. +// EditMessageCaptionOpts is the set of optional fields for Bot.EditMessageCaption and Bot.EditMessageCaptionWithContext. type EditMessageCaptionOpts struct { // Unique identifier of the business connection on behalf of which the message to be edited was sent BusinessConnectionId string @@ -2269,56 +1564,10 @@ type EditMessageCaptionOpts struct { // 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) - } - if opts.MessageId != 0 { - v["message_id"] = strconv.FormatInt(opts.MessageId, 10) - } - v["inline_message_id"] = opts.InlineMessageId - v["caption"] = opts.Caption - v["parse_mode"] = opts.ParseMode - if opts.CaptionEntities != nil { - bs, err := json.Marshal(opts.CaptionEntities) - if err != nil { - return nil, false, fmt.Errorf("failed to marshal field caption_entities: %w", err) - } - v["caption_entities"] = string(bs) - } - v["show_caption_above_media"] = strconv.FormatBool(opts.ShowCaptionAboveMedia) - bs, err := json.Marshal(opts.ReplyMarkup) - if err != nil { - return nil, false, fmt.Errorf("failed to marshal field reply_markup: %w", err) - } - v["reply_markup"] = string(bs) - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("editMessageCaption", v, nil, reqOpts) - if err != nil { - return nil, false, err - } - - var m Message - if err := json.Unmarshal(r, &m); err != nil { - var b bool - if err := json.Unmarshal(r, &b); err != nil { - return nil, false, err - } - return nil, b, nil - } - return &m, true, nil - + return bot.EditMessageCaptionWithContext(context.Background(), opts) } -// EditMessageCaptionWithContext is the same as EditMessageCaption, but with a context.Context parameter. +// EditMessageCaptionWithContext is the same as Bot.EditMessageCaption, but with a context.Context parameter func (bot *Bot) EditMessageCaptionWithContext(ctx context.Context, opts *EditMessageCaptionOpts) (*Message, bool, error) { v := map[string]string{} if opts != nil { @@ -2369,7 +1618,7 @@ func (bot *Bot) EditMessageCaptionWithContext(ctx context.Context, opts *EditMes } -// EditMessageLiveLocationOpts is the set of optional fields for Bot.EditMessageLiveLocation. +// EditMessageLiveLocationOpts is the set of optional fields for Bot.EditMessageLiveLocation and Bot.EditMessageLiveLocationWithContext. type EditMessageLiveLocationOpts struct { // Unique identifier of the business connection on behalf of which the message to be edited was sent BusinessConnectionId string @@ -2400,60 +1649,10 @@ type EditMessageLiveLocationOpts struct { // - longitude (type float64): Longitude of new location // - opts (type EditMessageLiveLocationOpts): All optional parameters. func (bot *Bot) EditMessageLiveLocation(latitude float64, longitude float64, opts *EditMessageLiveLocationOpts) (*Message, bool, error) { - v := map[string]string{} - 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) - } - if opts.MessageId != 0 { - v["message_id"] = strconv.FormatInt(opts.MessageId, 10) - } - v["inline_message_id"] = opts.InlineMessageId - if opts.LivePeriod != nil { - v["live_period"] = strconv.FormatInt(*opts.LivePeriod, 10) - } - if opts.HorizontalAccuracy != 0.0 { - v["horizontal_accuracy"] = strconv.FormatFloat(opts.HorizontalAccuracy, 'f', -1, 64) - } - if opts.Heading != 0 { - v["heading"] = strconv.FormatInt(opts.Heading, 10) - } - if opts.ProximityAlertRadius != 0 { - v["proximity_alert_radius"] = strconv.FormatInt(opts.ProximityAlertRadius, 10) - } - bs, err := json.Marshal(opts.ReplyMarkup) - if err != nil { - return nil, false, fmt.Errorf("failed to marshal field reply_markup: %w", err) - } - v["reply_markup"] = string(bs) - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("editMessageLiveLocation", v, nil, reqOpts) - if err != nil { - return nil, false, err - } - - var m Message - if err := json.Unmarshal(r, &m); err != nil { - var b bool - if err := json.Unmarshal(r, &b); err != nil { - return nil, false, err - } - return nil, b, nil - } - return &m, true, nil - + return bot.EditMessageLiveLocationWithContext(context.Background(), latitude, longitude, opts) } -// EditMessageLiveLocationWithContext is the same as EditMessageLiveLocation, but with a context.Context parameter. +// EditMessageLiveLocationWithContext is the same as Bot.EditMessageLiveLocation, but with a context.Context parameter func (bot *Bot) EditMessageLiveLocationWithContext(ctx context.Context, latitude float64, longitude float64, opts *EditMessageLiveLocationOpts) (*Message, bool, error) { v := map[string]string{} v["latitude"] = strconv.FormatFloat(latitude, 'f', -1, 64) @@ -2508,7 +1707,7 @@ func (bot *Bot) EditMessageLiveLocationWithContext(ctx context.Context, latitude } -// EditMessageMediaOpts is the set of optional fields for Bot.EditMessageMedia. +// EditMessageMediaOpts is the set of optional fields for Bot.EditMessageMedia and Bot.EditMessageMediaWithContext. type EditMessageMediaOpts struct { // Unique identifier of the business connection on behalf of which the message to be edited was sent BusinessConnectionId string @@ -2530,52 +1729,10 @@ type EditMessageMediaOpts struct { // - 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) { - v := map[string]string{} - data := map[string]FileReader{} - inputBs, err := media.InputParams("media", data) - if err != nil { - return nil, false, fmt.Errorf("failed to marshal field media: %w", err) - } - 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) - } - if opts.MessageId != 0 { - v["message_id"] = strconv.FormatInt(opts.MessageId, 10) - } - v["inline_message_id"] = opts.InlineMessageId - bs, err := json.Marshal(opts.ReplyMarkup) - if err != nil { - return nil, false, fmt.Errorf("failed to marshal field reply_markup: %w", err) - } - v["reply_markup"] = string(bs) - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("editMessageMedia", v, data, reqOpts) - if err != nil { - return nil, false, err - } - - var m Message - if err := json.Unmarshal(r, &m); err != nil { - var b bool - if err := json.Unmarshal(r, &b); err != nil { - return nil, false, err - } - return nil, b, nil - } - return &m, true, nil - + return bot.EditMessageMediaWithContext(context.Background(), media, opts) } -// EditMessageMediaWithContext is the same as EditMessageMedia, but with a context.Context parameter. +// EditMessageMediaWithContext is the same as Bot.EditMessageMedia, but with a context.Context parameter func (bot *Bot) EditMessageMediaWithContext(ctx context.Context, media InputMedia, opts *EditMessageMediaOpts) (*Message, bool, error) { v := map[string]string{} data := map[string]FileReader{} @@ -2622,7 +1779,7 @@ func (bot *Bot) EditMessageMediaWithContext(ctx context.Context, media InputMedi } -// EditMessageReplyMarkupOpts is the set of optional fields for Bot.EditMessageReplyMarkup. +// EditMessageReplyMarkupOpts is the set of optional fields for Bot.EditMessageReplyMarkup and Bot.EditMessageReplyMarkupWithContext. type EditMessageReplyMarkupOpts struct { // Unique identifier of the business connection on behalf of which the message to be edited was sent BusinessConnectionId string @@ -2643,46 +1800,10 @@ type EditMessageReplyMarkupOpts struct { // 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) - } - if opts.MessageId != 0 { - v["message_id"] = strconv.FormatInt(opts.MessageId, 10) - } - v["inline_message_id"] = opts.InlineMessageId - bs, err := json.Marshal(opts.ReplyMarkup) - if err != nil { - return nil, false, fmt.Errorf("failed to marshal field reply_markup: %w", err) - } - v["reply_markup"] = string(bs) - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("editMessageReplyMarkup", v, nil, reqOpts) - if err != nil { - return nil, false, err - } - - var m Message - if err := json.Unmarshal(r, &m); err != nil { - var b bool - if err := json.Unmarshal(r, &b); err != nil { - return nil, false, err - } - return nil, b, nil - } - return &m, true, nil - + return bot.EditMessageReplyMarkupWithContext(context.Background(), opts) } -// EditMessageReplyMarkupWithContext is the same as EditMessageReplyMarkup, but with a context.Context parameter. +// EditMessageReplyMarkupWithContext is the same as Bot.EditMessageReplyMarkup, but with a context.Context parameter func (bot *Bot) EditMessageReplyMarkupWithContext(ctx context.Context, opts *EditMessageReplyMarkupOpts) (*Message, bool, error) { v := map[string]string{} if opts != nil { @@ -2723,7 +1844,7 @@ func (bot *Bot) EditMessageReplyMarkupWithContext(ctx context.Context, opts *Edi } -// EditMessageTextOpts is the set of optional fields for Bot.EditMessageText. +// EditMessageTextOpts is the set of optional fields for Bot.EditMessageText and Bot.EditMessageTextWithContext. type EditMessageTextOpts struct { // Unique identifier of the business connection on behalf of which the message to be edited was sent BusinessConnectionId string @@ -2751,62 +1872,10 @@ type EditMessageTextOpts struct { // - 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) - } - if opts.MessageId != 0 { - v["message_id"] = strconv.FormatInt(opts.MessageId, 10) - } - v["inline_message_id"] = opts.InlineMessageId - v["parse_mode"] = opts.ParseMode - if opts.Entities != nil { - bs, err := json.Marshal(opts.Entities) - if err != nil { - return nil, false, fmt.Errorf("failed to marshal field entities: %w", err) - } - v["entities"] = string(bs) - } - if opts.LinkPreviewOptions != nil { - bs, err := json.Marshal(opts.LinkPreviewOptions) - if err != nil { - return nil, false, fmt.Errorf("failed to marshal field link_preview_options: %w", err) - } - v["link_preview_options"] = string(bs) - } - bs, err := json.Marshal(opts.ReplyMarkup) - if err != nil { - return nil, false, fmt.Errorf("failed to marshal field reply_markup: %w", err) - } - v["reply_markup"] = string(bs) - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("editMessageText", v, nil, reqOpts) - if err != nil { - return nil, false, err - } - - var m Message - if err := json.Unmarshal(r, &m); err != nil { - var b bool - if err := json.Unmarshal(r, &b); err != nil { - return nil, false, err - } - return nil, b, nil - } - return &m, true, nil - + return bot.EditMessageTextWithContext(context.Background(), text, opts) } -// EditMessageTextWithContext is the same as EditMessageText, but with a context.Context parameter. +// EditMessageTextWithContext is the same as Bot.EditMessageText, but with a context.Context parameter func (bot *Bot) EditMessageTextWithContext(ctx context.Context, text string, opts *EditMessageTextOpts) (*Message, bool, error) { v := map[string]string{} v["text"] = text @@ -2863,7 +1932,7 @@ func (bot *Bot) EditMessageTextWithContext(ctx context.Context, text string, opt } -// ExportChatInviteLinkOpts is the set of optional fields for Bot.ExportChatInviteLink. +// ExportChatInviteLinkOpts is the set of optional fields for Bot.ExportChatInviteLink and Bot.ExportChatInviteLinkWithContext. type ExportChatInviteLinkOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests RequestOpts *RequestOpts @@ -2875,24 +1944,10 @@ type ExportChatInviteLinkOpts struct { // - chatId (type int64): Unique identifier for the target chat // - opts (type ExportChatInviteLinkOpts): All optional parameters. func (bot *Bot) ExportChatInviteLink(chatId int64, opts *ExportChatInviteLinkOpts) (string, error) { - v := map[string]string{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("exportChatInviteLink", v, nil, reqOpts) - if err != nil { - return "", err - } - - var s string - return s, json.Unmarshal(r, &s) + return bot.ExportChatInviteLinkWithContext(context.Background(), chatId, opts) } -// ExportChatInviteLinkWithContext is the same as ExportChatInviteLink, but with a context.Context parameter. +// ExportChatInviteLinkWithContext is the same as Bot.ExportChatInviteLink, but with a context.Context parameter func (bot *Bot) ExportChatInviteLinkWithContext(ctx context.Context, chatId int64, opts *ExportChatInviteLinkOpts) (string, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) @@ -2911,7 +1966,7 @@ func (bot *Bot) ExportChatInviteLinkWithContext(ctx context.Context, chatId int6 return s, json.Unmarshal(r, &s) } -// ForwardMessageOpts is the set of optional fields for Bot.ForwardMessage. +// ForwardMessageOpts is the set of optional fields for Bot.ForwardMessage and Bot.ForwardMessageWithContext. type ForwardMessageOpts struct { // Unique identifier for the target message thread (topic) of the forum; for forum supergroups only MessageThreadId int64 @@ -2931,33 +1986,10 @@ type ForwardMessageOpts struct { // - messageId (type int64): Message identifier in the chat specified in from_chat_id // - opts (type ForwardMessageOpts): All optional parameters. func (bot *Bot) ForwardMessage(chatId int64, fromChatId int64, messageId int64, opts *ForwardMessageOpts) (*Message, error) { - v := map[string]string{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - v["from_chat_id"] = strconv.FormatInt(fromChatId, 10) - v["message_id"] = strconv.FormatInt(messageId, 10) - if opts != nil { - if opts.MessageThreadId != 0 { - v["message_thread_id"] = strconv.FormatInt(opts.MessageThreadId, 10) - } - v["disable_notification"] = strconv.FormatBool(opts.DisableNotification) - v["protect_content"] = strconv.FormatBool(opts.ProtectContent) - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("forwardMessage", v, nil, reqOpts) - if err != nil { - return nil, err - } - - var m Message - return &m, json.Unmarshal(r, &m) + return bot.ForwardMessageWithContext(context.Background(), chatId, fromChatId, messageId, opts) } -// ForwardMessageWithContext is the same as ForwardMessage, but with a context.Context parameter. +// ForwardMessageWithContext is the same as Bot.ForwardMessage, but with a context.Context parameter func (bot *Bot) ForwardMessageWithContext(ctx context.Context, chatId int64, fromChatId int64, messageId int64, opts *ForwardMessageOpts) (*Message, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) @@ -2985,7 +2017,7 @@ func (bot *Bot) ForwardMessageWithContext(ctx context.Context, chatId int64, fro return &m, json.Unmarshal(r, &m) } -// ForwardMessagesOpts is the set of optional fields for Bot.ForwardMessages. +// ForwardMessagesOpts is the set of optional fields for Bot.ForwardMessages and Bot.ForwardMessagesWithContext. type ForwardMessagesOpts struct { // Unique identifier for the target message thread (topic) of the forum; for forum supergroups only MessageThreadId int64 @@ -3005,39 +2037,10 @@ type ForwardMessagesOpts struct { // - messageIds (type []int64): A JSON-serialized list of 1-100 identifiers of messages in the chat from_chat_id to forward. The identifiers must be specified in a strictly increasing order. // - opts (type ForwardMessagesOpts): All optional parameters. func (bot *Bot) ForwardMessages(chatId int64, fromChatId int64, messageIds []int64, opts *ForwardMessagesOpts) ([]MessageId, error) { - v := map[string]string{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - v["from_chat_id"] = strconv.FormatInt(fromChatId, 10) - if messageIds != nil { - bs, err := json.Marshal(messageIds) - if err != nil { - return nil, fmt.Errorf("failed to marshal field message_ids: %w", err) - } - v["message_ids"] = string(bs) - } - if opts != nil { - if opts.MessageThreadId != 0 { - v["message_thread_id"] = strconv.FormatInt(opts.MessageThreadId, 10) - } - v["disable_notification"] = strconv.FormatBool(opts.DisableNotification) - v["protect_content"] = strconv.FormatBool(opts.ProtectContent) - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("forwardMessages", v, nil, reqOpts) - if err != nil { - return nil, err - } - - var m []MessageId - return m, json.Unmarshal(r, &m) + return bot.ForwardMessagesWithContext(context.Background(), chatId, fromChatId, messageIds, opts) } -// ForwardMessagesWithContext is the same as ForwardMessages, but with a context.Context parameter. +// ForwardMessagesWithContext is the same as Bot.ForwardMessages, but with a context.Context parameter func (bot *Bot) ForwardMessagesWithContext(ctx context.Context, chatId int64, fromChatId int64, messageIds []int64, opts *ForwardMessagesOpts) ([]MessageId, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) @@ -3071,7 +2074,7 @@ func (bot *Bot) ForwardMessagesWithContext(ctx context.Context, chatId int64, fr return m, json.Unmarshal(r, &m) } -// GetBusinessConnectionOpts is the set of optional fields for Bot.GetBusinessConnection. +// GetBusinessConnectionOpts is the set of optional fields for Bot.GetBusinessConnection and Bot.GetBusinessConnectionWithContext. type GetBusinessConnectionOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests RequestOpts *RequestOpts @@ -3083,6 +2086,11 @@ type GetBusinessConnectionOpts struct { // - businessConnectionId (type string): Unique identifier of the business connection // - opts (type GetBusinessConnectionOpts): All optional parameters. func (bot *Bot) GetBusinessConnection(businessConnectionId string, opts *GetBusinessConnectionOpts) (*BusinessConnection, error) { + return bot.GetBusinessConnectionWithContext(context.Background(), businessConnectionId, opts) +} + +// GetBusinessConnectionWithContext is the same as Bot.GetBusinessConnection, but with a context.Context parameter +func (bot *Bot) GetBusinessConnectionWithContext(ctx context.Context, businessConnectionId string, opts *GetBusinessConnectionOpts) (*BusinessConnection, error) { v := map[string]string{} v["business_connection_id"] = businessConnectionId @@ -3091,7 +2099,7 @@ func (bot *Bot) GetBusinessConnection(businessConnectionId string, opts *GetBusi reqOpts = opts.RequestOpts } - r, err := bot.Request("getBusinessConnection", v, nil, reqOpts) + r, err := bot.RequestWithContext(ctx, "getBusinessConnection", v, nil, reqOpts) if err != nil { return nil, err } @@ -3100,30 +2108,11 @@ func (bot *Bot) GetBusinessConnection(businessConnectionId string, opts *GetBusi return &b, json.Unmarshal(r, &b) } -// GetBusinessConnectionWithContext is the same as GetBusinessConnection, but with a context.Context parameter. -func (bot *Bot) GetBusinessConnectionWithContext(ctx context.Context, businessConnectionId string, opts *GetBusinessConnectionOpts) (*BusinessConnection, error) { - v := map[string]string{} - v["business_connection_id"] = businessConnectionId - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.RequestWithContext(ctx, "getBusinessConnection", v, nil, reqOpts) - if err != nil { - return nil, err - } - - var b BusinessConnection - return &b, json.Unmarshal(r, &b) -} - -// GetChatOpts is the set of optional fields for Bot.GetChat. -type GetChatOpts struct { - // RequestOpts are an additional optional field to configure timeouts for individual requests - RequestOpts *RequestOpts -} +// GetChatOpts is the set of optional fields for Bot.GetChat and Bot.GetChatWithContext. +type GetChatOpts struct { + // RequestOpts are an additional optional field to configure timeouts for individual requests + RequestOpts *RequestOpts +} // GetChat (https://core.telegram.org/bots/api#getchat) // @@ -3131,24 +2120,10 @@ type GetChatOpts struct { // - chatId (type int64): Unique identifier for the target chat // - opts (type GetChatOpts): All optional parameters. func (bot *Bot) GetChat(chatId int64, opts *GetChatOpts) (*ChatFullInfo, error) { - v := map[string]string{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("getChat", v, nil, reqOpts) - if err != nil { - return nil, err - } - - var c ChatFullInfo - return &c, json.Unmarshal(r, &c) + return bot.GetChatWithContext(context.Background(), chatId, opts) } -// GetChatWithContext is the same as GetChat, but with a context.Context parameter. +// GetChatWithContext is the same as Bot.GetChat, but with a context.Context parameter func (bot *Bot) GetChatWithContext(ctx context.Context, chatId int64, opts *GetChatOpts) (*ChatFullInfo, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) @@ -3167,7 +2142,7 @@ func (bot *Bot) GetChatWithContext(ctx context.Context, chatId int64, opts *GetC return &c, json.Unmarshal(r, &c) } -// GetChatAdministratorsOpts is the set of optional fields for Bot.GetChatAdministrators. +// GetChatAdministratorsOpts is the set of optional fields for Bot.GetChatAdministrators and Bot.GetChatAdministratorsWithContext. type GetChatAdministratorsOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests RequestOpts *RequestOpts @@ -3179,23 +2154,10 @@ type GetChatAdministratorsOpts struct { // - chatId (type int64): Unique identifier for the target chat // - opts (type GetChatAdministratorsOpts): All optional parameters. func (bot *Bot) GetChatAdministrators(chatId int64, opts *GetChatAdministratorsOpts) ([]ChatMember, error) { - v := map[string]string{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("getChatAdministrators", v, nil, reqOpts) - if err != nil { - return nil, err - } - - return unmarshalChatMemberArray(r) + return bot.GetChatAdministratorsWithContext(context.Background(), chatId, opts) } -// GetChatAdministratorsWithContext is the same as GetChatAdministrators, but with a context.Context parameter. +// GetChatAdministratorsWithContext is the same as Bot.GetChatAdministrators, but with a context.Context parameter func (bot *Bot) GetChatAdministratorsWithContext(ctx context.Context, chatId int64, opts *GetChatAdministratorsOpts) ([]ChatMember, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) @@ -3213,7 +2175,7 @@ func (bot *Bot) GetChatAdministratorsWithContext(ctx context.Context, chatId int return unmarshalChatMemberArray(r) } -// GetChatMemberOpts is the set of optional fields for Bot.GetChatMember. +// GetChatMemberOpts is the set of optional fields for Bot.GetChatMember and Bot.GetChatMemberWithContext. type GetChatMemberOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests RequestOpts *RequestOpts @@ -3226,24 +2188,10 @@ type GetChatMemberOpts struct { // - userId (type int64): Unique identifier of the target user // - opts (type GetChatMemberOpts): All optional parameters. func (bot *Bot) GetChatMember(chatId int64, userId int64, opts *GetChatMemberOpts) (ChatMember, error) { - v := map[string]string{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - v["user_id"] = strconv.FormatInt(userId, 10) - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("getChatMember", v, nil, reqOpts) - if err != nil { - return nil, err - } - - return unmarshalChatMember(r) + return bot.GetChatMemberWithContext(context.Background(), chatId, userId, opts) } -// GetChatMemberWithContext is the same as GetChatMember, but with a context.Context parameter. +// GetChatMemberWithContext is the same as Bot.GetChatMember, but with a context.Context parameter func (bot *Bot) GetChatMemberWithContext(ctx context.Context, chatId int64, userId int64, opts *GetChatMemberOpts) (ChatMember, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) @@ -3262,7 +2210,7 @@ func (bot *Bot) GetChatMemberWithContext(ctx context.Context, chatId int64, user return unmarshalChatMember(r) } -// GetChatMemberCountOpts is the set of optional fields for Bot.GetChatMemberCount. +// GetChatMemberCountOpts is the set of optional fields for Bot.GetChatMemberCount and Bot.GetChatMemberCountWithContext. type GetChatMemberCountOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests RequestOpts *RequestOpts @@ -3274,24 +2222,10 @@ type GetChatMemberCountOpts struct { // - chatId (type int64): Unique identifier for the target chat // - opts (type GetChatMemberCountOpts): All optional parameters. func (bot *Bot) GetChatMemberCount(chatId int64, opts *GetChatMemberCountOpts) (int64, error) { - v := map[string]string{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("getChatMemberCount", v, nil, reqOpts) - if err != nil { - return 0, err - } - - var i int64 - return i, json.Unmarshal(r, &i) + return bot.GetChatMemberCountWithContext(context.Background(), chatId, opts) } -// GetChatMemberCountWithContext is the same as GetChatMemberCount, but with a context.Context parameter. +// GetChatMemberCountWithContext is the same as Bot.GetChatMemberCount, but with a context.Context parameter func (bot *Bot) GetChatMemberCountWithContext(ctx context.Context, chatId int64, opts *GetChatMemberCountOpts) (int64, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) @@ -3310,7 +2244,7 @@ func (bot *Bot) GetChatMemberCountWithContext(ctx context.Context, chatId int64, return i, json.Unmarshal(r, &i) } -// GetChatMenuButtonOpts is the set of optional fields for Bot.GetChatMenuButton. +// GetChatMenuButtonOpts is the set of optional fields for Bot.GetChatMenuButton and Bot.GetChatMenuButtonWithContext. type GetChatMenuButtonOpts struct { // Unique identifier for the target private chat. If not specified, default bot's menu button will be returned ChatId *int64 @@ -3323,27 +2257,10 @@ type GetChatMenuButtonOpts struct { // Use this method to get the current value of the bot's menu button in a private chat, or the default menu button. Returns MenuButton on success. // - opts (type GetChatMenuButtonOpts): All optional parameters. func (bot *Bot) GetChatMenuButton(opts *GetChatMenuButtonOpts) (MenuButton, error) { - v := map[string]string{} - if opts != nil { - if opts.ChatId != nil { - v["chat_id"] = strconv.FormatInt(*opts.ChatId, 10) - } - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("getChatMenuButton", v, nil, reqOpts) - if err != nil { - return nil, err - } - - return unmarshalMenuButton(r) + return bot.GetChatMenuButtonWithContext(context.Background(), opts) } -// GetChatMenuButtonWithContext is the same as GetChatMenuButton, but with a context.Context parameter. +// GetChatMenuButtonWithContext is the same as Bot.GetChatMenuButton, but with a context.Context parameter func (bot *Bot) GetChatMenuButtonWithContext(ctx context.Context, opts *GetChatMenuButtonOpts) (MenuButton, error) { v := map[string]string{} if opts != nil { @@ -3365,7 +2282,7 @@ func (bot *Bot) GetChatMenuButtonWithContext(ctx context.Context, opts *GetChatM return unmarshalMenuButton(r) } -// GetCustomEmojiStickersOpts is the set of optional fields for Bot.GetCustomEmojiStickers. +// GetCustomEmojiStickersOpts is the set of optional fields for Bot.GetCustomEmojiStickers and Bot.GetCustomEmojiStickersWithContext. type GetCustomEmojiStickersOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests RequestOpts *RequestOpts @@ -3377,30 +2294,10 @@ type GetCustomEmojiStickersOpts struct { // - customEmojiIds (type []string): A JSON-serialized list of custom emoji identifiers. At most 200 custom emoji identifiers can be specified. // - opts (type GetCustomEmojiStickersOpts): All optional parameters. func (bot *Bot) GetCustomEmojiStickers(customEmojiIds []string, opts *GetCustomEmojiStickersOpts) ([]Sticker, error) { - v := map[string]string{} - if customEmojiIds != nil { - bs, err := json.Marshal(customEmojiIds) - if err != nil { - return nil, fmt.Errorf("failed to marshal field custom_emoji_ids: %w", err) - } - v["custom_emoji_ids"] = string(bs) - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("getCustomEmojiStickers", v, nil, reqOpts) - if err != nil { - return nil, err - } - - var s []Sticker - return s, json.Unmarshal(r, &s) + return bot.GetCustomEmojiStickersWithContext(context.Background(), customEmojiIds, opts) } -// GetCustomEmojiStickersWithContext is the same as GetCustomEmojiStickers, but with a context.Context parameter. +// GetCustomEmojiStickersWithContext is the same as Bot.GetCustomEmojiStickers, but with a context.Context parameter func (bot *Bot) GetCustomEmojiStickersWithContext(ctx context.Context, customEmojiIds []string, opts *GetCustomEmojiStickersOpts) ([]Sticker, error) { v := map[string]string{} if customEmojiIds != nil { @@ -3425,7 +2322,7 @@ func (bot *Bot) GetCustomEmojiStickersWithContext(ctx context.Context, customEmo return s, json.Unmarshal(r, &s) } -// GetFileOpts is the set of optional fields for Bot.GetFile. +// GetFileOpts is the set of optional fields for Bot.GetFile and Bot.GetFileWithContext. type GetFileOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests RequestOpts *RequestOpts @@ -3438,24 +2335,10 @@ type GetFileOpts struct { // - fileId (type string): File identifier to get information about // - opts (type GetFileOpts): All optional parameters. func (bot *Bot) GetFile(fileId string, opts *GetFileOpts) (*File, error) { - v := map[string]string{} - v["file_id"] = fileId - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("getFile", v, nil, reqOpts) - if err != nil { - return nil, err - } - - var f File - return &f, json.Unmarshal(r, &f) + return bot.GetFileWithContext(context.Background(), fileId, opts) } -// GetFileWithContext is the same as GetFile, but with a context.Context parameter. +// GetFileWithContext is the same as Bot.GetFile, but with a context.Context parameter func (bot *Bot) GetFileWithContext(ctx context.Context, fileId string, opts *GetFileOpts) (*File, error) { v := map[string]string{} v["file_id"] = fileId @@ -3474,7 +2357,7 @@ func (bot *Bot) GetFileWithContext(ctx context.Context, fileId string, opts *Get return &f, json.Unmarshal(r, &f) } -// GetForumTopicIconStickersOpts is the set of optional fields for Bot.GetForumTopicIconStickers. +// GetForumTopicIconStickersOpts is the set of optional fields for Bot.GetForumTopicIconStickers and Bot.GetForumTopicIconStickersWithContext. type GetForumTopicIconStickersOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests RequestOpts *RequestOpts @@ -3485,23 +2368,10 @@ type GetForumTopicIconStickersOpts struct { // Use this method to get custom emoji stickers, which can be used as a forum topic icon by any user. Requires no parameters. Returns an Array of Sticker objects. // - opts (type GetForumTopicIconStickersOpts): All optional parameters. func (bot *Bot) GetForumTopicIconStickers(opts *GetForumTopicIconStickersOpts) ([]Sticker, error) { - v := map[string]string{} - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("getForumTopicIconStickers", v, nil, reqOpts) - if err != nil { - return nil, err - } - - var s []Sticker - return s, json.Unmarshal(r, &s) + return bot.GetForumTopicIconStickersWithContext(context.Background(), opts) } -// GetForumTopicIconStickersWithContext is the same as GetForumTopicIconStickers, but with a context.Context parameter. +// GetForumTopicIconStickersWithContext is the same as Bot.GetForumTopicIconStickers, but with a context.Context parameter func (bot *Bot) GetForumTopicIconStickersWithContext(ctx context.Context, opts *GetForumTopicIconStickersOpts) ([]Sticker, error) { v := map[string]string{} @@ -3519,7 +2389,7 @@ func (bot *Bot) GetForumTopicIconStickersWithContext(ctx context.Context, opts * return s, json.Unmarshal(r, &s) } -// GetGameHighScoresOpts is the set of optional fields for Bot.GetGameHighScores. +// GetGameHighScoresOpts is the set of optional fields for Bot.GetGameHighScores and Bot.GetGameHighScoresWithContext. type GetGameHighScoresOpts struct { // Required if inline_message_id is not specified. Unique identifier for the target chat ChatId int64 @@ -3537,33 +2407,10 @@ type GetGameHighScoresOpts struct { // - userId (type int64): Target user id // - opts (type GetGameHighScoresOpts): All optional parameters. func (bot *Bot) GetGameHighScores(userId int64, opts *GetGameHighScoresOpts) ([]GameHighScore, error) { - v := map[string]string{} - v["user_id"] = strconv.FormatInt(userId, 10) - if opts != nil { - if opts.ChatId != 0 { - v["chat_id"] = strconv.FormatInt(opts.ChatId, 10) - } - if opts.MessageId != 0 { - v["message_id"] = strconv.FormatInt(opts.MessageId, 10) - } - v["inline_message_id"] = opts.InlineMessageId - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("getGameHighScores", v, nil, reqOpts) - if err != nil { - return nil, err - } - - var g []GameHighScore - return g, json.Unmarshal(r, &g) + return bot.GetGameHighScoresWithContext(context.Background(), userId, opts) } -// GetGameHighScoresWithContext is the same as GetGameHighScores, but with a context.Context parameter. +// GetGameHighScoresWithContext is the same as Bot.GetGameHighScores, but with a context.Context parameter func (bot *Bot) GetGameHighScoresWithContext(ctx context.Context, userId int64, opts *GetGameHighScoresOpts) ([]GameHighScore, error) { v := map[string]string{} v["user_id"] = strconv.FormatInt(userId, 10) @@ -3591,7 +2438,7 @@ func (bot *Bot) GetGameHighScoresWithContext(ctx context.Context, userId int64, return g, json.Unmarshal(r, &g) } -// GetMeOpts is the set of optional fields for Bot.GetMe. +// GetMeOpts is the set of optional fields for Bot.GetMe and Bot.GetMeWithContext. type GetMeOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests RequestOpts *RequestOpts @@ -3602,23 +2449,10 @@ type GetMeOpts struct { // 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. // - opts (type GetMeOpts): All optional parameters. func (bot *Bot) GetMe(opts *GetMeOpts) (*User, error) { - v := map[string]string{} - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("getMe", v, nil, reqOpts) - if err != nil { - return nil, err - } - - var u User - return &u, json.Unmarshal(r, &u) + return bot.GetMeWithContext(context.Background(), opts) } -// GetMeWithContext is the same as GetMe, but with a context.Context parameter. +// GetMeWithContext is the same as Bot.GetMe, but with a context.Context parameter func (bot *Bot) GetMeWithContext(ctx context.Context, opts *GetMeOpts) (*User, error) { v := map[string]string{} @@ -3636,7 +2470,7 @@ func (bot *Bot) GetMeWithContext(ctx context.Context, opts *GetMeOpts) (*User, e return &u, json.Unmarshal(r, &u) } -// GetMyCommandsOpts is the set of optional fields for Bot.GetMyCommands. +// GetMyCommandsOpts is the set of optional fields for Bot.GetMyCommands and Bot.GetMyCommandsWithContext. type GetMyCommandsOpts struct { // A JSON-serialized object, describing scope of users. Defaults to BotCommandScopeDefault. Scope BotCommandScope @@ -3651,31 +2485,10 @@ type GetMyCommandsOpts struct { // Use this method to get the current list of the bot's commands for the given scope and user language. Returns an Array of BotCommand objects. If commands aren't set, an empty list is returned. // - opts (type GetMyCommandsOpts): All optional parameters. func (bot *Bot) GetMyCommands(opts *GetMyCommandsOpts) ([]BotCommand, error) { - v := map[string]string{} - if opts != nil { - bs, err := json.Marshal(opts.Scope) - if err != nil { - return nil, fmt.Errorf("failed to marshal field scope: %w", err) - } - v["scope"] = string(bs) - v["language_code"] = opts.LanguageCode - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("getMyCommands", v, nil, reqOpts) - if err != nil { - return nil, err - } - - var b []BotCommand - return b, json.Unmarshal(r, &b) + return bot.GetMyCommandsWithContext(context.Background(), opts) } -// GetMyCommandsWithContext is the same as GetMyCommands, but with a context.Context parameter. +// GetMyCommandsWithContext is the same as Bot.GetMyCommands, but with a context.Context parameter func (bot *Bot) GetMyCommandsWithContext(ctx context.Context, opts *GetMyCommandsOpts) ([]BotCommand, error) { v := map[string]string{} if opts != nil { @@ -3701,7 +2514,7 @@ func (bot *Bot) GetMyCommandsWithContext(ctx context.Context, opts *GetMyCommand return b, json.Unmarshal(r, &b) } -// GetMyDefaultAdministratorRightsOpts is the set of optional fields for Bot.GetMyDefaultAdministratorRights. +// GetMyDefaultAdministratorRightsOpts is the set of optional fields for Bot.GetMyDefaultAdministratorRights and Bot.GetMyDefaultAdministratorRightsWithContext. type GetMyDefaultAdministratorRightsOpts struct { // Pass True to get default administrator rights of the bot in channels. Otherwise, default administrator rights of the bot for groups and supergroups will be returned. ForChannels bool @@ -3714,26 +2527,10 @@ type GetMyDefaultAdministratorRightsOpts struct { // Use this method to get the current default administrator rights of the bot. Returns ChatAdministratorRights on success. // - opts (type GetMyDefaultAdministratorRightsOpts): All optional parameters. func (bot *Bot) GetMyDefaultAdministratorRights(opts *GetMyDefaultAdministratorRightsOpts) (*ChatAdministratorRights, error) { - v := map[string]string{} - if opts != nil { - v["for_channels"] = strconv.FormatBool(opts.ForChannels) - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("getMyDefaultAdministratorRights", v, nil, reqOpts) - if err != nil { - return nil, err - } - - var c ChatAdministratorRights - return &c, json.Unmarshal(r, &c) + return bot.GetMyDefaultAdministratorRightsWithContext(context.Background(), opts) } -// GetMyDefaultAdministratorRightsWithContext is the same as GetMyDefaultAdministratorRights, but with a context.Context parameter. +// GetMyDefaultAdministratorRightsWithContext is the same as Bot.GetMyDefaultAdministratorRights, but with a context.Context parameter func (bot *Bot) GetMyDefaultAdministratorRightsWithContext(ctx context.Context, opts *GetMyDefaultAdministratorRightsOpts) (*ChatAdministratorRights, error) { v := map[string]string{} if opts != nil { @@ -3754,7 +2551,7 @@ func (bot *Bot) GetMyDefaultAdministratorRightsWithContext(ctx context.Context, return &c, json.Unmarshal(r, &c) } -// GetMyDescriptionOpts is the set of optional fields for Bot.GetMyDescription. +// GetMyDescriptionOpts is the set of optional fields for Bot.GetMyDescription and Bot.GetMyDescriptionWithContext. type GetMyDescriptionOpts struct { // A two-letter ISO 639-1 language code or an empty string LanguageCode string @@ -3767,26 +2564,10 @@ type GetMyDescriptionOpts struct { // Use this method to get the current bot description for the given user language. Returns BotDescription on success. // - opts (type GetMyDescriptionOpts): All optional parameters. func (bot *Bot) GetMyDescription(opts *GetMyDescriptionOpts) (*BotDescription, error) { - v := map[string]string{} - if opts != nil { - v["language_code"] = opts.LanguageCode - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("getMyDescription", v, nil, reqOpts) - if err != nil { - return nil, err - } - - var b BotDescription - return &b, json.Unmarshal(r, &b) + return bot.GetMyDescriptionWithContext(context.Background(), opts) } -// GetMyDescriptionWithContext is the same as GetMyDescription, but with a context.Context parameter. +// GetMyDescriptionWithContext is the same as Bot.GetMyDescription, but with a context.Context parameter func (bot *Bot) GetMyDescriptionWithContext(ctx context.Context, opts *GetMyDescriptionOpts) (*BotDescription, error) { v := map[string]string{} if opts != nil { @@ -3807,7 +2588,7 @@ func (bot *Bot) GetMyDescriptionWithContext(ctx context.Context, opts *GetMyDesc return &b, json.Unmarshal(r, &b) } -// GetMyNameOpts is the set of optional fields for Bot.GetMyName. +// GetMyNameOpts is the set of optional fields for Bot.GetMyName and Bot.GetMyNameWithContext. type GetMyNameOpts struct { // A two-letter ISO 639-1 language code or an empty string LanguageCode string @@ -3820,26 +2601,10 @@ type GetMyNameOpts struct { // Use this method to get the current bot name for the given user language. Returns BotName on success. // - opts (type GetMyNameOpts): All optional parameters. func (bot *Bot) GetMyName(opts *GetMyNameOpts) (*BotName, error) { - v := map[string]string{} - if opts != nil { - v["language_code"] = opts.LanguageCode - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("getMyName", v, nil, reqOpts) - if err != nil { - return nil, err - } - - var b BotName - return &b, json.Unmarshal(r, &b) + return bot.GetMyNameWithContext(context.Background(), opts) } -// GetMyNameWithContext is the same as GetMyName, but with a context.Context parameter. +// GetMyNameWithContext is the same as Bot.GetMyName, but with a context.Context parameter func (bot *Bot) GetMyNameWithContext(ctx context.Context, opts *GetMyNameOpts) (*BotName, error) { v := map[string]string{} if opts != nil { @@ -3860,7 +2625,7 @@ func (bot *Bot) GetMyNameWithContext(ctx context.Context, opts *GetMyNameOpts) ( return &b, json.Unmarshal(r, &b) } -// GetMyShortDescriptionOpts is the set of optional fields for Bot.GetMyShortDescription. +// GetMyShortDescriptionOpts is the set of optional fields for Bot.GetMyShortDescription and Bot.GetMyShortDescriptionWithContext. type GetMyShortDescriptionOpts struct { // A two-letter ISO 639-1 language code or an empty string LanguageCode string @@ -3873,6 +2638,11 @@ type GetMyShortDescriptionOpts struct { // Use this method to get the current bot short description for the given user language. Returns BotShortDescription on success. // - opts (type GetMyShortDescriptionOpts): All optional parameters. func (bot *Bot) GetMyShortDescription(opts *GetMyShortDescriptionOpts) (*BotShortDescription, error) { + return bot.GetMyShortDescriptionWithContext(context.Background(), opts) +} + +// GetMyShortDescriptionWithContext is the same as Bot.GetMyShortDescription, but with a context.Context parameter +func (bot *Bot) GetMyShortDescriptionWithContext(ctx context.Context, opts *GetMyShortDescriptionOpts) (*BotShortDescription, error) { v := map[string]string{} if opts != nil { v["language_code"] = opts.LanguageCode @@ -3883,7 +2653,7 @@ func (bot *Bot) GetMyShortDescription(opts *GetMyShortDescriptionOpts) (*BotShor reqOpts = opts.RequestOpts } - r, err := bot.Request("getMyShortDescription", v, nil, reqOpts) + r, err := bot.RequestWithContext(ctx, "getMyShortDescription", v, nil, reqOpts) if err != nil { return nil, err } @@ -3892,28 +2662,7 @@ func (bot *Bot) GetMyShortDescription(opts *GetMyShortDescriptionOpts) (*BotShor return &b, json.Unmarshal(r, &b) } -// GetMyShortDescriptionWithContext is the same as GetMyShortDescription, but with a context.Context parameter. -func (bot *Bot) GetMyShortDescriptionWithContext(ctx context.Context, opts *GetMyShortDescriptionOpts) (*BotShortDescription, error) { - v := map[string]string{} - if opts != nil { - v["language_code"] = opts.LanguageCode - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.RequestWithContext(ctx, "getMyShortDescription", v, nil, reqOpts) - if err != nil { - return nil, err - } - - var b BotShortDescription - return &b, json.Unmarshal(r, &b) -} - -// GetStarTransactionsOpts is the set of optional fields for Bot.GetStarTransactions. +// GetStarTransactionsOpts is the set of optional fields for Bot.GetStarTransactions and Bot.GetStarTransactionsWithContext. type GetStarTransactionsOpts struct { // Number of transactions to skip in the response Offset int64 @@ -3928,31 +2677,10 @@ type GetStarTransactionsOpts struct { // 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) + return bot.GetStarTransactionsWithContext(context.Background(), opts) } -// GetStarTransactionsWithContext is the same as GetStarTransactions, but with a context.Context parameter. +// GetStarTransactionsWithContext is the same as Bot.GetStarTransactions, but with a context.Context parameter func (bot *Bot) GetStarTransactionsWithContext(ctx context.Context, opts *GetStarTransactionsOpts) (*StarTransactions, error) { v := map[string]string{} if opts != nil { @@ -3978,7 +2706,7 @@ func (bot *Bot) GetStarTransactionsWithContext(ctx context.Context, opts *GetSta return &s, json.Unmarshal(r, &s) } -// GetStickerSetOpts is the set of optional fields for Bot.GetStickerSet. +// GetStickerSetOpts is the set of optional fields for Bot.GetStickerSet and Bot.GetStickerSetWithContext. type GetStickerSetOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests RequestOpts *RequestOpts @@ -3990,24 +2718,10 @@ type GetStickerSetOpts struct { // - name (type string): Name of the sticker set // - opts (type GetStickerSetOpts): All optional parameters. func (bot *Bot) GetStickerSet(name string, opts *GetStickerSetOpts) (*StickerSet, error) { - v := map[string]string{} - v["name"] = name - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("getStickerSet", v, nil, reqOpts) - if err != nil { - return nil, err - } - - var s StickerSet - return &s, json.Unmarshal(r, &s) + return bot.GetStickerSetWithContext(context.Background(), name, opts) } -// GetStickerSetWithContext is the same as GetStickerSet, but with a context.Context parameter. +// GetStickerSetWithContext is the same as Bot.GetStickerSet, but with a context.Context parameter func (bot *Bot) GetStickerSetWithContext(ctx context.Context, name string, opts *GetStickerSetOpts) (*StickerSet, error) { v := map[string]string{} v["name"] = name @@ -4026,7 +2740,7 @@ func (bot *Bot) GetStickerSetWithContext(ctx context.Context, name string, opts return &s, json.Unmarshal(r, &s) } -// GetUpdatesOpts is the set of optional fields for Bot.GetUpdates. +// GetUpdatesOpts is the set of optional fields for Bot.GetUpdates and Bot.GetUpdatesWithContext. type GetUpdatesOpts struct { // Identifier of the first update to be returned. Must be greater by one than the highest among the identifiers of previously received updates. By default, updates starting with the earliest unconfirmed update are returned. An update is considered confirmed as soon as getUpdates is called with an offset higher than its update_id. The negative offset can be specified to retrieve updates starting from -offset update from the end of the updates queue. All previous updates will be forgotten. Offset int64 @@ -4045,41 +2759,10 @@ type GetUpdatesOpts struct { // Use this method to receive incoming updates using long polling (wiki). Returns an Array of Update objects. // - opts (type GetUpdatesOpts): All optional parameters. func (bot *Bot) GetUpdates(opts *GetUpdatesOpts) ([]Update, 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) - } - if opts.Timeout != 0 { - v["timeout"] = strconv.FormatInt(opts.Timeout, 10) - } - if opts.AllowedUpdates != nil { - bs, err := json.Marshal(opts.AllowedUpdates) - if err != nil { - return nil, fmt.Errorf("failed to marshal field allowed_updates: %w", err) - } - v["allowed_updates"] = string(bs) - } - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("getUpdates", v, nil, reqOpts) - if err != nil { - return nil, err - } - - var u []Update - return u, json.Unmarshal(r, &u) + return bot.GetUpdatesWithContext(context.Background(), opts) } -// GetUpdatesWithContext is the same as GetUpdates, but with a context.Context parameter. +// GetUpdatesWithContext is the same as Bot.GetUpdates, but with a context.Context parameter func (bot *Bot) GetUpdatesWithContext(ctx context.Context, opts *GetUpdatesOpts) ([]Update, error) { v := map[string]string{} if opts != nil { @@ -4115,7 +2798,7 @@ func (bot *Bot) GetUpdatesWithContext(ctx context.Context, opts *GetUpdatesOpts) return u, json.Unmarshal(r, &u) } -// GetUserChatBoostsOpts is the set of optional fields for Bot.GetUserChatBoosts. +// GetUserChatBoostsOpts is the set of optional fields for Bot.GetUserChatBoosts and Bot.GetUserChatBoostsWithContext. type GetUserChatBoostsOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests RequestOpts *RequestOpts @@ -4128,25 +2811,10 @@ type GetUserChatBoostsOpts struct { // - userId (type int64): Unique identifier of the target user // - opts (type GetUserChatBoostsOpts): All optional parameters. func (bot *Bot) GetUserChatBoosts(chatId int64, userId int64, opts *GetUserChatBoostsOpts) (*UserChatBoosts, error) { - v := map[string]string{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - v["user_id"] = strconv.FormatInt(userId, 10) - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("getUserChatBoosts", v, nil, reqOpts) - if err != nil { - return nil, err - } - - var u UserChatBoosts - return &u, json.Unmarshal(r, &u) + return bot.GetUserChatBoostsWithContext(context.Background(), chatId, userId, opts) } -// GetUserChatBoostsWithContext is the same as GetUserChatBoosts, but with a context.Context parameter. +// GetUserChatBoostsWithContext is the same as Bot.GetUserChatBoosts, but with a context.Context parameter func (bot *Bot) GetUserChatBoostsWithContext(ctx context.Context, chatId int64, userId int64, opts *GetUserChatBoostsOpts) (*UserChatBoosts, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) @@ -4166,7 +2834,7 @@ func (bot *Bot) GetUserChatBoostsWithContext(ctx context.Context, chatId int64, return &u, json.Unmarshal(r, &u) } -// GetUserProfilePhotosOpts is the set of optional fields for Bot.GetUserProfilePhotos. +// GetUserProfilePhotosOpts is the set of optional fields for Bot.GetUserProfilePhotos and Bot.GetUserProfilePhotosWithContext. type GetUserProfilePhotosOpts struct { // Sequential number of the first photo to be returned. By default, all photos are returned. Offset int64 @@ -4182,32 +2850,10 @@ type GetUserProfilePhotosOpts struct { // - userId (type int64): Unique identifier of the target user // - opts (type GetUserProfilePhotosOpts): All optional parameters. func (bot *Bot) GetUserProfilePhotos(userId int64, opts *GetUserProfilePhotosOpts) (*UserProfilePhotos, error) { - v := map[string]string{} - v["user_id"] = strconv.FormatInt(userId, 10) - 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("getUserProfilePhotos", v, nil, reqOpts) - if err != nil { - return nil, err - } - - var u UserProfilePhotos - return &u, json.Unmarshal(r, &u) + return bot.GetUserProfilePhotosWithContext(context.Background(), userId, opts) } -// GetUserProfilePhotosWithContext is the same as GetUserProfilePhotos, but with a context.Context parameter. +// GetUserProfilePhotosWithContext is the same as Bot.GetUserProfilePhotos, but with a context.Context parameter func (bot *Bot) GetUserProfilePhotosWithContext(ctx context.Context, userId int64, opts *GetUserProfilePhotosOpts) (*UserProfilePhotos, error) { v := map[string]string{} v["user_id"] = strconv.FormatInt(userId, 10) @@ -4234,7 +2880,7 @@ func (bot *Bot) GetUserProfilePhotosWithContext(ctx context.Context, userId int6 return &u, json.Unmarshal(r, &u) } -// GetWebhookInfoOpts is the set of optional fields for Bot.GetWebhookInfo. +// GetWebhookInfoOpts is the set of optional fields for Bot.GetWebhookInfo and Bot.GetWebhookInfoWithContext. type GetWebhookInfoOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests RequestOpts *RequestOpts @@ -4245,23 +2891,10 @@ type GetWebhookInfoOpts struct { // Use this method to get current webhook status. Requires no parameters. On success, returns a WebhookInfo object. If the bot is using getUpdates, will return an object with the url field empty. // - opts (type GetWebhookInfoOpts): All optional parameters. func (bot *Bot) GetWebhookInfo(opts *GetWebhookInfoOpts) (*WebhookInfo, error) { - v := map[string]string{} - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("getWebhookInfo", v, nil, reqOpts) - if err != nil { - return nil, err - } - - var w WebhookInfo - return &w, json.Unmarshal(r, &w) + return bot.GetWebhookInfoWithContext(context.Background(), opts) } -// GetWebhookInfoWithContext is the same as GetWebhookInfo, but with a context.Context parameter. +// GetWebhookInfoWithContext is the same as Bot.GetWebhookInfo, but with a context.Context parameter func (bot *Bot) GetWebhookInfoWithContext(ctx context.Context, opts *GetWebhookInfoOpts) (*WebhookInfo, error) { v := map[string]string{} @@ -4279,7 +2912,7 @@ func (bot *Bot) GetWebhookInfoWithContext(ctx context.Context, opts *GetWebhookI return &w, json.Unmarshal(r, &w) } -// HideGeneralForumTopicOpts is the set of optional fields for Bot.HideGeneralForumTopic. +// HideGeneralForumTopicOpts is the set of optional fields for Bot.HideGeneralForumTopic and Bot.HideGeneralForumTopicWithContext. type HideGeneralForumTopicOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests RequestOpts *RequestOpts @@ -4291,24 +2924,10 @@ type HideGeneralForumTopicOpts struct { // - chatId (type int64): Unique identifier for the target chat // - opts (type HideGeneralForumTopicOpts): All optional parameters. func (bot *Bot) HideGeneralForumTopic(chatId int64, opts *HideGeneralForumTopicOpts) (bool, error) { - v := map[string]string{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("hideGeneralForumTopic", v, nil, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) + return bot.HideGeneralForumTopicWithContext(context.Background(), chatId, opts) } -// HideGeneralForumTopicWithContext is the same as HideGeneralForumTopic, but with a context.Context parameter. +// HideGeneralForumTopicWithContext is the same as Bot.HideGeneralForumTopic, but with a context.Context parameter func (bot *Bot) HideGeneralForumTopicWithContext(ctx context.Context, chatId int64, opts *HideGeneralForumTopicOpts) (bool, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) @@ -4327,7 +2946,7 @@ func (bot *Bot) HideGeneralForumTopicWithContext(ctx context.Context, chatId int return b, json.Unmarshal(r, &b) } -// LeaveChatOpts is the set of optional fields for Bot.LeaveChat. +// LeaveChatOpts is the set of optional fields for Bot.LeaveChat and Bot.LeaveChatWithContext. type LeaveChatOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests RequestOpts *RequestOpts @@ -4339,24 +2958,10 @@ type LeaveChatOpts struct { // - chatId (type int64): Unique identifier for the target chat // - opts (type LeaveChatOpts): All optional parameters. func (bot *Bot) LeaveChat(chatId int64, opts *LeaveChatOpts) (bool, error) { - v := map[string]string{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("leaveChat", v, nil, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) + return bot.LeaveChatWithContext(context.Background(), chatId, opts) } -// LeaveChatWithContext is the same as LeaveChat, but with a context.Context parameter. +// LeaveChatWithContext is the same as Bot.LeaveChat, but with a context.Context parameter func (bot *Bot) LeaveChatWithContext(ctx context.Context, chatId int64, opts *LeaveChatOpts) (bool, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) @@ -4375,7 +2980,7 @@ func (bot *Bot) LeaveChatWithContext(ctx context.Context, chatId int64, opts *Le return b, json.Unmarshal(r, &b) } -// LogOutOpts is the set of optional fields for Bot.LogOut. +// LogOutOpts is the set of optional fields for Bot.LogOut and Bot.LogOutWithContext. type LogOutOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests RequestOpts *RequestOpts @@ -4386,23 +2991,10 @@ type LogOutOpts struct { // Use this method to log out from the cloud Bot API server before launching the bot locally. You must log out the bot before running it locally, otherwise there is no guarantee that the bot will receive updates. After a successful call, you can immediately log in on a local server, but will not be able to log in back to the cloud Bot API server for 10 minutes. Returns True on success. Requires no parameters. // - opts (type LogOutOpts): All optional parameters. func (bot *Bot) LogOut(opts *LogOutOpts) (bool, error) { - v := map[string]string{} - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("logOut", v, nil, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) + return bot.LogOutWithContext(context.Background(), opts) } -// LogOutWithContext is the same as LogOut, but with a context.Context parameter. +// LogOutWithContext is the same as Bot.LogOut, but with a context.Context parameter func (bot *Bot) LogOutWithContext(ctx context.Context, opts *LogOutOpts) (bool, error) { v := map[string]string{} @@ -4420,7 +3012,7 @@ func (bot *Bot) LogOutWithContext(ctx context.Context, opts *LogOutOpts) (bool, return b, json.Unmarshal(r, &b) } -// PinChatMessageOpts is the set of optional fields for Bot.PinChatMessage. +// PinChatMessageOpts is the set of optional fields for Bot.PinChatMessage and Bot.PinChatMessageWithContext. type PinChatMessageOpts struct { // Unique identifier of the business connection on behalf of which the message will be pinned BusinessConnectionId string @@ -4437,29 +3029,10 @@ type PinChatMessageOpts struct { // - messageId (type int64): Identifier of a message to pin // - opts (type PinChatMessageOpts): All optional parameters. func (bot *Bot) PinChatMessage(chatId int64, messageId int64, opts *PinChatMessageOpts) (bool, error) { - v := map[string]string{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - v["message_id"] = strconv.FormatInt(messageId, 10) - if opts != nil { - v["business_connection_id"] = opts.BusinessConnectionId - v["disable_notification"] = strconv.FormatBool(opts.DisableNotification) - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("pinChatMessage", v, nil, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) + return bot.PinChatMessageWithContext(context.Background(), chatId, messageId, opts) } -// PinChatMessageWithContext is the same as PinChatMessage, but with a context.Context parameter. +// PinChatMessageWithContext is the same as Bot.PinChatMessage, but with a context.Context parameter func (bot *Bot) PinChatMessageWithContext(ctx context.Context, chatId int64, messageId int64, opts *PinChatMessageOpts) (bool, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) @@ -4483,7 +3056,7 @@ func (bot *Bot) PinChatMessageWithContext(ctx context.Context, chatId int64, mes return b, json.Unmarshal(r, &b) } -// PromoteChatMemberOpts is the set of optional fields for Bot.PromoteChatMember. +// PromoteChatMemberOpts is the set of optional fields for Bot.PromoteChatMember and Bot.PromoteChatMemberWithContext. type PromoteChatMemberOpts struct { // Pass True if the administrator's presence in the chat is hidden IsAnonymous bool @@ -4526,42 +3099,10 @@ type PromoteChatMemberOpts struct { // - userId (type int64): Unique identifier of the target user // - opts (type PromoteChatMemberOpts): All optional parameters. func (bot *Bot) PromoteChatMember(chatId int64, userId int64, opts *PromoteChatMemberOpts) (bool, error) { - v := map[string]string{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - v["user_id"] = strconv.FormatInt(userId, 10) - if opts != nil { - v["is_anonymous"] = strconv.FormatBool(opts.IsAnonymous) - v["can_manage_chat"] = strconv.FormatBool(opts.CanManageChat) - v["can_delete_messages"] = strconv.FormatBool(opts.CanDeleteMessages) - v["can_manage_video_chats"] = strconv.FormatBool(opts.CanManageVideoChats) - v["can_restrict_members"] = strconv.FormatBool(opts.CanRestrictMembers) - v["can_promote_members"] = strconv.FormatBool(opts.CanPromoteMembers) - v["can_change_info"] = strconv.FormatBool(opts.CanChangeInfo) - v["can_invite_users"] = strconv.FormatBool(opts.CanInviteUsers) - v["can_post_stories"] = strconv.FormatBool(opts.CanPostStories) - v["can_edit_stories"] = strconv.FormatBool(opts.CanEditStories) - v["can_delete_stories"] = strconv.FormatBool(opts.CanDeleteStories) - v["can_post_messages"] = strconv.FormatBool(opts.CanPostMessages) - v["can_edit_messages"] = strconv.FormatBool(opts.CanEditMessages) - v["can_pin_messages"] = strconv.FormatBool(opts.CanPinMessages) - v["can_manage_topics"] = strconv.FormatBool(opts.CanManageTopics) - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("promoteChatMember", v, nil, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) + return bot.PromoteChatMemberWithContext(context.Background(), chatId, userId, opts) } -// PromoteChatMemberWithContext is the same as PromoteChatMember, but with a context.Context parameter. +// PromoteChatMemberWithContext is the same as Bot.PromoteChatMember, but with a context.Context parameter func (bot *Bot) PromoteChatMemberWithContext(ctx context.Context, chatId int64, userId int64, opts *PromoteChatMemberOpts) (bool, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) @@ -4598,7 +3139,7 @@ func (bot *Bot) PromoteChatMemberWithContext(ctx context.Context, chatId int64, return b, json.Unmarshal(r, &b) } -// RefundStarPaymentOpts is the set of optional fields for Bot.RefundStarPayment. +// RefundStarPaymentOpts is the set of optional fields for Bot.RefundStarPayment and Bot.RefundStarPaymentWithContext. type RefundStarPaymentOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests RequestOpts *RequestOpts @@ -4611,25 +3152,10 @@ type RefundStarPaymentOpts struct { // - telegramPaymentChargeId (type string): Telegram payment identifier // - opts (type RefundStarPaymentOpts): All optional parameters. func (bot *Bot) RefundStarPayment(userId int64, telegramPaymentChargeId string, opts *RefundStarPaymentOpts) (bool, error) { - v := map[string]string{} - v["user_id"] = strconv.FormatInt(userId, 10) - v["telegram_payment_charge_id"] = telegramPaymentChargeId - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("refundStarPayment", v, nil, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) + return bot.RefundStarPaymentWithContext(context.Background(), userId, telegramPaymentChargeId, opts) } -// RefundStarPaymentWithContext is the same as RefundStarPayment, but with a context.Context parameter. +// RefundStarPaymentWithContext is the same as Bot.RefundStarPayment, but with a context.Context parameter func (bot *Bot) RefundStarPaymentWithContext(ctx context.Context, userId int64, telegramPaymentChargeId string, opts *RefundStarPaymentOpts) (bool, error) { v := map[string]string{} v["user_id"] = strconv.FormatInt(userId, 10) @@ -4649,7 +3175,7 @@ func (bot *Bot) RefundStarPaymentWithContext(ctx context.Context, userId int64, return b, json.Unmarshal(r, &b) } -// ReopenForumTopicOpts is the set of optional fields for Bot.ReopenForumTopic. +// ReopenForumTopicOpts is the set of optional fields for Bot.ReopenForumTopic and Bot.ReopenForumTopicWithContext. type ReopenForumTopicOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests RequestOpts *RequestOpts @@ -4662,25 +3188,10 @@ type ReopenForumTopicOpts struct { // - messageThreadId (type int64): Unique identifier for the target message thread of the forum topic // - opts (type ReopenForumTopicOpts): All optional parameters. func (bot *Bot) ReopenForumTopic(chatId int64, messageThreadId int64, opts *ReopenForumTopicOpts) (bool, error) { - v := map[string]string{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - v["message_thread_id"] = strconv.FormatInt(messageThreadId, 10) - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("reopenForumTopic", v, nil, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) + return bot.ReopenForumTopicWithContext(context.Background(), chatId, messageThreadId, opts) } -// ReopenForumTopicWithContext is the same as ReopenForumTopic, but with a context.Context parameter. +// ReopenForumTopicWithContext is the same as Bot.ReopenForumTopic, but with a context.Context parameter func (bot *Bot) ReopenForumTopicWithContext(ctx context.Context, chatId int64, messageThreadId int64, opts *ReopenForumTopicOpts) (bool, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) @@ -4700,7 +3211,7 @@ func (bot *Bot) ReopenForumTopicWithContext(ctx context.Context, chatId int64, m return b, json.Unmarshal(r, &b) } -// ReopenGeneralForumTopicOpts is the set of optional fields for Bot.ReopenGeneralForumTopic. +// ReopenGeneralForumTopicOpts is the set of optional fields for Bot.ReopenGeneralForumTopic and Bot.ReopenGeneralForumTopicWithContext. type ReopenGeneralForumTopicOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests RequestOpts *RequestOpts @@ -4712,6 +3223,11 @@ type ReopenGeneralForumTopicOpts struct { // - chatId (type int64): Unique identifier for the target chat // - opts (type ReopenGeneralForumTopicOpts): All optional parameters. func (bot *Bot) ReopenGeneralForumTopic(chatId int64, opts *ReopenGeneralForumTopicOpts) (bool, error) { + return bot.ReopenGeneralForumTopicWithContext(context.Background(), chatId, opts) +} + +// ReopenGeneralForumTopicWithContext is the same as Bot.ReopenGeneralForumTopic, but with a context.Context parameter +func (bot *Bot) ReopenGeneralForumTopicWithContext(ctx context.Context, chatId int64, opts *ReopenGeneralForumTopicOpts) (bool, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) @@ -4720,7 +3236,7 @@ func (bot *Bot) ReopenGeneralForumTopic(chatId int64, opts *ReopenGeneralForumTo reqOpts = opts.RequestOpts } - r, err := bot.Request("reopenGeneralForumTopic", v, nil, reqOpts) + r, err := bot.RequestWithContext(ctx, "reopenGeneralForumTopic", v, nil, reqOpts) if err != nil { return false, err } @@ -4729,26 +3245,7 @@ func (bot *Bot) ReopenGeneralForumTopic(chatId int64, opts *ReopenGeneralForumTo return b, json.Unmarshal(r, &b) } -// ReopenGeneralForumTopicWithContext is the same as ReopenGeneralForumTopic, but with a context.Context parameter. -func (bot *Bot) ReopenGeneralForumTopicWithContext(ctx context.Context, chatId int64, opts *ReopenGeneralForumTopicOpts) (bool, error) { - v := map[string]string{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.RequestWithContext(ctx, "reopenGeneralForumTopic", v, nil, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) -} - -// ReplaceStickerInSetOpts is the set of optional fields for Bot.ReplaceStickerInSet. +// ReplaceStickerInSetOpts is the set of optional fields for Bot.ReplaceStickerInSet and Bot.ReplaceStickerInSetWithContext. type ReplaceStickerInSetOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests RequestOpts *RequestOpts @@ -4763,32 +3260,10 @@ type ReplaceStickerInSetOpts struct { // - sticker (type InputSticker): A JSON-serialized object with information about the added sticker. If exactly the same sticker had already been added to the set, then the set remains unchanged. // - opts (type ReplaceStickerInSetOpts): All optional parameters. func (bot *Bot) ReplaceStickerInSet(userId int64, name string, oldSticker string, sticker InputSticker, opts *ReplaceStickerInSetOpts) (bool, error) { - v := map[string]string{} - data := map[string]FileReader{} - v["user_id"] = strconv.FormatInt(userId, 10) - v["name"] = name - v["old_sticker"] = oldSticker - inputBs, err := sticker.InputParams("sticker", data) - if err != nil { - return false, fmt.Errorf("failed to marshal field sticker: %w", err) - } - v["sticker"] = string(inputBs) - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("replaceStickerInSet", v, data, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) + return bot.ReplaceStickerInSetWithContext(context.Background(), userId, name, oldSticker, sticker, opts) } -// ReplaceStickerInSetWithContext is the same as ReplaceStickerInSet, but with a context.Context parameter. +// ReplaceStickerInSetWithContext is the same as Bot.ReplaceStickerInSet, but with a context.Context parameter func (bot *Bot) ReplaceStickerInSetWithContext(ctx context.Context, userId int64, name string, oldSticker string, sticker InputSticker, opts *ReplaceStickerInSetOpts) (bool, error) { v := map[string]string{} data := map[string]FileReader{} @@ -4815,7 +3290,7 @@ func (bot *Bot) ReplaceStickerInSetWithContext(ctx context.Context, userId int64 return b, json.Unmarshal(r, &b) } -// RestrictChatMemberOpts is the set of optional fields for Bot.RestrictChatMember. +// RestrictChatMemberOpts is the set of optional fields for Bot.RestrictChatMember and Bot.RestrictChatMemberWithContext. type RestrictChatMemberOpts struct { // Pass True if chat permissions are set independently. Otherwise, the can_send_other_messages and can_add_web_page_previews permissions will imply the can_send_messages, can_send_audios, can_send_documents, can_send_photos, can_send_videos, can_send_video_notes, and can_send_voice_notes permissions; the can_send_polls permission will imply the can_send_messages permission. UseIndependentChatPermissions bool @@ -4833,36 +3308,10 @@ type RestrictChatMemberOpts struct { // - permissions (type ChatPermissions): A JSON-serialized object for new user permissions // - opts (type RestrictChatMemberOpts): All optional parameters. func (bot *Bot) RestrictChatMember(chatId int64, userId int64, permissions ChatPermissions, opts *RestrictChatMemberOpts) (bool, error) { - v := map[string]string{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - v["user_id"] = strconv.FormatInt(userId, 10) - bs, err := json.Marshal(permissions) - if err != nil { - return false, fmt.Errorf("failed to marshal field permissions: %w", err) - } - v["permissions"] = string(bs) - if opts != nil { - v["use_independent_chat_permissions"] = strconv.FormatBool(opts.UseIndependentChatPermissions) - if opts.UntilDate != 0 { - v["until_date"] = strconv.FormatInt(opts.UntilDate, 10) - } - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("restrictChatMember", v, nil, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) + return bot.RestrictChatMemberWithContext(context.Background(), chatId, userId, permissions, opts) } -// RestrictChatMemberWithContext is the same as RestrictChatMember, but with a context.Context parameter. +// RestrictChatMemberWithContext is the same as Bot.RestrictChatMember, but with a context.Context parameter func (bot *Bot) RestrictChatMemberWithContext(ctx context.Context, chatId int64, userId int64, permissions ChatPermissions, opts *RestrictChatMemberOpts) (bool, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) @@ -4893,7 +3342,7 @@ func (bot *Bot) RestrictChatMemberWithContext(ctx context.Context, chatId int64, return b, json.Unmarshal(r, &b) } -// RevokeChatInviteLinkOpts is the set of optional fields for Bot.RevokeChatInviteLink. +// RevokeChatInviteLinkOpts is the set of optional fields for Bot.RevokeChatInviteLink and Bot.RevokeChatInviteLinkWithContext. type RevokeChatInviteLinkOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests RequestOpts *RequestOpts @@ -4906,25 +3355,10 @@ type RevokeChatInviteLinkOpts struct { // - inviteLink (type string): The invite link to revoke // - opts (type RevokeChatInviteLinkOpts): All optional parameters. func (bot *Bot) RevokeChatInviteLink(chatId int64, inviteLink string, opts *RevokeChatInviteLinkOpts) (*ChatInviteLink, error) { - v := map[string]string{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - v["invite_link"] = inviteLink - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("revokeChatInviteLink", v, nil, reqOpts) - if err != nil { - return nil, err - } - - var c ChatInviteLink - return &c, json.Unmarshal(r, &c) + return bot.RevokeChatInviteLinkWithContext(context.Background(), chatId, inviteLink, opts) } -// RevokeChatInviteLinkWithContext is the same as RevokeChatInviteLink, but with a context.Context parameter. +// RevokeChatInviteLinkWithContext is the same as Bot.RevokeChatInviteLink, but with a context.Context parameter func (bot *Bot) RevokeChatInviteLinkWithContext(ctx context.Context, chatId int64, inviteLink string, opts *RevokeChatInviteLinkOpts) (*ChatInviteLink, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) @@ -4944,7 +3378,7 @@ func (bot *Bot) RevokeChatInviteLinkWithContext(ctx context.Context, chatId int6 return &c, json.Unmarshal(r, &c) } -// SendAnimationOpts is the set of optional fields for Bot.SendAnimation. +// SendAnimationOpts is the set of optional fields for Bot.SendAnimation and Bot.SendAnimationWithContext. type SendAnimationOpts struct { // Unique identifier of the business connection on behalf of which the message will be sent BusinessConnectionId string @@ -4989,82 +3423,10 @@ type SendAnimationOpts struct { // - animation (type InputFileOrString): Animation to send. Pass a file_id as String to send an animation that exists on the Telegram servers (recommended), pass an HTTP URL as a String for Telegram to get an animation from the Internet, or upload a new animation using multipart/form-data. More information on Sending Files: https://core.telegram.org/bots/api#sending-files // - opts (type SendAnimationOpts): All optional parameters. func (bot *Bot) SendAnimation(chatId int64, animation InputFileOrString, opts *SendAnimationOpts) (*Message, error) { - v := map[string]string{} - data := map[string]FileReader{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - if animation != nil { - err := animation.Attach("animation", data) - if err != nil { - return nil, fmt.Errorf("failed to attach 'animation' input file: %w", err) - } - v["animation"] = animation.getValue() - } - if opts != nil { - v["business_connection_id"] = opts.BusinessConnectionId - if opts.MessageThreadId != 0 { - v["message_thread_id"] = strconv.FormatInt(opts.MessageThreadId, 10) - } - if opts.Duration != 0 { - v["duration"] = strconv.FormatInt(opts.Duration, 10) - } - if opts.Width != 0 { - v["width"] = strconv.FormatInt(opts.Width, 10) - } - if opts.Height != 0 { - v["height"] = strconv.FormatInt(opts.Height, 10) - } - if opts.Thumbnail != nil { - err := opts.Thumbnail.Attach("thumbnail", data) - if err != nil { - return nil, fmt.Errorf("failed to attach 'thumbnail' input file: %w", err) - } - v["thumbnail"] = opts.Thumbnail.getValue() - } - v["caption"] = opts.Caption - v["parse_mode"] = opts.ParseMode - if opts.CaptionEntities != nil { - bs, err := json.Marshal(opts.CaptionEntities) - if err != nil { - return nil, fmt.Errorf("failed to marshal field caption_entities: %w", err) - } - v["caption_entities"] = string(bs) - } - v["show_caption_above_media"] = strconv.FormatBool(opts.ShowCaptionAboveMedia) - v["has_spoiler"] = strconv.FormatBool(opts.HasSpoiler) - v["disable_notification"] = strconv.FormatBool(opts.DisableNotification) - v["protect_content"] = strconv.FormatBool(opts.ProtectContent) - v["message_effect_id"] = opts.MessageEffectId - if opts.ReplyParameters != nil { - bs, err := json.Marshal(opts.ReplyParameters) - if err != nil { - return nil, fmt.Errorf("failed to marshal field reply_parameters: %w", err) - } - v["reply_parameters"] = string(bs) - } - if opts.ReplyMarkup != nil { - bs, err := json.Marshal(opts.ReplyMarkup) - if err != nil { - return nil, fmt.Errorf("failed to marshal field reply_markup: %w", err) - } - v["reply_markup"] = string(bs) - } - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("sendAnimation", v, data, reqOpts) - if err != nil { - return nil, err - } - - var m Message - return &m, json.Unmarshal(r, &m) + return bot.SendAnimationWithContext(context.Background(), chatId, animation, opts) } -// SendAnimationWithContext is the same as SendAnimation, but with a context.Context parameter. +// SendAnimationWithContext is the same as Bot.SendAnimation, but with a context.Context parameter func (bot *Bot) SendAnimationWithContext(ctx context.Context, chatId int64, animation InputFileOrString, opts *SendAnimationOpts) (*Message, error) { v := map[string]string{} data := map[string]FileReader{} @@ -5141,7 +3503,7 @@ func (bot *Bot) SendAnimationWithContext(ctx context.Context, chatId int64, anim return &m, json.Unmarshal(r, &m) } -// SendAudioOpts is the set of optional fields for Bot.SendAudio. +// SendAudioOpts is the set of optional fields for Bot.SendAudio and Bot.SendAudioWithContext. type SendAudioOpts struct { // Unique identifier of the business connection on behalf of which the message will be sent BusinessConnectionId string @@ -5183,76 +3545,10 @@ type SendAudioOpts struct { // - audio (type InputFileOrString): Audio file to send. Pass a file_id as String to send an audio file that exists on the Telegram servers (recommended), pass an HTTP URL as a String for Telegram to get an audio file from the Internet, or upload a new one using multipart/form-data. More information on Sending Files: https://core.telegram.org/bots/api#sending-files // - opts (type SendAudioOpts): All optional parameters. func (bot *Bot) SendAudio(chatId int64, audio InputFileOrString, opts *SendAudioOpts) (*Message, error) { - v := map[string]string{} - data := map[string]FileReader{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - if audio != nil { - err := audio.Attach("audio", data) - if err != nil { - return nil, fmt.Errorf("failed to attach 'audio' input file: %w", err) - } - v["audio"] = audio.getValue() - } - if opts != nil { - v["business_connection_id"] = opts.BusinessConnectionId - if opts.MessageThreadId != 0 { - v["message_thread_id"] = strconv.FormatInt(opts.MessageThreadId, 10) - } - v["caption"] = opts.Caption - v["parse_mode"] = opts.ParseMode - if opts.CaptionEntities != nil { - bs, err := json.Marshal(opts.CaptionEntities) - if err != nil { - return nil, fmt.Errorf("failed to marshal field caption_entities: %w", err) - } - v["caption_entities"] = string(bs) - } - if opts.Duration != 0 { - v["duration"] = strconv.FormatInt(opts.Duration, 10) - } - v["performer"] = opts.Performer - v["title"] = opts.Title - if opts.Thumbnail != nil { - err := opts.Thumbnail.Attach("thumbnail", data) - if err != nil { - return nil, fmt.Errorf("failed to attach 'thumbnail' input file: %w", err) - } - v["thumbnail"] = opts.Thumbnail.getValue() - } - v["disable_notification"] = strconv.FormatBool(opts.DisableNotification) - v["protect_content"] = strconv.FormatBool(opts.ProtectContent) - v["message_effect_id"] = opts.MessageEffectId - if opts.ReplyParameters != nil { - bs, err := json.Marshal(opts.ReplyParameters) - if err != nil { - return nil, fmt.Errorf("failed to marshal field reply_parameters: %w", err) - } - v["reply_parameters"] = string(bs) - } - if opts.ReplyMarkup != nil { - bs, err := json.Marshal(opts.ReplyMarkup) - if err != nil { - return nil, fmt.Errorf("failed to marshal field reply_markup: %w", err) - } - v["reply_markup"] = string(bs) - } - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("sendAudio", v, data, reqOpts) - if err != nil { - return nil, err - } - - var m Message - return &m, json.Unmarshal(r, &m) + return bot.SendAudioWithContext(context.Background(), chatId, audio, opts) } -// SendAudioWithContext is the same as SendAudio, but with a context.Context parameter. +// SendAudioWithContext is the same as Bot.SendAudio, but with a context.Context parameter func (bot *Bot) SendAudioWithContext(ctx context.Context, chatId int64, audio InputFileOrString, opts *SendAudioOpts) (*Message, error) { v := map[string]string{} data := map[string]FileReader{} @@ -5323,7 +3619,7 @@ func (bot *Bot) SendAudioWithContext(ctx context.Context, chatId int64, audio In return &m, json.Unmarshal(r, &m) } -// SendChatActionOpts is the set of optional fields for Bot.SendChatAction. +// SendChatActionOpts is the set of optional fields for Bot.SendChatAction and Bot.SendChatActionWithContext. type SendChatActionOpts struct { // Unique identifier of the business connection on behalf of which the action will be sent BusinessConnectionId string @@ -5341,31 +3637,10 @@ type SendChatActionOpts struct { // - action (type string): Type of action to broadcast. Choose one, depending on what the user is about to receive: typing for text messages, upload_photo for photos, record_video or upload_video for videos, record_voice or upload_voice for voice notes, upload_document for general files, choose_sticker for stickers, find_location for location data, record_video_note or upload_video_note for video notes. // - opts (type SendChatActionOpts): All optional parameters. func (bot *Bot) SendChatAction(chatId int64, action string, opts *SendChatActionOpts) (bool, error) { - v := map[string]string{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - v["action"] = action - if opts != nil { - v["business_connection_id"] = opts.BusinessConnectionId - if opts.MessageThreadId != 0 { - v["message_thread_id"] = strconv.FormatInt(opts.MessageThreadId, 10) - } - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("sendChatAction", v, nil, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) + return bot.SendChatActionWithContext(context.Background(), chatId, action, opts) } -// SendChatActionWithContext is the same as SendChatAction, but with a context.Context parameter. +// SendChatActionWithContext is the same as Bot.SendChatAction, but with a context.Context parameter func (bot *Bot) SendChatActionWithContext(ctx context.Context, chatId int64, action string, opts *SendChatActionOpts) (bool, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) @@ -5391,7 +3666,7 @@ func (bot *Bot) SendChatActionWithContext(ctx context.Context, chatId int64, act return b, json.Unmarshal(r, &b) } -// SendContactOpts is the set of optional fields for Bot.SendContact. +// SendContactOpts is the set of optional fields for Bot.SendContact and Bot.SendContactWithContext. type SendContactOpts struct { // Unique identifier of the business connection on behalf of which the message will be sent BusinessConnectionId string @@ -5423,6 +3698,11 @@ type SendContactOpts struct { // - firstName (type string): Contact's first name // - opts (type SendContactOpts): All optional parameters. func (bot *Bot) SendContact(chatId int64, phoneNumber string, firstName string, opts *SendContactOpts) (*Message, error) { + return bot.SendContactWithContext(context.Background(), chatId, phoneNumber, firstName, opts) +} + +// SendContactWithContext is the same as Bot.SendContact, but with a context.Context parameter +func (bot *Bot) SendContactWithContext(ctx context.Context, chatId int64, phoneNumber string, firstName string, opts *SendContactOpts) (*Message, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) v["phone_number"] = phoneNumber @@ -5458,7 +3738,7 @@ func (bot *Bot) SendContact(chatId int64, phoneNumber string, firstName string, reqOpts = opts.RequestOpts } - r, err := bot.Request("sendContact", v, nil, reqOpts) + r, err := bot.RequestWithContext(ctx, "sendContact", v, nil, reqOpts) if err != nil { return nil, err } @@ -5467,53 +3747,7 @@ func (bot *Bot) SendContact(chatId int64, phoneNumber string, firstName string, return &m, json.Unmarshal(r, &m) } -// SendContactWithContext is the same as SendContact, but with a context.Context parameter. -func (bot *Bot) SendContactWithContext(ctx context.Context, chatId int64, phoneNumber string, firstName string, opts *SendContactOpts) (*Message, error) { - v := map[string]string{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - v["phone_number"] = phoneNumber - v["first_name"] = firstName - if opts != nil { - v["business_connection_id"] = opts.BusinessConnectionId - if opts.MessageThreadId != 0 { - v["message_thread_id"] = strconv.FormatInt(opts.MessageThreadId, 10) - } - v["last_name"] = opts.LastName - v["vcard"] = opts.Vcard - v["disable_notification"] = strconv.FormatBool(opts.DisableNotification) - v["protect_content"] = strconv.FormatBool(opts.ProtectContent) - v["message_effect_id"] = opts.MessageEffectId - if opts.ReplyParameters != nil { - bs, err := json.Marshal(opts.ReplyParameters) - if err != nil { - return nil, fmt.Errorf("failed to marshal field reply_parameters: %w", err) - } - v["reply_parameters"] = string(bs) - } - if opts.ReplyMarkup != nil { - bs, err := json.Marshal(opts.ReplyMarkup) - if err != nil { - return nil, fmt.Errorf("failed to marshal field reply_markup: %w", err) - } - v["reply_markup"] = string(bs) - } - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.RequestWithContext(ctx, "sendContact", v, nil, reqOpts) - if err != nil { - return nil, err - } - - var m Message - return &m, json.Unmarshal(r, &m) -} - -// SendDiceOpts is the set of optional fields for Bot.SendDice. +// SendDiceOpts is the set of optional fields for Bot.SendDice and Bot.SendDiceWithContext. type SendDiceOpts struct { // Unique identifier of the business connection on behalf of which the message will be sent BusinessConnectionId string @@ -5541,48 +3775,10 @@ type SendDiceOpts struct { // - chatId (type int64): Unique identifier for the target chat // - opts (type SendDiceOpts): All optional parameters. func (bot *Bot) SendDice(chatId int64, opts *SendDiceOpts) (*Message, error) { - v := map[string]string{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - if opts != nil { - v["business_connection_id"] = opts.BusinessConnectionId - if opts.MessageThreadId != 0 { - v["message_thread_id"] = strconv.FormatInt(opts.MessageThreadId, 10) - } - v["emoji"] = opts.Emoji - v["disable_notification"] = strconv.FormatBool(opts.DisableNotification) - v["protect_content"] = strconv.FormatBool(opts.ProtectContent) - v["message_effect_id"] = opts.MessageEffectId - if opts.ReplyParameters != nil { - bs, err := json.Marshal(opts.ReplyParameters) - if err != nil { - return nil, fmt.Errorf("failed to marshal field reply_parameters: %w", err) - } - v["reply_parameters"] = string(bs) - } - if opts.ReplyMarkup != nil { - bs, err := json.Marshal(opts.ReplyMarkup) - if err != nil { - return nil, fmt.Errorf("failed to marshal field reply_markup: %w", err) - } - v["reply_markup"] = string(bs) - } - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("sendDice", v, nil, reqOpts) - if err != nil { - return nil, err - } - - var m Message - return &m, json.Unmarshal(r, &m) + return bot.SendDiceWithContext(context.Background(), chatId, opts) } -// SendDiceWithContext is the same as SendDice, but with a context.Context parameter. +// SendDiceWithContext is the same as Bot.SendDice, but with a context.Context parameter func (bot *Bot) SendDiceWithContext(ctx context.Context, chatId int64, opts *SendDiceOpts) (*Message, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) @@ -5625,7 +3821,7 @@ func (bot *Bot) SendDiceWithContext(ctx context.Context, chatId int64, opts *Sen return &m, json.Unmarshal(r, &m) } -// SendDocumentOpts is the set of optional fields for Bot.SendDocument. +// SendDocumentOpts is the set of optional fields for Bot.SendDocument and Bot.SendDocumentWithContext. type SendDocumentOpts struct { // Unique identifier of the business connection on behalf of which the message will be sent BusinessConnectionId string @@ -5662,72 +3858,10 @@ type SendDocumentOpts struct { // - document (type InputFileOrString): File to send. Pass a file_id as String to send a file that exists on the Telegram servers (recommended), pass an HTTP URL as a String for Telegram to get a file from the Internet, or upload a new one using multipart/form-data. More information on Sending Files: https://core.telegram.org/bots/api#sending-files // - opts (type SendDocumentOpts): All optional parameters. func (bot *Bot) SendDocument(chatId int64, document InputFileOrString, opts *SendDocumentOpts) (*Message, error) { - v := map[string]string{} - data := map[string]FileReader{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - if document != nil { - err := document.Attach("document", data) - if err != nil { - return nil, fmt.Errorf("failed to attach 'document' input file: %w", err) - } - v["document"] = document.getValue() - } - if opts != nil { - v["business_connection_id"] = opts.BusinessConnectionId - if opts.MessageThreadId != 0 { - v["message_thread_id"] = strconv.FormatInt(opts.MessageThreadId, 10) - } - if opts.Thumbnail != nil { - err := opts.Thumbnail.Attach("thumbnail", data) - if err != nil { - return nil, fmt.Errorf("failed to attach 'thumbnail' input file: %w", err) - } - v["thumbnail"] = opts.Thumbnail.getValue() - } - v["caption"] = opts.Caption - v["parse_mode"] = opts.ParseMode - if opts.CaptionEntities != nil { - bs, err := json.Marshal(opts.CaptionEntities) - if err != nil { - return nil, fmt.Errorf("failed to marshal field caption_entities: %w", err) - } - v["caption_entities"] = string(bs) - } - v["disable_content_type_detection"] = strconv.FormatBool(opts.DisableContentTypeDetection) - v["disable_notification"] = strconv.FormatBool(opts.DisableNotification) - v["protect_content"] = strconv.FormatBool(opts.ProtectContent) - v["message_effect_id"] = opts.MessageEffectId - if opts.ReplyParameters != nil { - bs, err := json.Marshal(opts.ReplyParameters) - if err != nil { - return nil, fmt.Errorf("failed to marshal field reply_parameters: %w", err) - } - v["reply_parameters"] = string(bs) - } - if opts.ReplyMarkup != nil { - bs, err := json.Marshal(opts.ReplyMarkup) - if err != nil { - return nil, fmt.Errorf("failed to marshal field reply_markup: %w", err) - } - v["reply_markup"] = string(bs) - } - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("sendDocument", v, data, reqOpts) - if err != nil { - return nil, err - } - - var m Message - return &m, json.Unmarshal(r, &m) + return bot.SendDocumentWithContext(context.Background(), chatId, document, opts) } -// SendDocumentWithContext is the same as SendDocument, but with a context.Context parameter. +// SendDocumentWithContext is the same as Bot.SendDocument, but with a context.Context parameter func (bot *Bot) SendDocumentWithContext(ctx context.Context, chatId int64, document InputFileOrString, opts *SendDocumentOpts) (*Message, error) { v := map[string]string{} data := map[string]FileReader{} @@ -5794,7 +3928,7 @@ func (bot *Bot) SendDocumentWithContext(ctx context.Context, chatId int64, docum return &m, json.Unmarshal(r, &m) } -// SendGameOpts is the set of optional fields for Bot.SendGame. +// SendGameOpts is the set of optional fields for Bot.SendGame and Bot.SendGameWithContext. type SendGameOpts struct { // Unique identifier of the business connection on behalf of which the message will be sent BusinessConnectionId string @@ -5821,46 +3955,10 @@ type SendGameOpts struct { // - gameShortName (type string): Short name of the game, serves as the unique identifier for the game. Set up your games via @BotFather. // - opts (type SendGameOpts): All optional parameters. func (bot *Bot) SendGame(chatId int64, gameShortName string, opts *SendGameOpts) (*Message, error) { - v := map[string]string{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - v["game_short_name"] = gameShortName - if opts != nil { - v["business_connection_id"] = opts.BusinessConnectionId - if opts.MessageThreadId != 0 { - v["message_thread_id"] = strconv.FormatInt(opts.MessageThreadId, 10) - } - v["disable_notification"] = strconv.FormatBool(opts.DisableNotification) - v["protect_content"] = strconv.FormatBool(opts.ProtectContent) - v["message_effect_id"] = opts.MessageEffectId - if opts.ReplyParameters != nil { - bs, err := json.Marshal(opts.ReplyParameters) - if err != nil { - return nil, fmt.Errorf("failed to marshal field reply_parameters: %w", err) - } - v["reply_parameters"] = string(bs) - } - bs, err := json.Marshal(opts.ReplyMarkup) - if err != nil { - return nil, fmt.Errorf("failed to marshal field reply_markup: %w", err) - } - v["reply_markup"] = string(bs) - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("sendGame", v, nil, reqOpts) - if err != nil { - return nil, err - } - - var m Message - return &m, json.Unmarshal(r, &m) + return bot.SendGameWithContext(context.Background(), chatId, gameShortName, opts) } -// SendGameWithContext is the same as SendGame, but with a context.Context parameter. +// SendGameWithContext is the same as Bot.SendGame, but with a context.Context parameter func (bot *Bot) SendGameWithContext(ctx context.Context, chatId int64, gameShortName string, opts *SendGameOpts) (*Message, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) @@ -5901,7 +3999,7 @@ func (bot *Bot) SendGameWithContext(ctx context.Context, chatId int64, gameShort return &m, json.Unmarshal(r, &m) } -// SendInvoiceOpts is the set of optional fields for Bot.SendInvoice. +// SendInvoiceOpts is the set of optional fields for Bot.SendInvoice and Bot.SendInvoiceWithContext. type SendInvoiceOpts struct { // Unique identifier for the target message thread (topic) of the forum; for forum supergroups only MessageThreadId int64 @@ -5962,85 +4060,10 @@ type SendInvoiceOpts struct { // - prices (type []LabeledPrice): Price breakdown, a JSON-serialized list of components (e.g. product price, tax, discount, delivery cost, delivery tax, bonus, etc.). Must contain exactly one item for payments in Telegram Stars. // - opts (type SendInvoiceOpts): All optional parameters. func (bot *Bot) SendInvoice(chatId int64, title string, description string, payload string, currency string, prices []LabeledPrice, opts *SendInvoiceOpts) (*Message, error) { - v := map[string]string{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - v["title"] = title - v["description"] = description - v["payload"] = payload - v["currency"] = currency - if prices != nil { - bs, err := json.Marshal(prices) - if err != nil { - return nil, fmt.Errorf("failed to marshal field prices: %w", err) - } - v["prices"] = string(bs) - } - if opts != nil { - if opts.MessageThreadId != 0 { - v["message_thread_id"] = strconv.FormatInt(opts.MessageThreadId, 10) - } - v["provider_token"] = opts.ProviderToken - if opts.MaxTipAmount != 0 { - v["max_tip_amount"] = strconv.FormatInt(opts.MaxTipAmount, 10) - } - if opts.SuggestedTipAmounts != nil { - bs, err := json.Marshal(opts.SuggestedTipAmounts) - if err != nil { - return nil, fmt.Errorf("failed to marshal field suggested_tip_amounts: %w", err) - } - v["suggested_tip_amounts"] = string(bs) - } - v["start_parameter"] = opts.StartParameter - v["provider_data"] = opts.ProviderData - v["photo_url"] = opts.PhotoUrl - if opts.PhotoSize != 0 { - v["photo_size"] = strconv.FormatInt(opts.PhotoSize, 10) - } - if opts.PhotoWidth != 0 { - v["photo_width"] = strconv.FormatInt(opts.PhotoWidth, 10) - } - if opts.PhotoHeight != 0 { - v["photo_height"] = strconv.FormatInt(opts.PhotoHeight, 10) - } - v["need_name"] = strconv.FormatBool(opts.NeedName) - v["need_phone_number"] = strconv.FormatBool(opts.NeedPhoneNumber) - v["need_email"] = strconv.FormatBool(opts.NeedEmail) - v["need_shipping_address"] = strconv.FormatBool(opts.NeedShippingAddress) - v["send_phone_number_to_provider"] = strconv.FormatBool(opts.SendPhoneNumberToProvider) - v["send_email_to_provider"] = strconv.FormatBool(opts.SendEmailToProvider) - v["is_flexible"] = strconv.FormatBool(opts.IsFlexible) - v["disable_notification"] = strconv.FormatBool(opts.DisableNotification) - v["protect_content"] = strconv.FormatBool(opts.ProtectContent) - v["message_effect_id"] = opts.MessageEffectId - if opts.ReplyParameters != nil { - bs, err := json.Marshal(opts.ReplyParameters) - if err != nil { - return nil, fmt.Errorf("failed to marshal field reply_parameters: %w", err) - } - v["reply_parameters"] = string(bs) - } - bs, err := json.Marshal(opts.ReplyMarkup) - if err != nil { - return nil, fmt.Errorf("failed to marshal field reply_markup: %w", err) - } - v["reply_markup"] = string(bs) - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("sendInvoice", v, nil, reqOpts) - if err != nil { - return nil, err - } - - var m Message - return &m, json.Unmarshal(r, &m) + return bot.SendInvoiceWithContext(context.Background(), chatId, title, description, payload, currency, prices, opts) } -// SendInvoiceWithContext is the same as SendInvoice, but with a context.Context parameter. +// SendInvoiceWithContext is the same as Bot.SendInvoice, but with a context.Context parameter func (bot *Bot) SendInvoiceWithContext(ctx context.Context, chatId int64, title string, description string, payload string, currency string, prices []LabeledPrice, opts *SendInvoiceOpts) (*Message, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) @@ -6120,7 +4143,7 @@ func (bot *Bot) SendInvoiceWithContext(ctx context.Context, chatId int64, title return &m, json.Unmarshal(r, &m) } -// SendLocationOpts is the set of optional fields for Bot.SendLocation. +// SendLocationOpts is the set of optional fields for Bot.SendLocation and Bot.SendLocationWithContext. type SendLocationOpts struct { // Unique identifier of the business connection on behalf of which the message will be sent BusinessConnectionId string @@ -6156,6 +4179,11 @@ type SendLocationOpts struct { // - longitude (type float64): Longitude of the location // - opts (type SendLocationOpts): All optional parameters. func (bot *Bot) SendLocation(chatId int64, latitude float64, longitude float64, opts *SendLocationOpts) (*Message, error) { + return bot.SendLocationWithContext(context.Background(), chatId, latitude, longitude, opts) +} + +// SendLocationWithContext is the same as Bot.SendLocation, but with a context.Context parameter +func (bot *Bot) SendLocationWithContext(ctx context.Context, chatId int64, latitude float64, longitude float64, opts *SendLocationOpts) (*Message, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) v["latitude"] = strconv.FormatFloat(latitude, 'f', -1, 64) @@ -6201,7 +4229,7 @@ func (bot *Bot) SendLocation(chatId int64, latitude float64, longitude float64, reqOpts = opts.RequestOpts } - r, err := bot.Request("sendLocation", v, nil, reqOpts) + r, err := bot.RequestWithContext(ctx, "sendLocation", v, nil, reqOpts) if err != nil { return nil, err } @@ -6210,63 +4238,7 @@ func (bot *Bot) SendLocation(chatId int64, latitude float64, longitude float64, return &m, json.Unmarshal(r, &m) } -// SendLocationWithContext is the same as SendLocation, but with a context.Context parameter. -func (bot *Bot) SendLocationWithContext(ctx context.Context, chatId int64, latitude float64, longitude float64, opts *SendLocationOpts) (*Message, error) { - v := map[string]string{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - 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.MessageThreadId != 0 { - v["message_thread_id"] = strconv.FormatInt(opts.MessageThreadId, 10) - } - if opts.HorizontalAccuracy != 0.0 { - v["horizontal_accuracy"] = strconv.FormatFloat(opts.HorizontalAccuracy, 'f', -1, 64) - } - if opts.LivePeriod != 0 { - v["live_period"] = strconv.FormatInt(opts.LivePeriod, 10) - } - if opts.Heading != 0 { - v["heading"] = strconv.FormatInt(opts.Heading, 10) - } - if opts.ProximityAlertRadius != 0 { - v["proximity_alert_radius"] = strconv.FormatInt(opts.ProximityAlertRadius, 10) - } - v["disable_notification"] = strconv.FormatBool(opts.DisableNotification) - v["protect_content"] = strconv.FormatBool(opts.ProtectContent) - v["message_effect_id"] = opts.MessageEffectId - if opts.ReplyParameters != nil { - bs, err := json.Marshal(opts.ReplyParameters) - if err != nil { - return nil, fmt.Errorf("failed to marshal field reply_parameters: %w", err) - } - v["reply_parameters"] = string(bs) - } - if opts.ReplyMarkup != nil { - bs, err := json.Marshal(opts.ReplyMarkup) - if err != nil { - return nil, fmt.Errorf("failed to marshal field reply_markup: %w", err) - } - v["reply_markup"] = string(bs) - } - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.RequestWithContext(ctx, "sendLocation", v, nil, reqOpts) - if err != nil { - return nil, err - } - - var m Message - return &m, json.Unmarshal(r, &m) -} - -// SendMediaGroupOpts is the set of optional fields for Bot.SendMediaGroup. +// SendMediaGroupOpts is the set of optional fields for Bot.SendMediaGroup and Bot.SendMediaGroupWithContext. type SendMediaGroupOpts struct { // Unique identifier of the business connection on behalf of which the message will be sent BusinessConnectionId string @@ -6291,56 +4263,10 @@ type SendMediaGroupOpts struct { // - media (type []InputMedia): A JSON-serialized array describing messages to be sent, must include 2-10 items // - opts (type SendMediaGroupOpts): All optional parameters. func (bot *Bot) SendMediaGroup(chatId int64, media []InputMedia, opts *SendMediaGroupOpts) ([]Message, error) { - v := map[string]string{} - data := map[string]FileReader{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - if media != nil { - var rawList []json.RawMessage - for idx, im := range media { - inputBs, err := im.InputParams("media"+strconv.Itoa(idx), data) - if err != nil { - return nil, fmt.Errorf("failed to marshal list item %d for field media: %w", idx, err) - } - rawList = append(rawList, inputBs) - } - bs, err := json.Marshal(rawList) - if err != nil { - return nil, fmt.Errorf("failed to marshal raw json list for field: media %w", err) - } - v["media"] = string(bs) - } - if opts != nil { - v["business_connection_id"] = opts.BusinessConnectionId - if opts.MessageThreadId != 0 { - v["message_thread_id"] = strconv.FormatInt(opts.MessageThreadId, 10) - } - v["disable_notification"] = strconv.FormatBool(opts.DisableNotification) - v["protect_content"] = strconv.FormatBool(opts.ProtectContent) - v["message_effect_id"] = opts.MessageEffectId - if opts.ReplyParameters != nil { - bs, err := json.Marshal(opts.ReplyParameters) - if err != nil { - return nil, fmt.Errorf("failed to marshal field reply_parameters: %w", err) - } - v["reply_parameters"] = string(bs) - } - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("sendMediaGroup", v, data, reqOpts) - if err != nil { - return nil, err - } - - var m []Message - return m, json.Unmarshal(r, &m) + return bot.SendMediaGroupWithContext(context.Background(), chatId, media, opts) } -// SendMediaGroupWithContext is the same as SendMediaGroup, but with a context.Context parameter. +// SendMediaGroupWithContext is the same as Bot.SendMediaGroup, but with a context.Context parameter func (bot *Bot) SendMediaGroupWithContext(ctx context.Context, chatId int64, media []InputMedia, opts *SendMediaGroupOpts) ([]Message, error) { v := map[string]string{} data := map[string]FileReader{} @@ -6391,7 +4317,7 @@ func (bot *Bot) SendMediaGroupWithContext(ctx context.Context, chatId int64, med return m, json.Unmarshal(r, &m) } -// SendMessageOpts is the set of optional fields for Bot.SendMessage. +// SendMessageOpts is the set of optional fields for Bot.SendMessage and Bot.SendMessageWithContext. type SendMessageOpts struct { // Unique identifier of the business connection on behalf of which the message will be sent BusinessConnectionId string @@ -6424,63 +4350,10 @@ type SendMessageOpts struct { // - text (type string): Text of the message to be sent, 1-4096 characters after entities parsing // - opts (type SendMessageOpts): All optional parameters. func (bot *Bot) SendMessage(chatId int64, text string, opts *SendMessageOpts) (*Message, error) { - v := map[string]string{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - v["text"] = text - if opts != nil { - v["business_connection_id"] = opts.BusinessConnectionId - if opts.MessageThreadId != 0 { - v["message_thread_id"] = strconv.FormatInt(opts.MessageThreadId, 10) - } - v["parse_mode"] = opts.ParseMode - if opts.Entities != nil { - bs, err := json.Marshal(opts.Entities) - if err != nil { - return nil, fmt.Errorf("failed to marshal field entities: %w", err) - } - v["entities"] = string(bs) - } - if opts.LinkPreviewOptions != nil { - bs, err := json.Marshal(opts.LinkPreviewOptions) - if err != nil { - return nil, fmt.Errorf("failed to marshal field link_preview_options: %w", err) - } - v["link_preview_options"] = string(bs) - } - v["disable_notification"] = strconv.FormatBool(opts.DisableNotification) - v["protect_content"] = strconv.FormatBool(opts.ProtectContent) - v["message_effect_id"] = opts.MessageEffectId - if opts.ReplyParameters != nil { - bs, err := json.Marshal(opts.ReplyParameters) - if err != nil { - return nil, fmt.Errorf("failed to marshal field reply_parameters: %w", err) - } - v["reply_parameters"] = string(bs) - } - if opts.ReplyMarkup != nil { - bs, err := json.Marshal(opts.ReplyMarkup) - if err != nil { - return nil, fmt.Errorf("failed to marshal field reply_markup: %w", err) - } - v["reply_markup"] = string(bs) - } - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("sendMessage", v, nil, reqOpts) - if err != nil { - return nil, err - } - - var m Message - return &m, json.Unmarshal(r, &m) + return bot.SendMessageWithContext(context.Background(), chatId, text, opts) } -// SendMessageWithContext is the same as SendMessage, but with a context.Context parameter. +// SendMessageWithContext is the same as Bot.SendMessage, but with a context.Context parameter func (bot *Bot) SendMessageWithContext(ctx context.Context, chatId int64, text string, opts *SendMessageOpts) (*Message, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) @@ -6538,7 +4411,7 @@ func (bot *Bot) SendMessageWithContext(ctx context.Context, chatId int64, text s return &m, json.Unmarshal(r, &m) } -// SendPaidMediaOpts is the set of optional fields for Bot.SendPaidMedia. +// SendPaidMediaOpts is the set of optional fields for Bot.SendPaidMedia and Bot.SendPaidMediaWithContext. type SendPaidMediaOpts struct { // Unique identifier of the business connection on behalf of which the message will be sent BusinessConnectionId string @@ -6570,70 +4443,10 @@ type SendPaidMediaOpts struct { // - media (type []InputPaidMedia): A JSON-serialized array describing the media to be sent; up to 10 items // - opts (type SendPaidMediaOpts): All optional parameters. func (bot *Bot) SendPaidMedia(chatId int64, starCount int64, media []InputPaidMedia, opts *SendPaidMediaOpts) (*Message, error) { - v := map[string]string{} - data := map[string]FileReader{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - v["star_count"] = strconv.FormatInt(starCount, 10) - if media != nil { - var rawList []json.RawMessage - for idx, im := range media { - inputBs, err := im.InputParams("media"+strconv.Itoa(idx), data) - if err != nil { - return nil, fmt.Errorf("failed to marshal list item %d for field media: %w", idx, err) - } - rawList = append(rawList, inputBs) - } - bs, err := json.Marshal(rawList) - if err != nil { - return nil, fmt.Errorf("failed to marshal raw json list for field: media %w", err) - } - v["media"] = string(bs) - } - if opts != nil { - v["business_connection_id"] = opts.BusinessConnectionId - v["caption"] = opts.Caption - v["parse_mode"] = opts.ParseMode - if opts.CaptionEntities != nil { - bs, err := json.Marshal(opts.CaptionEntities) - if err != nil { - return nil, fmt.Errorf("failed to marshal field caption_entities: %w", err) - } - v["caption_entities"] = string(bs) - } - v["show_caption_above_media"] = strconv.FormatBool(opts.ShowCaptionAboveMedia) - v["disable_notification"] = strconv.FormatBool(opts.DisableNotification) - v["protect_content"] = strconv.FormatBool(opts.ProtectContent) - if opts.ReplyParameters != nil { - bs, err := json.Marshal(opts.ReplyParameters) - if err != nil { - return nil, fmt.Errorf("failed to marshal field reply_parameters: %w", err) - } - v["reply_parameters"] = string(bs) - } - if opts.ReplyMarkup != nil { - bs, err := json.Marshal(opts.ReplyMarkup) - if err != nil { - return nil, fmt.Errorf("failed to marshal field reply_markup: %w", err) - } - v["reply_markup"] = string(bs) - } - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("sendPaidMedia", v, data, reqOpts) - if err != nil { - return nil, err - } - - var m Message - return &m, json.Unmarshal(r, &m) + return bot.SendPaidMediaWithContext(context.Background(), chatId, starCount, media, opts) } -// SendPaidMediaWithContext is the same as SendPaidMedia, but with a context.Context parameter. +// SendPaidMediaWithContext is the same as Bot.SendPaidMedia, but with a context.Context parameter func (bot *Bot) SendPaidMediaWithContext(ctx context.Context, chatId int64, starCount int64, media []InputPaidMedia, opts *SendPaidMediaOpts) (*Message, error) { v := map[string]string{} data := map[string]FileReader{} @@ -6698,7 +4511,7 @@ func (bot *Bot) SendPaidMediaWithContext(ctx context.Context, chatId int64, star return &m, json.Unmarshal(r, &m) } -// SendPhotoOpts is the set of optional fields for Bot.SendPhoto. +// SendPhotoOpts is the set of optional fields for Bot.SendPhoto and Bot.SendPhotoWithContext. type SendPhotoOpts struct { // Unique identifier of the business connection on behalf of which the message will be sent BusinessConnectionId string @@ -6735,6 +4548,11 @@ type SendPhotoOpts struct { // - photo (type InputFileOrString): Photo to send. Pass a file_id as String to send a photo that exists on the Telegram servers (recommended), pass an HTTP URL as a String for Telegram to get a photo from the Internet, or upload a new photo using multipart/form-data. The photo must be at most 10 MB in size. The photo's width and height must not exceed 10000 in total. Width and height ratio must be at most 20. More information on Sending Files: https://core.telegram.org/bots/api#sending-files // - opts (type SendPhotoOpts): All optional parameters. func (bot *Bot) SendPhoto(chatId int64, photo InputFileOrString, opts *SendPhotoOpts) (*Message, error) { + return bot.SendPhotoWithContext(context.Background(), chatId, photo, opts) +} + +// SendPhotoWithContext is the same as Bot.SendPhoto, but with a context.Context parameter +func (bot *Bot) SendPhotoWithContext(ctx context.Context, chatId int64, photo InputFileOrString, opts *SendPhotoOpts) (*Message, error) { v := map[string]string{} data := map[string]FileReader{} v["chat_id"] = strconv.FormatInt(chatId, 10) @@ -6785,7 +4603,7 @@ func (bot *Bot) SendPhoto(chatId int64, photo InputFileOrString, opts *SendPhoto reqOpts = opts.RequestOpts } - r, err := bot.Request("sendPhoto", v, data, reqOpts) + r, err := bot.RequestWithContext(ctx, "sendPhoto", v, data, reqOpts) if err != nil { return nil, err } @@ -6794,68 +4612,7 @@ func (bot *Bot) SendPhoto(chatId int64, photo InputFileOrString, opts *SendPhoto return &m, json.Unmarshal(r, &m) } -// SendPhotoWithContext is the same as SendPhoto, but with a context.Context parameter. -func (bot *Bot) SendPhotoWithContext(ctx context.Context, chatId int64, photo InputFileOrString, opts *SendPhotoOpts) (*Message, error) { - v := map[string]string{} - data := map[string]FileReader{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - if photo != nil { - err := photo.Attach("photo", data) - if err != nil { - return nil, fmt.Errorf("failed to attach 'photo' input file: %w", err) - } - v["photo"] = photo.getValue() - } - if opts != nil { - v["business_connection_id"] = opts.BusinessConnectionId - if opts.MessageThreadId != 0 { - v["message_thread_id"] = strconv.FormatInt(opts.MessageThreadId, 10) - } - v["caption"] = opts.Caption - v["parse_mode"] = opts.ParseMode - if opts.CaptionEntities != nil { - bs, err := json.Marshal(opts.CaptionEntities) - if err != nil { - return nil, fmt.Errorf("failed to marshal field caption_entities: %w", err) - } - v["caption_entities"] = string(bs) - } - v["show_caption_above_media"] = strconv.FormatBool(opts.ShowCaptionAboveMedia) - v["has_spoiler"] = strconv.FormatBool(opts.HasSpoiler) - v["disable_notification"] = strconv.FormatBool(opts.DisableNotification) - v["protect_content"] = strconv.FormatBool(opts.ProtectContent) - v["message_effect_id"] = opts.MessageEffectId - if opts.ReplyParameters != nil { - bs, err := json.Marshal(opts.ReplyParameters) - if err != nil { - return nil, fmt.Errorf("failed to marshal field reply_parameters: %w", err) - } - v["reply_parameters"] = string(bs) - } - if opts.ReplyMarkup != nil { - bs, err := json.Marshal(opts.ReplyMarkup) - if err != nil { - return nil, fmt.Errorf("failed to marshal field reply_markup: %w", err) - } - v["reply_markup"] = string(bs) - } - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.RequestWithContext(ctx, "sendPhoto", v, data, reqOpts) - if err != nil { - return nil, err - } - - var m Message - return &m, json.Unmarshal(r, &m) -} - -// SendPollOpts is the set of optional fields for Bot.SendPoll. +// SendPollOpts is the set of optional fields for Bot.SendPoll and Bot.SendPollWithContext. type SendPollOpts struct { // Unique identifier of the business connection on behalf of which the message will be sent BusinessConnectionId string @@ -6907,86 +4664,10 @@ type SendPollOpts struct { // - options (type []InputPollOption): A JSON-serialized list of 2-10 answer options // - opts (type SendPollOpts): All optional parameters. func (bot *Bot) SendPoll(chatId int64, question string, options []InputPollOption, opts *SendPollOpts) (*Message, error) { - v := map[string]string{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - v["question"] = question - if options != nil { - bs, err := json.Marshal(options) - if err != nil { - return nil, fmt.Errorf("failed to marshal field options: %w", err) - } - v["options"] = string(bs) - } - if opts != nil { - v["business_connection_id"] = opts.BusinessConnectionId - if opts.MessageThreadId != 0 { - v["message_thread_id"] = strconv.FormatInt(opts.MessageThreadId, 10) - } - v["question_parse_mode"] = opts.QuestionParseMode - if opts.QuestionEntities != nil { - bs, err := json.Marshal(opts.QuestionEntities) - if err != nil { - return nil, fmt.Errorf("failed to marshal field question_entities: %w", err) - } - v["question_entities"] = string(bs) - } - v["is_anonymous"] = strconv.FormatBool(opts.IsAnonymous) - v["type"] = opts.Type - v["allows_multiple_answers"] = strconv.FormatBool(opts.AllowsMultipleAnswers) - if opts.Type == "quiz" { - // correct_option_id should always be set when the type is "quiz" - it doesnt need to be set for type "regular". - v["correct_option_id"] = strconv.FormatInt(opts.CorrectOptionId, 10) - } - v["explanation"] = opts.Explanation - v["explanation_parse_mode"] = opts.ExplanationParseMode - if opts.ExplanationEntities != nil { - bs, err := json.Marshal(opts.ExplanationEntities) - if err != nil { - return nil, fmt.Errorf("failed to marshal field explanation_entities: %w", err) - } - v["explanation_entities"] = string(bs) - } - if opts.OpenPeriod != 0 { - v["open_period"] = strconv.FormatInt(opts.OpenPeriod, 10) - } - if opts.CloseDate != 0 { - v["close_date"] = strconv.FormatInt(opts.CloseDate, 10) - } - v["is_closed"] = strconv.FormatBool(opts.IsClosed) - v["disable_notification"] = strconv.FormatBool(opts.DisableNotification) - v["protect_content"] = strconv.FormatBool(opts.ProtectContent) - v["message_effect_id"] = opts.MessageEffectId - if opts.ReplyParameters != nil { - bs, err := json.Marshal(opts.ReplyParameters) - if err != nil { - return nil, fmt.Errorf("failed to marshal field reply_parameters: %w", err) - } - v["reply_parameters"] = string(bs) - } - if opts.ReplyMarkup != nil { - bs, err := json.Marshal(opts.ReplyMarkup) - if err != nil { - return nil, fmt.Errorf("failed to marshal field reply_markup: %w", err) - } - v["reply_markup"] = string(bs) - } - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("sendPoll", v, nil, reqOpts) - if err != nil { - return nil, err - } - - var m Message - return &m, json.Unmarshal(r, &m) + return bot.SendPollWithContext(context.Background(), chatId, question, options, opts) } -// SendPollWithContext is the same as SendPoll, but with a context.Context parameter. +// SendPollWithContext is the same as Bot.SendPoll, but with a context.Context parameter func (bot *Bot) SendPollWithContext(ctx context.Context, chatId int64, question string, options []InputPollOption, opts *SendPollOpts) (*Message, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) @@ -7067,7 +4748,7 @@ func (bot *Bot) SendPollWithContext(ctx context.Context, chatId int64, question return &m, json.Unmarshal(r, &m) } -// SendStickerOpts is the set of optional fields for Bot.SendSticker. +// SendStickerOpts is the set of optional fields for Bot.SendSticker and Bot.SendStickerWithContext. type SendStickerOpts struct { // Unique identifier of the business connection on behalf of which the message will be sent BusinessConnectionId string @@ -7096,56 +4777,10 @@ type SendStickerOpts struct { // - sticker (type InputFileOrString): Sticker to send. Pass a file_id as String to send a file that exists on the Telegram servers (recommended), pass an HTTP URL as a String for Telegram to get a .WEBP sticker from the Internet, or upload a new .WEBP, .TGS, or .WEBM sticker using multipart/form-data. More information on Sending Files: https://core.telegram.org/bots/api#sending-files. Video and animated stickers can't be sent via an HTTP URL. // - opts (type SendStickerOpts): All optional parameters. func (bot *Bot) SendSticker(chatId int64, sticker InputFileOrString, opts *SendStickerOpts) (*Message, error) { - v := map[string]string{} - data := map[string]FileReader{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - if sticker != nil { - err := sticker.Attach("sticker", data) - if err != nil { - return nil, fmt.Errorf("failed to attach 'sticker' input file: %w", err) - } - v["sticker"] = sticker.getValue() - } - if opts != nil { - v["business_connection_id"] = opts.BusinessConnectionId - if opts.MessageThreadId != 0 { - v["message_thread_id"] = strconv.FormatInt(opts.MessageThreadId, 10) - } - v["emoji"] = opts.Emoji - v["disable_notification"] = strconv.FormatBool(opts.DisableNotification) - v["protect_content"] = strconv.FormatBool(opts.ProtectContent) - v["message_effect_id"] = opts.MessageEffectId - if opts.ReplyParameters != nil { - bs, err := json.Marshal(opts.ReplyParameters) - if err != nil { - return nil, fmt.Errorf("failed to marshal field reply_parameters: %w", err) - } - v["reply_parameters"] = string(bs) - } - if opts.ReplyMarkup != nil { - bs, err := json.Marshal(opts.ReplyMarkup) - if err != nil { - return nil, fmt.Errorf("failed to marshal field reply_markup: %w", err) - } - v["reply_markup"] = string(bs) - } - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("sendSticker", v, data, reqOpts) - if err != nil { - return nil, err - } - - var m Message - return &m, json.Unmarshal(r, &m) + return bot.SendStickerWithContext(context.Background(), chatId, sticker, opts) } -// SendStickerWithContext is the same as SendSticker, but with a context.Context parameter. +// SendStickerWithContext is the same as Bot.SendSticker, but with a context.Context parameter func (bot *Bot) SendStickerWithContext(ctx context.Context, chatId int64, sticker InputFileOrString, opts *SendStickerOpts) (*Message, error) { v := map[string]string{} data := map[string]FileReader{} @@ -7196,7 +4831,7 @@ func (bot *Bot) SendStickerWithContext(ctx context.Context, chatId int64, sticke return &m, json.Unmarshal(r, &m) } -// SendVenueOpts is the set of optional fields for Bot.SendVenue. +// SendVenueOpts is the set of optional fields for Bot.SendVenue and Bot.SendVenueWithContext. type SendVenueOpts struct { // Unique identifier of the business connection on behalf of which the message will be sent BusinessConnectionId string @@ -7234,55 +4869,10 @@ type SendVenueOpts struct { // - address (type string): Address of the venue // - opts (type SendVenueOpts): All optional parameters. func (bot *Bot) SendVenue(chatId int64, latitude float64, longitude float64, title string, address string, opts *SendVenueOpts) (*Message, error) { - v := map[string]string{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - v["latitude"] = strconv.FormatFloat(latitude, 'f', -1, 64) - v["longitude"] = strconv.FormatFloat(longitude, 'f', -1, 64) - v["title"] = title - v["address"] = address - if opts != nil { - v["business_connection_id"] = opts.BusinessConnectionId - if opts.MessageThreadId != 0 { - v["message_thread_id"] = strconv.FormatInt(opts.MessageThreadId, 10) - } - v["foursquare_id"] = opts.FoursquareId - v["foursquare_type"] = opts.FoursquareType - v["google_place_id"] = opts.GooglePlaceId - v["google_place_type"] = opts.GooglePlaceType - v["disable_notification"] = strconv.FormatBool(opts.DisableNotification) - v["protect_content"] = strconv.FormatBool(opts.ProtectContent) - v["message_effect_id"] = opts.MessageEffectId - if opts.ReplyParameters != nil { - bs, err := json.Marshal(opts.ReplyParameters) - if err != nil { - return nil, fmt.Errorf("failed to marshal field reply_parameters: %w", err) - } - v["reply_parameters"] = string(bs) - } - if opts.ReplyMarkup != nil { - bs, err := json.Marshal(opts.ReplyMarkup) - if err != nil { - return nil, fmt.Errorf("failed to marshal field reply_markup: %w", err) - } - v["reply_markup"] = string(bs) - } - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("sendVenue", v, nil, reqOpts) - if err != nil { - return nil, err - } - - var m Message - return &m, json.Unmarshal(r, &m) + return bot.SendVenueWithContext(context.Background(), chatId, latitude, longitude, title, address, opts) } -// SendVenueWithContext is the same as SendVenue, but with a context.Context parameter. +// SendVenueWithContext is the same as Bot.SendVenue, but with a context.Context parameter func (bot *Bot) SendVenueWithContext(ctx context.Context, chatId int64, latitude float64, longitude float64, title string, address string, opts *SendVenueOpts) (*Message, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) @@ -7332,7 +4922,7 @@ func (bot *Bot) SendVenueWithContext(ctx context.Context, chatId int64, latitude return &m, json.Unmarshal(r, &m) } -// SendVideoOpts is the set of optional fields for Bot.SendVideo. +// SendVideoOpts is the set of optional fields for Bot.SendVideo and Bot.SendVideoWithContext. type SendVideoOpts struct { // Unique identifier of the business connection on behalf of which the message will be sent BusinessConnectionId string @@ -7379,83 +4969,10 @@ type SendVideoOpts struct { // - video (type InputFileOrString): Video to send. Pass a file_id as String to send a video that exists on the Telegram servers (recommended), pass an HTTP URL as a String for Telegram to get a video from the Internet, or upload a new video using multipart/form-data. More information on Sending Files: https://core.telegram.org/bots/api#sending-files // - opts (type SendVideoOpts): All optional parameters. func (bot *Bot) SendVideo(chatId int64, video InputFileOrString, opts *SendVideoOpts) (*Message, error) { - v := map[string]string{} - data := map[string]FileReader{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - if video != nil { - err := video.Attach("video", data) - if err != nil { - return nil, fmt.Errorf("failed to attach 'video' input file: %w", err) - } - v["video"] = video.getValue() - } - if opts != nil { - v["business_connection_id"] = opts.BusinessConnectionId - if opts.MessageThreadId != 0 { - v["message_thread_id"] = strconv.FormatInt(opts.MessageThreadId, 10) - } - if opts.Duration != 0 { - v["duration"] = strconv.FormatInt(opts.Duration, 10) - } - if opts.Width != 0 { - v["width"] = strconv.FormatInt(opts.Width, 10) - } - if opts.Height != 0 { - v["height"] = strconv.FormatInt(opts.Height, 10) - } - if opts.Thumbnail != nil { - err := opts.Thumbnail.Attach("thumbnail", data) - if err != nil { - return nil, fmt.Errorf("failed to attach 'thumbnail' input file: %w", err) - } - v["thumbnail"] = opts.Thumbnail.getValue() - } - v["caption"] = opts.Caption - v["parse_mode"] = opts.ParseMode - if opts.CaptionEntities != nil { - bs, err := json.Marshal(opts.CaptionEntities) - if err != nil { - return nil, fmt.Errorf("failed to marshal field caption_entities: %w", err) - } - v["caption_entities"] = string(bs) - } - v["show_caption_above_media"] = strconv.FormatBool(opts.ShowCaptionAboveMedia) - v["has_spoiler"] = strconv.FormatBool(opts.HasSpoiler) - v["supports_streaming"] = strconv.FormatBool(opts.SupportsStreaming) - v["disable_notification"] = strconv.FormatBool(opts.DisableNotification) - v["protect_content"] = strconv.FormatBool(opts.ProtectContent) - v["message_effect_id"] = opts.MessageEffectId - if opts.ReplyParameters != nil { - bs, err := json.Marshal(opts.ReplyParameters) - if err != nil { - return nil, fmt.Errorf("failed to marshal field reply_parameters: %w", err) - } - v["reply_parameters"] = string(bs) - } - if opts.ReplyMarkup != nil { - bs, err := json.Marshal(opts.ReplyMarkup) - if err != nil { - return nil, fmt.Errorf("failed to marshal field reply_markup: %w", err) - } - v["reply_markup"] = string(bs) - } - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("sendVideo", v, data, reqOpts) - if err != nil { - return nil, err - } - - var m Message - return &m, json.Unmarshal(r, &m) + return bot.SendVideoWithContext(context.Background(), chatId, video, opts) } -// SendVideoWithContext is the same as SendVideo, but with a context.Context parameter. +// SendVideoWithContext is the same as Bot.SendVideo, but with a context.Context parameter func (bot *Bot) SendVideoWithContext(ctx context.Context, chatId int64, video InputFileOrString, opts *SendVideoOpts) (*Message, error) { v := map[string]string{} data := map[string]FileReader{} @@ -7533,7 +5050,7 @@ func (bot *Bot) SendVideoWithContext(ctx context.Context, chatId int64, video In return &m, json.Unmarshal(r, &m) } -// SendVideoNoteOpts is the set of optional fields for Bot.SendVideoNote. +// SendVideoNoteOpts is the set of optional fields for Bot.SendVideoNote and Bot.SendVideoNoteWithContext. type SendVideoNoteOpts struct { // Unique identifier of the business connection on behalf of which the message will be sent BusinessConnectionId string @@ -7566,68 +5083,10 @@ type SendVideoNoteOpts struct { // - videoNote (type InputFileOrString): Video note to send. Pass a file_id as String to send a video note that exists on the Telegram servers (recommended) or upload a new video using multipart/form-data. More information on Sending Files: https://core.telegram.org/bots/api#sending-files. Sending video notes by a URL is currently unsupported // - opts (type SendVideoNoteOpts): All optional parameters. func (bot *Bot) SendVideoNote(chatId int64, videoNote InputFileOrString, opts *SendVideoNoteOpts) (*Message, error) { - v := map[string]string{} - data := map[string]FileReader{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - if videoNote != nil { - err := videoNote.Attach("video_note", data) - if err != nil { - return nil, fmt.Errorf("failed to attach 'video_note' input file: %w", err) - } - v["video_note"] = videoNote.getValue() - } - if opts != nil { - v["business_connection_id"] = opts.BusinessConnectionId - if opts.MessageThreadId != 0 { - v["message_thread_id"] = strconv.FormatInt(opts.MessageThreadId, 10) - } - if opts.Duration != 0 { - v["duration"] = strconv.FormatInt(opts.Duration, 10) - } - if opts.Length != 0 { - v["length"] = strconv.FormatInt(opts.Length, 10) - } - if opts.Thumbnail != nil { - err := opts.Thumbnail.Attach("thumbnail", data) - if err != nil { - return nil, fmt.Errorf("failed to attach 'thumbnail' input file: %w", err) - } - v["thumbnail"] = opts.Thumbnail.getValue() - } - v["disable_notification"] = strconv.FormatBool(opts.DisableNotification) - v["protect_content"] = strconv.FormatBool(opts.ProtectContent) - v["message_effect_id"] = opts.MessageEffectId - if opts.ReplyParameters != nil { - bs, err := json.Marshal(opts.ReplyParameters) - if err != nil { - return nil, fmt.Errorf("failed to marshal field reply_parameters: %w", err) - } - v["reply_parameters"] = string(bs) - } - if opts.ReplyMarkup != nil { - bs, err := json.Marshal(opts.ReplyMarkup) - if err != nil { - return nil, fmt.Errorf("failed to marshal field reply_markup: %w", err) - } - v["reply_markup"] = string(bs) - } - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("sendVideoNote", v, data, reqOpts) - if err != nil { - return nil, err - } - - var m Message - return &m, json.Unmarshal(r, &m) + return bot.SendVideoNoteWithContext(context.Background(), chatId, videoNote, opts) } -// SendVideoNoteWithContext is the same as SendVideoNote, but with a context.Context parameter. +// SendVideoNoteWithContext is the same as Bot.SendVideoNote, but with a context.Context parameter func (bot *Bot) SendVideoNoteWithContext(ctx context.Context, chatId int64, videoNote InputFileOrString, opts *SendVideoNoteOpts) (*Message, error) { v := map[string]string{} data := map[string]FileReader{} @@ -7681,102 +5140,7 @@ func (bot *Bot) SendVideoNoteWithContext(ctx context.Context, chatId int64, vide reqOpts = opts.RequestOpts } - r, err := bot.RequestWithContext(ctx, "sendVideoNote", v, data, reqOpts) - if err != nil { - return nil, err - } - - var m Message - return &m, json.Unmarshal(r, &m) -} - -// SendVoiceOpts is the set of optional fields for Bot.SendVoice. -type SendVoiceOpts struct { - // Unique identifier of the business connection on behalf of which the message will be sent - BusinessConnectionId string - // Unique identifier for the target message thread (topic) of the forum; for forum supergroups only - MessageThreadId int64 - // Voice message caption, 0-1024 characters after entities parsing - Caption string - // Mode for parsing entities in the voice message caption. See formatting options for more details. - ParseMode string - // A JSON-serialized list of special entities that appear in the caption, which can be specified instead of parse_mode - CaptionEntities []MessageEntity - // Duration of the voice message in seconds - Duration int64 - // Sends the message silently. Users will receive a notification with no sound. - DisableNotification bool - // Protects the contents of the sent message from forwarding and saving - ProtectContent bool - // Unique identifier of the message effect to be added to the message; for private chats only - MessageEffectId string - // Description of the message to reply to - ReplyParameters *ReplyParameters - // Additional interface options. A JSON-serialized object for an inline keyboard, custom reply keyboard, instructions to remove a reply keyboard or to force a reply from the user - ReplyMarkup ReplyMarkup - // RequestOpts are an additional optional field to configure timeouts for individual requests - RequestOpts *RequestOpts -} - -// SendVoice (https://core.telegram.org/bots/api#sendvoice) -// -// Use this method to send audio files, if you want Telegram clients to display the file as a playable voice message. For this to work, your audio must be in an .OGG file encoded with OPUS, or in .MP3 format, or in .M4A format (other formats may be sent as Audio or Document). On success, the sent Message is returned. Bots can currently send voice messages of up to 50 MB in size, this limit may be changed in the future. -// - chatId (type int64): Unique identifier for the target chat -// - voice (type InputFileOrString): Audio file to send. Pass a file_id as String to send a file that exists on the Telegram servers (recommended), pass an HTTP URL as a String for Telegram to get a file from the Internet, or upload a new one using multipart/form-data. More information on Sending Files: https://core.telegram.org/bots/api#sending-files -// - opts (type SendVoiceOpts): All optional parameters. -func (bot *Bot) SendVoice(chatId int64, voice InputFileOrString, opts *SendVoiceOpts) (*Message, error) { - v := map[string]string{} - data := map[string]FileReader{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - if voice != nil { - err := voice.Attach("voice", data) - if err != nil { - return nil, fmt.Errorf("failed to attach 'voice' input file: %w", err) - } - v["voice"] = voice.getValue() - } - if opts != nil { - v["business_connection_id"] = opts.BusinessConnectionId - if opts.MessageThreadId != 0 { - v["message_thread_id"] = strconv.FormatInt(opts.MessageThreadId, 10) - } - v["caption"] = opts.Caption - v["parse_mode"] = opts.ParseMode - if opts.CaptionEntities != nil { - bs, err := json.Marshal(opts.CaptionEntities) - if err != nil { - return nil, fmt.Errorf("failed to marshal field caption_entities: %w", err) - } - v["caption_entities"] = string(bs) - } - if opts.Duration != 0 { - v["duration"] = strconv.FormatInt(opts.Duration, 10) - } - v["disable_notification"] = strconv.FormatBool(opts.DisableNotification) - v["protect_content"] = strconv.FormatBool(opts.ProtectContent) - v["message_effect_id"] = opts.MessageEffectId - if opts.ReplyParameters != nil { - bs, err := json.Marshal(opts.ReplyParameters) - if err != nil { - return nil, fmt.Errorf("failed to marshal field reply_parameters: %w", err) - } - v["reply_parameters"] = string(bs) - } - if opts.ReplyMarkup != nil { - bs, err := json.Marshal(opts.ReplyMarkup) - if err != nil { - return nil, fmt.Errorf("failed to marshal field reply_markup: %w", err) - } - v["reply_markup"] = string(bs) - } - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("sendVoice", v, data, reqOpts) + r, err := bot.RequestWithContext(ctx, "sendVideoNote", v, data, reqOpts) if err != nil { return nil, err } @@ -7785,7 +5149,45 @@ func (bot *Bot) SendVoice(chatId int64, voice InputFileOrString, opts *SendVoice return &m, json.Unmarshal(r, &m) } -// SendVoiceWithContext is the same as SendVoice, but with a context.Context parameter. +// SendVoiceOpts is the set of optional fields for Bot.SendVoice and Bot.SendVoiceWithContext. +type SendVoiceOpts struct { + // Unique identifier of the business connection on behalf of which the message will be sent + BusinessConnectionId string + // Unique identifier for the target message thread (topic) of the forum; for forum supergroups only + MessageThreadId int64 + // Voice message caption, 0-1024 characters after entities parsing + Caption string + // Mode for parsing entities in the voice message caption. See formatting options for more details. + ParseMode string + // A JSON-serialized list of special entities that appear in the caption, which can be specified instead of parse_mode + CaptionEntities []MessageEntity + // Duration of the voice message in seconds + Duration int64 + // Sends the message silently. Users will receive a notification with no sound. + DisableNotification bool + // Protects the contents of the sent message from forwarding and saving + ProtectContent bool + // Unique identifier of the message effect to be added to the message; for private chats only + MessageEffectId string + // Description of the message to reply to + ReplyParameters *ReplyParameters + // Additional interface options. A JSON-serialized object for an inline keyboard, custom reply keyboard, instructions to remove a reply keyboard or to force a reply from the user + ReplyMarkup ReplyMarkup + // RequestOpts are an additional optional field to configure timeouts for individual requests + RequestOpts *RequestOpts +} + +// SendVoice (https://core.telegram.org/bots/api#sendvoice) +// +// Use this method to send audio files, if you want Telegram clients to display the file as a playable voice message. For this to work, your audio must be in an .OGG file encoded with OPUS, or in .MP3 format, or in .M4A format (other formats may be sent as Audio or Document). On success, the sent Message is returned. Bots can currently send voice messages of up to 50 MB in size, this limit may be changed in the future. +// - chatId (type int64): Unique identifier for the target chat +// - voice (type InputFileOrString): Audio file to send. Pass a file_id as String to send a file that exists on the Telegram servers (recommended), pass an HTTP URL as a String for Telegram to get a file from the Internet, or upload a new one using multipart/form-data. More information on Sending Files: https://core.telegram.org/bots/api#sending-files +// - opts (type SendVoiceOpts): All optional parameters. +func (bot *Bot) SendVoice(chatId int64, voice InputFileOrString, opts *SendVoiceOpts) (*Message, error) { + return bot.SendVoiceWithContext(context.Background(), chatId, voice, opts) +} + +// SendVoiceWithContext is the same as Bot.SendVoice, but with a context.Context parameter func (bot *Bot) SendVoiceWithContext(ctx context.Context, chatId int64, voice InputFileOrString, opts *SendVoiceOpts) (*Message, error) { v := map[string]string{} data := map[string]FileReader{} @@ -7847,7 +5249,7 @@ func (bot *Bot) SendVoiceWithContext(ctx context.Context, chatId int64, voice In return &m, json.Unmarshal(r, &m) } -// SetChatAdministratorCustomTitleOpts is the set of optional fields for Bot.SetChatAdministratorCustomTitle. +// SetChatAdministratorCustomTitleOpts is the set of optional fields for Bot.SetChatAdministratorCustomTitle and Bot.SetChatAdministratorCustomTitleWithContext. type SetChatAdministratorCustomTitleOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests RequestOpts *RequestOpts @@ -7861,26 +5263,10 @@ type SetChatAdministratorCustomTitleOpts struct { // - customTitle (type string): New custom title for the administrator; 0-16 characters, emoji are not allowed // - opts (type SetChatAdministratorCustomTitleOpts): All optional parameters. func (bot *Bot) SetChatAdministratorCustomTitle(chatId int64, userId int64, customTitle string, opts *SetChatAdministratorCustomTitleOpts) (bool, error) { - v := map[string]string{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - v["user_id"] = strconv.FormatInt(userId, 10) - v["custom_title"] = customTitle - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("setChatAdministratorCustomTitle", v, nil, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) + return bot.SetChatAdministratorCustomTitleWithContext(context.Background(), chatId, userId, customTitle, opts) } -// SetChatAdministratorCustomTitleWithContext is the same as SetChatAdministratorCustomTitle, but with a context.Context parameter. +// SetChatAdministratorCustomTitleWithContext is the same as Bot.SetChatAdministratorCustomTitle, but with a context.Context parameter func (bot *Bot) SetChatAdministratorCustomTitleWithContext(ctx context.Context, chatId int64, userId int64, customTitle string, opts *SetChatAdministratorCustomTitleOpts) (bool, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) @@ -7901,7 +5287,7 @@ func (bot *Bot) SetChatAdministratorCustomTitleWithContext(ctx context.Context, return b, json.Unmarshal(r, &b) } -// SetChatDescriptionOpts is the set of optional fields for Bot.SetChatDescription. +// SetChatDescriptionOpts is the set of optional fields for Bot.SetChatDescription and Bot.SetChatDescriptionWithContext. type SetChatDescriptionOpts struct { // New chat description, 0-255 characters Description string @@ -7915,27 +5301,10 @@ type SetChatDescriptionOpts struct { // - chatId (type int64): Unique identifier for the target chat // - opts (type SetChatDescriptionOpts): All optional parameters. func (bot *Bot) SetChatDescription(chatId int64, opts *SetChatDescriptionOpts) (bool, error) { - v := map[string]string{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - if opts != nil { - v["description"] = opts.Description - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("setChatDescription", v, nil, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) + return bot.SetChatDescriptionWithContext(context.Background(), chatId, opts) } -// SetChatDescriptionWithContext is the same as SetChatDescription, but with a context.Context parameter. +// SetChatDescriptionWithContext is the same as Bot.SetChatDescription, but with a context.Context parameter func (bot *Bot) SetChatDescriptionWithContext(ctx context.Context, chatId int64, opts *SetChatDescriptionOpts) (bool, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) @@ -7957,7 +5326,7 @@ func (bot *Bot) SetChatDescriptionWithContext(ctx context.Context, chatId int64, return b, json.Unmarshal(r, &b) } -// SetChatMenuButtonOpts is the set of optional fields for Bot.SetChatMenuButton. +// SetChatMenuButtonOpts is the set of optional fields for Bot.SetChatMenuButton and Bot.SetChatMenuButtonWithContext. type SetChatMenuButtonOpts struct { // Unique identifier for the target private chat. If not specified, default bot's menu button will be changed ChatId *int64 @@ -7972,33 +5341,10 @@ type SetChatMenuButtonOpts struct { // Use this method to change the bot's menu button in a private chat, or the default menu button. Returns True on success. // - opts (type SetChatMenuButtonOpts): All optional parameters. func (bot *Bot) SetChatMenuButton(opts *SetChatMenuButtonOpts) (bool, error) { - v := map[string]string{} - if opts != nil { - if opts.ChatId != nil { - v["chat_id"] = strconv.FormatInt(*opts.ChatId, 10) - } - bs, err := json.Marshal(opts.MenuButton) - if err != nil { - return false, fmt.Errorf("failed to marshal field menu_button: %w", err) - } - v["menu_button"] = string(bs) - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("setChatMenuButton", v, nil, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) + return bot.SetChatMenuButtonWithContext(context.Background(), opts) } -// SetChatMenuButtonWithContext is the same as SetChatMenuButton, but with a context.Context parameter. +// SetChatMenuButtonWithContext is the same as Bot.SetChatMenuButton, but with a context.Context parameter func (bot *Bot) SetChatMenuButtonWithContext(ctx context.Context, opts *SetChatMenuButtonOpts) (bool, error) { v := map[string]string{} if opts != nil { @@ -8026,7 +5372,7 @@ func (bot *Bot) SetChatMenuButtonWithContext(ctx context.Context, opts *SetChatM return b, json.Unmarshal(r, &b) } -// SetChatPermissionsOpts is the set of optional fields for Bot.SetChatPermissions. +// SetChatPermissionsOpts is the set of optional fields for Bot.SetChatPermissions and Bot.SetChatPermissionsWithContext. type SetChatPermissionsOpts struct { // Pass True if chat permissions are set independently. Otherwise, the can_send_other_messages and can_add_web_page_previews permissions will imply the can_send_messages, can_send_audios, can_send_documents, can_send_photos, can_send_videos, can_send_video_notes, and can_send_voice_notes permissions; the can_send_polls permission will imply the can_send_messages permission. UseIndependentChatPermissions bool @@ -8041,32 +5387,10 @@ type SetChatPermissionsOpts struct { // - permissions (type ChatPermissions): A JSON-serialized object for new default chat permissions // - opts (type SetChatPermissionsOpts): All optional parameters. func (bot *Bot) SetChatPermissions(chatId int64, permissions ChatPermissions, opts *SetChatPermissionsOpts) (bool, error) { - v := map[string]string{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - bs, err := json.Marshal(permissions) - if err != nil { - return false, fmt.Errorf("failed to marshal field permissions: %w", err) - } - v["permissions"] = string(bs) - if opts != nil { - v["use_independent_chat_permissions"] = strconv.FormatBool(opts.UseIndependentChatPermissions) - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("setChatPermissions", v, nil, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) + return bot.SetChatPermissionsWithContext(context.Background(), chatId, permissions, opts) } -// SetChatPermissionsWithContext is the same as SetChatPermissions, but with a context.Context parameter. +// SetChatPermissionsWithContext is the same as Bot.SetChatPermissions, but with a context.Context parameter func (bot *Bot) SetChatPermissionsWithContext(ctx context.Context, chatId int64, permissions ChatPermissions, opts *SetChatPermissionsOpts) (bool, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) @@ -8093,7 +5417,7 @@ func (bot *Bot) SetChatPermissionsWithContext(ctx context.Context, chatId int64, return b, json.Unmarshal(r, &b) } -// SetChatPhotoOpts is the set of optional fields for Bot.SetChatPhoto. +// SetChatPhotoOpts is the set of optional fields for Bot.SetChatPhoto and Bot.SetChatPhotoWithContext. type SetChatPhotoOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests RequestOpts *RequestOpts @@ -8106,32 +5430,10 @@ type SetChatPhotoOpts struct { // - photo (type InputFile): New chat photo, uploaded using multipart/form-data // - opts (type SetChatPhotoOpts): All optional parameters. func (bot *Bot) SetChatPhoto(chatId int64, photo InputFile, opts *SetChatPhotoOpts) (bool, error) { - v := map[string]string{} - data := map[string]FileReader{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - if photo != nil { - err := photo.Attach("photo", data) - if err != nil { - return false, fmt.Errorf("failed to attach 'photo' input file: %w", err) - } - v["photo"] = photo.getValue() - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("setChatPhoto", v, data, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) + return bot.SetChatPhotoWithContext(context.Background(), chatId, photo, opts) } -// SetChatPhotoWithContext is the same as SetChatPhoto, but with a context.Context parameter. +// SetChatPhotoWithContext is the same as Bot.SetChatPhoto, but with a context.Context parameter func (bot *Bot) SetChatPhotoWithContext(ctx context.Context, chatId int64, photo InputFile, opts *SetChatPhotoOpts) (bool, error) { v := map[string]string{} data := map[string]FileReader{} @@ -8158,7 +5460,7 @@ func (bot *Bot) SetChatPhotoWithContext(ctx context.Context, chatId int64, photo return b, json.Unmarshal(r, &b) } -// SetChatStickerSetOpts is the set of optional fields for Bot.SetChatStickerSet. +// SetChatStickerSetOpts is the set of optional fields for Bot.SetChatStickerSet and Bot.SetChatStickerSetWithContext. type SetChatStickerSetOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests RequestOpts *RequestOpts @@ -8171,25 +5473,10 @@ type SetChatStickerSetOpts struct { // - stickerSetName (type string): Name of the sticker set to be set as the group sticker set // - opts (type SetChatStickerSetOpts): All optional parameters. func (bot *Bot) SetChatStickerSet(chatId int64, stickerSetName string, opts *SetChatStickerSetOpts) (bool, error) { - v := map[string]string{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - v["sticker_set_name"] = stickerSetName - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("setChatStickerSet", v, nil, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) + return bot.SetChatStickerSetWithContext(context.Background(), chatId, stickerSetName, opts) } -// SetChatStickerSetWithContext is the same as SetChatStickerSet, but with a context.Context parameter. +// SetChatStickerSetWithContext is the same as Bot.SetChatStickerSet, but with a context.Context parameter func (bot *Bot) SetChatStickerSetWithContext(ctx context.Context, chatId int64, stickerSetName string, opts *SetChatStickerSetOpts) (bool, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) @@ -8209,7 +5496,7 @@ func (bot *Bot) SetChatStickerSetWithContext(ctx context.Context, chatId int64, return b, json.Unmarshal(r, &b) } -// SetChatTitleOpts is the set of optional fields for Bot.SetChatTitle. +// SetChatTitleOpts is the set of optional fields for Bot.SetChatTitle and Bot.SetChatTitleWithContext. type SetChatTitleOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests RequestOpts *RequestOpts @@ -8222,25 +5509,10 @@ type SetChatTitleOpts struct { // - title (type string): New chat title, 1-128 characters // - opts (type SetChatTitleOpts): All optional parameters. func (bot *Bot) SetChatTitle(chatId int64, title string, opts *SetChatTitleOpts) (bool, error) { - v := map[string]string{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - v["title"] = title - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("setChatTitle", v, nil, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) + return bot.SetChatTitleWithContext(context.Background(), chatId, title, opts) } -// SetChatTitleWithContext is the same as SetChatTitle, but with a context.Context parameter. +// SetChatTitleWithContext is the same as Bot.SetChatTitle, but with a context.Context parameter func (bot *Bot) SetChatTitleWithContext(ctx context.Context, chatId int64, title string, opts *SetChatTitleOpts) (bool, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) @@ -8260,7 +5532,7 @@ func (bot *Bot) SetChatTitleWithContext(ctx context.Context, chatId int64, title return b, json.Unmarshal(r, &b) } -// SetCustomEmojiStickerSetThumbnailOpts is the set of optional fields for Bot.SetCustomEmojiStickerSetThumbnail. +// SetCustomEmojiStickerSetThumbnailOpts is the set of optional fields for Bot.SetCustomEmojiStickerSetThumbnail and Bot.SetCustomEmojiStickerSetThumbnailWithContext. type SetCustomEmojiStickerSetThumbnailOpts struct { // Custom emoji identifier of a sticker from the sticker set; pass an empty string to drop the thumbnail and use the first sticker as the thumbnail. CustomEmojiId string @@ -8274,27 +5546,10 @@ type SetCustomEmojiStickerSetThumbnailOpts struct { // - name (type string): Sticker set name // - opts (type SetCustomEmojiStickerSetThumbnailOpts): All optional parameters. func (bot *Bot) SetCustomEmojiStickerSetThumbnail(name string, opts *SetCustomEmojiStickerSetThumbnailOpts) (bool, error) { - v := map[string]string{} - v["name"] = name - if opts != nil { - v["custom_emoji_id"] = opts.CustomEmojiId - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("setCustomEmojiStickerSetThumbnail", v, nil, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) + return bot.SetCustomEmojiStickerSetThumbnailWithContext(context.Background(), name, opts) } -// SetCustomEmojiStickerSetThumbnailWithContext is the same as SetCustomEmojiStickerSetThumbnail, but with a context.Context parameter. +// SetCustomEmojiStickerSetThumbnailWithContext is the same as Bot.SetCustomEmojiStickerSetThumbnail, but with a context.Context parameter func (bot *Bot) SetCustomEmojiStickerSetThumbnailWithContext(ctx context.Context, name string, opts *SetCustomEmojiStickerSetThumbnailOpts) (bool, error) { v := map[string]string{} v["name"] = name @@ -8316,7 +5571,7 @@ func (bot *Bot) SetCustomEmojiStickerSetThumbnailWithContext(ctx context.Context return b, json.Unmarshal(r, &b) } -// SetGameScoreOpts is the set of optional fields for Bot.SetGameScore. +// SetGameScoreOpts is the set of optional fields for Bot.SetGameScore and Bot.SetGameScoreWithContext. type SetGameScoreOpts struct { // Pass True if the high score is allowed to decrease. This can be useful when fixing mistakes or banning cheaters Force bool @@ -8339,44 +5594,10 @@ type SetGameScoreOpts struct { // - score (type int64): New score, must be non-negative // - opts (type SetGameScoreOpts): All optional parameters. func (bot *Bot) SetGameScore(userId int64, score int64, opts *SetGameScoreOpts) (*Message, bool, error) { - v := map[string]string{} - v["user_id"] = strconv.FormatInt(userId, 10) - v["score"] = strconv.FormatInt(score, 10) - if opts != nil { - v["force"] = strconv.FormatBool(opts.Force) - v["disable_edit_message"] = strconv.FormatBool(opts.DisableEditMessage) - if opts.ChatId != 0 { - v["chat_id"] = strconv.FormatInt(opts.ChatId, 10) - } - if opts.MessageId != 0 { - v["message_id"] = strconv.FormatInt(opts.MessageId, 10) - } - v["inline_message_id"] = opts.InlineMessageId - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("setGameScore", v, nil, reqOpts) - if err != nil { - return nil, false, err - } - - var m Message - if err := json.Unmarshal(r, &m); err != nil { - var b bool - if err := json.Unmarshal(r, &b); err != nil { - return nil, false, err - } - return nil, b, nil - } - return &m, true, nil - + return bot.SetGameScoreWithContext(context.Background(), userId, score, opts) } -// SetGameScoreWithContext is the same as SetGameScore, but with a context.Context parameter. +// SetGameScoreWithContext is the same as Bot.SetGameScore, but with a context.Context parameter func (bot *Bot) SetGameScoreWithContext(ctx context.Context, userId int64, score int64, opts *SetGameScoreOpts) (*Message, bool, error) { v := map[string]string{} v["user_id"] = strconv.FormatInt(userId, 10) @@ -8415,52 +5636,27 @@ func (bot *Bot) SetGameScoreWithContext(ctx context.Context, userId int64, score } -// SetMessageReactionOpts is the set of optional fields for Bot.SetMessageReaction. -type SetMessageReactionOpts struct { - // A JSON-serialized list of reaction types to set on the message. Currently, as non-premium users, bots can set up to one reaction per message. A custom emoji reaction can be used if it is either already present on the message or explicitly allowed by chat administrators. Paid reactions can't be used by bots. - Reaction []ReactionType - // Pass True to set the reaction with a big animation - IsBig bool - // RequestOpts are an additional optional field to configure timeouts for individual requests - RequestOpts *RequestOpts -} - -// SetMessageReaction (https://core.telegram.org/bots/api#setmessagereaction) -// -// Use this method to change the chosen reactions on a message. Service messages can't be reacted to. Automatically forwarded messages from a channel to its discussion group have the same available reactions as messages in the channel. Bots can't use paid reactions. Returns True on success. -// - chatId (type int64): Unique identifier for the target chat -// - messageId (type int64): Identifier of the target message. If the message belongs to a media group, the reaction is set to the first non-deleted message in the group instead. -// - opts (type SetMessageReactionOpts): All optional parameters. -func (bot *Bot) SetMessageReaction(chatId int64, messageId int64, opts *SetMessageReactionOpts) (bool, error) { - v := map[string]string{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - v["message_id"] = strconv.FormatInt(messageId, 10) - if opts != nil { - if opts.Reaction != nil { - bs, err := json.Marshal(opts.Reaction) - if err != nil { - return false, fmt.Errorf("failed to marshal field reaction: %w", err) - } - v["reaction"] = string(bs) - } - v["is_big"] = strconv.FormatBool(opts.IsBig) - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("setMessageReaction", v, nil, reqOpts) - if err != nil { - return false, err - } +// SetMessageReactionOpts is the set of optional fields for Bot.SetMessageReaction and Bot.SetMessageReactionWithContext. +type SetMessageReactionOpts struct { + // A JSON-serialized list of reaction types to set on the message. Currently, as non-premium users, bots can set up to one reaction per message. A custom emoji reaction can be used if it is either already present on the message or explicitly allowed by chat administrators. Paid reactions can't be used by bots. + Reaction []ReactionType + // Pass True to set the reaction with a big animation + IsBig bool + // RequestOpts are an additional optional field to configure timeouts for individual requests + RequestOpts *RequestOpts +} - var b bool - return b, json.Unmarshal(r, &b) +// SetMessageReaction (https://core.telegram.org/bots/api#setmessagereaction) +// +// Use this method to change the chosen reactions on a message. Service messages can't be reacted to. Automatically forwarded messages from a channel to its discussion group have the same available reactions as messages in the channel. Bots can't use paid reactions. Returns True on success. +// - chatId (type int64): Unique identifier for the target chat +// - messageId (type int64): Identifier of the target message. If the message belongs to a media group, the reaction is set to the first non-deleted message in the group instead. +// - opts (type SetMessageReactionOpts): All optional parameters. +func (bot *Bot) SetMessageReaction(chatId int64, messageId int64, opts *SetMessageReactionOpts) (bool, error) { + return bot.SetMessageReactionWithContext(context.Background(), chatId, messageId, opts) } -// SetMessageReactionWithContext is the same as SetMessageReaction, but with a context.Context parameter. +// SetMessageReactionWithContext is the same as Bot.SetMessageReaction, but with a context.Context parameter func (bot *Bot) SetMessageReactionWithContext(ctx context.Context, chatId int64, messageId int64, opts *SetMessageReactionOpts) (bool, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) @@ -8490,7 +5686,7 @@ func (bot *Bot) SetMessageReactionWithContext(ctx context.Context, chatId int64, return b, json.Unmarshal(r, &b) } -// SetMyCommandsOpts is the set of optional fields for Bot.SetMyCommands. +// SetMyCommandsOpts is the set of optional fields for Bot.SetMyCommands and Bot.SetMyCommandsWithContext. type SetMyCommandsOpts struct { // A JSON-serialized object, describing scope of users for which the commands are relevant. Defaults to BotCommandScopeDefault. Scope BotCommandScope @@ -8506,38 +5702,10 @@ type SetMyCommandsOpts struct { // - commands (type []BotCommand): A JSON-serialized list of bot commands to be set as the list of the bot's commands. At most 100 commands can be specified. // - opts (type SetMyCommandsOpts): All optional parameters. func (bot *Bot) SetMyCommands(commands []BotCommand, opts *SetMyCommandsOpts) (bool, error) { - v := map[string]string{} - if commands != nil { - bs, err := json.Marshal(commands) - if err != nil { - return false, fmt.Errorf("failed to marshal field commands: %w", err) - } - v["commands"] = string(bs) - } - if opts != nil { - bs, err := json.Marshal(opts.Scope) - if err != nil { - return false, fmt.Errorf("failed to marshal field scope: %w", err) - } - v["scope"] = string(bs) - v["language_code"] = opts.LanguageCode - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("setMyCommands", v, nil, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) + return bot.SetMyCommandsWithContext(context.Background(), commands, opts) } -// SetMyCommandsWithContext is the same as SetMyCommands, but with a context.Context parameter. +// SetMyCommandsWithContext is the same as Bot.SetMyCommands, but with a context.Context parameter func (bot *Bot) SetMyCommandsWithContext(ctx context.Context, commands []BotCommand, opts *SetMyCommandsOpts) (bool, error) { v := map[string]string{} if commands != nil { @@ -8570,7 +5738,7 @@ func (bot *Bot) SetMyCommandsWithContext(ctx context.Context, commands []BotComm return b, json.Unmarshal(r, &b) } -// SetMyDefaultAdministratorRightsOpts is the set of optional fields for Bot.SetMyDefaultAdministratorRights. +// SetMyDefaultAdministratorRightsOpts is the set of optional fields for Bot.SetMyDefaultAdministratorRights and Bot.SetMyDefaultAdministratorRightsWithContext. type SetMyDefaultAdministratorRightsOpts struct { // A JSON-serialized object describing new default administrator rights. If not specified, the default administrator rights will be cleared. Rights *ChatAdministratorRights @@ -8585,33 +5753,10 @@ type SetMyDefaultAdministratorRightsOpts struct { // Use this method to change the default administrator rights requested by the bot when it's added as an administrator to groups or channels. These rights will be suggested to users, but they are free to modify the list before adding the bot. Returns True on success. // - opts (type SetMyDefaultAdministratorRightsOpts): All optional parameters. func (bot *Bot) SetMyDefaultAdministratorRights(opts *SetMyDefaultAdministratorRightsOpts) (bool, error) { - v := map[string]string{} - if opts != nil { - if opts.Rights != nil { - bs, err := json.Marshal(opts.Rights) - if err != nil { - return false, fmt.Errorf("failed to marshal field rights: %w", err) - } - v["rights"] = string(bs) - } - v["for_channels"] = strconv.FormatBool(opts.ForChannels) - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("setMyDefaultAdministratorRights", v, nil, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) + return bot.SetMyDefaultAdministratorRightsWithContext(context.Background(), opts) } -// SetMyDefaultAdministratorRightsWithContext is the same as SetMyDefaultAdministratorRights, but with a context.Context parameter. +// SetMyDefaultAdministratorRightsWithContext is the same as Bot.SetMyDefaultAdministratorRights, but with a context.Context parameter func (bot *Bot) SetMyDefaultAdministratorRightsWithContext(ctx context.Context, opts *SetMyDefaultAdministratorRightsOpts) (bool, error) { v := map[string]string{} if opts != nil { @@ -8639,7 +5784,7 @@ func (bot *Bot) SetMyDefaultAdministratorRightsWithContext(ctx context.Context, return b, json.Unmarshal(r, &b) } -// SetMyDescriptionOpts is the set of optional fields for Bot.SetMyDescription. +// SetMyDescriptionOpts is the set of optional fields for Bot.SetMyDescription and Bot.SetMyDescriptionWithContext. type SetMyDescriptionOpts struct { // New bot description; 0-512 characters. Pass an empty string to remove the dedicated description for the given language. Description string @@ -8654,27 +5799,10 @@ type SetMyDescriptionOpts struct { // Use this method to change the bot's description, which is shown in the chat with the bot if the chat is empty. Returns True on success. // - opts (type SetMyDescriptionOpts): All optional parameters. func (bot *Bot) SetMyDescription(opts *SetMyDescriptionOpts) (bool, error) { - v := map[string]string{} - if opts != nil { - v["description"] = opts.Description - v["language_code"] = opts.LanguageCode - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("setMyDescription", v, nil, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) + return bot.SetMyDescriptionWithContext(context.Background(), opts) } -// SetMyDescriptionWithContext is the same as SetMyDescription, but with a context.Context parameter. +// SetMyDescriptionWithContext is the same as Bot.SetMyDescription, but with a context.Context parameter func (bot *Bot) SetMyDescriptionWithContext(ctx context.Context, opts *SetMyDescriptionOpts) (bool, error) { v := map[string]string{} if opts != nil { @@ -8696,7 +5824,7 @@ func (bot *Bot) SetMyDescriptionWithContext(ctx context.Context, opts *SetMyDesc return b, json.Unmarshal(r, &b) } -// SetMyNameOpts is the set of optional fields for Bot.SetMyName. +// SetMyNameOpts is the set of optional fields for Bot.SetMyName and Bot.SetMyNameWithContext. type SetMyNameOpts struct { // New bot name; 0-64 characters. Pass an empty string to remove the dedicated name for the given language. Name string @@ -8711,27 +5839,10 @@ type SetMyNameOpts struct { // Use this method to change the bot's name. Returns True on success. // - opts (type SetMyNameOpts): All optional parameters. func (bot *Bot) SetMyName(opts *SetMyNameOpts) (bool, error) { - v := map[string]string{} - if opts != nil { - v["name"] = opts.Name - v["language_code"] = opts.LanguageCode - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("setMyName", v, nil, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) + return bot.SetMyNameWithContext(context.Background(), opts) } -// SetMyNameWithContext is the same as SetMyName, but with a context.Context parameter. +// SetMyNameWithContext is the same as Bot.SetMyName, but with a context.Context parameter func (bot *Bot) SetMyNameWithContext(ctx context.Context, opts *SetMyNameOpts) (bool, error) { v := map[string]string{} if opts != nil { @@ -8753,7 +5864,7 @@ func (bot *Bot) SetMyNameWithContext(ctx context.Context, opts *SetMyNameOpts) ( return b, json.Unmarshal(r, &b) } -// SetMyShortDescriptionOpts is the set of optional fields for Bot.SetMyShortDescription. +// SetMyShortDescriptionOpts is the set of optional fields for Bot.SetMyShortDescription and Bot.SetMyShortDescriptionWithContext. type SetMyShortDescriptionOpts struct { // New short description for the bot; 0-120 characters. Pass an empty string to remove the dedicated short description for the given language. ShortDescription string @@ -8768,27 +5879,10 @@ type SetMyShortDescriptionOpts struct { // Use this method to change the bot's short description, which is shown on the bot's profile page and is sent together with the link when users share the bot. Returns True on success. // - opts (type SetMyShortDescriptionOpts): All optional parameters. func (bot *Bot) SetMyShortDescription(opts *SetMyShortDescriptionOpts) (bool, error) { - v := map[string]string{} - if opts != nil { - v["short_description"] = opts.ShortDescription - v["language_code"] = opts.LanguageCode - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("setMyShortDescription", v, nil, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) + return bot.SetMyShortDescriptionWithContext(context.Background(), opts) } -// SetMyShortDescriptionWithContext is the same as SetMyShortDescription, but with a context.Context parameter. +// SetMyShortDescriptionWithContext is the same as Bot.SetMyShortDescription, but with a context.Context parameter func (bot *Bot) SetMyShortDescriptionWithContext(ctx context.Context, opts *SetMyShortDescriptionOpts) (bool, error) { v := map[string]string{} if opts != nil { @@ -8810,7 +5904,7 @@ func (bot *Bot) SetMyShortDescriptionWithContext(ctx context.Context, opts *SetM return b, json.Unmarshal(r, &b) } -// SetPassportDataErrorsOpts is the set of optional fields for Bot.SetPassportDataErrors. +// SetPassportDataErrorsOpts is the set of optional fields for Bot.SetPassportDataErrors and Bot.SetPassportDataErrorsWithContext. type SetPassportDataErrorsOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests RequestOpts *RequestOpts @@ -8824,31 +5918,10 @@ type SetPassportDataErrorsOpts struct { // - errors (type []PassportElementError): A JSON-serialized array describing the errors // - opts (type SetPassportDataErrorsOpts): All optional parameters. func (bot *Bot) SetPassportDataErrors(userId int64, errors []PassportElementError, opts *SetPassportDataErrorsOpts) (bool, error) { - v := map[string]string{} - v["user_id"] = strconv.FormatInt(userId, 10) - if errors != nil { - bs, err := json.Marshal(errors) - if err != nil { - return false, fmt.Errorf("failed to marshal field errors: %w", err) - } - v["errors"] = string(bs) - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("setPassportDataErrors", v, nil, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) + return bot.SetPassportDataErrorsWithContext(context.Background(), userId, errors, opts) } -// SetPassportDataErrorsWithContext is the same as SetPassportDataErrors, but with a context.Context parameter. +// SetPassportDataErrorsWithContext is the same as Bot.SetPassportDataErrors, but with a context.Context parameter func (bot *Bot) SetPassportDataErrorsWithContext(ctx context.Context, userId int64, errors []PassportElementError, opts *SetPassportDataErrorsOpts) (bool, error) { v := map[string]string{} v["user_id"] = strconv.FormatInt(userId, 10) @@ -8874,7 +5947,7 @@ func (bot *Bot) SetPassportDataErrorsWithContext(ctx context.Context, userId int return b, json.Unmarshal(r, &b) } -// SetStickerEmojiListOpts is the set of optional fields for Bot.SetStickerEmojiList. +// SetStickerEmojiListOpts is the set of optional fields for Bot.SetStickerEmojiList and Bot.SetStickerEmojiListWithContext. type SetStickerEmojiListOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests RequestOpts *RequestOpts @@ -8887,31 +5960,10 @@ type SetStickerEmojiListOpts struct { // - emojiList (type []string): A JSON-serialized list of 1-20 emoji associated with the sticker // - opts (type SetStickerEmojiListOpts): All optional parameters. func (bot *Bot) SetStickerEmojiList(sticker string, emojiList []string, opts *SetStickerEmojiListOpts) (bool, error) { - v := map[string]string{} - v["sticker"] = sticker - if emojiList != nil { - bs, err := json.Marshal(emojiList) - if err != nil { - return false, fmt.Errorf("failed to marshal field emoji_list: %w", err) - } - v["emoji_list"] = string(bs) - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("setStickerEmojiList", v, nil, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) + return bot.SetStickerEmojiListWithContext(context.Background(), sticker, emojiList, opts) } -// SetStickerEmojiListWithContext is the same as SetStickerEmojiList, but with a context.Context parameter. +// SetStickerEmojiListWithContext is the same as Bot.SetStickerEmojiList, but with a context.Context parameter func (bot *Bot) SetStickerEmojiListWithContext(ctx context.Context, sticker string, emojiList []string, opts *SetStickerEmojiListOpts) (bool, error) { v := map[string]string{} v["sticker"] = sticker @@ -8937,7 +5989,7 @@ func (bot *Bot) SetStickerEmojiListWithContext(ctx context.Context, sticker stri return b, json.Unmarshal(r, &b) } -// SetStickerKeywordsOpts is the set of optional fields for Bot.SetStickerKeywords. +// SetStickerKeywordsOpts is the set of optional fields for Bot.SetStickerKeywords and Bot.SetStickerKeywordsWithContext. type SetStickerKeywordsOpts struct { // A JSON-serialized list of 0-20 search keywords for the sticker with total length of up to 64 characters Keywords []string @@ -8951,33 +6003,10 @@ type SetStickerKeywordsOpts struct { // - sticker (type string): File identifier of the sticker // - opts (type SetStickerKeywordsOpts): All optional parameters. func (bot *Bot) SetStickerKeywords(sticker string, opts *SetStickerKeywordsOpts) (bool, error) { - v := map[string]string{} - v["sticker"] = sticker - if opts != nil { - if opts.Keywords != nil { - bs, err := json.Marshal(opts.Keywords) - if err != nil { - return false, fmt.Errorf("failed to marshal field keywords: %w", err) - } - v["keywords"] = string(bs) - } - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("setStickerKeywords", v, nil, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) + return bot.SetStickerKeywordsWithContext(context.Background(), sticker, opts) } -// SetStickerKeywordsWithContext is the same as SetStickerKeywords, but with a context.Context parameter. +// SetStickerKeywordsWithContext is the same as Bot.SetStickerKeywords, but with a context.Context parameter func (bot *Bot) SetStickerKeywordsWithContext(ctx context.Context, sticker string, opts *SetStickerKeywordsOpts) (bool, error) { v := map[string]string{} v["sticker"] = sticker @@ -9005,7 +6034,7 @@ func (bot *Bot) SetStickerKeywordsWithContext(ctx context.Context, sticker strin return b, json.Unmarshal(r, &b) } -// SetStickerMaskPositionOpts is the set of optional fields for Bot.SetStickerMaskPosition. +// SetStickerMaskPositionOpts is the set of optional fields for Bot.SetStickerMaskPosition and Bot.SetStickerMaskPositionWithContext. type SetStickerMaskPositionOpts struct { // A JSON-serialized object with the position where the mask should be placed on faces. Omit the parameter to remove the mask position. MaskPosition *MaskPosition @@ -9019,33 +6048,10 @@ type SetStickerMaskPositionOpts struct { // - sticker (type string): File identifier of the sticker // - opts (type SetStickerMaskPositionOpts): All optional parameters. func (bot *Bot) SetStickerMaskPosition(sticker string, opts *SetStickerMaskPositionOpts) (bool, error) { - v := map[string]string{} - v["sticker"] = sticker - if opts != nil { - if opts.MaskPosition != nil { - bs, err := json.Marshal(opts.MaskPosition) - if err != nil { - return false, fmt.Errorf("failed to marshal field mask_position: %w", err) - } - v["mask_position"] = string(bs) - } - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("setStickerMaskPosition", v, nil, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) + return bot.SetStickerMaskPositionWithContext(context.Background(), sticker, opts) } -// SetStickerMaskPositionWithContext is the same as SetStickerMaskPosition, but with a context.Context parameter. +// SetStickerMaskPositionWithContext is the same as Bot.SetStickerMaskPosition, but with a context.Context parameter func (bot *Bot) SetStickerMaskPositionWithContext(ctx context.Context, sticker string, opts *SetStickerMaskPositionOpts) (bool, error) { v := map[string]string{} v["sticker"] = sticker @@ -9073,7 +6079,7 @@ func (bot *Bot) SetStickerMaskPositionWithContext(ctx context.Context, sticker s return b, json.Unmarshal(r, &b) } -// SetStickerPositionInSetOpts is the set of optional fields for Bot.SetStickerPositionInSet. +// SetStickerPositionInSetOpts is the set of optional fields for Bot.SetStickerPositionInSet and Bot.SetStickerPositionInSetWithContext. type SetStickerPositionInSetOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests RequestOpts *RequestOpts @@ -9086,25 +6092,10 @@ type SetStickerPositionInSetOpts struct { // - position (type int64): New sticker position in the set, zero-based // - opts (type SetStickerPositionInSetOpts): All optional parameters. func (bot *Bot) SetStickerPositionInSet(sticker string, position int64, opts *SetStickerPositionInSetOpts) (bool, error) { - v := map[string]string{} - v["sticker"] = sticker - v["position"] = strconv.FormatInt(position, 10) - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("setStickerPositionInSet", v, nil, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) + return bot.SetStickerPositionInSetWithContext(context.Background(), sticker, position, opts) } -// SetStickerPositionInSetWithContext is the same as SetStickerPositionInSet, but with a context.Context parameter. +// SetStickerPositionInSetWithContext is the same as Bot.SetStickerPositionInSet, but with a context.Context parameter func (bot *Bot) SetStickerPositionInSetWithContext(ctx context.Context, sticker string, position int64, opts *SetStickerPositionInSetOpts) (bool, error) { v := map[string]string{} v["sticker"] = sticker @@ -9124,52 +6115,26 @@ func (bot *Bot) SetStickerPositionInSetWithContext(ctx context.Context, sticker return b, json.Unmarshal(r, &b) } -// SetStickerSetThumbnailOpts is the set of optional fields for Bot.SetStickerSetThumbnail. +// SetStickerSetThumbnailOpts is the set of optional fields for Bot.SetStickerSetThumbnail and Bot.SetStickerSetThumbnailWithContext. type SetStickerSetThumbnailOpts struct { // A .WEBP or .PNG image with the thumbnail, must be up to 128 kilobytes in size and have a width and height of exactly 100px, or a .TGS animation with a thumbnail up to 32 kilobytes in size (see https://core.telegram.org/stickers#animation-requirements for animated sticker technical requirements), or a WEBM video with the thumbnail up to 32 kilobytes in size; see https://core.telegram.org/stickers#video-requirements for video sticker technical requirements. Pass a file_id as a String to send a file that already exists on the Telegram servers, pass an HTTP URL as a String for Telegram to get a file from the Internet, or upload a new one using multipart/form-data. More information on Sending Files: https://core.telegram.org/bots/api#sending-files. Animated and video sticker set thumbnails can't be uploaded via HTTP URL. If omitted, then the thumbnail is dropped and the first sticker is used as the thumbnail. Thumbnail InputFile // RequestOpts are an additional optional field to configure timeouts for individual requests - RequestOpts *RequestOpts -} - -// SetStickerSetThumbnail (https://core.telegram.org/bots/api#setstickersetthumbnail) -// -// Use this method to set the thumbnail of a regular or mask sticker set. The format of the thumbnail file must match the format of the stickers in the set. Returns True on success. -// - name (type string): Sticker set name -// - userId (type int64): User identifier of the sticker set owner -// - format (type string): Format of the thumbnail, must be one of "static" for a .WEBP or .PNG image, "animated" for a .TGS animation, or "video" for a WEBM video -// - opts (type SetStickerSetThumbnailOpts): All optional parameters. -func (bot *Bot) SetStickerSetThumbnail(name string, userId int64, format string, opts *SetStickerSetThumbnailOpts) (bool, error) { - v := map[string]string{} - data := map[string]FileReader{} - v["name"] = name - v["user_id"] = strconv.FormatInt(userId, 10) - v["format"] = format - if opts != nil { - if opts.Thumbnail != nil { - err := opts.Thumbnail.Attach("thumbnail", data) - if err != nil { - return false, fmt.Errorf("failed to attach 'thumbnail' input file: %w", err) - } - v["thumbnail"] = opts.Thumbnail.getValue() - } - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("setStickerSetThumbnail", v, data, reqOpts) - if err != nil { - return false, err - } + RequestOpts *RequestOpts +} - var b bool - return b, json.Unmarshal(r, &b) +// SetStickerSetThumbnail (https://core.telegram.org/bots/api#setstickersetthumbnail) +// +// Use this method to set the thumbnail of a regular or mask sticker set. The format of the thumbnail file must match the format of the stickers in the set. Returns True on success. +// - name (type string): Sticker set name +// - userId (type int64): User identifier of the sticker set owner +// - format (type string): Format of the thumbnail, must be one of "static" for a .WEBP or .PNG image, "animated" for a .TGS animation, or "video" for a WEBM video +// - opts (type SetStickerSetThumbnailOpts): All optional parameters. +func (bot *Bot) SetStickerSetThumbnail(name string, userId int64, format string, opts *SetStickerSetThumbnailOpts) (bool, error) { + return bot.SetStickerSetThumbnailWithContext(context.Background(), name, userId, format, opts) } -// SetStickerSetThumbnailWithContext is the same as SetStickerSetThumbnail, but with a context.Context parameter. +// SetStickerSetThumbnailWithContext is the same as Bot.SetStickerSetThumbnail, but with a context.Context parameter func (bot *Bot) SetStickerSetThumbnailWithContext(ctx context.Context, name string, userId int64, format string, opts *SetStickerSetThumbnailOpts) (bool, error) { v := map[string]string{} data := map[string]FileReader{} @@ -9200,7 +6165,7 @@ func (bot *Bot) SetStickerSetThumbnailWithContext(ctx context.Context, name stri return b, json.Unmarshal(r, &b) } -// SetStickerSetTitleOpts is the set of optional fields for Bot.SetStickerSetTitle. +// SetStickerSetTitleOpts is the set of optional fields for Bot.SetStickerSetTitle and Bot.SetStickerSetTitleWithContext. type SetStickerSetTitleOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests RequestOpts *RequestOpts @@ -9213,25 +6178,10 @@ type SetStickerSetTitleOpts struct { // - title (type string): Sticker set title, 1-64 characters // - opts (type SetStickerSetTitleOpts): All optional parameters. func (bot *Bot) SetStickerSetTitle(name string, title string, opts *SetStickerSetTitleOpts) (bool, error) { - v := map[string]string{} - v["name"] = name - v["title"] = title - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("setStickerSetTitle", v, nil, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) + return bot.SetStickerSetTitleWithContext(context.Background(), name, title, opts) } -// SetStickerSetTitleWithContext is the same as SetStickerSetTitle, but with a context.Context parameter. +// SetStickerSetTitleWithContext is the same as Bot.SetStickerSetTitle, but with a context.Context parameter func (bot *Bot) SetStickerSetTitleWithContext(ctx context.Context, name string, title string, opts *SetStickerSetTitleOpts) (bool, error) { v := map[string]string{} v["name"] = name @@ -9251,7 +6201,7 @@ func (bot *Bot) SetStickerSetTitleWithContext(ctx context.Context, name string, return b, json.Unmarshal(r, &b) } -// SetWebhookOpts is the set of optional fields for Bot.SetWebhook. +// SetWebhookOpts is the set of optional fields for Bot.SetWebhook and Bot.SetWebhookWithContext. type SetWebhookOpts struct { // Upload your public key certificate so that the root certificate in use can be checked. See our self-signed guide for details. Certificate InputFile @@ -9276,47 +6226,10 @@ type SetWebhookOpts struct { // - url (type string): HTTPS URL to send updates to. Use an empty string to remove webhook integration // - opts (type SetWebhookOpts): All optional parameters. func (bot *Bot) SetWebhook(url string, opts *SetWebhookOpts) (bool, error) { - v := map[string]string{} - data := map[string]FileReader{} - v["url"] = url - if opts != nil { - if opts.Certificate != nil { - err := opts.Certificate.Attach("certificate", data) - if err != nil { - return false, fmt.Errorf("failed to attach 'certificate' input file: %w", err) - } - v["certificate"] = opts.Certificate.getValue() - } - v["ip_address"] = opts.IpAddress - if opts.MaxConnections != 0 { - v["max_connections"] = strconv.FormatInt(opts.MaxConnections, 10) - } - if opts.AllowedUpdates != nil { - bs, err := json.Marshal(opts.AllowedUpdates) - if err != nil { - return false, fmt.Errorf("failed to marshal field allowed_updates: %w", err) - } - v["allowed_updates"] = string(bs) - } - v["drop_pending_updates"] = strconv.FormatBool(opts.DropPendingUpdates) - v["secret_token"] = opts.SecretToken - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("setWebhook", v, data, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) + return bot.SetWebhookWithContext(context.Background(), url, opts) } -// SetWebhookWithContext is the same as SetWebhook, but with a context.Context parameter. +// SetWebhookWithContext is the same as Bot.SetWebhook, but with a context.Context parameter func (bot *Bot) SetWebhookWithContext(ctx context.Context, url string, opts *SetWebhookOpts) (bool, error) { v := map[string]string{} data := map[string]FileReader{} @@ -9358,7 +6271,7 @@ func (bot *Bot) SetWebhookWithContext(ctx context.Context, url string, opts *Set return b, json.Unmarshal(r, &b) } -// StopMessageLiveLocationOpts is the set of optional fields for Bot.StopMessageLiveLocation. +// StopMessageLiveLocationOpts is the set of optional fields for Bot.StopMessageLiveLocation and Bot.StopMessageLiveLocationWithContext. type StopMessageLiveLocationOpts struct { // Unique identifier of the business connection on behalf of which the message to be edited was sent BusinessConnectionId string @@ -9379,46 +6292,10 @@ type StopMessageLiveLocationOpts struct { // Use this method to stop updating a live location message before live_period expires. On success, if the message is not an inline message, the edited Message is returned, otherwise True is returned. // - opts (type StopMessageLiveLocationOpts): All optional parameters. 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) - } - if opts.MessageId != 0 { - v["message_id"] = strconv.FormatInt(opts.MessageId, 10) - } - v["inline_message_id"] = opts.InlineMessageId - bs, err := json.Marshal(opts.ReplyMarkup) - if err != nil { - return nil, false, fmt.Errorf("failed to marshal field reply_markup: %w", err) - } - v["reply_markup"] = string(bs) - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("stopMessageLiveLocation", v, nil, reqOpts) - if err != nil { - return nil, false, err - } - - var m Message - if err := json.Unmarshal(r, &m); err != nil { - var b bool - if err := json.Unmarshal(r, &b); err != nil { - return nil, false, err - } - return nil, b, nil - } - return &m, true, nil - + return bot.StopMessageLiveLocationWithContext(context.Background(), opts) } -// StopMessageLiveLocationWithContext is the same as StopMessageLiveLocation, but with a context.Context parameter. +// StopMessageLiveLocationWithContext is the same as Bot.StopMessageLiveLocation, but with a context.Context parameter func (bot *Bot) StopMessageLiveLocationWithContext(ctx context.Context, opts *StopMessageLiveLocationOpts) (*Message, bool, error) { v := map[string]string{} if opts != nil { @@ -9459,7 +6336,7 @@ func (bot *Bot) StopMessageLiveLocationWithContext(ctx context.Context, opts *St } -// StopPollOpts is the set of optional fields for Bot.StopPoll. +// StopPollOpts is the set of optional fields for Bot.StopPoll and Bot.StopPollWithContext. type StopPollOpts struct { // Unique identifier of the business connection on behalf of which the message to be edited was sent BusinessConnectionId string @@ -9476,33 +6353,10 @@ type StopPollOpts struct { // - messageId (type int64): Identifier of the original message with the poll // - opts (type StopPollOpts): All optional parameters. func (bot *Bot) StopPoll(chatId int64, messageId int64, opts *StopPollOpts) (*Poll, error) { - v := map[string]string{} - 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) - } - v["reply_markup"] = string(bs) - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("stopPoll", v, nil, reqOpts) - if err != nil { - return nil, err - } - - var p Poll - return &p, json.Unmarshal(r, &p) + return bot.StopPollWithContext(context.Background(), chatId, messageId, opts) } -// StopPollWithContext is the same as StopPoll, but with a context.Context parameter. +// StopPollWithContext is the same as Bot.StopPoll, but with a context.Context parameter func (bot *Bot) StopPollWithContext(ctx context.Context, chatId int64, messageId int64, opts *StopPollOpts) (*Poll, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) @@ -9530,7 +6384,7 @@ func (bot *Bot) StopPollWithContext(ctx context.Context, chatId int64, messageId return &p, json.Unmarshal(r, &p) } -// UnbanChatMemberOpts is the set of optional fields for Bot.UnbanChatMember. +// UnbanChatMemberOpts is the set of optional fields for Bot.UnbanChatMember and Bot.UnbanChatMemberWithContext. type UnbanChatMemberOpts struct { // Do nothing if the user is not banned OnlyIfBanned bool @@ -9545,28 +6399,10 @@ type UnbanChatMemberOpts struct { // - userId (type int64): Unique identifier of the target user // - opts (type UnbanChatMemberOpts): All optional parameters. func (bot *Bot) UnbanChatMember(chatId int64, userId int64, opts *UnbanChatMemberOpts) (bool, error) { - v := map[string]string{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - v["user_id"] = strconv.FormatInt(userId, 10) - if opts != nil { - v["only_if_banned"] = strconv.FormatBool(opts.OnlyIfBanned) - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("unbanChatMember", v, nil, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) + return bot.UnbanChatMemberWithContext(context.Background(), chatId, userId, opts) } -// UnbanChatMemberWithContext is the same as UnbanChatMember, but with a context.Context parameter. +// UnbanChatMemberWithContext is the same as Bot.UnbanChatMember, but with a context.Context parameter func (bot *Bot) UnbanChatMemberWithContext(ctx context.Context, chatId int64, userId int64, opts *UnbanChatMemberOpts) (bool, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) @@ -9589,7 +6425,7 @@ func (bot *Bot) UnbanChatMemberWithContext(ctx context.Context, chatId int64, us return b, json.Unmarshal(r, &b) } -// UnbanChatSenderChatOpts is the set of optional fields for Bot.UnbanChatSenderChat. +// UnbanChatSenderChatOpts is the set of optional fields for Bot.UnbanChatSenderChat and Bot.UnbanChatSenderChatWithContext. type UnbanChatSenderChatOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests RequestOpts *RequestOpts @@ -9602,25 +6438,10 @@ type UnbanChatSenderChatOpts struct { // - senderChatId (type int64): Unique identifier of the target sender chat // - opts (type UnbanChatSenderChatOpts): All optional parameters. func (bot *Bot) UnbanChatSenderChat(chatId int64, senderChatId int64, opts *UnbanChatSenderChatOpts) (bool, error) { - v := map[string]string{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - v["sender_chat_id"] = strconv.FormatInt(senderChatId, 10) - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("unbanChatSenderChat", v, nil, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) + return bot.UnbanChatSenderChatWithContext(context.Background(), chatId, senderChatId, opts) } -// UnbanChatSenderChatWithContext is the same as UnbanChatSenderChat, but with a context.Context parameter. +// UnbanChatSenderChatWithContext is the same as Bot.UnbanChatSenderChat, but with a context.Context parameter func (bot *Bot) UnbanChatSenderChatWithContext(ctx context.Context, chatId int64, senderChatId int64, opts *UnbanChatSenderChatOpts) (bool, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) @@ -9640,7 +6461,7 @@ func (bot *Bot) UnbanChatSenderChatWithContext(ctx context.Context, chatId int64 return b, json.Unmarshal(r, &b) } -// UnhideGeneralForumTopicOpts is the set of optional fields for Bot.UnhideGeneralForumTopic. +// UnhideGeneralForumTopicOpts is the set of optional fields for Bot.UnhideGeneralForumTopic and Bot.UnhideGeneralForumTopicWithContext. type UnhideGeneralForumTopicOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests RequestOpts *RequestOpts @@ -9652,24 +6473,10 @@ type UnhideGeneralForumTopicOpts struct { // - chatId (type int64): Unique identifier for the target chat // - opts (type UnhideGeneralForumTopicOpts): All optional parameters. func (bot *Bot) UnhideGeneralForumTopic(chatId int64, opts *UnhideGeneralForumTopicOpts) (bool, error) { - v := map[string]string{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("unhideGeneralForumTopic", v, nil, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) + return bot.UnhideGeneralForumTopicWithContext(context.Background(), chatId, opts) } -// UnhideGeneralForumTopicWithContext is the same as UnhideGeneralForumTopic, but with a context.Context parameter. +// UnhideGeneralForumTopicWithContext is the same as Bot.UnhideGeneralForumTopic, but with a context.Context parameter func (bot *Bot) UnhideGeneralForumTopicWithContext(ctx context.Context, chatId int64, opts *UnhideGeneralForumTopicOpts) (bool, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) @@ -9688,7 +6495,7 @@ func (bot *Bot) UnhideGeneralForumTopicWithContext(ctx context.Context, chatId i return b, json.Unmarshal(r, &b) } -// UnpinAllChatMessagesOpts is the set of optional fields for Bot.UnpinAllChatMessages. +// UnpinAllChatMessagesOpts is the set of optional fields for Bot.UnpinAllChatMessages and Bot.UnpinAllChatMessagesWithContext. type UnpinAllChatMessagesOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests RequestOpts *RequestOpts @@ -9700,24 +6507,10 @@ type UnpinAllChatMessagesOpts struct { // - chatId (type int64): Unique identifier for the target chat // - opts (type UnpinAllChatMessagesOpts): All optional parameters. func (bot *Bot) UnpinAllChatMessages(chatId int64, opts *UnpinAllChatMessagesOpts) (bool, error) { - v := map[string]string{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("unpinAllChatMessages", v, nil, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) + return bot.UnpinAllChatMessagesWithContext(context.Background(), chatId, opts) } -// UnpinAllChatMessagesWithContext is the same as UnpinAllChatMessages, but with a context.Context parameter. +// UnpinAllChatMessagesWithContext is the same as Bot.UnpinAllChatMessages, but with a context.Context parameter func (bot *Bot) UnpinAllChatMessagesWithContext(ctx context.Context, chatId int64, opts *UnpinAllChatMessagesOpts) (bool, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) @@ -9736,7 +6529,7 @@ func (bot *Bot) UnpinAllChatMessagesWithContext(ctx context.Context, chatId int6 return b, json.Unmarshal(r, &b) } -// UnpinAllForumTopicMessagesOpts is the set of optional fields for Bot.UnpinAllForumTopicMessages. +// UnpinAllForumTopicMessagesOpts is the set of optional fields for Bot.UnpinAllForumTopicMessages and Bot.UnpinAllForumTopicMessagesWithContext. type UnpinAllForumTopicMessagesOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests RequestOpts *RequestOpts @@ -9749,25 +6542,10 @@ type UnpinAllForumTopicMessagesOpts struct { // - messageThreadId (type int64): Unique identifier for the target message thread of the forum topic // - opts (type UnpinAllForumTopicMessagesOpts): All optional parameters. func (bot *Bot) UnpinAllForumTopicMessages(chatId int64, messageThreadId int64, opts *UnpinAllForumTopicMessagesOpts) (bool, error) { - v := map[string]string{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - v["message_thread_id"] = strconv.FormatInt(messageThreadId, 10) - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("unpinAllForumTopicMessages", v, nil, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) + return bot.UnpinAllForumTopicMessagesWithContext(context.Background(), chatId, messageThreadId, opts) } -// UnpinAllForumTopicMessagesWithContext is the same as UnpinAllForumTopicMessages, but with a context.Context parameter. +// UnpinAllForumTopicMessagesWithContext is the same as Bot.UnpinAllForumTopicMessages, but with a context.Context parameter func (bot *Bot) UnpinAllForumTopicMessagesWithContext(ctx context.Context, chatId int64, messageThreadId int64, opts *UnpinAllForumTopicMessagesOpts) (bool, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) @@ -9787,7 +6565,7 @@ func (bot *Bot) UnpinAllForumTopicMessagesWithContext(ctx context.Context, chatI return b, json.Unmarshal(r, &b) } -// UnpinAllGeneralForumTopicMessagesOpts is the set of optional fields for Bot.UnpinAllGeneralForumTopicMessages. +// UnpinAllGeneralForumTopicMessagesOpts is the set of optional fields for Bot.UnpinAllGeneralForumTopicMessages and Bot.UnpinAllGeneralForumTopicMessagesWithContext. type UnpinAllGeneralForumTopicMessagesOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests RequestOpts *RequestOpts @@ -9799,24 +6577,10 @@ type UnpinAllGeneralForumTopicMessagesOpts struct { // - chatId (type int64): Unique identifier for the target chat // - opts (type UnpinAllGeneralForumTopicMessagesOpts): All optional parameters. func (bot *Bot) UnpinAllGeneralForumTopicMessages(chatId int64, opts *UnpinAllGeneralForumTopicMessagesOpts) (bool, error) { - v := map[string]string{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("unpinAllGeneralForumTopicMessages", v, nil, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) + return bot.UnpinAllGeneralForumTopicMessagesWithContext(context.Background(), chatId, opts) } -// UnpinAllGeneralForumTopicMessagesWithContext is the same as UnpinAllGeneralForumTopicMessages, but with a context.Context parameter. +// UnpinAllGeneralForumTopicMessagesWithContext is the same as Bot.UnpinAllGeneralForumTopicMessages, but with a context.Context parameter func (bot *Bot) UnpinAllGeneralForumTopicMessagesWithContext(ctx context.Context, chatId int64, opts *UnpinAllGeneralForumTopicMessagesOpts) (bool, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) @@ -9835,7 +6599,7 @@ func (bot *Bot) UnpinAllGeneralForumTopicMessagesWithContext(ctx context.Context return b, json.Unmarshal(r, &b) } -// UnpinChatMessageOpts is the set of optional fields for Bot.UnpinChatMessage. +// UnpinChatMessageOpts is the set of optional fields for Bot.UnpinChatMessage and Bot.UnpinChatMessageWithContext. type UnpinChatMessageOpts struct { // Unique identifier of the business connection on behalf of which the message will be unpinned BusinessConnectionId string @@ -9851,30 +6615,10 @@ type UnpinChatMessageOpts struct { // - chatId (type int64): Unique identifier for the target chat // - opts (type UnpinChatMessageOpts): All optional parameters. func (bot *Bot) UnpinChatMessage(chatId int64, opts *UnpinChatMessageOpts) (bool, error) { - v := map[string]string{} - v["chat_id"] = strconv.FormatInt(chatId, 10) - if opts != nil { - v["business_connection_id"] = opts.BusinessConnectionId - if opts.MessageId != nil { - v["message_id"] = strconv.FormatInt(*opts.MessageId, 10) - } - } - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("unpinChatMessage", v, nil, reqOpts) - if err != nil { - return false, err - } - - var b bool - return b, json.Unmarshal(r, &b) + return bot.UnpinChatMessageWithContext(context.Background(), chatId, opts) } -// UnpinChatMessageWithContext is the same as UnpinChatMessage, but with a context.Context parameter. +// UnpinChatMessageWithContext is the same as Bot.UnpinChatMessage, but with a context.Context parameter func (bot *Bot) UnpinChatMessageWithContext(ctx context.Context, chatId int64, opts *UnpinChatMessageOpts) (bool, error) { v := map[string]string{} v["chat_id"] = strconv.FormatInt(chatId, 10) @@ -9899,7 +6643,7 @@ func (bot *Bot) UnpinChatMessageWithContext(ctx context.Context, chatId int64, o return b, json.Unmarshal(r, &b) } -// UploadStickerFileOpts is the set of optional fields for Bot.UploadStickerFile. +// UploadStickerFileOpts is the set of optional fields for Bot.UploadStickerFile and Bot.UploadStickerFileWithContext. type UploadStickerFileOpts struct { // RequestOpts are an additional optional field to configure timeouts for individual requests RequestOpts *RequestOpts @@ -9913,33 +6657,10 @@ type UploadStickerFileOpts struct { // - stickerFormat (type string): Format of the sticker, must be one of "static", "animated", "video" // - opts (type UploadStickerFileOpts): All optional parameters. func (bot *Bot) UploadStickerFile(userId int64, sticker InputFile, stickerFormat string, opts *UploadStickerFileOpts) (*File, error) { - v := map[string]string{} - data := map[string]FileReader{} - v["user_id"] = strconv.FormatInt(userId, 10) - if sticker != nil { - err := sticker.Attach("sticker", data) - if err != nil { - return nil, fmt.Errorf("failed to attach 'sticker' input file: %w", err) - } - v["sticker"] = sticker.getValue() - } - v["sticker_format"] = stickerFormat - - var reqOpts *RequestOpts - if opts != nil { - reqOpts = opts.RequestOpts - } - - r, err := bot.Request("uploadStickerFile", v, data, reqOpts) - if err != nil { - return nil, err - } - - var f File - return &f, json.Unmarshal(r, &f) + return bot.UploadStickerFileWithContext(context.Background(), userId, sticker, stickerFormat, opts) } -// UploadStickerFileWithContext is the same as UploadStickerFile, but with a context.Context parameter. +// UploadStickerFileWithContext is the same as Bot.UploadStickerFile, but with a context.Context parameter func (bot *Bot) UploadStickerFileWithContext(ctx context.Context, userId int64, sticker InputFile, stickerFormat string, opts *UploadStickerFileOpts) (*File, error) { v := map[string]string{} data := map[string]FileReader{} diff --git a/request.go b/request.go index 893abf8..dd703b2 100644 --- a/request.go +++ b/request.go @@ -22,8 +22,8 @@ const ( type BotClient interface { // RequestWithContext submits a POST HTTP request a bot API instance. RequestWithContext(ctx context.Context, token string, method string, params map[string]string, data map[string]FileReader, opts *RequestOpts) (json.RawMessage, error) - // TimeoutContext calculates the required timeout contect required given the passed RequestOpts, and any default opts defined by the BotClient. - TimeoutContext(opts *RequestOpts) (context.Context, context.CancelFunc) + // TimeoutContext wraps the existing context with the timeout from the provided RequestOpts, or any default opts defined by the BotClient. + TimeoutContext(ctx context.Context, opts *RequestOpts) (context.Context, context.CancelFunc) // GetAPIURL gets the URL of the API either in use by the bot or defined in the request opts. GetAPIURL(opts *RequestOpts) string // FileURL gets the URL of a file at the API address that the bot is interacting with. @@ -83,16 +83,20 @@ type RequestOpts struct { } // TimeoutContext returns the appropriate context for the current settings. -func (bot *BaseBotClient) TimeoutContext(opts *RequestOpts) (context.Context, context.CancelFunc) { +func (bot *BaseBotClient) TimeoutContext(parentCtx context.Context, opts *RequestOpts) (context.Context, context.CancelFunc) { + if parentCtx == nil { + parentCtx = context.Background() + } + if opts != nil { - ctx, cancelFunc := timeoutFromOpts(opts) + ctx, cancelFunc := timeoutFromOpts(parentCtx, opts) if ctx != nil { return ctx, cancelFunc } } if bot.DefaultRequestOpts != nil { - ctx, cancelFunc := timeoutFromOpts(bot.DefaultRequestOpts) + ctx, cancelFunc := timeoutFromOpts(parentCtx, bot.DefaultRequestOpts) if ctx != nil { return ctx, cancelFunc } @@ -101,19 +105,22 @@ func (bot *BaseBotClient) TimeoutContext(opts *RequestOpts) (context.Context, co return context.WithTimeout(context.Background(), DefaultTimeout) } -func timeoutFromOpts(opts *RequestOpts) (context.Context, context.CancelFunc) { +func timeoutFromOpts(parentCtx context.Context, opts *RequestOpts) (context.Context, context.CancelFunc) { // nothing? no timeout. if opts == nil { return nil, nil } + if parentCtx == nil { + parentCtx = context.Background() + } + if opts.Timeout > 0 { - // > 0 timeout defined. - return context.WithTimeout(context.Background(), opts.Timeout) + return context.WithTimeout(parentCtx, opts.Timeout) } else if opts.Timeout < 0 { // < 0 no timeout; infinite. - return context.Background(), func() {} + return parentCtx, func() {} } // 0 == nothing defined, use defaults. return nil, nil diff --git a/scripts/generate/methods.go b/scripts/generate/methods.go index f49c4cc..5c5c840 100644 --- a/scripts/generate/methods.go +++ b/scripts/generate/methods.go @@ -45,9 +45,20 @@ import ( func generateMethodDef(d APIDescription, tgMethod MethodDescription) (string, error) { method := strings.Builder{} - args, retTypes, optionalsStruct, err := generateMethodSignature(d, tgMethod) + req := tgMethod.getRequiredFields() + reqArgs, err := req.getFunctionArgs(d) if err != nil { - return "", err + return "", fmt.Errorf("failed to get required arguments for %s: %w", tgMethod.Name, err) + } + + optionalsStruct, err := tgMethod.getOptionalsStruct(d) + if err != nil { + return "", fmt.Errorf("failed to get optionals for method %s: %w", tgMethod.Name, err) + } + + retTypes, err := tgMethod.GetReturnTypes(d) + if err != nil { + return "", fmt.Errorf("failed to get return for %s: %w", tgMethod.Name, err) } if optionalsStruct != "" { @@ -75,21 +86,16 @@ func generateMethodDef(d APIDescription, tgMethod MethodDescription) (string, er return "", fmt.Errorf("failed to generate return values: %w", err) } - methodSignature := strings.Title(tgMethod.Name) + "(" + strings.Join(args, ", ") + ") (" + strings.Join(retTypes, ", ") + ", error)" - method.WriteString(desc) - method.WriteString(genMethodSig(methodSignature, valueGen, hasData, false, tgMethod, defaultRetVals, returnGen)) - - methodSignatureWithCtx := strings.Title(tgMethod.Name) + "WithContext(ctx context.Context, " + strings.Join(args, ", ") + ") (" + strings.Join(retTypes, ", ") + ", error)" - method.WriteString(fmt.Sprintf("\n// %sWithContext is the same as %s, but with a context.Context parameter.", strings.Title(tgMethod.Name), strings.Title(tgMethod.Name))) - method.WriteString(genMethodSig(methodSignatureWithCtx, valueGen, hasData, true, tgMethod, defaultRetVals, returnGen)) + joinedArgs := strings.Join(reqArgs, ", ") + joinedRetTypes := strings.Join(retTypes, ", ") - return method.String(), nil -} - -func genMethodSig(methodSignature string, valueGen string, hasData bool, withContext bool, tgMethod MethodDescription, defaultRetVals string, returnGen string) string { - method := strings.Builder{} + method.WriteString(desc) + method.WriteString("\nfunc (bot *Bot) " + strings.Title(tgMethod.Name) + "(" + joinedArgs + ") (" + joinedRetTypes + ", error) {") + method.WriteString("\n\treturn bot." + strings.Title(tgMethod.Name) + "WithContext(context.Background(), " + strings.Join(req.getNames(), ", ") + ")") + method.WriteString("\n}") - method.WriteString("\nfunc (bot *Bot) " + methodSignature + " {") + method.WriteString("\n// " + strings.Title(tgMethod.Name) + "WithContext is the same as Bot." + strings.Title(tgMethod.Name) + ", but with a context.Context parameter") + method.WriteString("\nfunc (bot *Bot) " + strings.Title(tgMethod.Name) + "WithContext(ctx context.Context, " + joinedArgs + ") (" + joinedRetTypes + ", error) {") method.WriteString("\n v := map[string]string{}") method.WriteString(valueGen) method.WriteString("\n") @@ -102,17 +108,9 @@ func genMethodSig(methodSignature string, valueGen string, hasData bool, withCon // If sending data, we need to do it over POST if hasData { - if withContext { - method.WriteString("\nr, err := bot.RequestWithContext(ctx, \"" + tgMethod.Name + "\", v, data, reqOpts)") - } else { - method.WriteString("\nr, err := bot.Request(\"" + tgMethod.Name + "\", v, data, reqOpts)") - } + method.WriteString("\nr, err := bot.RequestWithContext(ctx, \"" + tgMethod.Name + "\", v, data, reqOpts)") } else { - if withContext { - method.WriteString("\nr, err := bot.RequestWithContext(ctx, \"" + tgMethod.Name + "\", v, nil, reqOpts)") - } else { - method.WriteString("\nr, err := bot.Request(\"" + tgMethod.Name + "\", v, nil, reqOpts)") - } + method.WriteString("\nr, err := bot.RequestWithContext(ctx, \"" + tgMethod.Name + "\", v, nil, reqOpts)") } method.WriteString("\n if err != nil {") @@ -123,21 +121,7 @@ func genMethodSig(methodSignature string, valueGen string, hasData bool, withCon method.WriteString(returnGen) method.WriteString("\n}") - return method.String() -} - -func generateMethodSignature(d APIDescription, tgMethod MethodDescription) ([]string, []string, string, error) { - retTypes, err := tgMethod.GetReturnTypes(d) - if err != nil { - return nil, nil, "", fmt.Errorf("failed to get return for %s: %w", tgMethod.Name, err) - } - - args, optionalsStruct, err := tgMethod.getArgs(d) - if err != nil { - return nil, nil, "", fmt.Errorf("failed to get args for method %s: %w", tgMethod.Name, err) - } - - return args, retTypes, optionalsStruct, nil + return method.String(), nil } func returnValues(d APIDescription, retTypes []string) (string, error) { @@ -379,37 +363,76 @@ func getRetVarName(retType string) string { return strings.ToLower(retType[:1]) } -func (m MethodDescription) getArgs(d APIDescription) ([]string, string, error) { - var requiredArgs []string - optionals := strings.Builder{} +type Fields []Field +func (m MethodDescription) getRequiredFields() Fields { + var req []Field for _, f := range m.Fields { + if f.Required { + req = append(req, f) + } + } + req = append(req, Field{ + Name: "opts", + Types: []string{"*" + m.optsName()}, + Required: true, + Description: "All optional parameters.", + }) + return req +} + +func (fs Fields) getNames() []string { + var names []string + for _, f := range fs { + names = append(names, snakeToCamel(f.Name)) + } + return names +} + +func (fs Fields) getFunctionArgs(d APIDescription) ([]string, error) { + var args []string + for _, f := range fs { + if !f.Required { + continue + } + fieldType, err := f.getPreferredType(d) if err != nil { - return nil, "", fmt.Errorf("failed to get preferred type: %w", err) + return nil, fmt.Errorf("failed to get preferred type: %w", err) } + args = append(args, fmt.Sprintf("%s %s", snakeToCamel(f.Name), fieldType)) + } + return args, nil +} + +func (m MethodDescription) getOptionalsStruct(d APIDescription) (string, error) { + optionals := strings.Builder{} + + for _, f := range m.Fields { if f.Required { - requiredArgs = append(requiredArgs, fmt.Sprintf("%s %s", snakeToCamel(f.Name), fieldType)) continue } + fieldType, err := f.getPreferredType(d) + if err != nil { + return "", fmt.Errorf("failed to get preferred type: %w", err) + } + optionals.WriteString("\n// " + f.GetDescription()) optionals.WriteString("\n" + fmt.Sprintf("%s %s", snakeToTitle(f.Name), fieldType)) } optionalsName := m.optsName() optionalsStructBuilder := strings.Builder{} - optionalsStructBuilder.WriteString(fmt.Sprintf("\n// %s is the set of optional fields for Bot.%s.", optionalsName, strings.Title(m.Name))) + optionalsStructBuilder.WriteString(fmt.Sprintf("\n// %s is the set of optional fields for Bot.%s and Bot.%sWithContext.", optionalsName, strings.Title(m.Name), strings.Title(m.Name))) optionalsStructBuilder.WriteString("\ntype " + optionalsName + " struct {") optionalsStructBuilder.WriteString(optionals.String()) optionalsStructBuilder.WriteString("\n// RequestOpts are an additional optional field to configure timeouts for individual requests") optionalsStructBuilder.WriteString("\nRequestOpts *RequestOpts") optionalsStructBuilder.WriteString("\n}") - requiredArgs = append(requiredArgs, fmt.Sprintf("opts *%s", optionalsName)) - - return requiredArgs, optionalsStructBuilder.String(), nil + return optionalsStructBuilder.String(), nil } type readerBranchesData struct { From aaf009101f83b3cb82f519ff432dc2031156dae5 Mon Sep 17 00:00:00 2001 From: Paul Larsen Date: Tue, 3 Sep 2024 22:59:41 +0100 Subject: [PATCH 3/3] simplify interface and fix wrong context use --- bot.go | 7 ++----- request.go | 16 ++++++++-------- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/bot.go b/bot.go index 340847f..e029ed2 100644 --- a/bot.go +++ b/bot.go @@ -95,13 +95,10 @@ func (bot *Bot) Request(method string, params map[string]string, data map[string return bot.RequestWithContext(context.Background(), method, params, data, opts) } -func (bot *Bot) RequestWithContext(parentCtx context.Context, method string, params map[string]string, data map[string]FileReader, opts *RequestOpts) (json.RawMessage, error) { +func (bot *Bot) RequestWithContext(ctx context.Context, method string, params map[string]string, data map[string]FileReader, opts *RequestOpts) (json.RawMessage, error) { if bot.BotClient == nil { return nil, ErrNilBotClient } - childCtx, cancel := bot.BotClient.TimeoutContext(parentCtx, opts) - defer cancel() - - return bot.BotClient.RequestWithContext(childCtx, bot.Token, method, params, data, opts) + return bot.BotClient.RequestWithContext(ctx, bot.Token, method, params, data, opts) } diff --git a/request.go b/request.go index dd703b2..ee7e85c 100644 --- a/request.go +++ b/request.go @@ -22,8 +22,6 @@ const ( type BotClient interface { // RequestWithContext submits a POST HTTP request a bot API instance. RequestWithContext(ctx context.Context, token string, method string, params map[string]string, data map[string]FileReader, opts *RequestOpts) (json.RawMessage, error) - // TimeoutContext wraps the existing context with the timeout from the provided RequestOpts, or any default opts defined by the BotClient. - TimeoutContext(ctx context.Context, opts *RequestOpts) (context.Context, context.CancelFunc) // GetAPIURL gets the URL of the API either in use by the bot or defined in the request opts. GetAPIURL(opts *RequestOpts) string // FileURL gets the URL of a file at the API address that the bot is interacting with. @@ -82,8 +80,8 @@ type RequestOpts struct { APIURL string } -// TimeoutContext returns the appropriate context for the current settings. -func (bot *BaseBotClient) TimeoutContext(parentCtx context.Context, opts *RequestOpts) (context.Context, context.CancelFunc) { +// getTimeoutContext returns the appropriate context for the current settings. +func (bot *BaseBotClient) getTimeoutContext(parentCtx context.Context, opts *RequestOpts) (context.Context, context.CancelFunc) { if parentCtx == nil { parentCtx = context.Background() } @@ -102,7 +100,7 @@ func (bot *BaseBotClient) TimeoutContext(parentCtx context.Context, opts *Reques } } - return context.WithTimeout(context.Background(), DefaultTimeout) + return context.WithTimeout(parentCtx, DefaultTimeout) } func timeoutFromOpts(parentCtx context.Context, opts *RequestOpts) (context.Context, context.CancelFunc) { @@ -131,9 +129,11 @@ func timeoutFromOpts(parentCtx context.Context, opts *RequestOpts) (context.Cont // - method: the telegram API method to call. // - params: map of parameters to be sending to the telegram API. eg: chat_id, user_id, etc. // - data: map of any files to be sending to the telegram API. -// - opts: request opts to use. Note: Timeout opts are ignored when used in RequestWithContext. Timeout handling is the -// responsibility of the caller/context owner. -func (bot *BaseBotClient) RequestWithContext(ctx context.Context, token string, method string, params map[string]string, data map[string]FileReader, opts *RequestOpts) (json.RawMessage, error) { +// - opts: request opts to use. +func (bot *BaseBotClient) RequestWithContext(parentCtx context.Context, token string, method string, params map[string]string, data map[string]FileReader, opts *RequestOpts) (json.RawMessage, error) { + ctx, cancel := bot.getTimeoutContext(parentCtx, opts) + defer cancel() + b := &bytes.Buffer{} var contentType string