diff --git a/src/channels/telegram.rs b/src/channels/telegram.rs index b1dca1988..1eeb65d4b 100644 --- a/src/channels/telegram.rs +++ b/src/channels/telegram.rs @@ -246,6 +246,23 @@ fn strip_tool_call_tags(message: &str) -> String { super::strip_tool_call_tags(message) } +fn find_matching_close(s: &str) -> Option { + let mut depth = 1usize; + for (i, ch) in s.char_indices() { + match ch { + '[' => depth += 1, + ']' => { + depth -= 1; + if depth == 0 { + return Some(i); + } + } + _ => {} + } + } + None +} + fn parse_attachment_markers(message: &str) -> (String, Vec) { let mut cleaned = String::with_capacity(message.len()); let mut attachments = Vec::new(); @@ -260,12 +277,12 @@ fn parse_attachment_markers(message: &str) -> (String, Vec) let open = cursor + open_rel; cleaned.push_str(&message[cursor..open]); - let Some(close_rel) = message[open..].find(']') else { + let Some(close_rel) = find_matching_close(&message[open + 1..]) else { cleaned.push_str(&message[open..]); break; }; - let close = open + close_rel; + let close = open + 1 + close_rel; let marker = &message[open + 1..close]; let parsed = marker.split_once(':').and_then(|(kind, target)| {