From 65ca005e23060cdd6ebec934b542f20481528f72 Mon Sep 17 00:00:00 2001 From: Nikita <59097534+nentenpizza@users.noreply.github.com> Date: Fri, 11 Oct 2024 20:21:14 +0300 Subject: [PATCH] context: inherit incoming message_thread_id by default (#745) * context: make context inherit incoming message_thread_id by default * context: rename inheritContextOpts --- context.go | 28 ++++++++++++++++++++++++++++ options.go | 5 +++++ 2 files changed, 33 insertions(+) diff --git a/context.go b/context.go index f0a195c..26e30f0 100644 --- a/context.go +++ b/context.go @@ -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 @@ -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 } @@ -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 @@ -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 diff --git a/options.go b/options.go index 6008668..5291c73 100644 --- a/options.go +++ b/options.go @@ -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. @@ -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") }