Skip to content

Commit 7640832

Browse files
committed
feat: calculate picture info
1 parent 1f0790b commit 7640832

File tree

5 files changed

+61
-1
lines changed

5 files changed

+61
-1
lines changed

bot/bot.go

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ func RunPolling() {
4545
baseGroup.HandleMessageCtx(randomPicture, telegohandler.Or(telegohandler.CommandEqual("setu"), telegohandler.CommandEqual("random")), mentionIsBot)
4646
baseGroup.HandleMessageCtx(help, telegohandler.CommandEqual("help"), mentionIsBot)
4747
baseGroup.HandleMessageCtx(searchPicture, telegohandler.CommandEqual("search"), mentionIsBot)
48+
baseGroup.HandleMessageCtx(calculatePicture, telegohandler.CommandEqual("info"), mentionIsBot)
49+
4850
baseGroup.HandleMessageCtx(setAdmin, telegohandler.CommandEqual("set_admin"))
4951
baseGroup.HandleMessageCtx(deletePicture, telegohandler.Or(telegohandler.CommandEqual("del"), telegohandler.CommandEqual("delete")))
5052
baseGroup.HandleMessageCtx(fetchArtwork, telegohandler.CommandEqual("fetch"))

bot/handlers_common.go

+47
Original file line numberDiff line numberDiff line change
@@ -426,3 +426,50 @@ func inlineQuery(ctx context.Context, bot *telego.Bot, query telego.InlineQuery)
426426
Logger.Errorf("回复查询失败: %s", err)
427427
}
428428
}
429+
430+
func calculatePicture(ctx context.Context, bot *telego.Bot, message telego.Message) {
431+
if message.ReplyToMessage == nil {
432+
telegram.ReplyMessage(bot, message, "请使用该命令回复一条图片消息")
433+
return
434+
}
435+
var waitMessageID int
436+
msg, err := telegram.ReplyMessage(bot, message, "少女做高数中...(((φ(◎ロ◎;)φ)))")
437+
if err == nil {
438+
waitMessageID = msg.MessageID
439+
}
440+
fileBytes, err := telegram.GetMessagePhotoFileBytes(bot, message.ReplyToMessage)
441+
if err != nil {
442+
telegram.ReplyMessage(bot, message, "获取图片文件失败: "+err.Error())
443+
return
444+
}
445+
hash, err := common.GetPhash(fileBytes)
446+
if err != nil {
447+
telegram.ReplyMessage(bot, message, "计算图片信息失败: "+err.Error())
448+
return
449+
}
450+
blurScore, err := common.GetBlurScore(fileBytes)
451+
if err != nil {
452+
telegram.ReplyMessage(bot, message, "计算图片信息失败: "+err.Error())
453+
return
454+
}
455+
width, height, err := common.GetImageSize(fileBytes)
456+
if err != nil {
457+
telegram.ReplyMessage(bot, message, "计算图片信息失败: "+err.Error())
458+
return
459+
}
460+
text := fmt.Sprintf(
461+
"<b>Hash</b>: <code>%s</code>\n<b>模糊度</b>: %.2f\n<b>尺寸</b>: %d x %d",
462+
hash, blurScore, width, height,
463+
)
464+
if waitMessageID == 0 {
465+
telegram.ReplyMessageWithHTML(bot, message, text)
466+
return
467+
}
468+
bot.EditMessageText(&telego.EditMessageTextParams{
469+
ChatID: message.Chat.ChatID(),
470+
MessageID: waitMessageID,
471+
Text: text,
472+
ParseMode: telego.ModeHTML,
473+
})
474+
475+
}

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

1313
var VersionCmd = &cobra.Command{

telegram/telegram.go

+4
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ var (
4545
Command: "search",
4646
Description: "搜索图片",
4747
},
48+
{
49+
Command: "info",
50+
Description: "计算图片信息",
51+
},
4852
{
4953
Command: "help",
5054
Description: "食用指南",

telegram/utils.go

+7
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,13 @@ func ReplyMessageWithMarkdown(bot *telego.Bot, message telego.Message, text stri
156156
}).WithParseMode(telego.ModeMarkdownV2))
157157
}
158158

159+
func ReplyMessageWithHTML(bot *telego.Bot, message telego.Message, text string) (*telego.Message, error) {
160+
return bot.SendMessage(telegoutil.Message(message.Chat.ChatID(), text).
161+
WithReplyParameters(&telego.ReplyParameters{
162+
MessageID: message.MessageID,
163+
}).WithParseMode(telego.ModeHTML))
164+
}
165+
159166
func GetArtworkPostMessageURL(messageID int) string {
160167
return fmt.Sprintf("https://t.me/%s/%d", strings.ReplaceAll(ChannelChatID.String(), "@", ""), messageID)
161168
}

0 commit comments

Comments
 (0)