Skip to content

Commit

Permalink
options: fix default parse mode embedding
Browse files Browse the repository at this point in the history
  • Loading branch information
demget committed Aug 6, 2024
1 parent 2c98c59 commit 864bef4
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 23 deletions.
24 changes: 12 additions & 12 deletions bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ func (b *Bot) Send(to Recipient, what interface{}, opts ...interface{}) (*Messag
return nil, ErrBadRecipient
}

sendOpts := extractOptions(opts)
sendOpts := b.extractOptions(opts)

switch object := what.(type) {
case string:
Expand All @@ -311,7 +311,7 @@ func (b *Bot) SendAlbum(to Recipient, a Album, opts ...interface{}) ([]Message,
return nil, ErrBadRecipient
}

sendOpts := extractOptions(opts)
sendOpts := b.extractOptions(opts)
media := make([]string, len(a))
files := make(map[string]File)

Expand Down Expand Up @@ -377,7 +377,7 @@ func (b *Bot) SendAlbum(to Recipient, a Album, opts ...interface{}) ([]Message,
// Reply behaves just like Send() with an exception of "reply-to" indicator.
// This function will panic upon nil Message.
func (b *Bot) Reply(to *Message, what interface{}, opts ...interface{}) (*Message, error) {
sendOpts := extractOptions(opts)
sendOpts := b.extractOptions(opts)
if sendOpts == nil {
sendOpts = &SendOptions{}
}
Expand All @@ -400,7 +400,7 @@ func (b *Bot) Forward(to Recipient, msg Editable, opts ...interface{}) (*Message
"message_id": msgID,
}

sendOpts := extractOptions(opts)
sendOpts := b.extractOptions(opts)
b.embedSendOptions(params, sendOpts)

data, err := b.Raw("forwardMessage", params)
Expand All @@ -425,7 +425,7 @@ func (b *Bot) ForwardMany(to Recipient, msgs []Editable, opts ...*SendOptions) (
// Copy behaves just like Forward() but the copied message doesn't have a link to the original message (see Bots API).
//
// This function will panic upon nil Editable.
func (b *Bot) Copy(to Recipient, msg Editable, options ...interface{}) (*Message, error) {
func (b *Bot) Copy(to Recipient, msg Editable, opts ...interface{}) (*Message, error) {
if to == nil {
return nil, ErrBadRecipient
}
Expand All @@ -437,7 +437,7 @@ func (b *Bot) Copy(to Recipient, msg Editable, options ...interface{}) (*Message
"message_id": msgID,
}

sendOpts := extractOptions(options)
sendOpts := b.extractOptions(opts)
b.embedSendOptions(params, sendOpts)

data, err := b.Raw("copyMessage", params)
Expand Down Expand Up @@ -518,7 +518,7 @@ func (b *Bot) Edit(msg Editable, what interface{}, opts ...interface{}) (*Messag
params["message_id"] = msgID
}

sendOpts := extractOptions(opts)
sendOpts := b.extractOptions(opts)
b.embedSendOptions(params, sendOpts)

data, err := b.Raw(method, params)
Expand Down Expand Up @@ -582,7 +582,7 @@ func (b *Bot) EditCaption(msg Editable, caption string, opts ...interface{}) (*M
params["message_id"] = msgID
}

sendOpts := extractOptions(opts)
sendOpts := b.extractOptions(opts)
b.embedSendOptions(params, sendOpts)

data, err := b.Raw("editMessageCaption", params)
Expand Down Expand Up @@ -646,7 +646,7 @@ func (b *Bot) EditMedia(msg Editable, media Inputtable, opts ...interface{}) (*M
msgID, chatID := msg.MessageSig()
params := make(map[string]string)

sendOpts := extractOptions(opts)
sendOpts := b.extractOptions(opts)
b.embedSendOptions(params, sendOpts)

im := media.InputMedia()
Expand Down Expand Up @@ -947,7 +947,7 @@ func (b *Bot) StopLiveLocation(msg Editable, opts ...interface{}) (*Message, err
"message_id": msgID,
}

sendOpts := extractOptions(opts)
sendOpts := b.extractOptions(opts)
b.embedSendOptions(params, sendOpts)

data, err := b.Raw("stopMessageLiveLocation", params)
Expand All @@ -971,7 +971,7 @@ func (b *Bot) StopPoll(msg Editable, opts ...interface{}) (*Poll, error) {
"message_id": msgID,
}

sendOpts := extractOptions(opts)
sendOpts := b.extractOptions(opts)
b.embedSendOptions(params, sendOpts)

data, err := b.Raw("stopPoll", params)
Expand Down Expand Up @@ -1010,7 +1010,7 @@ func (b *Bot) Pin(msg Editable, opts ...interface{}) error {
"message_id": msgID,
}

sendOpts := extractOptions(opts)
sendOpts := b.extractOptions(opts)
b.embedSendOptions(params, sendOpts)

_, err := b.Raw("pinChatMessage", params)
Expand Down
7 changes: 4 additions & 3 deletions bot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ func TestBot(t *testing.T) {
})

t.Run("EditCaption()+ParseMode", func(t *testing.T) {
b.parseMode = ModeHTML
b.parseMode = "html"

edited, err := b.EditCaption(msg, "<b>new caption with html</b>")
require.NoError(t, err)
Expand All @@ -554,14 +554,15 @@ func TestBot(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, "new caption with markdown (V2)", edited.Caption)
assert.Equal(t, EntityItalic, edited.CaptionEntities[0].Type)

b.parseMode = ModeDefault
})

t.Run("Edit(what=Media)", func(t *testing.T) {
photo.Caption = "<code>new caption with html</code>"

edited, err := b.Edit(msg, photo)
require.NoError(t, err)
assert.Equal(t, edited.Photo.UniqueID, photo.UniqueID)
assert.Equal(t, EntityCode, edited.CaptionEntities[0].Type)

resp, err := http.Get("https://telegra.ph/file/274e5eb26f348b10bd8ee.mp4")
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion layout/example.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
settings:
token_env: TOKEN
parse_mode: html
parse_mode: HTML
long_poller: {}

commands:
Expand Down
2 changes: 1 addition & 1 deletion layout/layout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestLayout(t *testing.T) {

pref := lt.Settings()
assert.Equal(t, "TEST", pref.Token)
assert.Equal(t, "html", pref.ParseMode)
assert.Equal(t, tele.ModeHTML, pref.ParseMode)
assert.Equal(t, &tele.LongPoller{}, pref.Poller)
assert.Equal(t, pref, ltfs.Settings())

Expand Down
10 changes: 4 additions & 6 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,10 @@ func (og *SendOptions) copy() *SendOptions {
return &cp
}

func extractOptions(how []interface{}) *SendOptions {
opts := &SendOptions{}
func (b *Bot) extractOptions(how []interface{}) *SendOptions {
opts := &SendOptions{
ParseMode: b.parseMode,
}

for _, prop := range how {
switch opt := prop.(type) {
Expand Down Expand Up @@ -150,10 +152,6 @@ func extractOptions(how []interface{}) *SendOptions {
}

func (b *Bot) embedSendOptions(params map[string]string, opt *SendOptions) {
if b.parseMode != ModeDefault {
params["parse_mode"] = b.parseMode
}

if opt == nil {
return
}
Expand Down

0 comments on commit 864bef4

Please sign in to comment.