Skip to content

Commit

Permalink
Updated methods to Telegram Bot API v7.9
Browse files Browse the repository at this point in the history
  • Loading branch information
mymmrac committed Aug 20, 2024
1 parent b45fcde commit 823f19f
Show file tree
Hide file tree
Showing 5 changed files with 222 additions and 16 deletions.
3 changes: 2 additions & 1 deletion internal/generator/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
82 changes: 75 additions & 7 deletions methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
43 changes: 43 additions & 0 deletions methods_setters.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
44 changes: 36 additions & 8 deletions methods_setters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,29 +435,31 @@ 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").
WithCaptionEntities([]MessageEntity{{Type: "CaptionEntities"}}...).
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",
CaptionEntities: []MessageEntity{{Type: "CaptionEntities"}},
ShowCaptionAboveMedia: true,
DisableNotification: true,
ProtectContent: true,
ReplyParameters: &ReplyParameters{MessageID: 2},
ReplyParameters: &ReplyParameters{MessageID: 3},
ReplyMarkup: &ReplyKeyboardRemove{RemoveKeyboard: true},
}, s)
}
Expand Down Expand Up @@ -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)
}
Expand Down
66 changes: 66 additions & 0 deletions methods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 823f19f

Please sign in to comment.