escape [ ] ( ) from tg title #839
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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))
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user