fix long text in telegram
This commit is contained in:
+26
-20
@@ -129,7 +129,7 @@ func (b *Bot) sendMemory(ctx context.Context, mem *search.Memory) {
|
||||
return
|
||||
}
|
||||
|
||||
mainText, captionText := formatMemory(mem, b.publicURL)
|
||||
text := formatMemory(mem, b.publicURL)
|
||||
images := imageAttachments(mem.Memo)
|
||||
|
||||
var downloaded []imageFile
|
||||
@@ -137,7 +137,7 @@ func (b *Bot) sendMemory(ctx context.Context, mem *search.Memory) {
|
||||
downloaded = b.downloadImages(ctx, images)
|
||||
}
|
||||
|
||||
if err := b.sendWithRetry(ctx, mainText, captionText, downloaded); err != nil {
|
||||
if err := b.sendWithRetry(ctx, text, downloaded); err != nil {
|
||||
b.logger.Error("failed to send telegram message after retries", "error", err)
|
||||
}
|
||||
}
|
||||
@@ -206,12 +206,12 @@ func (b *Bot) downloadImages(ctx context.Context, attachments []memos.Attachment
|
||||
}
|
||||
|
||||
// sendWithRetry attempts to send the message with up to 3 retries.
|
||||
func (b *Bot) sendWithRetry(ctx context.Context, mainText, captionText string, images []imageFile) error {
|
||||
func (b *Bot) sendWithRetry(ctx context.Context, text string, images []imageFile) error {
|
||||
backoffs := []time.Duration{30 * time.Second, 60 * time.Second, 120 * time.Second}
|
||||
var lastErr error
|
||||
|
||||
for attempt := range 3 {
|
||||
lastErr = b.send(mainText, captionText, images)
|
||||
lastErr = b.send(text, images)
|
||||
if lastErr == nil {
|
||||
return nil
|
||||
}
|
||||
@@ -229,35 +229,41 @@ func (b *Bot) sendWithRetry(ctx context.Context, mainText, captionText string, i
|
||||
}
|
||||
|
||||
// send executes the actual Telegram API calls based on the sending strategy.
|
||||
func (b *Bot) send(mainText, captionText string, images []imageFile) error {
|
||||
// Long text is split into multiple messages. Images are sent with a caption
|
||||
// only if the full text fits within the caption limit.
|
||||
func (b *Bot) send(text string, images []imageFile) error {
|
||||
switch {
|
||||
case len(images) == 0:
|
||||
// No images — just text
|
||||
return b.sendText(mainText)
|
||||
return b.sendTextParts(splitText(text, maxMessageLen))
|
||||
|
||||
case len(images) == 1 && len(captionText) <= maxCaptionLen:
|
||||
// Single image with short caption
|
||||
return b.sendPhoto(images[0], captionText)
|
||||
case len(text) <= maxCaptionLen:
|
||||
// Short text — use as caption on image(s)
|
||||
if len(images) == 1 {
|
||||
return b.sendPhoto(images[0], text)
|
||||
}
|
||||
return b.sendMediaGroup(images, text)
|
||||
|
||||
case len(images) > 1 && len(captionText) <= maxCaptionLen:
|
||||
// Multiple images with short caption
|
||||
return b.sendMediaGroup(images, captionText)
|
||||
|
||||
case len(images) >= 1 && len(captionText) > maxCaptionLen:
|
||||
// Images with long text — send text first, then images without caption
|
||||
if err := b.sendText(mainText); err != nil {
|
||||
default:
|
||||
// Long text — send text messages first, then images without caption
|
||||
if err := b.sendTextParts(splitText(text, maxMessageLen)); err != nil {
|
||||
return err
|
||||
}
|
||||
if len(images) == 1 {
|
||||
return b.sendPhoto(images[0], "")
|
||||
}
|
||||
return b.sendMediaGroup(images, "")
|
||||
|
||||
default:
|
||||
return b.sendText(mainText)
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Bot) sendTextParts(parts []string) error {
|
||||
for _, part := range parts {
|
||||
if err := b.sendText(part); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *Bot) sendText(text string) error {
|
||||
msg := tgbotapi.NewMessage(b.chatID, text)
|
||||
msg.ParseMode = tgbotapi.ModeHTML
|
||||
|
||||
Reference in New Issue
Block a user