From 823f19f397d99672206b2223630e5311b570e59c Mon Sep 17 00:00:00 2001 From: Artem Yadelskyi Date: Tue, 20 Aug 2024 11:11:40 +0300 Subject: [PATCH] Updated methods to Telegram Bot API v7.9 --- internal/generator/methods.go | 3 +- methods.go | 82 ++++++++++++++++++++++++++++++++--- methods_setters.go | 43 ++++++++++++++++++ methods_setters_test.go | 44 +++++++++++++++---- methods_test.go | 66 ++++++++++++++++++++++++++++ 5 files changed, 222 insertions(+), 16 deletions(-) diff --git a/internal/generator/methods.go b/internal/generator/methods.go index 3a7dad4..8261e93 100644 --- a/internal/generator/methods.go +++ b/internal/generator/methods.go @@ -358,7 +358,8 @@ func parameterSpecialCases(parameter *tgMethodParameter, methodName string) { parameter.typ = "[]InputMedia" } - if (parameter.name == "UserId" || parameter.name == "ChatId" || parameter.name == "SenderChatId") && + if (parameter.name == "UserId" || parameter.name == "ChatId" || parameter.name == "SenderChatId" || + strings.Contains(parameter.description, "number of seconds")) && parameter.typ == "int" { parameter.typ = "int64" } diff --git a/methods.go b/methods.go index e6dac5a..35af6f7 100644 --- a/methods.go +++ b/methods.go @@ -1080,8 +1080,13 @@ func (b *Bot) SendVideoNote(params *SendVideoNoteParams) (*Message, error) { // SendPaidMediaParams - Represents parameters of sendPaidMedia method. type SendPaidMediaParams struct { + // BusinessConnectionID - Optional. Unique identifier of the business connection on behalf of which the + // message will be sent + BusinessConnectionID string `json:"business_connection_id,omitempty"` + // ChatID - Unique identifier for the target chat or username of the target channel (in the format - // @channel_username) + // @channel_username). If the chat is a channel, all Telegram Star proceeds from this media will be credited to + // the chat's balance. Otherwise, they will be credited to the bot's balance. ChatID ChatID `json:"chat_id"` // StarCount - The number of Telegram Stars that must be paid to buy access to the media @@ -1121,7 +1126,7 @@ type SendPaidMediaParams struct { ReplyMarkup ReplyMarkup `json:"reply_markup,omitempty"` } -// SendPaidMedia - Use this method to send paid media to channel chats. On success, the sent Message +// SendPaidMedia - Use this method to send paid media. On success, the sent Message // (https://core.telegram.org/bots/api#message) is returned. func (b *Bot) SendPaidMedia(params *SendPaidMediaParams) (*Message, error) { var message *Message @@ -1618,7 +1623,8 @@ type SetMessageReactionParams struct { // Reaction - Optional. 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. + // either already present on the message or explicitly allowed by chat administrators. Paid reactions can't be + // used by bots. Reaction []ReactionType `json:"reaction,omitempty"` // IsBig - Optional. Pass True to set the reaction with a big animation @@ -1627,7 +1633,7 @@ type SetMessageReactionParams struct { // 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. Returns True on success. +// available reactions as messages in the channel. Bots can't use paid reactions. Returns True on success. func (b *Bot) SetMessageReaction(params *SetMessageReactionParams) error { err := b.performRequest("setMessageReaction", params) if err != nil { @@ -2047,6 +2053,68 @@ func (b *Bot) EditChatInviteLink(params *EditChatInviteLinkParams) (*ChatInviteL return chatInviteLink, nil } +// CreateChatSubscriptionInviteLinkParams - Represents parameters of createChatSubscriptionInviteLink method. +type CreateChatSubscriptionInviteLinkParams struct { + // ChatID - Unique identifier for the target channel chat or username of the target channel (in the format + // @channel_username) + ChatID ChatID `json:"chat_id"` + + // Name - Optional. Invite link name; 0-32 characters + Name string `json:"name,omitempty"` + + // SubscriptionPeriod - The number of seconds the subscription will be active for before the next payment. + // Currently, it must always be 2592000 (30 days). + SubscriptionPeriod int64 `json:"subscription_period"` + + // SubscriptionPrice - 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 + SubscriptionPrice int `json:"subscription_price"` +} + +// CreateChatSubscriptionInviteLink - Use this method to create a subscription invite link +// (https://telegram.org/blog/superchannels-star-reactions-subscriptions#star-subscriptions) for a channel chat. +// The bot must have the can_invite_users administrator rights. The link can be edited using the method +// editChatSubscriptionInviteLink (https://core.telegram.org/bots/api#editchatsubscriptioninvitelink) or revoked +// using the method revokeChatInviteLink (https://core.telegram.org/bots/api#revokechatinvitelink). Returns the +// new invite link as a ChatInviteLink (https://core.telegram.org/bots/api#chatinvitelink) object. +func (b *Bot) CreateChatSubscriptionInviteLink( + params *CreateChatSubscriptionInviteLinkParams, +) (*ChatInviteLink, error) { + var chatInviteLink *ChatInviteLink + err := b.performRequest("createChatSubscriptionInviteLink", params, &chatInviteLink) + if err != nil { + return nil, fmt.Errorf("telego: createChatSubscriptionInviteLink(): %w", err) + } + + return chatInviteLink, nil +} + +// EditChatSubscriptionInviteLinkParams - Represents parameters of editChatSubscriptionInviteLink method. +type EditChatSubscriptionInviteLinkParams struct { + // ChatID - Unique identifier for the target chat or username of the target channel (in the format + // @channel_username) + ChatID ChatID `json:"chat_id"` + + // InviteLink - The invite link to edit + InviteLink string `json:"invite_link"` + + // Name - Optional. Invite link name; 0-32 characters + Name string `json:"name,omitempty"` +} + +// 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 (https://core.telegram.org/bots/api#chatinvitelink) object. +func (b *Bot) EditChatSubscriptionInviteLink(params *EditChatSubscriptionInviteLinkParams) (*ChatInviteLink, error) { + var chatInviteLink *ChatInviteLink + err := b.performRequest("editChatSubscriptionInviteLink", params, &chatInviteLink) + if err != nil { + return nil, fmt.Errorf("telego: editChatSubscriptionInviteLink(): %w", err) + } + + return chatInviteLink, nil +} + // RevokeChatInviteLinkParams - Represents parameters of revokeChatInviteLink method. type RevokeChatInviteLinkParams struct { // ChatID - Unique identifier of the target chat or username of the target channel (in the format @@ -2493,8 +2561,8 @@ type EditForumTopicParams struct { } // EditForumTopic - Use this method to edit name and icon of a topic in a forum supergroup chat. The bot must -// be an administrator in the chat for this to work and must have can_manage_topics administrator rights, unless -// it is the creator of the topic. Returns True on success. +// be an administrator in the chat for this to work and must have the can_manage_topics administrator rights, +// unless it is the creator of the topic. Returns True on success. func (b *Bot) EditForumTopic(params *EditForumTopicParams) error { err := b.performRequest("editForumTopic", params) if err != nil { @@ -2603,7 +2671,7 @@ type EditGeneralForumTopicParams struct { } // EditGeneralForumTopic - Use this method to edit the name of the 'General' topic in a forum supergroup -// chat. The bot must be an administrator in the chat for this to work and must have can_manage_topics +// chat. The bot must be an administrator in the chat for this to work and must have the can_manage_topics // administrator rights. Returns True on success. func (b *Bot) EditGeneralForumTopic(params *EditGeneralForumTopicParams) error { err := b.performRequest("editGeneralForumTopic", params) diff --git a/methods_setters.go b/methods_setters.go index b932966..81e86ec 100644 --- a/methods_setters.go +++ b/methods_setters.go @@ -966,6 +966,12 @@ func (p *SendVideoNoteParams) WithReplyMarkup(replyMarkup ReplyMarkup) *SendVide return p } +// WithBusinessConnectionID adds business connection ID parameter +func (p *SendPaidMediaParams) WithBusinessConnectionID(businessConnectionID string) *SendPaidMediaParams { + p.BusinessConnectionID = businessConnectionID + return p +} + // WithChatID adds chat ID parameter func (p *SendPaidMediaParams) WithChatID(chatID ChatID) *SendPaidMediaParams { p.ChatID = chatID @@ -1789,6 +1795,43 @@ func (p *EditChatInviteLinkParams) WithCreatesJoinRequest() *EditChatInviteLinkP return p } +// WithChatID adds chat ID parameter +func (p *CreateChatSubscriptionInviteLinkParams) WithChatID(chatID ChatID) *CreateChatSubscriptionInviteLinkParams { + p.ChatID = chatID + return p +} + +// WithName adds name parameter +func (p *CreateChatSubscriptionInviteLinkParams) WithName(name string) *CreateChatSubscriptionInviteLinkParams { + p.Name = name + return p +} + +// WithSubscriptionPrice adds subscription price parameter +func (p *CreateChatSubscriptionInviteLinkParams) WithSubscriptionPrice(subscriptionPrice int, +) *CreateChatSubscriptionInviteLinkParams { + p.SubscriptionPrice = subscriptionPrice + return p +} + +// WithChatID adds chat ID parameter +func (p *EditChatSubscriptionInviteLinkParams) WithChatID(chatID ChatID) *EditChatSubscriptionInviteLinkParams { + p.ChatID = chatID + return p +} + +// WithInviteLink adds invite link parameter +func (p *EditChatSubscriptionInviteLinkParams) WithInviteLink(inviteLink string) *EditChatSubscriptionInviteLinkParams { + p.InviteLink = inviteLink + return p +} + +// WithName adds name parameter +func (p *EditChatSubscriptionInviteLinkParams) WithName(name string) *EditChatSubscriptionInviteLinkParams { + p.Name = name + return p +} + // WithChatID adds chat ID parameter func (p *RevokeChatInviteLinkParams) WithChatID(chatID ChatID) *RevokeChatInviteLinkParams { p.ChatID = chatID diff --git a/methods_setters_test.go b/methods_setters_test.go index 13cda94..7abb2a3 100644 --- a/methods_setters_test.go +++ b/methods_setters_test.go @@ -435,8 +435,9 @@ func TestSendVideoNoteParams_Setters(t *testing.T) { func TestSendPaidMediaParams_Setters(t *testing.T) { s := (&SendPaidMediaParams{}). - WithChatID(ChatID{ID: 6}). - WithStarCount(1). + WithBusinessConnectionID("BusinessConnectionID"). + WithChatID(ChatID{ID: 1}). + WithStarCount(2). WithMedia([]InputPaidMedia{&InputPaidMediaPhoto{}}...). WithCaption("Caption"). WithParseMode("ParseMode"). @@ -444,12 +445,13 @@ func TestSendPaidMediaParams_Setters(t *testing.T) { WithShowCaptionAboveMedia(). WithDisableNotification(). WithProtectContent(). - WithReplyParameters(&ReplyParameters{MessageID: 2}). + WithReplyParameters(&ReplyParameters{MessageID: 3}). WithReplyMarkup(&ReplyKeyboardRemove{RemoveKeyboard: true}) assert.Equal(t, &SendPaidMediaParams{ - ChatID: ChatID{ID: 6}, - StarCount: 1, + BusinessConnectionID: "BusinessConnectionID", + ChatID: ChatID{ID: 1}, + StarCount: 2, Media: []InputPaidMedia{&InputPaidMediaPhoto{}}, Caption: "Caption", ParseMode: "ParseMode", @@ -457,7 +459,7 @@ func TestSendPaidMediaParams_Setters(t *testing.T) { ShowCaptionAboveMedia: true, DisableNotification: true, ProtectContent: true, - ReplyParameters: &ReplyParameters{MessageID: 2}, + ReplyParameters: &ReplyParameters{MessageID: 3}, ReplyMarkup: &ReplyKeyboardRemove{RemoveKeyboard: true}, }, s) } @@ -861,13 +863,39 @@ func TestEditChatInviteLinkParams_Setters(t *testing.T) { }, e) } +func TestCreateChatSubscriptionInviteLinkParams_Setters(t *testing.T) { + c := (&CreateChatSubscriptionInviteLinkParams{}). + WithChatID(ChatID{ID: 2}). + WithName("Name"). + WithSubscriptionPrice(1) + + assert.Equal(t, &CreateChatSubscriptionInviteLinkParams{ + ChatID: ChatID{ID: 2}, + Name: "Name", + SubscriptionPrice: 1, + }, c) +} + +func TestEditChatSubscriptionInviteLinkParams_Setters(t *testing.T) { + e := (&EditChatSubscriptionInviteLinkParams{}). + WithChatID(ChatID{ID: 2}). + WithInviteLink("InviteLink"). + WithName("Name") + + assert.Equal(t, &EditChatSubscriptionInviteLinkParams{ + ChatID: ChatID{ID: 2}, + InviteLink: "InviteLink", + Name: "Name", + }, e) +} + func TestRevokeChatInviteLinkParams_Setters(t *testing.T) { r := (&RevokeChatInviteLinkParams{}). - WithChatID(ChatID{ID: 2}). + WithChatID(ChatID{ID: 1}). WithInviteLink("InviteLink") assert.Equal(t, &RevokeChatInviteLinkParams{ - ChatID: ChatID{ID: 2}, + ChatID: ChatID{ID: 1}, InviteLink: "InviteLink", }, r) } diff --git a/methods_test.go b/methods_test.go index cb0a027..7401e18 100644 --- a/methods_test.go +++ b/methods_test.go @@ -1230,6 +1230,72 @@ func TestBot_EditChatInviteLink(t *testing.T) { }) } +func TestBot_CreateChatSubscriptionInviteLink(t *testing.T) { + ctrl := gomock.NewController(t) + m := newMockedBot(ctrl) + + t.Run("success", func(t *testing.T) { + m.MockRequestConstructor.EXPECT(). + JSONRequest(gomock.Any()). + Return(data, nil) + + expectedChatInviteLink := &ChatInviteLink{ + InviteLink: "InviteLink", + } + resp := telegoResponse(t, expectedChatInviteLink) + m.MockAPICaller.EXPECT(). + Call(gomock.Any(), gomock.Any()). + Return(resp, nil) + + chatInviteLink, err := m.Bot.CreateChatSubscriptionInviteLink(nil) + require.NoError(t, err) + assert.Equal(t, expectedChatInviteLink, chatInviteLink) + }) + + t.Run("error", func(t *testing.T) { + m.MockRequestConstructor.EXPECT(). + JSONRequest(gomock.Any()). + Return(nil, errTest) + + chatInviteLink, err := m.Bot.CreateChatSubscriptionInviteLink(nil) + require.Error(t, err) + assert.Nil(t, chatInviteLink) + }) +} + +func TestBot_EditChatSubscriptionInviteLink(t *testing.T) { + ctrl := gomock.NewController(t) + m := newMockedBot(ctrl) + + t.Run("success", func(t *testing.T) { + m.MockRequestConstructor.EXPECT(). + JSONRequest(gomock.Any()). + Return(data, nil) + + expectedChatInviteLink := &ChatInviteLink{ + InviteLink: "InviteLink", + } + resp := telegoResponse(t, expectedChatInviteLink) + m.MockAPICaller.EXPECT(). + Call(gomock.Any(), gomock.Any()). + Return(resp, nil) + + chatInviteLink, err := m.Bot.EditChatSubscriptionInviteLink(nil) + require.NoError(t, err) + assert.Equal(t, expectedChatInviteLink, chatInviteLink) + }) + + t.Run("error", func(t *testing.T) { + m.MockRequestConstructor.EXPECT(). + JSONRequest(gomock.Any()). + Return(nil, errTest) + + chatInviteLink, err := m.Bot.EditChatSubscriptionInviteLink(nil) + require.Error(t, err) + assert.Nil(t, chatInviteLink) + }) +} + func TestBot_RevokeChatInviteLink(t *testing.T) { ctrl := gomock.NewController(t) m := newMockedBot(ctrl)