fix(telegram): error handling for sendWithAttachments

This commit is contained in:
XZB-1248 2026-02-19 23:04:08 +08:00
parent 6e697f35a6
commit f5ca5d2e6c

View file

@ -215,7 +215,12 @@ func (c *TelegramChannel) sendWithAttachments(ctx context.Context, chatID int64,
}) })
tgMsg.ParseMode = "" tgMsg.ParseMode = ""
tgMsg.Text = content 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 { } else {
@ -228,29 +233,38 @@ func (c *TelegramChannel) sendWithAttachments(ctx context.Context, chatID int64,
}) })
tgMsg.ParseMode = "" tgMsg.ParseMode = ""
tgMsg.Text = content 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 // Now send files as separate messages
for _, attachment := range attachments { for _, attachment := range attachments {
file, err := os.Open(attachment.Path) uploadErr := func(att bus.Attachment) error {
file, err := os.Open(att.Path)
if err != nil { if err != nil {
return fmt.Errorf("failed to open attachment %s: %w", attachment.Path, err) return fmt.Errorf("failed to open attachment %s: %w", att.Path, err)
} }
defer file.Close()
document := tu.Document( document := tu.Document(
tu.ID(chatID), tu.ID(chatID),
tu.File(file), tu.File(file),
) )
document.Caption = attachment.Filename document.Caption = att.Filename
if _, err := c.bot.SendDocument(ctx, document); err != nil { if _, err := c.bot.SendDocument(ctx, document); err != nil {
file.Close() return fmt.Errorf("failed to send document %s: %w", att.Filename, err)
return fmt.Errorf("failed to send document %s: %w", attachment.Filename, err) }
return nil
}(attachment)
if uploadErr != nil {
return uploadErr
} }
file.Close()
} }
return nil return nil