From 10d348078218207a7f75e3773f21c225eff9c465 Mon Sep 17 00:00:00 2001 From: Demian Date: Mon, 20 Nov 2023 15:06:31 +0100 Subject: [PATCH] topic: finish and refactor the implementation --- context.go | 21 +++++++++++++- message.go | 12 ++++---- topic.go | 76 +++++++++++++++++++++++++-------------------------- update.go | 15 ++-------- video_chat.go | 36 ++++++++++++------------ 5 files changed, 86 insertions(+), 74 deletions(-) diff --git a/context.go b/context.go index 9654aae..3306c8f 100644 --- a/context.go +++ b/context.go @@ -46,12 +46,15 @@ type Context interface { // ChatMember returns chat member changes. ChatMember() *ChatMemberUpdate - // ChatJoinRequest returns cha + // ChatJoinRequest returns the chat join request. ChatJoinRequest() *ChatJoinRequest // Migration returns both migration from and to chat IDs. Migration() (int64, int64) + // Topic returns the topic changes. + Topic() *Topic + // Sender returns the current recipient, depending on the context type. // Returns nil if user is not presented. Sender() *User @@ -240,6 +243,22 @@ func (c *nativeContext) Migration() (int64, int64) { return c.u.Message.MigrateFrom, c.u.Message.MigrateTo } +func (c *nativeContext) Topic() *Topic { + m := c.u.Message + if m == nil { + return nil + } + switch { + case m.TopicCreated != nil: + return m.TopicCreated + case m.TopicReopened != nil: + return m.TopicReopened + case m.TopicEdited != nil: + return m.TopicEdited + } + return nil +} + func (c *nativeContext) Sender() *User { switch { case c.u.Callback != nil: diff --git a/message.go b/message.go index 2dec089..8299756 100644 --- a/message.go +++ b/message.go @@ -264,22 +264,22 @@ type Message struct { ReplyMarkup *ReplyMarkup `json:"reply_markup,omitempty"` // Service message: forum topic created - TopicCreated *TopicCreated `json:"forum_topic_created,omitempty"` + TopicCreated *Topic `json:"forum_topic_created,omitempty"` // Service message: forum topic closed - TopicClosed *TopicClosed `json:"forum_topic_closed,omitempty"` + TopicClosed *struct{} `json:"forum_topic_closed,omitempty"` // Service message: forum topic reopened - TopicReopened *TopicReopened `json:"forum_topic_reopened,omitempty"` + TopicReopened *Topic `json:"forum_topic_reopened,omitempty"` // Service message: forum topic deleted - TopicEdited *TopicEdited `json:"forum_topic_edited,omitempty"` + TopicEdited *Topic `json:"forum_topic_edited,omitempty"` // Service message: general forum topic hidden - GeneralTopicHidden *GeneralTopicHidden `json:"general_topic_hidden,omitempty"` + GeneralTopicHidden *struct{} `json:"general_topic_hidden,omitempty"` // Service message: general forum topic unhidden - GeneralTopicUnhidden *GeneralTopicUnhidden `json:"general_topic_unhidden,omitempty"` + GeneralTopicUnhidden *struct{} `json:"general_topic_unhidden,omitempty"` // Service message: represents spoiler information about the message. HasMediaSpoiler bool `json:"has_media_spoiler,omitempty"` diff --git a/topic.go b/topic.go index 40f2de2..d81fe9a 100644 --- a/topic.go +++ b/topic.go @@ -12,46 +12,46 @@ type Topic struct { ThreadID int `json:"message_thread_id"` } -type ( - TopicCreated struct{ Topic } - TopicClosed struct{} - TopicDeleted struct{ Topic } - TopicReopened struct{ Topic } - TopicEdited struct{ Topic } - GeneralTopicHidden struct{} - GeneralTopicUnhidden struct{} -) - // CreateTopic creates a topic in a forum supergroup chat. -func (b *Bot) CreateTopic(chat *Chat, forum *Topic) error { +func (b *Bot) CreateTopic(chat *Chat, topic *Topic) (*Topic, error) { params := map[string]string{ "chat_id": chat.Recipient(), - "name": forum.Name, + "name": topic.Name, } - if forum.IconColor != 0 { - params["icon_color"] = strconv.Itoa(forum.IconColor) + if topic.IconColor != 0 { + params["icon_color"] = strconv.Itoa(topic.IconColor) } - if forum.IconCustomEmojiID != "" { - params["icon_custom_emoji_id"] = forum.IconCustomEmojiID + if topic.IconCustomEmojiID != "" { + params["icon_custom_emoji_id"] = topic.IconCustomEmojiID } - _, err := b.Raw("createForumTopic", params) - return err + data, err := b.Raw("createForumTopic", params) + if err != nil { + return nil, err + } + + var resp struct { + Result *Topic + } + if err := json.Unmarshal(data, &resp); err != nil { + return nil, wrapError(err) + } + return resp.Result, err } // EditTopic edits name and icon of a topic in a forum supergroup chat. -func (b *Bot) EditTopic(chat *Chat, forum *Topic) error { +func (b *Bot) EditTopic(chat *Chat, topic *Topic) error { params := map[string]interface{}{ "chat_id": chat.Recipient(), - "message_thread_id": forum.ThreadID, + "message_thread_id": topic.ThreadID, } - if forum.Name != "" { - params["name"] = forum.Name + if topic.Name != "" { + params["name"] = topic.Name } - if forum.IconCustomEmojiID != "" { - params["icon_custom_emoji_id"] = forum.IconCustomEmojiID + if topic.IconCustomEmojiID != "" { + params["icon_custom_emoji_id"] = topic.IconCustomEmojiID } _, err := b.Raw("editForumTopic", params) @@ -59,10 +59,10 @@ func (b *Bot) EditTopic(chat *Chat, forum *Topic) error { } // CloseTopic closes an open topic in a forum supergroup chat. -func (b *Bot) CloseTopic(chat *Chat, forum *Topic) error { +func (b *Bot) CloseTopic(chat *Chat, topic *Topic) error { params := map[string]interface{}{ "chat_id": chat.Recipient(), - "message_thread_id": forum.ThreadID, + "message_thread_id": topic.ThreadID, } _, err := b.Raw("closeForumTopic", params) @@ -70,10 +70,10 @@ func (b *Bot) CloseTopic(chat *Chat, forum *Topic) error { } // ReopenTopic reopens a closed topic in a forum supergroup chat. -func (b *Bot) ReopenTopic(chat *Chat, forum *Topic) error { +func (b *Bot) ReopenTopic(chat *Chat, topic *Topic) error { params := map[string]interface{}{ "chat_id": chat.Recipient(), - "message_thread_id": forum.ThreadID, + "message_thread_id": topic.ThreadID, } _, err := b.Raw("reopenForumTopic", params) @@ -81,10 +81,10 @@ func (b *Bot) ReopenTopic(chat *Chat, forum *Topic) error { } // DeleteTopic deletes a forum topic along with all its messages in a forum supergroup chat. -func (b *Bot) DeleteTopic(chat *Chat, forum *Topic) error { +func (b *Bot) DeleteTopic(chat *Chat, topic *Topic) error { params := map[string]interface{}{ "chat_id": chat.Recipient(), - "message_thread_id": forum.ThreadID, + "message_thread_id": topic.ThreadID, } _, err := b.Raw("deleteForumTopic", params) @@ -92,10 +92,10 @@ func (b *Bot) DeleteTopic(chat *Chat, forum *Topic) error { } // UnpinAllTopicMessages clears the list of pinned messages in a forum topic. The bot must be an administrator in the chat for this to work and must have the can_pin_messages administrator right in the supergroup. -func (b *Bot) UnpinAllTopicMessages(chat *Chat, forum *Topic) error { +func (b *Bot) UnpinAllTopicMessages(chat *Chat, topic *Topic) error { params := map[string]interface{}{ "chat_id": chat.Recipient(), - "message_thread_id": forum.ThreadID, + "message_thread_id": topic.ThreadID, } _, err := b.Raw("unpinAllForumTopicMessages", params) @@ -121,10 +121,10 @@ func (b *Bot) TopicIconStickers() ([]Sticker, error) { } // EditGeneralTopic edits name of the 'General' topic in a forum supergroup chat. -func (b *Bot) EditGeneralTopic(chat *Chat, forum *Topic) error { +func (b *Bot) EditGeneralTopic(chat *Chat, topic *Topic) error { params := map[string]interface{}{ "chat_id": chat.Recipient(), - "name": forum.Name, + "name": topic.Name, } _, err := b.Raw("editGeneralForumTopic", params) @@ -132,7 +132,7 @@ func (b *Bot) EditGeneralTopic(chat *Chat, forum *Topic) error { } // CloseGeneralTopic closes an open 'General' topic in a forum supergroup chat. -func (b *Bot) CloseGeneralTopic(chat *Chat, forum *Topic) error { +func (b *Bot) CloseGeneralTopic(chat *Chat) error { params := map[string]interface{}{ "chat_id": chat.Recipient(), } @@ -142,7 +142,7 @@ func (b *Bot) CloseGeneralTopic(chat *Chat, forum *Topic) error { } // ReopenGeneralTopic reopens a closed 'General' topic in a forum supergroup chat. -func (b *Bot) ReopenGeneralTopic(chat *Chat, forum *Topic) error { +func (b *Bot) ReopenGeneralTopic(chat *Chat) error { params := map[string]interface{}{ "chat_id": chat.Recipient(), } @@ -152,7 +152,7 @@ func (b *Bot) ReopenGeneralTopic(chat *Chat, forum *Topic) error { } // HideGeneralTopic hides the 'General' topic in a forum supergroup chat. -func (b *Bot) HideGeneralTopic(chat *Chat, forum *Topic) error { +func (b *Bot) HideGeneralTopic(chat *Chat) error { params := map[string]interface{}{ "chat_id": chat.Recipient(), } @@ -162,7 +162,7 @@ func (b *Bot) HideGeneralTopic(chat *Chat, forum *Topic) error { } // UnhideGeneralTopic unhides the 'General' topic in a forum supergroup chat. -func (b *Bot) UnhideGeneralTopic(chat *Chat, forum *Topic) error { +func (b *Bot) UnhideGeneralTopic(chat *Chat) error { params := map[string]interface{}{ "chat_id": chat.Recipient(), } diff --git a/update.go b/update.go index 3f73d8b..12a065a 100644 --- a/update.go +++ b/update.go @@ -98,7 +98,8 @@ func (b *Bot) ProcessUpdate(u Update) { b.handle(OnPayment, c) return } - if m.TopicClosed != nil { + + if m.TopicCreated != nil { b.handle(OnTopicCreated, c) return } @@ -138,7 +139,6 @@ func (b *Bot) ProcessUpdate(u Update) { b.handle(OnUserJoined, c) return } - if m.UsersJoined != nil { for _, user := range m.UsersJoined { m.UserJoined = &user @@ -146,7 +146,6 @@ func (b *Bot) ProcessUpdate(u Update) { } return } - if m.UserLeft != nil { b.handle(OnUserLeft, c) return @@ -156,7 +155,6 @@ func (b *Bot) ProcessUpdate(u Update) { b.handle(OnUserShared, c) return } - if m.ChatShared != nil { b.handle(OnChatShared, c) return @@ -166,12 +164,10 @@ func (b *Bot) ProcessUpdate(u Update) { b.handle(OnNewGroupTitle, c) return } - if m.NewGroupPhoto != nil { b.handle(OnNewGroupPhoto, c) return } - if m.GroupPhotoDeleted { b.handle(OnGroupPhotoDeleted, c) return @@ -181,12 +177,10 @@ func (b *Bot) ProcessUpdate(u Update) { b.handle(OnGroupCreated, c) return } - if m.SuperGroupCreated { b.handle(OnSuperGroupCreated, c) return } - if m.ChannelCreated { b.handle(OnChannelCreated, c) return @@ -202,17 +196,14 @@ func (b *Bot) ProcessUpdate(u Update) { b.handle(OnVideoChatStarted, c) return } - if m.VideoChatEnded != nil { b.handle(OnVideoChatEnded, c) return } - if m.VideoChatParticipants != nil { b.handle(OnVideoChatParticipants, c) return } - if m.VideoChatScheduled != nil { b.handle(OnVideoChatScheduled, c) return @@ -220,13 +211,13 @@ func (b *Bot) ProcessUpdate(u Update) { if m.WebAppData != nil { b.handle(OnWebApp, c) + return } if m.ProximityAlert != nil { b.handle(OnProximityAlert, c) return } - if m.AutoDeleteTimer != nil { b.handle(OnAutoDeleteTimer, c) return diff --git a/video_chat.go b/video_chat.go index 448db41..4952e36 100644 --- a/video_chat.go +++ b/video_chat.go @@ -2,26 +2,28 @@ package telebot import "time" -// VideoChatStarted represents a service message about a video chat -// started in the chat. -type VideoChatStarted struct{} +type ( + // VideoChatStarted represents a service message about a video chat + // started in the chat. + VideoChatStarted struct{} -// VideoChatEnded represents a service message about a video chat -// ended in the chat. -type VideoChatEnded struct { - Duration int `json:"duration"` // in seconds -} + // VideoChatEnded represents a service message about a video chat + // ended in the chat. + VideoChatEnded struct { + Duration int `json:"duration"` // in seconds + } -// VideoChatParticipants represents a service message about new -// members invited to a video chat -type VideoChatParticipants struct { - Users []User `json:"users"` -} + // VideoChatParticipants represents a service message about new + // members invited to a video chat + VideoChatParticipants struct { + Users []User `json:"users"` + } -// VideoChatScheduled represents a service message about a video chat scheduled in the chat. -type VideoChatScheduled struct { - Unixtime int64 `json:"start_date"` -} + // VideoChatScheduled represents a service message about a video chat scheduled in the chat. + VideoChatScheduled struct { + Unixtime int64 `json:"start_date"` + } +) // StartsAt returns the point when the video chat is supposed to be started by a chat administrator. func (v *VideoChatScheduled) StartsAt() time.Time {