diff --git a/pkg/channels/telegram/telegram.go b/pkg/channels/telegram/telegram.go
index 7feb706aa..69bae64bd 100644
--- a/pkg/channels/telegram/telegram.go
+++ b/pkg/channels/telegram/telegram.go
@@ -36,6 +36,7 @@ var (
reListItem = regexp.MustCompile(`^[-*]\s+`)
reCodeBlock = regexp.MustCompile("```[\\w]*\\n?([\\s\\S]*?)```")
reInlineCode = regexp.MustCompile("`([^`]+)`")
+ reThinkBlock = regexp.MustCompile(`(?s)
HTML + text = convertBlockquotes(text) text = escapeHTML(text) @@ -659,6 +665,75 @@ func markdownToTelegramHTML(text string) string { ) } + // Restore think blocks as+ for i, content := range thinkBlocks.contents { + escaped := escapeHTML(strings.TrimSpace(content)) + text = strings.ReplaceAll( + text, + fmt.Sprintf("\x00TK%d\x00", i), + fmt.Sprintf("🧠%s", escaped), + ) + } + + return text +} + +// thinkBlockMatch holds extractedblocks. +type thinkBlockMatch struct { + text string + contents []string +} + +// extractThinkBlocks extracts ... blocks and replaces them with placeholders. +func extractThinkBlocks(text string) thinkBlockMatch { + matches := reThinkBlock.FindAllStringSubmatch(text, -1) + + contents := make([]string, 0, len(matches)) + for _, match := range matches { + contents = append(contents, match[1]) + } + + i := 0 + text = reThinkBlock.ReplaceAllStringFunc(text, func(m string) string { + placeholder := fmt.Sprintf("\x00TK%d\x00", i) + i++ + return placeholder + }) + + return thinkBlockMatch{text: text, contents: contents} +} + +// convertBlockquotes groups consecutive `> ...` lines intoblocks. +func convertBlockquotes(text string) string { + lines := strings.Split(text, "\n") + var result []string + inBlockquote := false + + for _, line := range lines { + if reBlockquote.MatchString(line) { + content := reBlockquote.ReplaceAllString(line, "$1") + if !inBlockquote { + result = append(result, "\x00BQ_START\x00"+content) + inBlockquote = true + } else { + result = append(result, content) + } + } else { + if inBlockquote { + result = append(result, "\x00BQ_END\x00") + inBlockquote = false + } + result = append(result, line) + } + } + if inBlockquote { + result = append(result, "\x00BQ_END\x00") + } + + text = strings.Join(result, "\n") + text = strings.ReplaceAll(text, "\x00BQ_START\x00", "") return text }") + text = strings.ReplaceAll(text, "\n\x00BQ_END\x00", "") + text = strings.ReplaceAll(text, "\x00BQ_END\x00", "