Skip to content

Commit

Permalink
react: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
demget committed Mar 10, 2024
1 parent 6fdf666 commit 9eb5343
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 72 deletions.
4 changes: 2 additions & 2 deletions bot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ func TestBotProcessUpdate(t *testing.T) {
b.ProcessUpdate(Update{Message: &Message{Text: "/start@other_bot"}})
b.ProcessUpdate(Update{Message: &Message{Text: "hello"}})
b.ProcessUpdate(Update{Message: &Message{Text: "text"}})
b.ProcessUpdate(Update{Message: &Message{PinnedMessage: &InaccessibleMessage{}}})
b.ProcessUpdate(Update{Message: &Message{PinnedMessage: &InaccessibleMessage{Message: &Message{}}}})
b.ProcessUpdate(Update{Message: &Message{Photo: &Photo{}}})
b.ProcessUpdate(Update{Message: &Message{Voice: &Voice{}}})
b.ProcessUpdate(Update{Message: &Message{Audio: &Audio{}}})
Expand All @@ -342,7 +342,7 @@ func TestBotProcessUpdate(t *testing.T) {
b.ProcessUpdate(Update{Message: &Message{Chat: &Chat{ID: 1}, MigrateTo: 2}})
b.ProcessUpdate(Update{EditedMessage: &Message{Text: "edited"}})
b.ProcessUpdate(Update{ChannelPost: &Message{Text: "post"}})
b.ProcessUpdate(Update{ChannelPost: &Message{PinnedMessage: &InaccessibleMessage{}}})
b.ProcessUpdate(Update{ChannelPost: &Message{PinnedMessage: &InaccessibleMessage{Message: &Message{}}}})
b.ProcessUpdate(Update{EditedChannelPost: &Message{Text: "edited post"}})
b.ProcessUpdate(Update{Callback: &Callback{MessageID: "inline", Data: "callback"}})
b.ProcessUpdate(Update{Callback: &Callback{Data: "callback"}})
Expand Down
54 changes: 26 additions & 28 deletions chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"encoding/json"
"strconv"
"time"

"gopkg.in/telebot.v3/react"
)

// User object represents a Telegram user, bot.
Expand Down Expand Up @@ -49,32 +47,32 @@ type Chat struct {
Username string `json:"username"`

// Returns only in getChat
Bio string `json:"bio,omitempty"`
Photo *ChatPhoto `json:"photo,omitempty"`
Description string `json:"description,omitempty"`
InviteLink string `json:"invite_link,omitempty"`
PinnedMessage *Message `json:"pinned_message,omitempty"`
Permissions *Rights `json:"permissions,omitempty"`
Reactions []react.Reaction `json:"available_reactions"`
SlowMode int `json:"slow_mode_delay,omitempty"`
StickerSet string `json:"sticker_set_name,omitempty"`
CanSetStickerSet bool `json:"can_set_sticker_set,omitempty"`
CustomEmojiSetName string `json:"custom_emoji_sticker_set_name"`
LinkedChatID int64 `json:"linked_chat_id,omitempty"`
ChatLocation *ChatLocation `json:"location,omitempty"`
Private bool `json:"has_private_forwards,omitempty"`
Protected bool `json:"has_protected_content,omitempty"`
NoVoiceAndVideo bool `json:"has_restricted_voice_and_video_messages"`
HasHiddenMembers bool `json:"has_hidden_members,omitempty"`
AggressiveAntiSpam bool `json:"has_aggressive_anti_spam_enabled,omitempty"`
CustomEmojiID string `json:"emoji_status_custom_emoji_id"`
EmojiExpirationUnixtime int64 `json:"emoji_status_expiration_date"`
BackgroundEmojiID string `json:"background_custom_emoji_id"`
AccentColorID int `json:"accent_color_id"`
ProfileAccentColorID int `json:"profile_accent_color_id"`
ProfileBackgroundEmojiID string `json:"profile_background_custom_emoji_id"`
HasVisibleHistory bool `json:"has_visible_history"`
UnrestrictBoosts int `json:"unrestrict_boost_count"`
Bio string `json:"bio,omitempty"`
Photo *ChatPhoto `json:"photo,omitempty"`
Description string `json:"description,omitempty"`
InviteLink string `json:"invite_link,omitempty"`
PinnedMessage *Message `json:"pinned_message,omitempty"`
Permissions *Rights `json:"permissions,omitempty"`
Reactions []Reaction `json:"available_reactions"`
SlowMode int `json:"slow_mode_delay,omitempty"`
StickerSet string `json:"sticker_set_name,omitempty"`
CanSetStickerSet bool `json:"can_set_sticker_set,omitempty"`
CustomEmojiSetName string `json:"custom_emoji_sticker_set_name"`
LinkedChatID int64 `json:"linked_chat_id,omitempty"`
ChatLocation *ChatLocation `json:"location,omitempty"`
Private bool `json:"has_private_forwards,omitempty"`
Protected bool `json:"has_protected_content,omitempty"`
NoVoiceAndVideo bool `json:"has_restricted_voice_and_video_messages"`
HasHiddenMembers bool `json:"has_hidden_members,omitempty"`
AggressiveAntiSpam bool `json:"has_aggressive_anti_spam_enabled,omitempty"`
CustomEmojiID string `json:"emoji_status_custom_emoji_id"`
EmojiExpirationUnixtime int64 `json:"emoji_status_expiration_date"`
BackgroundEmojiID string `json:"background_custom_emoji_id"`
AccentColorID int `json:"accent_color_id"`
ProfileAccentColorID int `json:"profile_accent_color_id"`
ProfileBackgroundEmojiID string `json:"profile_background_custom_emoji_id"`
HasVisibleHistory bool `json:"has_visible_history"`
UnrestrictBoosts int `json:"unrestrict_boost_count"`
}

// Recipient returns chat ID (see Recipient interface).
Expand Down
14 changes: 6 additions & 8 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"strconv"
"time"
"unicode/utf16"

"gopkg.in/telebot.v3/react"
)

