fix formatting for telegram reply notifications

This commit is contained in:
Dmitry Verkhoturov
2021-12-20 00:07:15 -06:00
committed by Umputun
parent 11c8bf6228
commit 4f672cdec5
2 changed files with 4 additions and 4 deletions
+3 -3
View File
@@ -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\"<i>%s</i>\"", 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
+1 -1
View File
@@ -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))
}