Skip to content

Commit 5023b8e

Browse files
committed
feat: 尝试搜索图片以使 /file 适用于任何图片消息
1 parent 11c93bd commit 5023b8e

File tree

6 files changed

+73
-44
lines changed

6 files changed

+73
-44
lines changed

bot/handlers_admin.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ func batchPostArtwork(ctx context.Context, bot *telego.Bot, message telego.Messa
517517
bot.EditMessageText(&telego.EditMessageTextParams{
518518
ChatID: message.Chat.ChatID(),
519519
MessageID: callbackMessage.MessageID,
520-
Text: fmt.Sprintf("总数: %d\n起始索引: %d\n间隔时间: %d秒\n已发布: %d\n失败: %d", count, startIndex, sleepTime, i, failed),
520+
Text: fmt.Sprintf("总数: %d\n起始索引: %d\n间隔时间: %d秒\n已处理: %d\n失败: %d", count, startIndex, sleepTime, i, failed),
521521
})
522522
}
523523
}
@@ -549,7 +549,7 @@ func batchPostArtwork(ctx context.Context, bot *telego.Bot, message telego.Messa
549549
time.Sleep(time.Duration(sleepTime) * time.Second)
550550
}
551551
if callbackMessage != nil {
552-
text := fmt.Sprintf("发布完成\n\n总数: %d\n起始索引: %d\n已发布: %d\n失败: %d", count, startIndex, count, failed)
552+
text := fmt.Sprintf("发布完成\n\n总数: %d\n起始索引: %d\n已处理: %d\n失败: %d", count, startIndex, count, failed)
553553
bot.EditMessageText(&telego.EditMessageTextParams{
554554
ChatID: message.Chat.ChatID(),
555555
MessageID: callbackMessage.MessageID,

bot/handlers_common.go

+25-28
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,29 @@ Inline 查询支持同样的参数格式.
8484
func getPictureFile(ctx context.Context, bot *telego.Bot, message telego.Message) {
8585
messageOrigin, ok := telegram.GetMessageOriginChannelArtworkPost(ctx, bot, message)
8686
if !ok {
87-
telegram.ReplyMessage(bot, message, "请使用该命令回复一条频道的图片消息")
88-
return
87+
go telegram.ReplyMessage(bot, message, "少女祈祷中...")
88+
fileBytes, err := telegram.GetMessagePhotoFileBytes(bot, message.ReplyToMessage)
89+
if err != nil {
90+
telegram.ReplyMessage(bot, message, "请回复一条频道的作品图片消息")
91+
return
92+
}
93+
hash, err := common.GetPhash(fileBytes)
94+
if err != nil {
95+
telegram.ReplyMessage(bot, message, "请回复一条频道的作品图片消息")
96+
return
97+
}
98+
pictures, err := service.GetPicturesByHashHammingDistance(ctx, hash, 10)
99+
if err != nil || len(pictures) == 0 {
100+
telegram.ReplyMessage(bot, message, "请回复一条频道的作品图片消息")
101+
return
102+
}
103+
picture := pictures[0]
104+
_, err = telegram.SendPictureFileByMessageID(ctx, bot, message, picture.TelegramInfo.MessageID)
105+
if err != nil {
106+
telegram.ReplyMessage(bot, message, "文件发送失败: "+err.Error())
107+
return
108+
}
89109
}
90-
91110
pictureMessageID := messageOrigin.MessageID
92111
cmd, _, args := telegoutil.ParseCommand(message.Text)
93112
multiple := cmd == "files"
@@ -120,7 +139,7 @@ func getPictureFile(ctx context.Context, bot *telego.Bot, message telego.Message
120139
}
121140
_, err = telegram.SendPictureFileByMessageID(ctx, bot, message, pictureMessageID)
122141
if err != nil {
123-
telegram.ReplyMessage(bot, message, "图片发送失败: "+err.Error())
142+
telegram.ReplyMessage(bot, message, "文件发送失败: "+err.Error())
124143
return
125144
}
126145
}
@@ -336,33 +355,11 @@ func searchPicture(ctx context.Context, bot *telego.Bot, message telego.Message)
336355
telegram.ReplyMessage(bot, message, "请使用该命令回复一条图片消息")
337356
return
338357
}
339-
if message.ReplyToMessage.Photo == nil && message.ReplyToMessage.Document == nil {
340-
telegram.ReplyMessage(bot, message, "目标消息不包含图片")
341-
return
342-
}
343358
go telegram.ReplyMessage(bot, message, "少女祈祷中...")
344-
photoFileID := ""
345-
if message.ReplyToMessage.Photo != nil {
346-
photoFileID = message.ReplyToMessage.Photo[len(message.ReplyToMessage.Photo)-1].FileID
347-
}
348-
if message.ReplyToMessage.Document != nil && strings.HasPrefix(message.ReplyToMessage.Document.MimeType, "image/") {
349-
if message.ReplyToMessage.Document.FileSize > 20*1024*1024 {
350-
telegram.ReplyMessage(bot, message, "文件过大")
351-
return
352-
}
353-
photoFileID = message.ReplyToMessage.Document.FileID
354-
}
355359

356-
tgFile, err := bot.GetFile(&telego.GetFileParams{
357-
FileID: photoFileID,
358-
})
359-
if err != nil {
360-
telegram.ReplyMessage(bot, message, "获取文件信息失败: "+err.Error())
361-
return
362-
}
363-
fileBytes, err := telegoutil.DownloadFile(bot.FileDownloadURL(tgFile.FilePath))
360+
fileBytes, err := telegram.GetMessagePhotoFileBytes(bot, message.ReplyToMessage)
364361
if err != nil {
365-
telegram.ReplyMessage(bot, message, "下载文件失败: "+err.Error())
362+
telegram.ReplyMessage(bot, message, "获取图片文件失败: "+err.Error())
366363
return
367364
}
368365
hash, err := common.GetPhash(fileBytes)

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.7.13"
10+
Version string = "0.7.14"
1111
)
1212

1313
var VersionCmd = &cobra.Command{

common/strings.go

+7
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ func EscapeHTML(text string) string {
3737
).Replace(text)
3838
}
3939

40+
func ReplaceChars(input string, oldChars []string, newChar string) string {
41+
for _, char := range oldChars {
42+
input = strings.ReplaceAll(input, char, newChar)
43+
}
44+
return input
45+
}
46+
4047
// 解析字符串为二维数组, 如果以字符串以引号包裹, 则无视分隔符
4148
//
4249
// ParseStringTo2DArray("1,2,3;4,5,6", ",", ";") => [][]string{{"1", "2", "3"}, {"4", "5", "6"}}

telegram/errors.go

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package telegram
2+
3+
import (
4+
"errors"
5+
)
6+
7+
var (
8+
ErrNoPhotoInMessage = errors.New("message has no photo")
9+
ErrFileTooLarge = errors.New("file too large (>20MB)")
10+
)

telegram/utils.go

+28-13
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,6 @@ import (
1717
"github.com/mymmrac/telego/telegoutil"
1818
)
1919

20-
func ReplaceChars(input string, oldChars []string, newChar string) string {
21-
for _, char := range oldChars {
22-
input = strings.ReplaceAll(input, char, newChar)
23-
}
24-
return input
25-
}
26-
2720
func GetMessageIDs(messages []telego.Message) []int {
2821
ids := make([]int, len(messages))
2922
for i, message := range messages {
@@ -110,9 +103,9 @@ func GetArtworkMarkdownCaption(artwork *types.Artwork) string {
110103
}
111104
tags := ""
112105
for _, tag := range artwork.Tags {
113-
tag = ReplaceChars(tag, []string{":", ":", "-", "(", ")", "「", "」", "*"}, "_")
114-
tag = ReplaceChars(tag, []string{"?"}, "")
115-
tag = ReplaceChars(tag, []string{"/"}, " "+"#")
106+
tag = common.ReplaceChars(tag, []string{":", ":", "-", "(", ")", "「", "」", "*"}, "_")
107+
tag = common.ReplaceChars(tag, []string{"?"}, "")
108+
tag = common.ReplaceChars(tag, []string{"/"}, " "+"#")
116109
tags += "\\#" + strings.Join(strings.Split(common.EscapeMarkdown(tag), " "), "") + " "
117110
}
118111
caption += "\n\n" + tags
@@ -138,9 +131,9 @@ func GetArtworkHTMLCaption(artwork *types.Artwork) string {
138131
}
139132
tags := ""
140133
for _, tag := range artwork.Tags {
141-
tag = ReplaceChars(tag, []string{":", ":", "-", "(", ")", "「", "」", "*"}, "_")
142-
tag = ReplaceChars(tag, []string{"?"}, "")
143-
tag = ReplaceChars(tag, []string{"/"}, " "+"#")
134+
tag = common.ReplaceChars(tag, []string{":", ":", "-", "(", ")", "「", "」", "*"}, "_")
135+
tag = common.ReplaceChars(tag, []string{"?"}, "")
136+
tag = common.ReplaceChars(tag, []string{"/"}, " "+"#")
144137
tags += "#" + strings.Join(strings.Split(common.EscapeHTML(tag), " "), "") + " "
145138
}
146139
caption += "\n\n" + tags
@@ -181,3 +174,25 @@ func GetPostedPictureInlineKeyboardButton(picture *types.Picture) []telego.Inlin
181174
telegoutil.InlineKeyboardButton("原图").WithURL(GetDeepLinkForFile(picture.TelegramInfo.MessageID)),
182175
}
183176
}
177+
178+
func GetMessagePhotoFileBytes(bot *telego.Bot, message *telego.Message) ([]byte, error) {
179+
fileID := ""
180+
if message.Photo != nil {
181+
fileID = message.Photo[len(message.Photo)-1].FileID
182+
}
183+
if message.Document != nil && strings.HasPrefix(message.Document.MimeType, "image/") {
184+
if message.Document.FileSize > 20*1024*1024 {
185+
return nil, ErrFileTooLarge
186+
}
187+
}
188+
if fileID == "" {
189+
return nil, ErrNoPhotoInMessage
190+
}
191+
tgFile, err := bot.GetFile(
192+
&telego.GetFileParams{FileID: fileID},
193+
)
194+
if err != nil {
195+
return nil, err
196+
}
197+
return telegoutil.DownloadFile(bot.FileDownloadURL(tgFile.FilePath))
198+
}

0 commit comments

Comments
 (0)