Skip to content

Commit

Permalink
topic: finish and refactor the implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
demget committed Nov 20, 2023
1 parent 06bbf9e commit 10d3480
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 74 deletions.
21 changes: 20 additions & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
12 changes: 6 additions & 6 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
76 changes: 38 additions & 38 deletions topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,90 +12,90 @@ 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)
return err
}

// 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)
return err
}

// 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)
return err
}

// 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)
return err
}

// 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)
Expand All @@ -121,18 +121,18 @@ 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)
return err
}

// 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(),
}
Expand All @@ -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(),
}
Expand All @@ -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(),
}
Expand All @@ -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(),
}
Expand Down
15 changes: 3 additions & 12 deletions update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -138,15 +139,13 @@ func (b *Bot) ProcessUpdate(u Update) {
b.handle(OnUserJoined, c)
return
}

if m.UsersJoined != nil {
for _, user := range m.UsersJoined {
m.UserJoined = &user
b.handle(OnUserJoined, c)
}
return
}

if m.UserLeft != nil {
b.handle(OnUserLeft, c)
return
Expand All @@ -156,7 +155,6 @@ func (b *Bot) ProcessUpdate(u Update) {
b.handle(OnUserShared, c)
return
}

if m.ChatShared != nil {
b.handle(OnChatShared, c)
return
Expand All @@ -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
Expand All @@ -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
Expand All @@ -202,31 +196,28 @@ 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
}

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
Expand Down
36 changes: 19 additions & 17 deletions video_chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 10d3480

Please sign in to comment.