Skip to content

Commit

Permalink
react: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
demget committed Jun 10, 2024
1 parent 10fbde9 commit a8acf84
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 36 deletions.
31 changes: 0 additions & 31 deletions message.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package telebot

import (
"encoding/json"
"strconv"
"time"
"unicode/utf16"
Expand Down Expand Up @@ -720,33 +719,3 @@ type ReplyParams struct {
// (Optional) Position of the quote in the original message in UTF-16 code units.
QuotePosition int `json:"quote_position"`
}

// 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 ...ReactionOptions) error {
if to == nil {
return ErrBadRecipient
}
msgID, _ := msg.MessageSig()

params := map[string]string{
"chat_id": to.Recipient(),
"message_id": msgID,
}

if len(opts) > 0 {
opt := opts[0]

if len(opt.Reactions) > 0 {
data, _ := json.Marshal(opt.Reactions)
params["reaction"] = string(data)
}
if opt.Big {
params["is_big"] = "true"
}
}

_, err := b.Raw("setMessageReaction", params)
return err
}
37 changes: 34 additions & 3 deletions react.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package telebot

// EmojiType defines emoji types.
type EmojiType = string
import (
"encoding/json"
)

// Reaction describes the type of reaction.
// Describes an instance of ReactionTypeCustomEmoji and ReactionTypeEmoji.
Expand All @@ -10,7 +11,7 @@ type Reaction struct {
Type string `json:"type"`

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

// Custom emoji identifier.
CustomEmoji string `json:"custom_emoji_id,omitempty"`
Expand All @@ -34,3 +35,33 @@ type ReactionOptions struct {
// Pass True to set the reaction with a big animation.
Big bool `json:"is_big"`
}

// 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 ...ReactionOptions) error {
if to == nil {
return ErrBadRecipient
}
msgID, _ := msg.MessageSig()

params := map[string]string{
"chat_id": to.Recipient(),
"message_id": msgID,
}

if len(opts) > 0 {
opt := opts[0]

if len(opt.Reactions) > 0 {
data, _ := json.Marshal(opt.Reactions)
params["reaction"] = string(data)
}
if opt.Big {
params["is_big"] = "true"
}
}

_, err := b.Raw("setMessageReaction", params)
return err
}
10 changes: 8 additions & 2 deletions react/reaction.go → react/react.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package react

import "gopkg.in/telebot.v3"
import (
tele "gopkg.in/telebot.v3"
)

type Reaction = tele.Reaction

type Reaction = telebot.Reaction
func React(r ...Reaction) tele.ReactionOptions {
return tele.ReactionOptions{Reactions: r}
}

// Currently available emojis.
var (
Expand Down

0 comments on commit a8acf84

Please sign in to comment.