Skip to content

Commit

Permalink
v7.0 (#55)
Browse files Browse the repository at this point in the history
* API v7.0

* readme
  • Loading branch information
negasus authored Jan 10, 2024
1 parent 9485c23 commit 96a4c35
Show file tree
Hide file tree
Showing 21 changed files with 1,123 additions and 321 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## v1.0.0 (2024-01-10)

- support API v7.0

## v0.8.3 (2023-12-07)

- Fix typo for setChatDescription params (#49)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

> [Telegram Group](https://t.me/gotelegrambotui)
> Supports Bot API version: [6.9](https://core.telegram.org/bots/api#september-22-2023) from September 22, 2023
> Supports Bot API version: [7.0](https://core.telegram.org/bots/api#december-29-2023) from December 29, 2023
It's a Go zero-dependencies telegram bot framework

Expand Down
35 changes: 35 additions & 0 deletions methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,27 @@ func (b *Bot) ForwardMessage(ctx context.Context, params *ForwardMessageParams)
return result, err
}

// ForwardMessages https://core.telegram.org/bots/api#forwardmessages
func (b *Bot) ForwardMessages(ctx context.Context, params *ForwardMessagesParams) ([]models.Message, error) {
var result []models.Message
err := b.rawRequest(ctx, "forwardMessages", params, result)
return result, err
}

// CopyMessage https://core.telegram.org/bots/api#copymessage
func (b *Bot) CopyMessage(ctx context.Context, params *CopyMessageParams) (*models.MessageID, error) {
result := &models.MessageID{}
err := b.rawRequest(ctx, "copyMessage", params, result)
return result, err
}

// CopyMessages https://core.telegram.org/bots/api#copymessages
func (b *Bot) CopyMessages(ctx context.Context, params *CopyMessagesParams) ([]models.MessageID, error) {
var result []models.MessageID
err := b.rawRequest(ctx, "copyMessages", params, result)
return result, err
}

// SendPhoto https://core.telegram.org/bots/api#sendphoto
func (b *Bot) SendPhoto(ctx context.Context, params *SendPhotoParams) (*models.Message, error) {
mes := &models.Message{}
Expand Down Expand Up @@ -181,6 +195,13 @@ func (b *Bot) SendChatAction(ctx context.Context, params *SendChatActionParams)
return result, err
}

// SetMessageReaction https://core.telegram.org/bots/api#setmessagereaction
func (b *Bot) SetMessageReaction(ctx context.Context, params *SetMessageReactionParams) (bool, error) {
var result bool
err := b.rawRequest(ctx, "setMessageReaction", params, result)
return result, err
}

// GetUserProfilePhotos https://core.telegram.org/bots/api#getuserprofilephotos
func (b *Bot) GetUserProfilePhotos(ctx context.Context, params *GetUserProfilePhotosParams) (*models.UserProfilePhotos, error) {
result := &models.UserProfilePhotos{}
Expand Down Expand Up @@ -489,6 +510,13 @@ func (b *Bot) AnswerCallbackQuery(ctx context.Context, params *AnswerCallbackQue
return result, err
}

// GetUserChatBoosts https://core.telegram.org/bots/api#getuserchatboosts
func (b *Bot) GetUserChatBoosts(ctx context.Context, params *GetUserChatBoostsParams) (*models.UserChatBoosts, error) {
result := &models.UserChatBoosts{}
err := b.rawRequest(ctx, "getUserChatBoosts", params, &result)
return result, err
}

// SetMyCommands https://core.telegram.org/bots/api#setmycommands
func (b *Bot) SetMyCommands(ctx context.Context, params *SetMyCommandsParams) (bool, error) {
var result bool
Expand Down Expand Up @@ -622,6 +650,13 @@ func (b *Bot) DeleteMessage(ctx context.Context, params *DeleteMessageParams) (b
return result, err
}

// DeleteMessages https://core.telegram.org/bots/api#deletemessages
func (b *Bot) DeleteMessages(ctx context.Context, params *DeleteMessagesParams) (bool, error) {
var result bool
err := b.rawRequest(ctx, "deleteMessages", params, &result)
return result, err
}

// SendSticker https://core.telegram.org/bots/api#sendsticker
func (b *Bot) SendSticker(ctx context.Context, params *SendStickerParams) (*models.Message, error) {
result := &models.Message{}
Expand Down
578 changes: 311 additions & 267 deletions methods_params.go

Large diffs are not rendered by default.

96 changes: 96 additions & 0 deletions models/boost.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package models

import (
"encoding/json"
"fmt"
)

// ChatBoostUpdated https://core.telegram.org/bots/api#chatboostupdated
type ChatBoostUpdated struct {
Chat Chat `json:"chat"`
Boost ChatBoost `json:"boost"`
}

// ChatBoostRemoved https://core.telegram.org/bots/api#chatboostremoved
type ChatBoostRemoved struct {
Chat Chat `json:"chat"`
BoostID string `json:"boost_id"`
RemoveDate int `json:"remove_date"`
Source ChatBoostSource `json:"source"`
}

// UserChatBoosts https://core.telegram.org/bots/api#userchatboosts
type UserChatBoosts struct {
Boosts []ChatBoost `json:"boosts"`
}

// ChatBoost https://core.telegram.org/bots/api#chatboost
type ChatBoost struct {
BoostID string `json:"boost_id"`
AddDate int `json:"add_date"`
ExpirationDate int `json:"expiration_date"`
Source ChatBoostSource `json:"source"`
}

// ChatBoostSource https://core.telegram.org/bots/api#chatboostsource
type ChatBoostSource struct {
Source ChatBoostSourceType

ChatBoostSourcePremium *ChatBoostSourcePremium
ChatBoostSourceGiftCode *ChatBoostSourceGiftCode
ChatBoostSourceGiveaway *ChatBoostSourceGiveaway
}

func (cbs *ChatBoostSource) UnmarshalJSON(data []byte) error {
v := struct {
Source string `json:"source"`
}{}
err := json.Unmarshal(data, &v)
if err != nil {
return err
}

switch v.Source {
case "premium":
cbs.Source = ChatBoostSourceTypePremium
cbs.ChatBoostSourcePremium = &ChatBoostSourcePremium{}
return json.Unmarshal(data, cbs.ChatBoostSourcePremium)
case "gift_code":
cbs.Source = ChatBoostSourceTypeGiftCode
cbs.ChatBoostSourceGiftCode = &ChatBoostSourceGiftCode{}
return json.Unmarshal(data, cbs.ChatBoostSourceGiftCode)
case "giveaway":
cbs.Source = ChatBoostSourceTypeGiveaway
cbs.ChatBoostSourceGiveaway = &ChatBoostSourceGiveaway{}
return json.Unmarshal(data, cbs.ChatBoostSourceGiveaway)
}

return fmt.Errorf("unsupported ChatBoostSource type")
}

// ChatBoostSourceType https://core.telegram.org/bots/api#chatboostsource
type ChatBoostSourceType int

const (
ChatBoostSourceTypePremium ChatBoostSourceType = iota
ChatBoostSourceTypeGiftCode
ChatBoostSourceTypeGiveaway
)

// ChatBoostSourcePremium https://core.telegram.org/bots/api#chatboostsourcepremium
type ChatBoostSourcePremium struct {
Source string `json:"source"` // always “premium”
User User `json:"user"`
}

// ChatBoostSourceGiftCode https://core.telegram.org/bots/api#chatboostsourcegiftcode
type ChatBoostSourceGiftCode struct {
Source string `json:"source"` // always “gift_code”
User User `json:"user"`
}

// ChatBoostSourceGiveaway https://core.telegram.org/bots/api#chatboostsourcegiveaway
type ChatBoostSourceGiveaway struct {
Source string `json:"source"` // always “giveaway”
User User `json:"user"`
}
72 changes: 72 additions & 0 deletions models/boost_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package models

import (
"encoding/json"
"testing"
)

func TestChatBoostSource_premium(t *testing.T) {
src := `{"source":"premium","user":{"id":123}}`

var cbs ChatBoostSource
err := json.Unmarshal([]byte(src), &cbs)
if err != nil {
t.Fatal(err)
}

if cbs.Source != ChatBoostSourceTypePremium {
t.Fatal("invalid type")
}

if cbs.ChatBoostSourcePremium == nil {
t.Fatal("invalid ChatBoostSourcePremium")
}

if cbs.ChatBoostSourcePremium.User.ID != 123 {
t.Fatal("invalid user id")
}
}

func TestChatBoostSource_gift_code(t *testing.T) {
src := `{"source":"gift_code","user":{"id":123}}`

var cbs ChatBoostSource
err := json.Unmarshal([]byte(src), &cbs)
if err != nil {
t.Fatal(err)
}

if cbs.Source != ChatBoostSourceTypeGiftCode {
t.Fatal("invalid type")
}

if cbs.ChatBoostSourceGiftCode == nil {
t.Fatal("invalid ChatBoostSourceGiftCode")
}

if cbs.ChatBoostSourceGiftCode.User.ID != 123 {
t.Fatal("invalid user id")
}
}

func TestChatBoostSource_giveaway(t *testing.T) {
src := `{"source":"giveaway","user":{"id":123}}`

var cbs ChatBoostSource
err := json.Unmarshal([]byte(src), &cbs)
if err != nil {
t.Fatal(err)
}

if cbs.Source != ChatBoostSourceTypeGiveaway {
t.Fatal("invalid type")
}

if cbs.ChatBoostSourceGiveaway == nil {
t.Fatal("invalid ChatBoostSourceGiveaway")
}

if cbs.ChatBoostSourceGiveaway.User.ID != 123 {
t.Fatal("invalid user id")
}
}
14 changes: 7 additions & 7 deletions models/callback_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package models

// CallbackQuery https://core.telegram.org/bots/api#callbackquery
type CallbackQuery struct {
ID string `json:"id"`
Sender User `json:"from,omitempty"`
Message *Message `json:"message,omitempty"`
InlineMessageID string `json:"inline_message_id,omitempty"`
ChatInstance string `json:"chat_instance,omitempty"`
Data string `json:"data,omitempty"`
GameShortName string `json:"game_short_name,omitempty"`
ID string `json:"id"`
From User `json:"from,omitempty"`
Message InaccessibleMessage `json:"message,omitempty"`
InlineMessageID string `json:"inline_message_id,omitempty"`
ChatInstance string `json:"chat_instance,omitempty"`
Data string `json:"data,omitempty"`
GameShortName string `json:"game_short_name,omitempty"`
}
6 changes: 6 additions & 0 deletions models/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ type Chat struct {
IsForum bool `json:"is_forum,omitempty"`
Photo *ChatPhoto `json:"photo,omitempty"`
ActiveUsernames []string `json:"active_usernames,omitempty"`
AvailableReactions []ReactionType `json:"available_reactions,omitempty"`
AccentColorID int `json:"accent_color_id,omitempty"`
BackgroundCustomEmojiID string `json:"background_custom_emoji_id,omitempty"`
ProfileAccentColorID int `json:"profile_accent_color_id,omitempty"`
ProfileBackgroundCustomEmojiID string `json:"profile_background_custom_emoji_id,omitempty"`
EmojiStatusCustomEmojiID string `json:"emoji_status_custom_emoji_id,omitempty"`
EmojiStatusExpirationDate int `json:"emoji_status_expiration_date,omitempty"`
Bio string `json:"bio"`
Expand All @@ -101,6 +106,7 @@ type Chat struct {
HasAggressiveAntiSpamEnabled bool `json:"has_aggressive_anti_spam_enabled,omitempty"`
HasHiddenMembers bool `json:"has_hidden_members,omitempty"`
HasProtectedContent bool `json:"has_protected_content,omitempty"`
HasVisibleHistory bool `json:"has_visible_history,omitempty"`
StickerSetName string `json:"sticker_set_name,omitempty"`
CanSetStickerSet bool `json:"can_set_sticker_set,omitempty"`
LinkedChatID int64 `json:"linked_chat_id,omitempty"`
Expand Down
26 changes: 14 additions & 12 deletions models/chat_member.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package models

import (
"bytes"
"encoding/json"
"fmt"
)
Expand Down Expand Up @@ -41,32 +40,35 @@ type ChatMember struct {
}

func (c *ChatMember) UnmarshalJSON(data []byte) error {
if bytes.Contains(data, []byte(`"status":"creator"`)) {
v := struct {
Status string `json:"status"`
}{}
if err := json.Unmarshal(data, &v); err != nil {
return err
}

switch v.Status {
case "creator":
c.Type = ChatMemberTypeOwner
c.Owner = &ChatMemberOwner{}
return json.Unmarshal(data, c.Owner)
}
if bytes.Contains(data, []byte(`"status":"administrator"`)) {
case "administrator":
c.Type = ChatMemberTypeAdministrator
c.Administrator = &ChatMemberAdministrator{}
return json.Unmarshal(data, c.Administrator)
}
if bytes.Contains(data, []byte(`"status":"member"`)) {
case "member":
c.Type = ChatMemberTypeMember
c.Member = &ChatMemberMember{}
return json.Unmarshal(data, c.Member)
}
if bytes.Contains(data, []byte(`"status":"restricted"`)) {
case "restricted":
c.Type = ChatMemberTypeRestricted
c.Restricted = &ChatMemberRestricted{}
return json.Unmarshal(data, c.Restricted)
}
if bytes.Contains(data, []byte(`"status":"left"`)) {
case "left":
c.Type = ChatMemberTypeLeft
c.Left = &ChatMemberLeft{}
return json.Unmarshal(data, c.Left)
}
if bytes.Contains(data, []byte(`"status":"kicked"`)) {
case "kicked":
c.Type = ChatMemberTypeBanned
c.Banned = &ChatMemberBanned{}
return json.Unmarshal(data, c.Banned)
Expand Down
6 changes: 6 additions & 0 deletions models/forum.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ type UserShared struct {
UserID int64 `json:"user_id"`
}

// UsersShared https://core.telegram.org/bots/api#usersshared
type UsersShared struct {
RequestID int `json:"request_id"`
UserIDs []int64 `json:"users_id"`
}

// ChatShared https://core.telegram.org/bots/api#chatshared
type ChatShared struct {
RequestID int `json:"request_id"`
Expand Down
Loading

0 comments on commit 96a4c35

Please sign in to comment.