Skip to content

fix: unescape literal \n sequences in Telegram message formatting#377

Merged
ptone merged 2 commits into
GoogleCloudPlatform:mainfrom
ptone:scion/telegram-newlines
Jun 9, 2026
Merged

fix: unescape literal \n sequences in Telegram message formatting#377
ptone merged 2 commits into
GoogleCloudPlatform:mainfrom
ptone:scion/telegram-newlines

Conversation

@ptone

@ptone ptone commented Jun 9, 2026

Copy link
Copy Markdown
Member

Message text arriving at the Telegram formatter can contain literal backslash-n escape sequences instead of actual newlines — either from JSON encoding round-trips (FormatForDelivery) or from shell arguments that don't interpret \n. Apply unescapeNewlines in FormatMessage and FormatMessageV2 so Telegram displays proper line breaks.

Closes #48

Fixes #<issue_number_goes_here>

It's a good idea to open an issue first for discussion.

  • Tests pass
  • Appropriate changes to documentation are included in the PR

Message text arriving at the Telegram formatter can contain literal
backslash-n escape sequences instead of actual newlines — either from
JSON encoding round-trips (FormatForDelivery) or from shell arguments
that don't interpret \n. Apply unescapeNewlines in FormatMessage and
FormatMessageV2 so Telegram displays proper line breaks.

Closes #48
@google-cla

google-cla Bot commented Jun 9, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces literal newline and tab unescaping for Telegram messages by adding an unescapeNewlines helper function and applying it in FormatMessage and FormatMessageV2. It also includes comprehensive unit tests to verify the new behavior. The review feedback suggests optimizing the unescapeNewlines function by defining the strings.Replacer as a package-level variable to avoid redundant allocations on every function call.

Comment on lines +339 to +341
func unescapeNewlines(s string) string {
return strings.NewReplacer(`\n`, "\n", `\t`, "\t").Replace(s)
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Creating a new strings.Replacer on every call to unescapeNewlines is inefficient because it compiles the replacement patterns each time. Declaring the replacer as a package-level variable and reusing it avoids these allocations and improves performance.

Suggested change
func unescapeNewlines(s string) string {
return strings.NewReplacer(`\n`, "\n", `\t`, "\t").Replace(s)
}
var newlineReplacer = strings.NewReplacer("\\n", "\n", "\\t", "\t")
func unescapeNewlines(s string) string {
return newlineReplacer.Replace(s)
}

Avoids re-allocating the replacer on every call to unescapeNewlines.
@ptone ptone merged commit 268638d into GoogleCloudPlatform:main Jun 9, 2026
7 of 8 checks passed
@ptone ptone deleted the scion/telegram-newlines branch June 9, 2026 17:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant