From f5ca5d2e6cc90a8e6f7db875fef98d7a9d7a7cf2 Mon Sep 17 00:00:00 2001 From: XZB-1248 <28593573+XZB-1248@users.noreply.github.com> Date: Thu, 19 Feb 2026 23:04:08 +0800 Subject: [PATCH] fix(telegram): error handling for `sendWithAttachments` --- pkg/channels/telegram.go | 50 +++++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 18 deletions(-) diff --git a/pkg/channels/telegram.go b/pkg/channels/telegram.go index b3974c77a..d7dd5a719 100644 --- a/pkg/channels/telegram.go +++ b/pkg/channels/telegram.go @@ -215,7 +215,12 @@ func (c *TelegramChannel) sendWithAttachments(ctx context.Context, chatID int64, }) tgMsg.ParseMode = "" tgMsg.Text = content - c.bot.SendMessage(ctx, tgMsg) + if _, err = c.bot.SendMessage(ctx, tgMsg); err != nil { + logger.ErrorCF("telegram", "Failed to send message with attachments", map[string]interface{}{ + "error": err.Error(), + }) + return fmt.Errorf("failed to send message: %w", err) + } } } } else { @@ -228,29 +233,38 @@ func (c *TelegramChannel) sendWithAttachments(ctx context.Context, chatID int64, }) tgMsg.ParseMode = "" tgMsg.Text = content - c.bot.SendMessage(ctx, tgMsg) + if _, err = c.bot.SendMessage(ctx, tgMsg); err != nil { + logger.ErrorCF("telegram", "Failed to send message with attachments", map[string]interface{}{ + "error": err.Error(), + }) + return fmt.Errorf("failed to send message: %w", err) + } } } // Now send files as separate messages for _, attachment := range attachments { - file, err := os.Open(attachment.Path) - if err != nil { - return fmt.Errorf("failed to open attachment %s: %w", attachment.Path, err) + uploadErr := func(att bus.Attachment) error { + file, err := os.Open(att.Path) + if err != nil { + return fmt.Errorf("failed to open attachment %s: %w", att.Path, err) + } + defer file.Close() + + document := tu.Document( + tu.ID(chatID), + tu.File(file), + ) + document.Caption = att.Filename + + if _, err := c.bot.SendDocument(ctx, document); err != nil { + return fmt.Errorf("failed to send document %s: %w", att.Filename, err) + } + return nil + }(attachment) + if uploadErr != nil { + return uploadErr } - - document := tu.Document( - tu.ID(chatID), - tu.File(file), - ) - document.Caption = attachment.Filename - - if _, err := c.bot.SendDocument(ctx, document); err != nil { - file.Close() - return fmt.Errorf("failed to send document %s: %w", attachment.Filename, err) - } - - file.Close() } return nil