"strings"
"sync"
"time"
+ "unicode/utf8"
)
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
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, "", ""
+}