mirror of
https://github.com/YspCoder/clawgo.git
synced 2026-05-13 15:57:29 +08:00
telegram: preserve markdown formatting when splitting long messages
This commit is contained in:
@@ -273,10 +273,10 @@ func (c *TelegramChannel) Send(ctx context.Context, msg bus.OutboundMessage) err
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len([]rune(htmlContent)) > 3500 {
|
if len([]rune(htmlContent)) > 3500 {
|
||||||
plain := plainTextFromTelegramHTML(htmlContent)
|
chunks := splitTelegramMarkdown(msg.Content, 3000)
|
||||||
chunks := splitTelegramText(plain, 3500)
|
|
||||||
for i, ch := range chunks {
|
for i, ch := range chunks {
|
||||||
sendParams := telegoutil.Message(chatID, ch)
|
htmlChunk := sanitizeTelegramHTML(markdownToTelegramHTML(ch))
|
||||||
|
sendParams := telegoutil.Message(chatID, htmlChunk).WithParseMode(telego.ModeHTML)
|
||||||
if i == 0 {
|
if i == 0 {
|
||||||
if markup != nil {
|
if markup != nil {
|
||||||
sendParams.WithReplyMarkup(markup)
|
sendParams.WithReplyMarkup(markup)
|
||||||
@@ -633,6 +633,29 @@ func min(a, b int) int {
|
|||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func splitTelegramMarkdown(s string, maxRunes int) []string {
|
||||||
|
s = strings.TrimSpace(s)
|
||||||
|
if s == "" {
|
||||||
|
return []string{""}
|
||||||
|
}
|
||||||
|
if maxRunes <= 0 {
|
||||||
|
maxRunes = 3000
|
||||||
|
}
|
||||||
|
parts := splitTelegramText(s, maxRunes)
|
||||||
|
if len(parts) <= 1 {
|
||||||
|
return parts
|
||||||
|
}
|
||||||
|
// Keep fenced code blocks balanced across chunks.
|
||||||
|
for i := 0; i < len(parts)-1; i++ {
|
||||||
|
if strings.Count(parts[i], "```")%2 == 1 {
|
||||||
|
parts[i] += "\n```"
|
||||||
|
parts[i+1] = "```\n" + parts[i+1]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return parts
|
||||||
|
}
|
||||||
|
|
||||||
func splitTelegramText(s string, maxRunes int) []string {
|
func splitTelegramText(s string, maxRunes int) []string {
|
||||||
s = strings.TrimSpace(s)
|
s = strings.TrimSpace(s)
|
||||||
if s == "" {
|
if s == "" {
|
||||||
@@ -719,7 +742,7 @@ func (c *TelegramChannel) handleAction(ctx context.Context, chatID int64, action
|
|||||||
case "edit":
|
case "edit":
|
||||||
htmlContent := sanitizeTelegramHTML(markdownToTelegramHTML(msg.Content))
|
htmlContent := sanitizeTelegramHTML(markdownToTelegramHTML(msg.Content))
|
||||||
if len([]rune(htmlContent)) > 3500 {
|
if len([]rune(htmlContent)) > 3500 {
|
||||||
htmlContent = sanitizeTelegramHTML(markdownToTelegramHTML(splitTelegramText(plainTextFromTelegramHTML(htmlContent), 3500)[0]))
|
htmlContent = sanitizeTelegramHTML(markdownToTelegramHTML(splitTelegramMarkdown(msg.Content, 3000)[0]))
|
||||||
}
|
}
|
||||||
editCtx, cancel := withTelegramAPITimeout(ctx)
|
editCtx, cancel := withTelegramAPITimeout(ctx)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|||||||
Reference in New Issue
Block a user