]> git.giacomolli.uk - onechat/blobdiff - telegram.go
Split long replies between Markdown blocks
[onechat] / telegram.go
index 9a24a4e861d7436bcd0a109480b50fbbb538377e..9dbe86e1cf24e6b629245bd71414aa9d2334bf58 100644 (file)
@@ -14,6 +14,7 @@ import (
        "strings"
        "sync"
        "time"
+       "unicode/utf8"
 )
 
 const (
@@ -33,6 +34,12 @@ const (
        // messageUnits is the most UTF-16 code units of one Telegram message.
        messageUnits = 4096
 
+       // richMessageBytes is the most bytes of the Markdown of one rich
+       // message. The Bot API allows 32768 characters of rendered text, and
+       // the Markdown of a text has at least as many bytes as the text has
+       // characters.
+       richMessageBytes = 32768
+
        // typingInterval is how often the bot shows the typing action while a
        // turn runs. Telegram shows the action for about five seconds.
        typingInterval = 4 * time.Second
@@ -548,3 +555,83 @@ func prefixEnd(text string, limit int) int {
 
        return len(text)
 }
+
+// splitRich cuts the Markdown of a reply into parts of at most limit bytes. It
+// cuts after a blank line outside a code block, so a table, a code block, or a
+// paragraph stays whole. When no such cut fits in a part, it cuts after the
+// last line break outside a code block, then after the last line break in a
+// code block, which it closes at the end of the part and opens again in the
+// next part with the same fence line. A line longer than limit is cut at the
+// limit. It never cuts a character.
+func splitRich(markdown string, limit int) []string {
+       var parts []string
+       for len(markdown) > limit {
+               cut, fence, opening := richCut(markdown, limit)
+               if fence == "" {
+                       parts = append(parts, markdown[:cut])
+                       markdown = markdown[cut:]
+
+                       continue
+               }
+
+               parts = append(parts, markdown[:cut]+fence+"\n")
+               markdown = opening + "\n" + markdown[cut:]
+       }
+
+       return append(parts, markdown)
+}
+
+// richCut returns where splitRich cuts markdown, which is longer than limit.
+// For a cut in a code block, it also returns the fence that closes the block
+// and the line that opens it. Each part before a cut holds text other than
+// space, and a cut in a code block leaves room for the fence, so each cut makes
+// the rest shorter.
+func richCut(markdown string, limit int) (int, string, string) {
+       blank, line, code := 0, 0, 0
+       var fence, opening, inFence, inOpening string
+       for start := 0; start < limit; {
+               end := strings.IndexByte(markdown[start:], '\n')
+               if end < 0 || start+end+1 > limit {
+                       break
+               }
+
+               end += start + 1
+               text := strings.TrimRight(markdown[start:end], "\r\n")
+               _, opens := codeFence(text)
+               switch {
+               case fence != "":
+                       if closesFence(text, fence) {
+                               fence = ""
+                       } else if end > len(opening)+1 && end+len(fence)+1 <= limit {
+                               code, inFence, inOpening = end, fence, opening
+                       }
+               case opens != "":
+                       fence, opening = opens, text
+               case strings.TrimSpace(markdown[:end]) == "":
+               case strings.TrimSpace(text) == "":
+                       blank = end
+               }
+
+               if fence == "" && strings.TrimSpace(markdown[:end]) != "" {
+                       line = end
+               }
+
+               start = end
+       }
+
+       switch {
+       case blank > 0:
+               return blank, "", ""
+       case line > 0:
+               return line, "", ""
+       case code > 0:
+               return code, inFence, inOpening
+       }
+
+       cut := limit
+       for !utf8.RuneStart(markdown[cut]) {
+               cut--
+       }
+
+       return cut, "", ""
+}