diff --git a/telegram/message/draft.go b/telegram/message/draft.go index 88677f1899..0349195485 100644 --- a/telegram/message/draft.go +++ b/telegram/message/draft.go @@ -52,14 +52,14 @@ func (b *Builder) SaveDraft(ctx context.Context, msg string) error { // SaveStyledDraft saves given styled message as draft. // // See https://core.telegram.org/api/drafts#saving-drafts. -func (b *Builder) SaveStyledDraft(ctx context.Context, text StyledTextOption, texts ...StyledTextOption) error { +func (b *Builder) SaveStyledDraft(ctx context.Context, texts ...StyledTextOption) error { p, err := b.peer(ctx) if err != nil { return xerrors.Errorf("peer: %w", err) } tb := entity.Builder{} - if err := styling.Perform(&tb, text, texts...); err != nil { + if err := styling.Perform(&tb, texts...); err != nil { return err } msg, entities := tb.Complete() diff --git a/telegram/message/edit.go b/telegram/message/edit.go index 0ffef5a05d..388788a23b 100644 --- a/telegram/message/edit.go +++ b/telegram/message/edit.go @@ -53,17 +53,14 @@ func (b *EditMessageBuilder) Textf(ctx context.Context, format string, args ...i } // StyledText edits message using given message. -func (b *EditMessageBuilder) StyledText( - ctx context.Context, - text StyledTextOption, texts ...StyledTextOption, -) (tg.UpdatesClass, error) { +func (b *EditMessageBuilder) StyledText(ctx context.Context, texts ...StyledTextOption) (tg.UpdatesClass, error) { p, err := b.builder.peer(ctx) if err != nil { return nil, xerrors.Errorf("peer: %w", err) } tb := entity.Builder{} - if err := styling.Perform(&tb, text, texts...); err != nil { + if err := styling.Perform(&tb, texts...); err != nil { return nil, err } msg, entities := tb.Complete() diff --git a/telegram/message/inline/message_auto_media.go b/telegram/message/inline/message_auto_media.go index 8f1a79fbf2..1f54a346de 100644 --- a/telegram/message/inline/message_auto_media.go +++ b/telegram/message/inline/message_auto_media.go @@ -10,13 +10,12 @@ import ( // MessageMediaAutoBuilder is a builder of inline result text message. type MessageMediaAutoBuilder struct { message *tg.InputBotInlineMessageMediaAuto - option styling.StyledTextOption options []styling.StyledTextOption } func (b *MessageMediaAutoBuilder) apply() (tg.InputBotInlineMessageClass, error) { tb := entity.Builder{} - if err := styling.Perform(&tb, b.option, b.options...); err != nil { + if err := styling.Perform(&tb, b.options...); err != nil { return nil, err } msg, entities := tb.Complete() @@ -33,10 +32,9 @@ func MediaAuto(msg string) *MessageMediaAutoBuilder { } // MediaAutoStyled creates new message text option builder. -func MediaAutoStyled(text styling.StyledTextOption, texts ...styling.StyledTextOption) *MessageMediaAutoBuilder { +func MediaAutoStyled(texts ...styling.StyledTextOption) *MessageMediaAutoBuilder { return &MessageMediaAutoBuilder{ message: &tg.InputBotInlineMessageMediaAuto{}, - option: text, options: texts, } } diff --git a/telegram/message/inline/message_text.go b/telegram/message/inline/message_text.go index 2fbf141056..00fdf737e2 100644 --- a/telegram/message/inline/message_text.go +++ b/telegram/message/inline/message_text.go @@ -10,13 +10,12 @@ import ( // MessageTextBuilder is a builder of inline result text message. type MessageTextBuilder struct { message *tg.InputBotInlineMessageText - option styling.StyledTextOption options []styling.StyledTextOption } func (b *MessageTextBuilder) apply() (tg.InputBotInlineMessageClass, error) { tb := entity.Builder{} - if err := styling.Perform(&tb, b.option, b.options...); err != nil { + if err := styling.Perform(&tb, b.options...); err != nil { return nil, err } msg, entities := tb.Complete() @@ -33,10 +32,9 @@ func MessageText(msg string) *MessageTextBuilder { } // MessageStyledText creates new message text option builder. -func MessageStyledText(text styling.StyledTextOption, texts ...styling.StyledTextOption) *MessageTextBuilder { +func MessageStyledText(texts ...styling.StyledTextOption) *MessageTextBuilder { return &MessageTextBuilder{ message: &tg.InputBotInlineMessageText{}, - option: text, options: texts, } } diff --git a/telegram/message/media.go b/telegram/message/media.go index 8415e0b0f6..acfccabf34 100644 --- a/telegram/message/media.go +++ b/telegram/message/media.go @@ -13,7 +13,7 @@ import ( func performTextOptions(media *tg.InputSingleMedia, opts []StyledTextOption) error { if len(opts) > 0 { tb := entity.Builder{} - if err := styling.Perform(&tb, opts[0], opts[1:]...); err != nil { + if err := styling.Perform(&tb, opts...); err != nil { return err } media.Message, media.Entities = tb.Complete() diff --git a/telegram/message/poll.go b/telegram/message/poll.go index 7aaf43c0d4..d86c6fb1ac 100644 --- a/telegram/message/poll.go +++ b/telegram/message/poll.go @@ -56,7 +56,6 @@ func CorrectPollAnswer(text string) PollAnswerOption { type PollBuilder struct { input tg.InputMediaPoll answers []PollAnswerOption - opt StyledTextOption opts []StyledTextOption } @@ -118,8 +117,7 @@ func (p *PollBuilder) Explanation(msg string) *PollBuilder { } // StyledExplanation sets styled explanation message. -func (p *PollBuilder) StyledExplanation(text StyledTextOption, texts ...StyledTextOption) *PollBuilder { - p.opt = text +func (p *PollBuilder) StyledExplanation(texts ...StyledTextOption) *PollBuilder { p.opts = texts return p } @@ -134,10 +132,9 @@ func (p *PollBuilder) apply(ctx context.Context, b *multiMediaBuilder) error { p.input.Poll.ID = id } - // Check if p.opt is not zero. - if !p.opt.Zero() { + if len(p.opts) > 0 { tb := entity.Builder{} - if err := styling.Perform(&tb, p.opt, p.opts...); err != nil { + if err := styling.Perform(&tb, p.opts...); err != nil { return err } p.input.Solution, p.input.SolutionEntities = tb.Complete() diff --git a/telegram/message/style_example_test.go b/telegram/message/style_example_test.go index e8264ee8c1..d63046ecc0 100644 --- a/telegram/message/style_example_test.go +++ b/telegram/message/style_example_test.go @@ -42,7 +42,7 @@ func sendStyledText(ctx context.Context) error { } _, err := message.NewSender(tg.NewClient(client)). - Self().StyledText(ctx, formats[0], formats[1:]...) + Self().StyledText(ctx, formats...) return err }) } diff --git a/telegram/message/styling/builder.go b/telegram/message/styling/builder.go index 0199f05906..ccfaa281a3 100644 --- a/telegram/message/styling/builder.go +++ b/telegram/message/styling/builder.go @@ -10,17 +10,14 @@ type textBuilder struct { *entity.Builder } -func (b *textBuilder) Perform(text StyledTextOption, texts ...StyledTextOption) error { - b.GrowEntities(len(texts) + 1) - length := text.size +func (b *textBuilder) Perform(texts ...StyledTextOption) error { + b.GrowEntities(len(texts)) + var length int for i := range texts { length += texts[i].size } b.GrowText(length) - if err := text.perform(b); err != nil { - return xerrors.Errorf("perform first styling option: %w", err) - } for idx, opt := range texts { if err := opt.perform(b); err != nil { return xerrors.Errorf("perform %d styling option: %w", idx+2, err) @@ -31,7 +28,7 @@ func (b *textBuilder) Perform(text StyledTextOption, texts ...StyledTextOption) } // Perform performs all options to the given builder. -func Perform(builder *entity.Builder, text StyledTextOption, texts ...StyledTextOption) error { +func Perform(builder *entity.Builder, texts ...StyledTextOption) error { tb := &textBuilder{Builder: builder} - return tb.Perform(text, texts...) + return tb.Perform(texts...) } diff --git a/telegram/message/text.go b/telegram/message/text.go index 301bec559f..2969186019 100644 --- a/telegram/message/text.go +++ b/telegram/message/text.go @@ -50,16 +50,14 @@ func (b *Builder) Textf(ctx context.Context, format string, args ...interface{}) } // StyledText sends styled text message. -func (b *Builder) StyledText( - ctx context.Context, text StyledTextOption, texts ...StyledTextOption, -) (tg.UpdatesClass, error) { +func (b *Builder) StyledText(ctx context.Context, texts ...StyledTextOption) (tg.UpdatesClass, error) { p, err := b.peer(ctx) if err != nil { return nil, xerrors.Errorf("peer: %w", err) } tb := entity.Builder{} - if err := styling.Perform(&tb, text, texts...); err != nil { + if err := styling.Perform(&tb, texts...); err != nil { return nil, err } msg, entities := tb.Complete()