Skip to content

Implement Bot API 6.4 changes #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
191 changes: 163 additions & 28 deletions configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,21 @@ func (edit BaseEdit) params() (Params, error) {
return params, err
}

// BaseSpoiler is base type of structures with spoilers.
type BaseSpoiler struct {
HasSpoiler bool
}

func (spoiler BaseSpoiler) params() (Params, error) {
params := make(Params)

if spoiler.HasSpoiler {
params.AddBool("has_spoiler", true)
}

return params, nil
}

// MessageConfig contains information about a SendMessage request.
type MessageConfig struct {
BaseChat
Expand Down Expand Up @@ -407,6 +422,7 @@ func (config CopyMessageConfig) method() string {
// PhotoConfig contains information about a SendPhoto request.
type PhotoConfig struct {
BaseFile
BaseSpoiler
Thumb RequestFileData
Caption string
ParseMode string
Expand All @@ -422,6 +438,15 @@ func (config PhotoConfig) params() (Params, error) {
params.AddNonEmpty("caption", config.Caption)
params.AddNonEmpty("parse_mode", config.ParseMode)
err = params.AddInterface("caption_entities", config.CaptionEntities)
if err != nil {
return params, err
}

p1, err := config.BaseSpoiler.params()
if err != nil {
return params, err
}
params.Merge(p1)

return params, err
}
Expand Down Expand Up @@ -557,6 +582,7 @@ func (config StickerConfig) files() []RequestFile {
// VideoConfig contains information about a SendVideo request.
type VideoConfig struct {
BaseFile
BaseSpoiler
Thumb RequestFileData
Duration int
Caption string
Expand All @@ -576,6 +602,15 @@ func (config VideoConfig) params() (Params, error) {
params.AddNonEmpty("parse_mode", config.ParseMode)
params.AddBool("supports_streaming", config.SupportsStreaming)
err = params.AddInterface("caption_entities", config.CaptionEntities)
if err != nil {
return params, err
}

p1, err := config.BaseSpoiler.params()
if err != nil {
return params, err
}
params.Merge(p1)

return params, err
}
Expand Down Expand Up @@ -603,6 +638,7 @@ func (config VideoConfig) files() []RequestFile {
// AnimationConfig contains information about a SendAnimation request.
type AnimationConfig struct {
BaseFile
BaseSpoiler
Duration int
Thumb RequestFileData
Caption string
Expand All @@ -620,6 +656,15 @@ func (config AnimationConfig) params() (Params, error) {
params.AddNonEmpty("caption", config.Caption)
params.AddNonEmpty("parse_mode", config.ParseMode)
err = params.AddInterface("caption_entities", config.CaptionEntities)
if err != nil {
return params, err
}

p1, err := config.BaseSpoiler.params()
if err != nil {
return params, err
}
params.Merge(p1)

return params, err
}
Expand Down Expand Up @@ -976,13 +1021,15 @@ func (config GetGameHighScoresConfig) method() string {
// ChatActionConfig contains information about a SendChatAction request.
type ChatActionConfig struct {
BaseChat
MessageThreadID int
Action string // required
}

func (config ChatActionConfig) params() (Params, error) {
params, err := config.BaseChat.params()

params["action"] = config.Action
params.AddNonZero("message_thread_id", config.MessageThreadID)

return params, err
}
Expand Down Expand Up @@ -2314,14 +2361,27 @@ func (config GetForumTopicIconStickersConfig) params() (Params, error) {
return nil, nil
}

// BaseForum is a base type for all forum config types.
type BaseForum struct {
ChatID int64
SuperGroupUsername string
}

func (config BaseForum) params() (Params, error) {
params := make(Params)

params.AddFirstValid("chat_id", config.ChatID, config.SuperGroupUsername)

return params, nil
}

// CreateForumTopicConfig allows you to create a topic
// in a forum supergroup chat.
type CreateForumTopicConfig struct {
ChatID int64
Name string
IconColor int
IconCustomEmojiID string
SuperGroupUsername string
BaseForum
Name string
IconColor int
IconCustomEmojiID string
}

func (config CreateForumTopicConfig) method() string {
Expand All @@ -2331,22 +2391,23 @@ func (config CreateForumTopicConfig) method() string {
func (config CreateForumTopicConfig) params() (Params, error) {
params := make(Params)

params.AddFirstValid("chat_id", config.ChatID, config.SuperGroupUsername)
params.AddNonEmpty("name", config.Name)
params.AddNonZero("icon_color", config.IconColor)
params.AddNonEmpty("icon_custom_emoji_id", config.IconCustomEmojiID)

p1, _ := config.BaseForum.params()
params.Merge(p1)

return params, nil
}

// EditForumTopicConfig allows you to edit
// name and icon of a topic in a forum supergroup chat.
type EditForumTopicConfig struct {
ChatID int64
MessageThreadID int
Name string
IconCustomEmojiID string
SuperGroupUsername string
BaseForum
MessageThreadID int
Name string
IconCustomEmojiID string
}

func (config EditForumTopicConfig) method() string {
Expand All @@ -2356,20 +2417,21 @@ func (config EditForumTopicConfig) method() string {
func (config EditForumTopicConfig) params() (Params, error) {
params := make(Params)

params.AddFirstValid("chat_id", config.ChatID, config.SuperGroupUsername)
params.AddNonZero("message_thread_id", config.MessageThreadID)
params.AddNonEmpty("icon_color", config.Name)
params.AddNonEmpty("name", config.Name)
params.AddNonEmpty("icon_custom_emoji_id", config.IconCustomEmojiID)

p1, _ := config.BaseForum.params()
params.Merge(p1)

return params, nil
}

// CloseForumTopicConfig allows you to close
// an open topic in a forum supergroup chat.
type CloseForumTopicConfig struct {
ChatID int64
MessageThreadID int
SuperGroupUsername string
BaseForum
MessageThreadID int
}

func (config CloseForumTopicConfig) method() string {
Expand All @@ -2379,18 +2441,19 @@ func (config CloseForumTopicConfig) method() string {
func (config CloseForumTopicConfig) params() (Params, error) {
params := make(Params)

params.AddFirstValid("chat_id", config.ChatID, config.SuperGroupUsername)
params.AddNonZero("message_thread_id", config.MessageThreadID)

p1, _ := config.BaseForum.params()
params.Merge(p1)

return params, nil
}

// ReopenForumTopicConfig allows you to reopen
// an closed topic in a forum supergroup chat.
type ReopenForumTopicConfig struct {
ChatID int64
MessageThreadID int
SuperGroupUsername string
BaseForum
MessageThreadID int
}

func (config ReopenForumTopicConfig) method() string {
Expand All @@ -2403,15 +2466,17 @@ func (config ReopenForumTopicConfig) params() (Params, error) {
params.AddFirstValid("chat_id", config.ChatID, config.SuperGroupUsername)
params.AddNonZero("message_thread_id", config.MessageThreadID)

p1, _ := config.BaseForum.params()
params.Merge(p1)

return params, nil
}

// DeleteForumTopicConfig allows you to delete a forum topic
// along with all its messages in a forum supergroup chat.
type DeleteForumTopicConfig struct {
ChatID int64
MessageThreadID int
SuperGroupUsername string
BaseForum
MessageThreadID int
}

func (config DeleteForumTopicConfig) method() string {
Expand All @@ -2421,18 +2486,19 @@ func (config DeleteForumTopicConfig) method() string {
func (config DeleteForumTopicConfig) params() (Params, error) {
params := make(Params)

params.AddFirstValid("chat_id", config.ChatID, config.SuperGroupUsername)
params.AddNonZero("message_thread_id", config.MessageThreadID)

p1, _ := config.BaseForum.params()
params.Merge(p1)

return params, nil
}

// UnpinAllForumTopicMessagesConfig allows you to clear the list
// of pinned messages in a forum topic.
type UnpinAllForumTopicMessagesConfig struct {
ChatID int64
MessageThreadID int
SuperGroupUsername string
BaseForum
MessageThreadID int
}

func (config UnpinAllForumTopicMessagesConfig) method() string {
Expand All @@ -2445,15 +2511,84 @@ func (config UnpinAllForumTopicMessagesConfig) params() (Params, error) {
params.AddFirstValid("chat_id", config.ChatID, config.SuperGroupUsername)
params.AddNonZero("message_thread_id", config.MessageThreadID)

p1, _ := config.BaseForum.params()
params.Merge(p1)

return params, nil
}

// UnpinAllForumTopicMessagesConfig allows you 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 administrator rights. Returns True on success.
type EditGeneralForumTopicConfig struct {
BaseForum
Name string
}

func (config EditGeneralForumTopicConfig) method() string {
return "editGeneralForumTopic"
}

func (config EditGeneralForumTopicConfig) params() (Params, error) {
params := make(Params)

params.AddNonEmpty("name", config.Name)

p1, _ := config.BaseForum.params()
params.Merge(p1)

return params, nil
}

// CloseGeneralForumTopicConfig allows you to to close an open 'General' topic
// in a forum supergroup 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.
type CloseGeneralForumTopicConfig struct{ BaseForum }

func (config CloseGeneralForumTopicConfig) method() string {
return "closeGeneralForumTopic"
}

// CloseGeneralForumTopicConfig allows you to reopen a closed 'General' topic
// in a forum supergroup chat. The bot must be an administrator in the chat
// for this to work and must have the can_manage_topics administrator rights.
// The topic will be automatically unhidden if it was hidden.
// Returns True on success.
type ReopenGeneralForumTopicConfig struct{ BaseForum }

func (config ReopenGeneralForumTopicConfig) method() string {
return "reopenGeneralForumTopic"
}

// HideGeneralForumTopicConfig allows you to hide the 'General' topic
// in a forum supergroup chat. The bot must be an administrator in the chat
// for this to work and must have the can_manage_topics administrator rights.
// The topic will be automatically closed if it was open.
// Returns True on success.
type HideGeneralForumTopicConfig struct{ BaseForum }

func (config HideGeneralForumTopicConfig) method() string {
return "hideGeneralForumTopic"
}

// UnhideGeneralForumTopicConfig allows you to unhide the 'General' topic
// in a forum supergroup 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.
type UnhideGeneralForumTopicConfig struct{ BaseForum }

func (config UnhideGeneralForumTopicConfig) method() string {
return "unhideGeneralForumTopic"
}

// MediaGroupConfig allows you to send a group of media.
//
// Media consist of InputMedia items (InputMediaPhoto, InputMediaVideo).
type MediaGroupConfig struct {
BaseChat
Media []interface{}
Media []interface{}
}

func (config MediaGroupConfig) method() string {
Expand Down
7 changes: 7 additions & 0 deletions params.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,10 @@ func (p Params) AddFirstValid(key string, args ...interface{}) error {

return nil
}

// Merge merges two sets of parameters. Overwrites old fields if present
func (p *Params) Merge(p1 Params) {
for k, v := range p1 {
(*p)[k] = v
}
}
Loading