// Message object represents a message.
Expand Down Expand Up @@ -107,7 +105,7 @@ type Message struct {
// etc. that appear in the text.
Entities Entities `json:"entities,omitempty"`

// (Optional) Options used for link preview generation for the message,
// (Optional) ReactionOptions used for link preview generation for the message,
// if it is a text message and link preview options were changed
PreviewOptions PreviewOptions `json:"link_preview_options,omitempty"`

Expand Down Expand Up @@ -551,10 +549,10 @@ type MessageReaction struct {
DateUnixtime int64 `json:"date"`

// Previous list of reaction types that were set by the user.
OldReaction []react.Reaction `json:"old_reaction"`
OldReaction []Reaction `json:"old_reaction"`

// New list of reaction types that have been set by the user.
NewReaction []react.Reaction `json:"new_reaction"`
NewReaction []Reaction `json:"new_reaction"`
}

func (mu *MessageReaction) Time() time.Time {
Expand All @@ -574,7 +572,7 @@ type MessageReactionCount struct {
DateUnixtime int64 `json:"date"`

// List of reactions that are present on the message.
Reactions *react.Count `json:"reactions"`
Reactions *ReactionCount `json:"reactions"`
}

// Time returns the moment of change in local time.
Expand Down Expand Up @@ -649,7 +647,7 @@ type ExternalReplyInfo struct {
// Available only if the original chat is a supergroup or a channel.
MessageID int `json:"message_id"`

// (Optional) Options used for link preview generation for the original message,
// (Optional) ReactionOptions used for link preview generation for the original message,
// if it is a text message.
PreviewOptions *PreviewOptions `json:"link_preview_options"`

Expand Down Expand Up @@ -746,7 +744,7 @@ type ReplyParams struct {
// React changes the chosen reactions on a message. Service messages can't be
// reacted to. Automatically forwarded messages from a channel to its discussion group have
// the same available reactions as messages in the channel.
func (b *Bot) React(to Recipient, msg Editable, opts ...react.Options) error {
func (b *Bot) React(to Recipient, msg Editable, opts ...ReactionOptions) error {
if to == nil {
return ErrBadRecipient
}
Expand Down
36 changes: 36 additions & 0 deletions react.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package telebot

// EmojiType defines emoji types.
type EmojiType = string

// Reaction describes the type of reaction.
// Describes an instance of ReactionTypeCustomEmoji and ReactionTypeEmoji.
type Reaction struct {
// Type of the reaction, always “emoji”
Type string `json:"type"`

// Reaction emoji.
Emoji EmojiType `json:"emoji,omitempty"`

// Custom emoji identifier.
CustomEmoji string `json:"custom_emoji_id,omitempty"`
}

// ReactionCount represents a reaction added to a message along
// with the number of times it was added.
type ReactionCount struct {
// Type of the reaction.
Type Reaction `json:"type"`

// Number of times the reaction was added.
Count int `json:"total_count"`
}

// ReactionOptions represents an object of reaction options.
type ReactionOptions struct {
// List of reaction types to set on the message.
Reactions []Reaction `json:"reaction"`

// Pass True to set the reaction with a big animation.
Big bool `json:"is_big"`
}
37 changes: 3 additions & 34 deletions react/reaction.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package react

// EmojiType defines emoji types.
type EmojiType = string
import "gopkg.in/telebot.v3"

type Reaction = telebot.Reaction

// Currently available emojis.
var (
Expand Down Expand Up @@ -78,35 +79,3 @@ var (
WomanShrugging = Reaction{Emoji: "🤷‍♀️"}
PoutingFace = Reaction{Emoji: "😡"}
)

// Reaction describes the type of reaction.
// Describes an instance of ReactionTypeCustomEmoji and ReactionTypeEmoji.
type Reaction struct {
// Type of the reaction, always “emoji”
Type string `json:"type"`

// Reaction emoji.
Emoji EmojiType `json:"emoji,omitempty"`

// Custom emoji identifier.
CustomEmoji string `json:"custom_emoji_id,omitempty"`
}

// Count represents a reaction added to a message along
// with the number of times it was added.
type Count struct {
// Type of the reaction.
Type Reaction `json:"type"`

// Number of times the reaction was added.
Count int `json:"total_count"`
}

// Options represents an object of reaction options.
type Options struct {
// List of reaction types to set on the message.
Reactions []Reaction `json:"reaction"`

// Pass True to set the reaction with a big animation.
Big bool `json:"is_big"`
}

0 comments on commit 9eb5343

Please sign in to comment.