]> git.giacomolli.uk - onechat/blob - telegram.go
Submit root events through the local socket
[onechat] / telegram.go
1 package main
2
3 import (
4 "bytes"
5 "context"
6 "encoding/json"
7 "errors"
8 "fmt"
9 "io"
10 "log/slog"
11 "net/http"
12 "os"
13 "path/filepath"
14 "strings"
15 "sync"
16 "time"
17 "unicode/utf8"
18 )
19
20 const (
21 // telegramRetryDelay is the wait before the first retry of a failed
22 // call.
23 telegramRetryDelay = time.Second
24
25 // telegramRetryLimit is the longest wait between retries of a failed
26 // call.
27 telegramRetryLimit = time.Minute
28
29 // telegramAPIURL is the Telegram Bot API service.
30 telegramAPIURL = "https://api.telegram.org"
31
32 // telegramConfigFileName is the bot config in the data directory. The
33 // daemon runs the bot only when the file exists.
34 telegramConfigFileName = "telegram.json"
35
36 // telegramOffsetFileName is the update offset of the bot in the agent
37 // directory.
38 telegramOffsetFileName = "telegram-offset.json"
39
40 // telegramSource is the source of the user messages from Telegram.
41 telegramSource = "telegram"
42
43 // telegramMessageUnits is the most UTF-16 code units of one Telegram
44 // message.
45 telegramMessageUnits = 4096
46
47 // telegramRichBytes is the most bytes of the Markdown of one rich
48 // message. The Bot API allows 32768 characters of rendered text, and
49 // the Markdown of a text has at least as many bytes as the text has
50 // characters.
51 telegramRichBytes = 32768
52
53 // telegramTypingInterval is how often the bot shows the typing action
54 // while a turn runs. Telegram shows the action for about five seconds.
55 telegramTypingInterval = 4 * time.Second
56
57 // telegramTextOnly is the answer to a message that is not text.
58 telegramTextOnly = "Only text messages are supported."
59 )
60
61 // TelegramConfig holds the settings of telegram.json: the bot token and the
62 // user ID of the owner. The token is a secret, so the file must have mode 0600.
63 type TelegramConfig struct {
64 Token string `json:"token"`
65 Owner int64 `json:"owner"`
66 }
67
68 // TelegramBot connects the private chat of the owner with the agent. It polls
69 // the Bot API for new messages and sends owner text, images, and voice notes to
70 // the agent. It ignores every other update. Its sender sends the replies, the
71 // errors, the notices of the bot, and the user messages of the other clients to
72 // the chat, and shows the typing action while a turn runs. Only the replies
73 // have a format: their Markdown goes as rich messages, which Telegram renders
74 // itself, and as message entities only when Telegram refuses a rich message.
75 // The rest is plain text. Tool calls do not go to the chat.
76 //
77 // The offset file holds the ID of the next update. It moves past an update only
78 // after the message of the update is durable in the log, so a restart does not
79 // lose a message. A crash between the flush and the offset write makes Telegram
80 // send the update again, and the message repeats once.
81 //
82 // A failed Bot API call waits and tries again, from telegramRetryDelay up to
83 // telegramRetryLimit, or as long as the Bot API asks. It never stops the agent.
84 // A TelegramOutage logs the first failure and the end of each run of failures,
85 // and the retries between them log nothing.
86 type TelegramBot struct {
87 api *TelegramAPI
88 voice *VoiceTranscriber
89 owner int64
90 offsetPath string
91 logger *slog.Logger
92
93 mutex sync.Mutex
94 queue []Event
95 ready chan struct{}
96 }
97
98 // TelegramOutage is one run of failures of a kind of Bot API call. The first
99 // failure logs loss, its cause, and the wait of the next try. The retries log
100 // nothing. The next answer of Telegram logs back and the length of the outage.
101 // loss and back say what the user loses and gets again.
102 type TelegramOutage struct {
103 logger *slog.Logger
104 loss string
105 back string
106 start time.Time
107 }
108
109 // TelegramPart is the text of one message to the chat, at most
110 // telegramMessageUnits long, and its entities.
111 type TelegramPart struct {
112 text string
113 entities []TelegramEntity
114 }
115
116 // TelegramOffsetFile is the content of the offset file.
117 type TelegramOffsetFile struct {
118 Offset int64 `json:"offset"`
119 }
120
121 // loadTelegramConfig reads telegram.json in dir. It returns false when the file
122 // does not exist. An error never holds the token.
123 func loadTelegramConfig(dir string) (TelegramConfig, bool, error) {
124 path := filepath.Join(dir, telegramConfigFileName)
125 info, err := os.Stat(path)
126 if errors.Is(err, os.ErrNotExist) {
127 return TelegramConfig{}, false, nil
128 }
129
130 if err != nil {
131 return TelegramConfig{}, false, fmt.Errorf("read bot config: %w", err)
132 }
133
134 if info.Mode().Perm()&0o077 != 0 {
135 return TelegramConfig{}, false, fmt.Errorf("%s holds the bot token, so its mode must be 0600", telegramConfigFileName)
136 }
137
138 data, err := os.ReadFile(path)
139 if err != nil {
140 return TelegramConfig{}, false, fmt.Errorf("read bot config: %w", err)
141 }
142
143 var config TelegramConfig
144
145 decoder := json.NewDecoder(bytes.NewReader(data))
146 decoder.DisallowUnknownFields()
147
148 err = decoder.Decode(&config)
149 if err == nil && !errors.Is(decoder.Decode(&struct{}{}), io.EOF) {
150 err = errors.New("data after the JSON object")
151 }
152
153 if err != nil {
154 return TelegramConfig{}, false, fmt.Errorf("%s is not valid JSON with the fields token and owner", telegramConfigFileName)
155 }
156
157 err = config.validate()
158 if err != nil {
159 return TelegramConfig{}, false, fmt.Errorf("%s: %w", telegramConfigFileName, err)
160 }
161
162 return config, true, nil
163 }
164
165 // newTelegramBot returns the bot of owner on api. The offset file is at
166 // offsetPath, and logger gets the failures. No log line holds the text of a
167 // message.
168 func newTelegramBot(api *TelegramAPI, owner int64, offsetPath string, logger *slog.Logger) *TelegramBot {
169 return &TelegramBot{
170 api: api,
171 voice: &VoiceTranscriber{},
172 owner: owner,
173 offsetPath: offsetPath,
174 logger: logger,
175 ready: make(chan struct{}, 1),
176 }
177 }
178
179 // validate makes sure that the token has the form of a bot token and that the
180 // owner is a user ID. The token goes into a URL path, so it can only hold the
181 // characters of a bot token.
182 func (c TelegramConfig) validate() error {
183 id, secret, found := strings.Cut(c.Token, ":")
184 if !found || id == "" || secret == "" || strings.Trim(id, "0123456789") != "" || strings.Trim(secret, "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-") != "" {
185 return errors.New("token does not have the form of a bot token from BotFather")
186 }
187
188 if c.Owner <= 0 {
189 return errors.New("owner must be the numeric Telegram user ID of the owner")
190 }
191
192 return nil
193 }
194
195 // Run polls for the messages of the owner and sends the queued events to the
196 // chat until ctx ends. It returns an error only when a message or the offset or
197 // image cannot be written, because then the bot would lose or repeat messages.
198 func (b *TelegramBot) Run(ctx context.Context, l *TurnLoop, whatsapp *WhatsApp) error {
199 ctx, cancel := context.WithCancel(ctx)
200 defer cancel()
201
202 var sender sync.WaitGroup
203
204 sender.Add(1)
205
206 go func() {
207 defer sender.Done()
208
209 b.send(ctx)
210 }()
211
212 err := b.poll(ctx, l, whatsapp)
213
214 cancel()
215 sender.Wait()
216
217 return err
218 }
219
220 // send sends each queued event to the chat in order until ctx ends. While a
221 // turn runs, it shows the typing action every telegramTypingInterval and after
222 // each message, because a message ends the action.
223 func (b *TelegramBot) send(ctx context.Context) {
224 ticker := time.NewTicker(telegramTypingInterval)
225 defer ticker.Stop()
226
227 busy := false
228 for ctx.Err() == nil {
229 event, found := b.next()
230 if !found {
231 select {
232 case <-b.ready:
233 case <-ticker.C:
234 if busy {
235 b.typing(ctx)
236 }
237 case <-ctx.Done():
238 }
239
240 continue
241 }
242
243 switch event.Kind {
244 case "busy":
245 busy = true
246
247 b.typing(ctx)
248 ticker.Reset(telegramTypingInterval)
249 case "idle":
250 busy = false
251 default:
252 b.post(ctx, event)
253
254 if busy {
255 b.typing(ctx)
256 }
257 }
258 }
259 }
260
261 // poll gets updates from the stored offset and handles them until ctx ends. A
262 // failed image download retries from the same offset. A submitted message is
263 // flushed even if ctx ends, and only then can the offset advance.
264 func (b *TelegramBot) poll(ctx context.Context, l *TurnLoop, whatsapp *WhatsApp) error {
265 offset, err := telegramLoadOffset(b.offsetPath)
266 if err != nil {
267 return fmt.Errorf("load Telegram offset: %w", err)
268 }
269
270 outage := &TelegramOutage{
271 logger: b.logger,
272 loss: "receive failed",
273 back: "receive back",
274 }
275
276 imageOutage := &TelegramOutage{
277 logger: b.logger,
278 loss: "image receive failed",
279 back: "image receive back",
280 }
281
282 imageWait := telegramRetryDelay
283 wait := telegramRetryDelay
284
285 poll:
286 for ctx.Err() == nil {
287 updates, err := b.api.GetUpdates(ctx, offset)
288 if ctx.Err() != nil {
289 return nil
290 }
291
292 if err != nil {
293 _, asked := telegramRetryable(err)
294 wait = b.pause(ctx, outage, err, wait, asked)
295
296 continue
297 }
298
299 outage.End()
300
301 wait = telegramRetryDelay
302
303 for _, update := range updates {
304 if ctx.Err() != nil {
305 return nil
306 }
307
308 err = b.handle(ctx, l, whatsapp, update)
309 if err != nil {
310 retry, asked := telegramRetryable(err)
311 if retry {
312 imageWait = b.pause(ctx, imageOutage, err, imageWait, asked)
313 continue poll
314 }
315
316 return fmt.Errorf("handle Telegram update: %w", err)
317 }
318
319 imageOutage.End()
320
321 imageWait = telegramRetryDelay
322
323 offset = update.UpdateID + 1
324 err = telegramSaveOffset(b.offsetPath, offset)
325 if err != nil {
326 return fmt.Errorf("save Telegram offset: %w", err)
327 }
328 }
329 }
330
331 return nil
332 }
333
334 // next removes the first queued event and returns it.
335 func (b *TelegramBot) next() (Event, bool) {
336 b.mutex.Lock()
337 defer b.mutex.Unlock()
338
339 if len(b.queue) == 0 {
340 return Event{}, false
341 }
342
343 event := b.queue[0]
344 b.queue = b.queue[1:]
345
346 return event, true
347 }
348
349 // typing shows the typing action once. A failure is not logged, because the
350 // next message or poll logs the state of the network.
351 func (b *TelegramBot) typing(ctx context.Context) {
352 // The next message or poll reports a network failure.
353 b.api.SendTyping(ctx, b.owner) // nolint: errcheck
354 }
355
356 // post sends event to the chat. A reply goes as rich messages of its Markdown,
357 // with each < and & outside code escaped by telegramEscapeTags and then split
358 // with telegramSplitRich, so that the length limit counts the escaped text.
359 // Every other event goes as plain text, split with telegramSplitMessage.
360 func (b *TelegramBot) post(ctx context.Context, event Event) {
361 if event.Kind != "reply" {
362 for _, text := range telegramSplitMessage(telegramText(event), telegramMessageUnits) {
363 b.deliver(ctx, TelegramPart{
364 text: text,
365 })
366 }
367
368 return
369 }
370
371 for _, markdown := range telegramSplitRich(telegramEscapeTags(event.Text), telegramRichBytes) {
372 b.deliverRich(ctx, markdown)
373 }
374 }
375
376 // pause gives err to outage and waits the longer of wait and asked, or until
377 // ctx ends. It returns the wait of the next failure: twice wait, up to
378 // telegramRetryLimit.
379 func (b *TelegramBot) pause(ctx context.Context, outage *TelegramOutage, err error, wait, asked time.Duration) time.Duration {
380 delay := max(wait, asked)
381 outage.Fail(err, delay)
382
383 timer := time.NewTimer(delay)
384 defer timer.Stop()
385
386 select {
387 case <-timer.C:
388 case <-ctx.Done():
389 }
390
391 return min(2*wait, telegramRetryLimit)
392 }
393
394 // handle runs one update. A text message of the owner in the private chat goes
395 // to the agent, and handle returns when it is durable in the log. The line
396 // /stop interrupts the turn, the line /stop NAME stops the subagent NAME, and a
397 // line that starts with /whatsapp runs a WhatsApp command on whatsapp, whose
398 // answers go to the chat only. Photos and image documents enter the shared
399 // image store with their caption. Voice notes keep their audio and local
400 // transcript. Other media keep the text-only answer. Every other update is
401 // ignored, and its content is not logged.
402 func (b *TelegramBot) handle(ctx context.Context, l *TurnLoop, whatsapp *WhatsApp, update TelegramUpdate) error {
403 message := update.Message
404 if message == nil || message.Chat.Type != "private" || message.Chat.ID != b.owner || message.From == nil || message.From.ID != b.owner {
405 b.logger.Info("message ignored: not from the owner")
406
407 return nil
408 }
409
410 name, stop := stopSubagentName(message.Text)
411 args, isWhatsApp := whatsappCommandArgs(message.Text)
412 switch {
413 case message.Voice != nil:
414 text := telegramVoiceText(ctx, b.api, l.agent.Voices(), b.voice, message)
415 _, err := l.Submit(telegramSource, text)
416 if err != nil {
417 return fmt.Errorf("submit Telegram voice: %w", err)
418 }
419
420 err = l.Flush(context.WithoutCancel(ctx))
421 if err != nil {
422 return fmt.Errorf("flush Telegram voice: %w", err)
423 }
424 case len(message.Photo) > 0 || message.Document != nil:
425 text, err := telegramImageText(ctx, b.api, l.agent.Images(), message)
426 if err != nil {
427 retry, _ := telegramRetryable(err)
428 if retry {
429 return fmt.Errorf("receive image: %w", err)
430 }
431
432 switch {
433 case errors.Is(err, errTelegramOtherMedia):
434 b.notice(telegramTextOnly)
435 case errors.Is(err, errImageSize), errors.Is(err, errImageFormat), errors.Is(err, errTelegramPhotoSize):
436 b.notice(err.Error())
437 default:
438 var answer *TelegramError
439
440 if !errors.As(err, &answer) {
441 return fmt.Errorf("receive Telegram image: %w", err)
442 }
443
444 b.notice("Cannot download image.")
445 }
446
447 return nil
448 }
449
450 _, err = l.Submit(telegramSource, text)
451 if err != nil {
452 return fmt.Errorf("submit Telegram image: %w", err)
453 }
454
455 err = l.Flush(context.WithoutCancel(ctx))
456 if err != nil {
457 return fmt.Errorf("flush Telegram image: %w", err)
458 }
459 case message.Text == "":
460 b.notice(telegramTextOnly)
461 case strings.TrimSpace(message.Text) == stopCommand:
462 if !l.Interrupt() {
463 b.notice("No turn runs.")
464 }
465 case stop:
466 found, err := l.StopSubagent(name)
467 if err != nil {
468 return fmt.Errorf("stop subagent %s: %w", name, err)
469 }
470
471 if !found {
472 b.notice("No subagent " + name + " runs.")
473 }
474 case isWhatsApp:
475 whatsapp.Command(args, b.notice)
476 default:
477 _, err := l.Submit(telegramSource, message.Text)
478 if err != nil {
479 return fmt.Errorf("submit Telegram text: %w", err)
480 }
481
482 err = l.Flush(context.WithoutCancel(ctx))
483 if err != nil {
484 return fmt.Errorf("flush the message from Telegram: %w", err)
485 }
486 }
487
488 return nil
489 }
490
491 // deliver sends part to the chat. When Telegram refuses a part with entities
492 // (HTTP 400), it sends the text of the part again once with no entities, so a
493 // reply is not lost to a bad entity. It tries again after each failure that a
494 // later try can fix, until ctx ends. It drops the part after any other failure.
495 func (b *TelegramBot) deliver(ctx context.Context, part TelegramPart) {
496 outage := b.sendOutage()
497 wait := telegramRetryDelay
498 for {
499 err := b.api.SendMessage(ctx, b.owner, part.text, part.entities)
500 retry, asked := telegramRetryable(err)
501 if !retry {
502 outage.End()
503 }
504
505 if err == nil || ctx.Err() != nil {
506 return
507 }
508
509 var answer *TelegramError
510
511 if len(part.entities) > 0 && errors.As(err, &answer) && answer.Status == http.StatusBadRequest {
512 b.logger.Warn("reply sent as plain text", "err", err)
513
514 part.entities = nil
515
516 continue
517 }
518
519 if !retry {
520 b.logger.Error("reply lost", "err", err)
521
522 return
523 }
524
525 wait = b.pause(ctx, outage, err, wait, asked)
526 }
527 }
528
529 // deliverRich sends markdown, escaped with telegramEscapeTags, to the chat as a
530 // rich message. When the Bot API refuses it with HTTP 400 or 404, the message
531 // was not sent, so deliverRich sends the text of the part once with message
532 // entities, and the reply is not lost and does not repeat. Message entities
533 // read no HTML, so the fallback gets the Markdown of telegramUnescapeTags, as
534 // the model wrote it. It tries again after each failure that a later try can
535 // fix, until ctx ends. It drops the message after any other failure.
536 func (b *TelegramBot) deliverRich(ctx context.Context, markdown string) {
537 outage := b.sendOutage()
538 wait := telegramRetryDelay
539 for {
540 err := b.api.SendRichMessage(ctx, b.owner, markdown)
541 retry, asked := telegramRetryable(err)
542 if !retry {
543 outage.End()
544 }
545
546 if err == nil || ctx.Err() != nil {
547 return
548 }
549
550 if telegramRejected(err) {
551 b.logger.Warn("reply sent with simple formatting", "err", err)
552
553 for _, part := range telegramReplyParts(telegramUnescapeTags(markdown)) {
554 b.deliver(ctx, part)
555 }
556
557 return
558 }
559
560 if !retry {
561 b.logger.Error("reply lost", "err", err)
562
563 return
564 }
565
566 wait = b.pause(ctx, outage, err, wait, asked)
567 }
568 }
569
570 // notice queues a short answer of the bot for the chat.
571 func (b *TelegramBot) notice(text string) {
572 b.Notify(Event{
573 Kind: "notice",
574 Text: text,
575 })
576 }
577
578 // sendOutage returns a new outage of the messages to the chat.
579 func (b *TelegramBot) sendOutage() *TelegramOutage {
580 return &TelegramOutage{
581 logger: b.logger,
582 loss: "send failed",
583 back: "send back",
584 }
585 }
586
587 // Notify queues event for the chat. It never blocks, so a slow network does not
588 // stop the agent. External events, WhatsApp batches, and schedule runs do not
589 // go to the chat: the reply of the agent tells the user what needs the user.
590 func (b *TelegramBot) Notify(event Event) {
591 switch event.Kind {
592 case "busy", "idle", "reply", "error", "notice", "subagent":
593 case "work":
594 if event.Source == whatsappSource || event.Source == scheduleSource || event.Source == notifySource {
595 return
596 }
597 case "user":
598 if event.Source == telegramSource {
599 return
600 }
601 default:
602 return
603 }
604
605 b.mutex.Lock()
606
607 b.queue = append(b.queue, event)
608 b.mutex.Unlock()
609
610 select {
611 case b.ready <- struct{}{}:
612 default:
613 }
614 }
615
616 // End logs the end of the outage that runs, with its length.
617 func (o *TelegramOutage) End() {
618 if o.start.IsZero() {
619 return
620 }
621
622 o.logger.Info(o.back, "after", time.Since(o.start))
623
624 o.start = time.Time{}
625 }
626
627 // Fail logs the start of an outage with err and delay, unless an outage runs.
628 func (o *TelegramOutage) Fail(err error, delay time.Duration) {
629 if !o.start.IsZero() {
630 return
631 }
632
633 o.start = time.Now()
634 o.logger.Warn(o.loss, "err", err, "retry", delay)
635 }
636
637 // telegramLoadOffset reads the update offset at path. A missing file gives 0,
638 // which asks for every update that Telegram keeps.
639 func telegramLoadOffset(path string) (int64, error) {
640 data, err := os.ReadFile(path)
641 if errors.Is(err, os.ErrNotExist) {
642 return 0, nil
643 }
644
645 if err != nil {
646 return 0, fmt.Errorf("read update offset: %w", err)
647 }
648
649 var file TelegramOffsetFile
650
651 err = json.Unmarshal(data, &file)
652 if err != nil {
653 return 0, fmt.Errorf("%s: %w", path, err)
654 }
655
656 return file.Offset, nil
657 }
658
659 // telegramSaveOffset replaces the update offset at path.
660 func telegramSaveOffset(path string, offset int64) error {
661 data, err := json.Marshal(TelegramOffsetFile{
662 Offset: offset,
663 })
664 if err != nil {
665 return fmt.Errorf("encode update offset: %w", err)
666 }
667
668 err = writeFileAtomic(path, append(data, '\n'))
669 if err != nil {
670 return fmt.Errorf("write update offset: %w", err)
671 }
672
673 return nil
674 }
675
676 // telegramSplitMessage cuts text into parts of at most limit UTF-16 code units,
677 // the unit of the Telegram message limit. It cuts after the last line break or
678 // space of a part when one is in its second half, and else at the limit. It
679 // never cuts a character.
680 func telegramSplitMessage(text string, limit int) []string {
681 var parts []string
682
683 for {
684 end := telegramPrefixEnd(text, limit)
685 if end == len(text) {
686 return append(parts, text)
687 }
688
689 cut := strings.LastIndexByte(text[:end], '\n') + 1
690 if cut <= end/2 {
691 cut = strings.LastIndexByte(text[:end], ' ') + 1
692 }
693
694 if cut <= end/2 {
695 cut = end
696 }
697
698 parts = append(parts, text[:cut])
699 text = text[cut:]
700 }
701 }
702
703 // telegramText returns the text of event for the chat. A user message of
704 // another client has its source as a mark. A subagent shows only its start and
705 // how its run ended, because the reply of the agent carries the content of the
706 // report.
707 func telegramText(event Event) string {
708 switch event.Kind {
709 case "user":
710 return "[" + event.Source + "] " + event.Text
711 case "subagent":
712 return "[" + event.Text + "] started"
713 case "work":
714 return "[" + event.Source + "] " + event.Status
715 case "error":
716 return "Error: " + event.Text
717 default:
718 return event.Text
719 }
720 }
721
722 // telegramSplitRich cuts the Markdown of a reply into parts of at most limit
723 // bytes. It cuts after a blank line outside a code block, so a table, a code
724 // block, or a paragraph stays whole. When no such cut fits in a part, it cuts
725 // after the last line break outside a code block, then after the last line
726 // break in a code block, which it closes at the end of the part and opens again
727 // in the next part with the same fence line. A line longer than limit is cut at
728 // the limit, or before an & in the four bytes before the limit, so that it does
729 // not cut an entity of telegramEscapeTags. It never cuts a character.
730 func telegramSplitRich(markdown string, limit int) []string {
731 var parts []string
732
733 for len(markdown) > limit {
734 cut, fence, opening := telegramRichCut(markdown, limit)
735 if fence == "" {
736 parts = append(parts, markdown[:cut])
737 markdown = markdown[cut:]
738
739 continue
740 }
741
742 parts = append(parts, markdown[:cut]+fence+"\n")
743 markdown = opening + "\n" + markdown[cut:]
744 }
745
746 return append(parts, markdown)
747 }
748
749 // telegramReplyParts returns the messages of the Markdown of a reply with
750 // message entities. A reply that has no text left after the conversion, such as
751 // an empty code block, goes as its Markdown.
752 func telegramReplyParts(markdown string) []TelegramPart {
753 text, entities := telegramMarkdownEntities(markdown)
754 if strings.TrimSpace(text) == "" {
755 text, entities = markdown, nil
756 }
757
758 return telegramSplitEntities(text, entities, telegramMessageUnits)
759 }
760
761 // telegramPrefixEnd returns the byte length of the longest prefix of text with
762 // at most limit UTF-16 code units.
763 func telegramPrefixEnd(text string, limit int) int {
764 units := 0
765 for index, char := range text {
766 size := 1
767 if char > 0xFFFF {
768 size = 2
769 }
770
771 if units+size > limit {
772 return index
773 }
774
775 units += size
776 }
777
778 return len(text)
779 }
780
781 // telegramRichCut returns where telegramSplitRich cuts markdown, which is
782 // longer than limit. For a cut in a code block, it also returns the fence that
783 // closes the block and the line that opens it. Each part before a cut holds
784 // text other than space, and a cut in a code block leaves room for the fence,
785 // so each cut makes the rest shorter.
786 func telegramRichCut(markdown string, limit int) (int, string, string) {
787 blank, line, code := 0, 0, 0
788
789 var fence, opening, inFence, inOpening string
790
791 for start := 0; start < limit; {
792 end := strings.IndexByte(markdown[start:], '\n')
793 if end < 0 || start+end+1 > limit {
794 break
795 }
796
797 end += start + 1
798 text := strings.TrimRight(markdown[start:end], "\r\n")
799 _, opens := markdownCodeFence(text)
800 switch {
801 case fence != "":
802 if markdownClosesFence(text, fence) {
803 fence = ""
804 } else if end > len(opening)+1 && end+len(fence)+1 <= limit {
805 code, inFence, inOpening = end, fence, opening
806 }
807 case opens != "":
808 fence, opening = opens, text
809 case strings.TrimSpace(markdown[:end]) == "":
810 case strings.TrimSpace(text) == "":
811 blank = end
812 }
813
814 if fence == "" && strings.TrimSpace(markdown[:end]) != "" {
815 line = end
816 }
817
818 start = end
819 }
820
821 switch {
822 case blank > 0:
823 return blank, "", ""
824 case line > 0:
825 return line, "", ""
826 case code > 0:
827 return code, inFence, inOpening
828 }
829
830 cut := limit
831 for !utf8.RuneStart(markdown[cut]) {
832 cut--
833 }
834
835 start := max(cut-len("&amp;")+1, 0)
836 entity := strings.LastIndexByte(markdown[start:cut], '&')
837 if entity >= 0 && start+entity > 0 {
838 cut = start + entity
839 }
840
841 return cut, "", ""
842 }
843
844 // telegramSplitEntities cuts text into parts with telegramSplitMessage, and
845 // gives each part the entities in it, with offsets from the start of the part.
846 // An entity that crosses a cut becomes one entity in each part.
847 func telegramSplitEntities(text string, entities []TelegramEntity, limit int) []TelegramPart {
848 var parts []TelegramPart
849
850 start := 0
851 for _, part := range telegramSplitMessage(text, limit) {
852 end := start + telegramUTF16Units(part)
853
854 var kept []TelegramEntity
855
856 for _, entity := range entities {
857 from := max(entity.Offset, start)
858 to := min(entity.Offset+entity.Length, end)
859 if to <= from {
860 continue
861 }
862
863 entity.Offset = from - start
864 entity.Length = to - from
865 kept = append(kept, entity)
866 }
867
868 parts = append(parts, TelegramPart{
869 text: part,
870 entities: kept,
871 })
872
873 start = end
874 }
875
876 return parts
877 }