]> git.giacomolli.uk - onechat/blobdiff - telegram.go
Capitalize type names so variables can use them
[onechat] / telegram.go
index dabf2e8a70be50b919fcd9006522e2cf8d6a709b..dd62ef0221db72fc88a1893b1421ce301cab0cff 100644 (file)
@@ -8,11 +8,13 @@ import (
        "fmt"
        "io"
        "log"
        "fmt"
        "io"
        "log"
+       "net/http"
        "os"
        "path/filepath"
        "strings"
        "sync"
        "time"
        "os"
        "path/filepath"
        "strings"
        "sync"
        "time"
+       "unicode/utf8"
 )
 
 const (
 )
 
 const (
@@ -32,6 +34,12 @@ const (
        // messageUnits is the most UTF-16 code units of one Telegram message.
        messageUnits = 4096
 
        // 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
        // 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
@@ -50,9 +58,11 @@ type botConfig struct {
 // telegramBot connects the private chat of the owner with the agent. It polls
 // the Bot API for new messages and sends each text message of the owner to the
 // agent. It ignores every other update. Its sender sends the replies, the
 // telegramBot connects the private chat of the owner with the agent. It polls
 // the Bot API for new messages and sends each text message of the owner to the
 // agent. It ignores every other update. Its sender sends the replies, the
-// errors, and the user messages of the other clients to the chat as plain text,
-// and shows the typing action while a turn runs. Tool calls do not go to the
-// chat.
+// errors, the notices of the bot, and the user messages of the other clients to
+// the chat, and shows the typing action while a turn runs. Only the replies
+// have a format: their Markdown goes as rich messages, which Telegram renders
+// itself, and as message entities only when Telegram refuses a rich message.
+// The rest is plain text. Tool calls do not go to the chat.
 //
 // The offset file holds the ID of the next update. It moves past an update
 // only after the message of the update is durable in the log, so a restart
 //
 // The offset file holds the ID of the next update. It moves past an update
 // only after the message of the update is durable in the log, so a restart
@@ -74,6 +84,13 @@ type telegramBot struct {
        ready chan struct{}
 }
 
        ready chan struct{}
 }
 
+// botPart is the text of one message to the chat, at most messageUnits long,
+// and its entities.
+type botPart struct {
+       text     string
+       entities []botEntity
+}
+
 // offsetFile is the content of the offset file.
 type offsetFile struct {
        Offset int64 `json:"offset"`
 // offsetFile is the content of the offset file.
 type offsetFile struct {
        Offset int64 `json:"offset"`
@@ -156,7 +173,7 @@ func newTelegramBot(api *botAPI, owner int64, offsetPath string, logger *log.Log
 // stop the agent.
 func (b *telegramBot) Notify(event Event) {
        switch event.Kind {
 // stop the agent.
 func (b *telegramBot) Notify(event Event) {
        switch event.Kind {
-       case "busy", "idle", "reply", "error":
+       case "busy", "idle", "reply", "error", "notice":
        case "user":
                if event.Source == telegramSource {
                        return
        case "user":
                if event.Source == telegramSource {
                        return
@@ -228,9 +245,7 @@ func (b *telegramBot) send(ctx context.Context) {
                case "idle":
                        busy = false
                default:
                case "idle":
                        busy = false
                default:
-                       for _, part := range splitMessage(botText(event), messageUnits) {
-                               b.deliver(ctx, part)
-                       }
+                       b.post(ctx, event)
 
                        if busy {
                                b.typing(ctx)
 
                        if busy {
                                b.typing(ctx)
@@ -260,16 +275,77 @@ func (b *telegramBot) typing(ctx context.Context) {
        _ = b.api.SendTyping(ctx, b.owner)
 }
 
        _ = b.api.SendTyping(ctx, b.owner)
 }
 
-// deliver sends text to the chat. It tries again after each failure that a
-// later try can fix, until ctx ends. It drops the text after any other failure.
-func (b *telegramBot) deliver(ctx context.Context, text string) {
+// post sends event to the chat. A reply goes as rich messages of its Markdown,
+// split with splitRich. Every other event goes as plain text, split with
+// splitMessage.
+func (b *telegramBot) post(ctx context.Context, event Event) {
+       if event.Kind != "reply" {
+               for _, text := range splitMessage(botText(event), messageUnits) {
+                       b.deliver(ctx, botPart{
+                               text: text,
+                       })
+               }
+
+               return
+       }
+
+       for _, markdown := range splitRich(event.Text, richMessageBytes) {
+               b.deliverRich(ctx, markdown)
+       }
+}
+
+// deliverRich sends markdown to the chat as a rich message. When the Bot API
+// refuses it with HTTP 400 or 404, the message was not sent, so deliverRich
+// sends the same Markdown once with message entities, and the reply is not
+// lost and does not repeat. It tries again after each failure that a later try
+// can fix, until ctx ends. It drops the message after any other failure.
+func (b *telegramBot) deliverRich(ctx context.Context, markdown string) {
        wait := b.minWait
        for {
        wait := b.minWait
        for {
-               err := b.api.SendMessage(ctx, b.owner, text)
+               err := b.api.SendRichMessage(ctx, b.owner, markdown)
                if err == nil || ctx.Err() != nil {
                        return
                }
 
                if err == nil || ctx.Err() != nil {
                        return
                }
 
+               if rejected(err) {
+                       b.logger.Printf("rich message refused, the reply goes with entities: %v", err)
+                       for _, part := range replyParts(markdown) {
+                               b.deliver(ctx, part)
+                       }
+
+                       return
+               }
+
+               retry, asked := retryable(err)
+               if !retry {
+                       b.logger.Printf("message dropped: %v", err)
+                       return
+               }
+
+               wait = b.pause(ctx, err, wait, asked)
+       }
+}
+
+// deliver sends part to the chat. When Telegram refuses a part with entities
+// (HTTP 400), it sends the text of the part again once with no entities, so a
+// reply is not lost to a bad entity. It tries again after each failure that a
+// later try can fix, until ctx ends. It drops the part after any other failure.
+func (b *telegramBot) deliver(ctx context.Context, part botPart) {
+       wait := b.minWait
+       for {
+               err := b.api.SendMessage(ctx, b.owner, part.text, part.entities)
+               if err == nil || ctx.Err() != nil {
+                       return
+               }
+
+               var answer *botError
+               if len(part.entities) > 0 && errors.As(err, &answer) && answer.Status == http.StatusBadRequest {
+                       b.logger.Printf("entities refused, the message goes again as plain text: %v", err)
+                       part.entities = nil
+
+                       continue
+               }
+
                retry, asked := retryable(err)
                if !retry {
                        b.logger.Printf("message dropped: %v", err)
                retry, asked := retryable(err)
                if !retry {
                        b.logger.Printf("message dropped: %v", err)
@@ -379,7 +455,7 @@ func (b *telegramBot) handle(ctx context.Context, l *loop, update botUpdate) err
 // notice queues a short answer of the bot for the chat.
 func (b *telegramBot) notice(text string) {
        b.Notify(Event{
 // notice queues a short answer of the bot for the chat.
 func (b *telegramBot) notice(text string) {
        b.Notify(Event{
-               Kind: "reply",
+               Kind: "notice",
                Text: text,
        })
 }
                Text: text,
        })
 }
@@ -480,3 +556,127 @@ func prefixEnd(text string, limit int) int {
 
        return len(text)
 }
 
        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, "", ""
+}
+
+// replyParts returns the messages of the Markdown of a reply with message
+// entities. A reply that has no text left after the conversion, such as an
+// empty code block, goes as its Markdown.
+func replyParts(markdown string) []botPart {
+       text, entities := markdownEntities(markdown)
+       if strings.TrimSpace(text) == "" {
+               text, entities = markdown, nil
+       }
+
+       return splitEntities(text, entities, messageUnits)
+}
+
+// splitEntities cuts text into parts with splitMessage, and gives each part the
+// entities in it, with offsets from the start of the part. An entity that
+// crosses a cut becomes one entity in each part.
+func splitEntities(text string, entities []botEntity, limit int) []botPart {
+       var parts []botPart
+       start := 0
+       for _, part := range splitMessage(text, limit) {
+               end := start + utf16Units(part)
+               var kept []botEntity
+               for _, entity := range entities {
+                       from := max(entity.Offset, start)
+                       to := min(entity.Offset+entity.Length, end)
+                       if to <= from {
+                               continue
+                       }
+
+                       entity.Offset = from - start
+                       entity.Length = to - from
+                       kept = append(kept, entity)
+               }
+
+               parts = append(parts, botPart{
+                       text:     part,
+                       entities: kept,
+               })
+
+               start = end
+       }
+
+       return parts
+}