From 4f672cdec563caa1abacbfd73b966495a64cda18 Mon Sep 17 00:00:00 2001 From: Dmitry Verkhoturov Date: Mon, 20 Dec 2021 06:28:15 +0100 Subject: [PATCH] fix formatting for telegram reply notifications --- backend/app/notify/telegram.go | 6 +++--- backend/app/notify/telegram_test.go | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/backend/app/notify/telegram.go b/backend/app/notify/telegram.go index 2d9c1527..54f57877 100644 --- a/backend/app/notify/telegram.go +++ b/backend/app/notify/telegram.go @@ -153,7 +153,7 @@ func buildMessage(req Request) ([]byte, error) { msg += fmt.Sprintf("\n\n%s", telegramSupportedHTML(req.Comment.Text)) if req.Comment.ParentID != "" { - msg += fmt.Sprintf("\n\n \"_%s_\"", telegramSupportedHTML(req.parent.Text)) + msg += fmt.Sprintf("\n\n\"%s\"", telegramSupportedHTML(req.parent.Text)) } if req.Comment.PostTitle != "" { @@ -168,14 +168,14 @@ func buildMessage(req Request) ([]byte, error) { return b, nil } -// returns HTML with only tags allowed in Telegram HTML message payload +// returns HTML with only tags allowed in Telegram HTML message payload, also trims ending newlines // https://core.telegram.org/bots/api#html-style func telegramSupportedHTML(htmlText string) string { p := bluemonday.NewPolicy() p.AllowElements("b", "strong", "i", "em", "u", "ins", "s", "strike", "del", "a", "code", "pre") p.AllowAttrs("href").OnElements("a") p.AllowAttrs("class").OnElements("code") - return p.Sanitize(htmlText) + return strings.TrimRight(p.Sanitize(htmlText), "\n") } // returns text sanitized of symbols not allowed inside other HTML tags in Telegram HTML message payload diff --git a/backend/app/notify/telegram_test.go b/backend/app/notify/telegram_test.go index 5df299e7..c9e149f0 100644 --- a/backend/app/notify/telegram_test.go +++ b/backend/app/notify/telegram_test.go @@ -163,7 +163,7 @@ func TestTelegram_Send(t *testing.T) { assert.NoError(t, err) assert.Equal(t, `{"text":"\u003ca href=\"http://example.org/#remark42__comment-999\"\u003efrom\u003c/a\u003e -\u003e \u003ca href=\"http://example.org/#remark42__comment-\"\u003eto\u003c/a\u003e\n\n`+ `some text\n\n`+ - ` \"_some parent text with a \u003ca href=\"http://example.org\"\u003elink\u003c/a\u003e and special text:\u0026amp; \u0026lt; \u0026gt; \u0026amp;_\"\n\n`+ + `\"\u003ci\u003esome parent text with a \u003ca href=\"http://example.org\"\u003elink\u003c/a\u003e and special text:\u0026amp; \u0026lt; \u0026gt; \u0026amp;\u003c/i\u003e\"\n\n`+ `↦ \u003ca href=\"http://example.org/\"\u003e[test title]\u003c/a\u003e","parse_mode":"HTML"}`, string(res)) }