@@ -22,6 +22,7 @@ import (
22
22
"github.com/google/uuid"
23
23
"github.com/mymmrac/telego"
24
24
"github.com/mymmrac/telego/telegoutil"
25
+ "go.mongodb.org/mongo-driver/bson/primitive"
25
26
"go.mongodb.org/mongo-driver/mongo"
26
27
)
27
28
@@ -93,7 +94,7 @@ func getPictureFile(ctx context.Context, bot *telego.Bot, message telego.Message
93
94
telegram .ReplyMessage (bot , message , "请回复一条频道的图片消息" )
94
95
return
95
96
}
96
- hash , err := common .GetPhash (fileBytes )
97
+ hash , err := common .GetImagePhash (fileBytes )
97
98
if err != nil {
98
99
telegram .ReplyMessage (bot , message , "请回复一条频道的图片消息" )
99
100
return
@@ -301,10 +302,10 @@ func getArtworkInfo(ctx context.Context, bot *telego.Bot, message telego.Message
301
302
}
302
303
}
303
304
304
- text := telegram .GetArtworkHTMLCaption (artwork ) + fmt .Sprintf ("\n 该作品共有 %d张图片" , len (artwork .Pictures ))
305
+ text := telegram .GetArtworkHTMLCaption (artwork ) + fmt .Sprintf ("\n <i>该作品共有 %d张图片</i> " , len (artwork .Pictures ))
305
306
deleteModel , _ := service .GetDeletedByURL (ctx , sourceURL )
306
307
if deleteModel != nil && hasPermission {
307
- text += fmt .Sprintf ("\n \n 这是一个在 %s 删除的作品\n 如果发布则会取消删除" , deleteModel .DeletedAt .Time ().Format ("2006-01-02 15:04:05" ))
308
+ text += fmt .Sprintf ("\n <i>这是一个在 %s 删除的作品\n 如果发布则会取消删除</i> " , common . EscapeHTML ( deleteModel .DeletedAt .Time ().Format ("2006-01-02 15:04:05" ) ))
308
309
}
309
310
var replyMarkup telego.ReplyMarkup
310
311
if isAlreadyPosted {
@@ -320,21 +321,68 @@ func getArtworkInfo(ctx context.Context, bot *telego.Bot, message telego.Message
320
321
telegoutil .InlineKeyboardButton ("发布" ).WithCallbackData ("post_artwork " + cbId ),
321
322
telegoutil .InlineKeyboardButton ("设为R18并发布" ).WithCallbackData ("post_artwork_r18 " + cbId ),
322
323
},
324
+ []telego.InlineKeyboardButton {
325
+ telegoutil .InlineKeyboardButton ("删除这张" ).WithCallbackData ("artwork_preview " + cbId + " delete" ),
326
+ telegoutil .InlineKeyboardButton ("下一张" ).WithCallbackData ("artwork_preview " + cbId + " 1" ),
327
+ },
323
328
)
324
329
}
325
- _ , err = bot .SendMessage (telegoutil .Message (message .Chat .ChatID (), text ).
326
- WithReplyMarkup (replyMarkup ).
330
+
331
+ var inputFile telego.InputFile
332
+ needUpdatePreview := false
333
+ if artwork .Pictures [0 ].TelegramInfo != nil && artwork .Pictures [0 ].TelegramInfo .PhotoFileID != "" {
334
+ inputFile = telegoutil .FileFromID (artwork .Pictures [0 ].TelegramInfo .PhotoFileID )
335
+ } else {
336
+ if fileBytes := common .GetReqCachedFile (artwork .Pictures [0 ].Original ); fileBytes != nil {
337
+ fileBytes , err = common .CompressImageWithCache (fileBytes , 10 , 2560 , artwork .Pictures [0 ].Original )
338
+ if err != nil {
339
+ telegram .ReplyMessage (bot , message , "压缩图片失败: " + err .Error ())
340
+ return
341
+ }
342
+ inputFile = telegoutil .File (telegoutil .NameReader (bytes .NewReader (fileBytes ), artwork .Title ))
343
+ } else {
344
+ needUpdatePreview = true
345
+ if botPhotoFileId := service .GetEtcData (ctx , "bot_photo_file_id" ); botPhotoFileId != nil {
346
+ inputFile = telegoutil .FileFromID (botPhotoFileId .(string ))
347
+ } else if botPhotoFileBytes := service .GetEtcData (ctx , "bot_photo_bytes" ); botPhotoFileBytes != nil {
348
+ if data , ok := botPhotoFileBytes .(primitive.Binary ); ok {
349
+ inputFile = telegoutil .File (telegoutil .NameReader (bytes .NewReader (data .Data ), artwork .Title ))
350
+ } else {
351
+ inputFile = telegoutil .FileFromURL (artwork .Pictures [0 ].Thumbnail )
352
+ }
353
+ } else {
354
+ inputFile = telegoutil .FileFromURL (artwork .Pictures [0 ].Thumbnail )
355
+ }
356
+ }
357
+ }
358
+
359
+ photo := telegoutil .Photo (message .Chat .ChatID (), inputFile ).
327
360
WithReplyParameters (& telego.ReplyParameters {MessageID : message .MessageID }).
328
- WithParseMode (telego .ModeHTML ).
329
- WithLinkPreviewOptions (
330
- & telego.LinkPreviewOptions {
331
- URL : artwork .Pictures [0 ].Original ,
332
- ShowAboveText : true ,
333
- },
334
- ))
361
+ WithCaption (text ).
362
+ WithReplyMarkup (replyMarkup ).
363
+ WithParseMode (telego .ModeHTML )
364
+
365
+ if artwork .R18 && ! needUpdatePreview {
366
+ photo .WithHasSpoiler ()
367
+ }
368
+ msg , err := bot .SendPhoto (photo )
335
369
if err != nil {
336
- Logger .Errorf ("发送消息失败: %s" , err )
370
+ telegram .ReplyMessage (bot , message , "发送图片失败: " + err .Error ())
371
+ return
337
372
}
373
+ if needUpdatePreview {
374
+ if err := UpdateLinkPreview (ctx , msg , artwork , bot , 0 , photo ); err != nil {
375
+ Logger .Errorf ("更新预览失败: %s" , err )
376
+ bot .EditMessageCaption (& telego.EditMessageCaptionParams {
377
+ ChatID : message .Chat .ChatID (),
378
+ MessageID : msg .MessageID ,
379
+ Caption : text + "\n <i>更新预览失败</i>" ,
380
+ ParseMode : telego .ModeHTML ,
381
+ ReplyMarkup : msg .ReplyMarkup ,
382
+ })
383
+ }
384
+ }
385
+
338
386
}
339
387
340
388
func searchPicture (ctx context.Context , bot * telego.Bot , message telego.Message ) {
@@ -350,7 +398,7 @@ func searchPicture(ctx context.Context, bot *telego.Bot, message telego.Message)
350
398
telegram .ReplyMessage (bot , message , "获取图片文件失败: " + err .Error ())
351
399
return
352
400
}
353
- hash , err := common .GetPhash (fileBytes )
401
+ hash , err := common .GetImagePhash (fileBytes )
354
402
if err != nil {
355
403
telegram .ReplyMessage (bot , message , "获取图片哈希失败: " + err .Error ())
356
404
return
@@ -428,12 +476,12 @@ func calculatePicture(ctx context.Context, bot *telego.Bot, message telego.Messa
428
476
telegram .ReplyMessage (bot , message , "获取图片文件失败: " + err .Error ())
429
477
return
430
478
}
431
- hash , err := common .GetPhash (fileBytes )
479
+ hash , err := common .GetImagePhash (fileBytes )
432
480
if err != nil {
433
481
telegram .ReplyMessage (bot , message , "计算图片信息失败: " + err .Error ())
434
482
return
435
483
}
436
- blurScore , err := common .GetBlurScore (fileBytes )
484
+ blurScore , err := common .GetImageBlurScore (fileBytes )
437
485
if err != nil {
438
486
telegram .ReplyMessage (bot , message , "计算图片信息失败: " + err .Error ())
439
487
return
@@ -451,10 +499,10 @@ func calculatePicture(ctx context.Context, bot *telego.Bot, message telego.Messa
451
499
telegram .ReplyMessageWithHTML (bot , message , text )
452
500
return
453
501
}
454
- bot .EditMessageText (& telego.EditMessageTextParams {
502
+ bot .EditMessageCaption (& telego.EditMessageCaptionParams {
455
503
ChatID : message .Chat .ChatID (),
456
504
MessageID : waitMessageID ,
457
- Text : text ,
505
+ Caption : text ,
458
506
ParseMode : telego .ModeHTML ,
459
507
})
460
508
0 commit comments