X-Git-Url: https://git.giacomolli.uk/onechat/blobdiff_plain/f80b058ae0550a56b4e62f8e65c2a74349b970e4..b64d7548bd9f61921bdc650228642e77c73b0777:/telegram.go?ds=sidebyside diff --git a/telegram.go b/telegram.go index 9a24a4e..9dbe86e 100644 --- a/telegram.go +++ b/telegram.go @@ -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, "", "" +}