Skip to content

Commit

Permalink
context: inherit incoming message_thread_id by default (#745)
Browse files Browse the repository at this point in the history
* context: make context inherit incoming message_thread_id by default

* context: rename inheritContextOpts
  • Loading branch information
nentenpizza authored Oct 11, 2024
1 parent 4d98111 commit 65ca005
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
28 changes: 28 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,10 +424,33 @@ func (c *nativeContext) Args() []string {
}

func (c *nativeContext) Send(what interface{}, opts ...interface{}) error {
c.inheritOpts(opts)
_, err := c.b.Send(c.Recipient(), what, opts...)
return err
}

func (c *nativeContext) inheritOpts(opts ...interface{}) {
var (
ignoreThread bool
)

for _, opt := range opts {
switch opt.(type) {
case Option:
switch opt {
case IgnoreThread:
ignoreThread = true
default:
}
}
}

switch {
case !ignoreThread && c.Message() != nil && c.Message().ThreadID != 0:
opts = append(opts, Topic{ThreadID: c.Message().ThreadID})
}
}

func (c *nativeContext) SendAlbum(a Album, opts ...interface{}) error {
_, err := c.b.SendAlbum(c.Recipient(), a, opts...)
return err
Expand All @@ -438,6 +461,7 @@ func (c *nativeContext) Reply(what interface{}, opts ...interface{}) error {
if msg == nil {
return ErrBadContext
}
c.inheritOpts(opts)
_, err := c.b.Reply(msg, what, opts...)
return err
}
Expand All @@ -457,6 +481,8 @@ func (c *nativeContext) ForwardTo(to Recipient, opts ...interface{}) error {
}

func (c *nativeContext) Edit(what interface{}, opts ...interface{}) error {
c.inheritOpts(opts)

if c.u.InlineResult != nil {
_, err := c.b.Edit(c.u.InlineResult, what, opts...)
return err
Expand All @@ -469,6 +495,8 @@ func (c *nativeContext) Edit(what interface{}, opts ...interface{}) error {
}

func (c *nativeContext) EditCaption(caption string, opts ...interface{}) error {
c.inheritOpts(opts)

if c.u.InlineResult != nil {
_, err := c.b.EditCaption(c.u.InlineResult, caption, opts...)
return err
Expand Down
5 changes: 5 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ const (

// RemoveKeyboard = ReplyMarkup.RemoveKeyboard
RemoveKeyboard

// IgnoreThread is used to ignore the thread when responding to a message via context.
IgnoreThread
)

// Placeholder is used to set input field placeholder as a send option.
Expand Down Expand Up @@ -149,6 +152,8 @@ func (b *Bot) extractOptions(how []interface{}) *SendOptions {
opts.ParseMode = opt
case Entities:
opts.Entities = opt
case Topic:
opts.ThreadID = opt.ThreadID
default:
panic("telebot: unsupported send-option")
}
Expand Down

0 comments on commit 65ca005

Please sign in to comment.