Skip to content

Commit 4e35979

Browse files
committed
chore: Update common.EscapeMarkdown function to handle more escape characters
1 parent f04ad1f commit 4e35979

File tree

6 files changed

+46
-45
lines changed

6 files changed

+46
-45
lines changed

bot/handlers_admin.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package bot
22

33
import (
4+
"ManyACG/common"
45
"ManyACG/config"
56
"ManyACG/fetcher"
67
"ManyACG/service"
@@ -77,7 +78,7 @@ func setAdmin(ctx context.Context, bot *telego.Bot, message telego.Message) {
7778
}
7879

7980
if len(unsupportedPermissions) > 0 {
80-
telegram.ReplyMessageWithMarkdown(bot, message, telegram.EscapeMarkdown(fmt.Sprintf("权限不存在: %v\n支持的权限:\n", unsupportedPermissions))+supportedPermissionsText)
81+
telegram.ReplyMessageWithMarkdown(bot, message, common.EscapeMarkdown(fmt.Sprintf("权限不存在: %v\n支持的权限:\n", unsupportedPermissions))+supportedPermissionsText)
8182
return
8283
}
8384

bot/handlers_common.go

+7-13
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,7 @@ func randomPicture(ctx context.Context, bot *telego.Bot, message telego.Message)
156156
WithReplyParameters(&telego.ReplyParameters{
157157
MessageID: message.MessageID,
158158
}).WithCaption(artwork[0].Title).WithReplyMarkup(
159-
telegoutil.InlineKeyboard([]telego.InlineKeyboardButton{
160-
telegoutil.InlineKeyboardButton("来源").WithURL(telegram.GetArtworkPostMessageURL(picture.TelegramInfo.MessageID)),
161-
telegoutil.InlineKeyboardButton("原图").WithURL(telegram.GetDeepLinkForFile(picture.TelegramInfo.MessageID)),
162-
}),
159+
telegoutil.InlineKeyboard(telegram.GetPostedPictureInlineKeyboardButton(picture)),
163160
)
164161
if artwork[0].R18 {
165162
photo.WithHasSpoiler()
@@ -237,7 +234,7 @@ func getArtworkInfo(ctx context.Context, bot *telego.Bot, message telego.Message
237234
deletedModel, _ := service.GetDeletedByURL(ctx, sourceURL)
238235
artworkInfoCaption := telegram.GetArtworkMarkdownCaption(artwork)
239236
if deletedModel != nil && hasPermission {
240-
photo.WithCaption(artworkInfoCaption + telegram.EscapeMarkdown(fmt.Sprintf("\n\n这是一个在 %s 删除的作品\n\n"+
237+
photo.WithCaption(artworkInfoCaption + common.EscapeMarkdown(fmt.Sprintf("\n\n这是一个在 %s 删除的作品\n\n"+
241238
"如果发布则会取消删除", deletedModel.DeletedAt.Time().Format("2006-01-02 15:04:05"))))
242239
} else {
243240
photo.WithCaption(telegram.GetArtworkMarkdownCaption(artwork))
@@ -315,13 +312,13 @@ func searchPicture(ctx context.Context, bot *telego.Bot, message telego.Message)
315312
for _, picture := range pictures {
316313
artwork, err := service.GetArtworkByMessageID(ctx, picture.TelegramInfo.MessageID)
317314
if err != nil {
318-
text += telegram.EscapeMarkdown(fmt.Sprintf("%s 模糊度: %.2f\n\n", telegram.GetArtworkPostMessageURL(picture.TelegramInfo.MessageID), picture.BlurScore))
315+
text += common.EscapeMarkdown(fmt.Sprintf("%s 模糊度: %.2f\n\n", telegram.GetArtworkPostMessageURL(picture.TelegramInfo.MessageID), picture.BlurScore))
319316
}
320317
text += fmt.Sprintf("[%s\\_%d](%s) ",
321-
telegram.EscapeMarkdown(artwork.Title),
318+
common.EscapeMarkdown(artwork.Title),
322319
picture.Index+1,
323-
telegram.EscapeMarkdown(telegram.GetArtworkPostMessageURL(picture.TelegramInfo.MessageID)))
324-
text += telegram.EscapeMarkdown(fmt.Sprintf("模糊度: %.2f\n\n", picture.BlurScore))
320+
common.EscapeMarkdown(telegram.GetArtworkPostMessageURL(picture.TelegramInfo.MessageID)))
321+
text += common.EscapeMarkdown(fmt.Sprintf("模糊度: %.2f\n\n", picture.BlurScore))
325322
}
326323
telegram.ReplyMessageWithMarkdown(bot, message, text)
327324
return
@@ -355,10 +352,7 @@ func inlineQuery(ctx context.Context, bot *telego.Bot, query telego.InlineQuery)
355352
continue
356353
}
357354
result := telegoutil.ResultCachedPhoto(uuid.NewString(), picture.TelegramInfo.PhotoFileID).WithCaption(artwork.Title)
358-
result.WithReplyMarkup(telegoutil.InlineKeyboard([]telego.InlineKeyboardButton{
359-
telegoutil.InlineKeyboardButton("详情").WithURL(telegram.GetArtworkPostMessageURL(picture.TelegramInfo.MessageID)),
360-
telegoutil.InlineKeyboardButton("原图").WithURL(telegram.GetDeepLinkForFile(picture.TelegramInfo.MessageID)),
361-
}))
355+
result.WithReplyMarkup(telegoutil.InlineKeyboard(telegram.GetPostedPictureInlineKeyboardButton(picture)))
362356
results = append(results, result)
363357
}
364358
if err := bot.AnswerInlineQuery(telegoutil.InlineQuery(query.ID, results...).WithCacheTime(1)); err != nil {

cmd/version.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
)
88

99
const (
10-
Version string = "0.6.5"
10+
Version string = "0.6.6"
1111
)
1212

1313
var VersionCmd = &cobra.Command{

common/strings.go

+15
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,21 @@ func ReplaceFileNameInvalidChar(fileName string) string {
2222
).Replace(fileName)
2323
}
2424

25+
var (
26+
escapeChars = []string{
27+
"_", "*", "[", "]", "(", ")", "~", "`", ">", "#", "+", "-", "=", "|", "{", "}", ".", "!",
28+
}
29+
)
30+
31+
func EscapeMarkdown(text string) string {
32+
text = strings.ReplaceAll(text, "\\", "\\\\")
33+
for _, char := range escapeChars {
34+
text = strings.ReplaceAll(text, char, "\\"+char)
35+
}
36+
return text
37+
// return regexp.MustCompile(`([_\*[\]\(\)~`+"`"+`>#\+\-=|{}\.!])`).ReplaceAllString(text, `\$1`)
38+
}
39+
2540
// 解析字符串为二维数组, 如果以字符串以引号包裹, 则无视分隔符
2641
//
2742
// ParseStringTo2DArray("1,2,3;4,5,6", ",", ";") => [][]string{{"1", "2", "3"}, {"4", "5", "6"}}

fetcher/service.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package fetcher
22

33
import (
4+
"ManyACG/common"
45
"ManyACG/errors"
56
"ManyACG/service"
67
"ManyACG/storage"
@@ -90,7 +91,7 @@ func afterCreate(ctx context.Context, artwork *types.Artwork, bot *telego.Bot, _
9091

9192
sendNotify := fromID != 0 && bot != nil
9293
artworkID, err := service.GetArtworkIDByPicture(ctx, artwork.Pictures[0])
93-
artworkTitleMarkdown := telegram.EscapeMarkdown(artwork.Title)
94+
artworkTitleMarkdown := common.EscapeMarkdown(artwork.Title)
9495
if err != nil {
9596
Logger.Errorf("error when getting artwork by URL: %s", err)
9697
if sendNotify {
@@ -134,21 +135,21 @@ func afterCreate(ctx context.Context, artwork *types.Artwork, bot *telego.Bot, _
134135

135136
text := fmt.Sprintf("*刚刚发布的作品 [%s](%s) 中第 %d 张图片搜索到有%d个相似图片*\n",
136137
artworkTitleMarkdown,
137-
telegram.EscapeMarkdown(telegram.GetArtworkPostMessageURL(picture.TelegramInfo.MessageID)),
138+
common.EscapeMarkdown(telegram.GetArtworkPostMessageURL(picture.TelegramInfo.MessageID)),
138139
picture.Index+1,
139140
len(similarPictures))
140-
text += telegram.EscapeMarkdown(fmt.Sprintf("该图像模糊度: %.2f\n搜索到的相似图片列表:\n\n", picture.BlurScore))
141+
text += common.EscapeMarkdown(fmt.Sprintf("该图像模糊度: %.2f\n搜索到的相似图片列表:\n\n", picture.BlurScore))
141142
for _, similarPicture := range similarPictures {
142143
artworkOfSimilarPicture, err := service.GetArtworkByMessageID(ctx, similarPicture.TelegramInfo.MessageID)
143144
if err != nil {
144-
text += telegram.EscapeMarkdown(fmt.Sprintf("%s 模糊度: %.2f\n\n", telegram.GetArtworkPostMessageURL(picture.TelegramInfo.MessageID), similarPicture.BlurScore))
145+
text += common.EscapeMarkdown(fmt.Sprintf("%s 模糊度: %.2f\n\n", telegram.GetArtworkPostMessageURL(picture.TelegramInfo.MessageID), similarPicture.BlurScore))
145146
continue
146147
}
147148
text += fmt.Sprintf("[%s\\_%d](%s) ",
148-
telegram.EscapeMarkdown(artworkOfSimilarPicture.Title),
149+
common.EscapeMarkdown(artworkOfSimilarPicture.Title),
149150
similarPicture.Index+1,
150-
telegram.EscapeMarkdown(telegram.GetArtworkPostMessageURL(similarPicture.TelegramInfo.MessageID)))
151-
text += telegram.EscapeMarkdown(fmt.Sprintf("模糊度: %.2f\n\n", similarPicture.BlurScore))
151+
common.EscapeMarkdown(telegram.GetArtworkPostMessageURL(similarPicture.TelegramInfo.MessageID)))
152+
text += common.EscapeMarkdown(fmt.Sprintf("模糊度: %.2f\n\n", similarPicture.BlurScore))
152153
}
153154
text += "_模糊度使用原图文件计算得出, 越小图像质量越好_"
154155
_, err = bot.SendMessage(telegoutil.Messagef(telegoutil.ID(fromID), text).WithParseMode(telego.ModeMarkdownV2))

telegram/utils.go

+13-23
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package telegram
22

33
import (
4+
"ManyACG/common"
45
"ManyACG/service"
56
"ManyACG/sources"
67
"ManyACG/storage"
@@ -16,21 +17,6 @@ import (
1617
"github.com/mymmrac/telego/telegoutil"
1718
)
1819

19-
var (
20-
escapeChars = []string{
21-
"_", "*", "[", "]", "(", ")", "~", "`", ">", "#", "+", "-", "=", "|", "{", "}", ".", "!",
22-
}
23-
)
24-
25-
func EscapeMarkdown(text string) string {
26-
text = strings.ReplaceAll(text, "\\", "\\\\")
27-
for _, char := range escapeChars {
28-
text = strings.ReplaceAll(text, char, "\\"+char)
29-
}
30-
return text
31-
// return regexp.MustCompile(`([_\*[\]\(\)~`+"`"+`>#\+\-=|{}\.!])`).ReplaceAllString(text, `\$1`)
32-
}
33-
3420
func ReplaceChars(input string, oldChars []string, newChar string) string {
3521
for _, char := range oldChars {
3622
input = strings.ReplaceAll(input, char, newChar)
@@ -116,13 +102,13 @@ func GetMessageOriginChannelArtworkPost(ctx context.Context, bot *telego.Bot, me
116102
}
117103

118104
func GetArtworkMarkdownCaption(artwork *types.Artwork) string {
119-
caption := fmt.Sprintf("[*%s*](%s)", EscapeMarkdown(artwork.Title), artwork.SourceURL)
120-
caption += "\n" + "*Author:* " + EscapeMarkdown(artwork.Artist.Name)
105+
caption := fmt.Sprintf("[*%s*](%s)", common.EscapeMarkdown(artwork.Title), artwork.SourceURL)
106+
caption += "\n" + "*Author:* " + common.EscapeMarkdown(artwork.Artist.Name)
121107
if artwork.Description != "" {
122108
desc := artwork.Description
123109
lines := strings.Split(desc, "\n")
124110
for i, line := range lines {
125-
lines[i] = ">" + EscapeMarkdown(line)
111+
lines[i] = ">" + common.EscapeMarkdown(line)
126112
if i == len(lines)-1 {
127113
lines[i] += "**"
128114
}
@@ -139,7 +125,7 @@ func GetArtworkMarkdownCaption(artwork *types.Artwork) string {
139125
tag = ReplaceChars(tag, []string{":", ":", "-", "(", ")", "「", "」", "*"}, "_")
140126
tag = ReplaceChars(tag, []string{"?"}, "")
141127
tag = ReplaceChars(tag, []string{"/"}, " "+"#")
142-
tags += "\\#" + strings.Join(strings.Split(EscapeMarkdown(tag), " "), "") + " "
128+
tags += "\\#" + strings.Join(strings.Split(common.EscapeMarkdown(tag), " "), "") + " "
143129
}
144130
caption += "\n\n" + tags
145131
return caption
@@ -169,9 +155,13 @@ func GetDeepLinkForFile(messageID int) string {
169155

170156
func GetPostedPictureReplyMarkup(picture *types.Picture) telego.ReplyMarkup {
171157
return telegoutil.InlineKeyboard(
172-
[]telego.InlineKeyboardButton{
173-
telegoutil.InlineKeyboardButton("详情").WithURL(GetArtworkPostMessageURL(picture.TelegramInfo.MessageID)),
174-
telegoutil.InlineKeyboardButton("原图").WithURL(GetDeepLinkForFile(picture.TelegramInfo.MessageID)),
175-
},
158+
GetPostedPictureInlineKeyboardButton(picture),
176159
)
177160
}
161+
162+
func GetPostedPictureInlineKeyboardButton(picture *types.Picture) []telego.InlineKeyboardButton {
163+
return []telego.InlineKeyboardButton{
164+
telegoutil.InlineKeyboardButton("详情").WithURL(GetArtworkPostMessageURL(picture.TelegramInfo.MessageID)),
165+
telegoutil.InlineKeyboardButton("原图").WithURL(GetDeepLinkForFile(picture.TelegramInfo.MessageID)),
166+
}
167+
}

0 commit comments

Comments
 (0)