topic: finish and refactor the implementation

pull/625/head^2
Demian 7 months ago
parent 06bbf9e69f
commit 10d3480782

@ -46,12 +46,15 @@ type Context interface {
// ChatMember returns chat member changes. // ChatMember returns chat member changes.
ChatMember() *ChatMemberUpdate ChatMember() *ChatMemberUpdate
// ChatJoinRequest returns cha // ChatJoinRequest returns the chat join request.
ChatJoinRequest() *ChatJoinRequest ChatJoinRequest() *ChatJoinRequest
// Migration returns both migration from and to chat IDs. // Migration returns both migration from and to chat IDs.
Migration() (int64, int64) Migration() (int64, int64)
// Topic returns the topic changes.
Topic() *Topic
// Sender returns the current recipient, depending on the context type. // Sender returns the current recipient, depending on the context type.
// Returns nil if user is not presented. // Returns nil if user is not presented.
Sender() *User Sender() *User
@ -240,6 +243,22 @@ func (c *nativeContext) Migration() (int64, int64) {
return c.u.Message.MigrateFrom, c.u.Message.MigrateTo 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 { func (c *nativeContext) Sender() *User {
switch { switch {
case c.u.Callback != nil: case c.u.Callback != nil:

@ -264,22 +264,22 @@ type Message struct {
ReplyMarkup *ReplyMarkup `json:"reply_markup,omitempty"` ReplyMarkup *ReplyMarkup `json:"reply_markup,omitempty"`
// Service message: forum topic created // Service message: forum topic created
TopicCreated *TopicCreated `json:"forum_topic_created,omitempty"` TopicCreated *Topic `json:"forum_topic_created,omitempty"`
// Service message: forum topic closed // Service message: forum topic closed
TopicClosed *TopicClosed `json:"forum_topic_closed,omitempty"` TopicClosed *struct{} `json:"forum_topic_closed,omitempty"`
// Service message: forum topic reopened // Service message: forum topic reopened
TopicReopened *TopicReopened `json:"forum_topic_reopened,omitempty"` TopicReopened *Topic `json:"forum_topic_reopened,omitempty"`
// Service message: forum topic deleted // 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 // 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 // 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. // Service message: represents spoiler information about the message.
HasMediaSpoiler bool `json:"has_media_spoiler,omitempty"` HasMediaSpoiler bool `json:"has_media_spoiler,omitempty"`

@ -12,46 +12,46 @@ type Topic struct {
ThreadID int `json:"message_thread_id"` 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. // 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{ params := map[string]string{
"chat_id": chat.Recipient(), "chat_id": chat.Recipient(),
"name": forum.Name, "name": topic.Name,
} }
if forum.IconColor != 0 { if topic.IconColor != 0 {
params["icon_color"] = strconv.Itoa(forum.IconColor) params["icon_color"] = strconv.Itoa(topic.IconColor)
} }
if forum.IconCustomEmojiID != "" { if topic.IconCustomEmojiID != "" {
params["icon_custom_emoji_id"] = forum.IconCustomEmojiID params["icon_custom_emoji_id"] = topic.IconCustomEmojiID
} }
_, err := b.Raw("createForumTopic", params) data, err := b.Raw("createForumTopic", params)
return err 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. // 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{}{ params := map[string]interface{}{
"chat_id": chat.Recipient(), "chat_id": chat.Recipient(),
"message_thread_id": forum.ThreadID, "message_thread_id": topic.ThreadID,
} }
if forum.Name != "" { if topic.Name != "" {
params["name"] = forum.Name params["name"] = topic.Name
} }
if forum.IconCustomEmojiID != "" { if topic.IconCustomEmojiID != "" {
params["icon_custom_emoji_id"] = forum.IconCustomEmojiID params["icon_custom_emoji_id"] = topic.IconCustomEmojiID
} }
_, err := b.Raw("editForumTopic", params) _, 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. // 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{}{ params := map[string]interface{}{
"chat_id": chat.Recipient(), "chat_id": chat.Recipient(),
"message_thread_id": forum.ThreadID, "message_thread_id": topic.ThreadID,
} }
_, err := b.Raw("closeForumTopic", params) _, 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. // 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{}{ params := map[string]interface{}{
"chat_id": chat.Recipient(), "chat_id": chat.Recipient(),
"message_thread_id": forum.ThreadID, "message_thread_id": topic.ThreadID,
} }
_, err := b.Raw("reopenForumTopic", params) _, 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. // 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{}{ params := map[string]interface{}{
"chat_id": chat.Recipient(), "chat_id": chat.Recipient(),
"message_thread_id": forum.ThreadID, "message_thread_id": topic.ThreadID,
} }
_, err := b.Raw("deleteForumTopic", params) _, 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. // 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{}{ params := map[string]interface{}{
"chat_id": chat.Recipient(), "chat_id": chat.Recipient(),
"message_thread_id": forum.ThreadID, "message_thread_id": topic.ThreadID,
} }
_, err := b.Raw("unpinAllForumTopicMessages", params) _, 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. // 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{}{ params := map[string]interface{}{
"chat_id": chat.Recipient(), "chat_id": chat.Recipient(),
"name": forum.Name, "name": topic.Name,
} }
_, err := b.Raw("editGeneralForumTopic", params) _, 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. // 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{}{ params := map[string]interface{}{
"chat_id": chat.Recipient(), "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. // 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{}{ params := map[string]interface{}{
"chat_id": chat.Recipient(), "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. // 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{}{ params := map[string]interface{}{
"chat_id": chat.Recipient(), "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. // 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{}{ params := map[string]interface{}{
"chat_id": chat.Recipient(), "chat_id": chat.Recipient(),
} }

