fix: split long telegram messages to avoid 4096 char limit
This commit is contained in:
parent
3733b84f02
commit
29fa56eb4f
1 changed files with 67 additions and 18 deletions
|
|
@ -119,34 +119,83 @@ func (c *TelegramChannel) Send(ctx context.Context, msg bus.OutboundMessage) err
|
||||||
c.stopThinking.Delete(msg.ChatID)
|
c.stopThinking.Delete(msg.ChatID)
|
||||||
}
|
}
|
||||||
|
|
||||||
htmlContent := markdownToTelegramHTML(msg.Content)
|
// Split message if too long
|
||||||
|
chunks := splitText(msg.Content, 3800)
|
||||||
|
|
||||||
// Try to edit placeholder
|
for i, chunk := range chunks {
|
||||||
|
htmlContent := markdownToTelegramHTML(chunk)
|
||||||
|
|
||||||
|
// Try to edit placeholder for the FIRST chunk only
|
||||||
|
if i == 0 {
|
||||||
if pID, ok := c.placeholders.Load(msg.ChatID); ok {
|
if pID, ok := c.placeholders.Load(msg.ChatID); ok {
|
||||||
c.placeholders.Delete(msg.ChatID)
|
c.placeholders.Delete(msg.ChatID)
|
||||||
editMsg := tgbotapi.NewEditMessageText(chatID, pID.(int), htmlContent)
|
editMsg := tgbotapi.NewEditMessageText(chatID, pID.(int), htmlContent)
|
||||||
editMsg.ParseMode = tgbotapi.ModeHTML
|
editMsg.ParseMode = tgbotapi.ModeHTML
|
||||||
|
|
||||||
if _, err := c.bot.Send(editMsg); err == nil {
|
if _, err := c.bot.Send(editMsg); err == nil {
|
||||||
return nil
|
continue
|
||||||
}
|
}
|
||||||
// Fallback to new message if edit fails
|
// Fallback to new message if edit fails
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
tgMsg := tgbotapi.NewMessage(chatID, htmlContent)
|
tgMsg := tgbotapi.NewMessage(chatID, htmlContent)
|
||||||
tgMsg.ParseMode = tgbotapi.ModeHTML
|
tgMsg.ParseMode = tgbotapi.ModeHTML
|
||||||
|
|
||||||
if _, err := c.bot.Send(tgMsg); err != nil {
|
if _, err := c.bot.Send(tgMsg); err != nil {
|
||||||
log.Printf("HTML parse failed, falling back to plain text: %v", err)
|
log.Printf("HTML parse failed or chunk too long, falling back to plain text: %v", err)
|
||||||
tgMsg = tgbotapi.NewMessage(chatID, msg.Content)
|
tgMsg = tgbotapi.NewMessage(chatID, chunk)
|
||||||
tgMsg.ParseMode = ""
|
tgMsg.ParseMode = ""
|
||||||
_, err = c.bot.Send(tgMsg)
|
if _, err = c.bot.Send(tgMsg); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Small delay between chunks to preserve order and avoid rate limits
|
||||||
|
if i < len(chunks)-1 {
|
||||||
|
time.Sleep(200 * time.Millisecond)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func splitText(text string, limit int) []string {
|
||||||
|
if len(text) <= limit {
|
||||||
|
return []string{text}
|
||||||
|
}
|
||||||
|
|
||||||
|
var chunks []string
|
||||||
|
remaining := text
|
||||||
|
for len(remaining) > limit {
|
||||||
|
// Try to find a good place to split: double newline first
|
||||||
|
splitIdx := strings.LastIndex(remaining[:limit], "\n\n")
|
||||||
|
if splitIdx == -1 {
|
||||||
|
// Then single newline
|
||||||
|
splitIdx = strings.LastIndex(remaining[:limit], "\n")
|
||||||
|
}
|
||||||
|
if splitIdx == -1 {
|
||||||
|
// Then space
|
||||||
|
splitIdx = strings.LastIndex(remaining[:limit], " ")
|
||||||
|
}
|
||||||
|
if splitIdx == -1 {
|
||||||
|
// Force split at limit
|
||||||
|
splitIdx = limit
|
||||||
|
}
|
||||||
|
|
||||||
|
chunks = append(chunks, remaining[:splitIdx])
|
||||||
|
remaining = remaining[splitIdx:]
|
||||||
|
remaining = strings.TrimSpace(remaining)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(remaining) > 0 {
|
||||||
|
chunks = append(chunks, remaining)
|
||||||
|
}
|
||||||
|
|
||||||
|
return chunks
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
func (c *TelegramChannel) handleMessage(update tgbotapi.Update) {
|
func (c *TelegramChannel) handleMessage(update tgbotapi.Update) {
|
||||||
message := update.Message
|
message := update.Message
|
||||||
if message == nil {
|
if message == nil {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue