From 0cb0232fa142127460bcb177b171a406a66276c9 Mon Sep 17 00:00:00 2001 From: Umputun Date: Sun, 3 Jan 2021 03:09:21 -0600 Subject: [PATCH] escape [ ] ( ) from tg title #839 --- backend/app/notify/telegram.go | 12 +++++++++++- backend/app/notify/telegram_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/backend/app/notify/telegram.go b/backend/app/notify/telegram.go index 0912f7c8..ac184a8b 100644 --- a/backend/app/notify/telegram.go +++ b/backend/app/notify/telegram.go @@ -8,6 +8,7 @@ import ( "html" "net/http" "strconv" + "strings" "time" log "github.com/go-pkgz/lgr" @@ -95,7 +96,7 @@ func (t *Telegram) Send(ctx context.Context, req Request) error { from = "*" + from + "*" link := fmt.Sprintf("↦ [original comment](%s)", req.Comment.Locator.URL+uiNav+req.Comment.ID) if req.Comment.PostTitle != "" { - link = fmt.Sprintf("↦ [%s](%s)", req.Comment.PostTitle, req.Comment.Locator.URL+uiNav+req.Comment.ID) + link = fmt.Sprintf("↦ [%s](%s)", t.escapeTitle(req.Comment.PostTitle), req.Comment.Locator.URL+uiNav+req.Comment.ID) } u := fmt.Sprintf("%s%s/sendMessage?chat_id=%s&parse_mode=Markdown&disable_web_page_preview=true", t.apiPrefix, t.token, t.channelID) @@ -142,6 +143,15 @@ func (t *Telegram) Send(ctx context.Context, req Request) error { return nil } +func (t *Telegram) escapeTitle(title string) string { + escSymbols := []string{"[", "]", "(", ")"} + res := title + for _, esc := range escSymbols { + res = strings.Replace(res, esc, "\\"+esc, -1) + } + return res +} + // SendVerification is not implemented for telegram func (t *Telegram) SendVerification(_ context.Context, _ VerificationRequest) error { return nil diff --git a/backend/app/notify/telegram_test.go b/backend/app/notify/telegram_test.go index 9f86e380..8bfa370f 100644 --- a/backend/app/notify/telegram_test.go +++ b/backend/app/notify/telegram_test.go @@ -4,6 +4,7 @@ import ( "context" "net/http" "net/http/httptest" + "strconv" "testing" "time" @@ -71,6 +72,12 @@ func TestTelegram_Send(t *testing.T) { err = tb.Send(context.TODO(), Request{Comment: c, parent: cp}) assert.NoError(t, err) + err = tb.Send(context.TODO(), Request{Comment: c, parent: cp}) + assert.NoError(t, err) + c.PostTitle = "[test title]" + err = tb.Send(context.TODO(), Request{Comment: c, parent: cp}) + assert.NoError(t, err) + tb, err = NewTelegram("non-json-resp", "remark_test", 2*time.Second, ts.URL+"/") assert.Error(t, err, "should failed") err = tb.Send(context.TODO(), Request{Comment: c, parent: cp}) @@ -134,3 +141,25 @@ func mockTelegramServer() *httptest.Server { return httptest.NewServer(router) } + +func TestTelegram_escapeTitle(t *testing.T) { + + tbl := []struct { + inp string + out string + }{ + {"", ""}, + {"something 123", "something 123"}, + {"something [123]", "something \\[123\\]"}, + {"something (123)", "something \\(123\\)"}, + {"something (123) [aaa]", "something \\(123\\) \\[aaa\\]"}, + } + + tb := Telegram{} + for i, tt := range tbl { + t.Run(strconv.Itoa(i), func(t *testing.T) { + assert.Equal(t, tt.out, tb.escapeTitle(tt.inp)) + }) + } + +}