@ -98,7 +98,8 @@ func (b *Bot) ProcessUpdate(u Update) {
b.handle(OnPayment, c) b.handle(OnPayment, c)
return return
} }
if m.TopicClosed != nil {
if m.TopicCreated != nil {
b.handle(OnTopicCreated, c) b.handle(OnTopicCreated, c)
return return
} }
@ -138,7 +139,6 @@ func (b *Bot) ProcessUpdate(u Update) {
b.handle(OnUserJoined, c) b.handle(OnUserJoined, c)
return return
} }
if m.UsersJoined != nil { if m.UsersJoined != nil {
for _, user := range m.UsersJoined { for _, user := range m.UsersJoined {
m.UserJoined = &user m.UserJoined = &user
@ -146,7 +146,6 @@ func (b *Bot) ProcessUpdate(u Update) {
} }
return return
} }
if m.UserLeft != nil { if m.UserLeft != nil {
b.handle(OnUserLeft, c) b.handle(OnUserLeft, c)
return return
@ -156,7 +155,6 @@ func (b *Bot) ProcessUpdate(u Update) {
b.handle(OnUserShared, c) b.handle(OnUserShared, c)
return return
} }
if m.ChatShared != nil { if m.ChatShared != nil {
b.handle(OnChatShared, c) b.handle(OnChatShared, c)
return return
@ -166,12 +164,10 @@ func (b *Bot) ProcessUpdate(u Update) {
b.handle(OnNewGroupTitle, c) b.handle(OnNewGroupTitle, c)
return return
} }
if m.NewGroupPhoto != nil { if m.NewGroupPhoto != nil {
b.handle(OnNewGroupPhoto, c) b.handle(OnNewGroupPhoto, c)
return return
} }
if m.GroupPhotoDeleted { if m.GroupPhotoDeleted {
b.handle(OnGroupPhotoDeleted, c) b.handle(OnGroupPhotoDeleted, c)
return return
@ -181,12 +177,10 @@ func (b *Bot) ProcessUpdate(u Update) {
b.handle(OnGroupCreated, c) b.handle(OnGroupCreated, c)
return return
} }
if m.SuperGroupCreated { if m.SuperGroupCreated {
b.handle(OnSuperGroupCreated, c) b.handle(OnSuperGroupCreated, c)
return return
} }
if m.ChannelCreated { if m.ChannelCreated {
b.handle(OnChannelCreated, c) b.handle(OnChannelCreated, c)
return return
@ -202,17 +196,14 @@ func (b *Bot) ProcessUpdate(u Update) {
b.handle(OnVideoChatStarted, c) b.handle(OnVideoChatStarted, c)
return return
} }
if m.VideoChatEnded != nil { if m.VideoChatEnded != nil {
b.handle(OnVideoChatEnded, c) b.handle(OnVideoChatEnded, c)
return return
} }
if m.VideoChatParticipants != nil { if m.VideoChatParticipants != nil {
b.handle(OnVideoChatParticipants, c) b.handle(OnVideoChatParticipants, c)
return return
} }
if m.VideoChatScheduled != nil { if m.VideoChatScheduled != nil {
b.handle(OnVideoChatScheduled, c) b.handle(OnVideoChatScheduled, c)
return return
@ -220,13 +211,13 @@ func (b *Bot) ProcessUpdate(u Update) {
if m.WebAppData != nil { if m.WebAppData != nil {
b.handle(OnWebApp, c) b.handle(OnWebApp, c)
return
} }
if m.ProximityAlert != nil { if m.ProximityAlert != nil {
b.handle(OnProximityAlert, c) b.handle(OnProximityAlert, c)
return return
} }
if m.AutoDeleteTimer != nil { if m.AutoDeleteTimer != nil {
b.handle(OnAutoDeleteTimer, c) b.handle(OnAutoDeleteTimer, c)
return return

@ -2,26 +2,28 @@ package telebot
import "time" import "time"
// VideoChatStarted represents a service message about a video chat type (
// started in the chat. // VideoChatStarted represents a service message about a video chat
type VideoChatStarted struct{} // started in the chat.
VideoChatStarted struct{}
// VideoChatEnded represents a service message about a video chat // VideoChatEnded represents a service message about a video chat
// ended in the chat. // ended in the chat.
type VideoChatEnded struct { VideoChatEnded struct {
Duration int `json:"duration"` // in seconds Duration int `json:"duration"` // in seconds
} }
// VideoChatParticipants represents a service message about new // VideoChatParticipants represents a service message about new
// members invited to a video chat // members invited to a video chat
type VideoChatParticipants struct { VideoChatParticipants struct {
Users []User `json:"users"` Users []User `json:"users"`
} }
// VideoChatScheduled represents a service message about a video chat scheduled in the chat. // VideoChatScheduled represents a service message about a video chat scheduled in the chat.
type VideoChatScheduled struct { VideoChatScheduled struct {
Unixtime int64 `json:"start_date"` Unixtime int64 `json:"start_date"`
} }
)
// StartsAt returns the point when the video chat is supposed to be started by a chat administrator. // StartsAt returns the point when the video chat is supposed to be started by a chat administrator.
func (v *VideoChatScheduled) StartsAt() time.Time { func (v *VideoChatScheduled) StartsAt() time.Time {

Loading…
Cancel
Save