@@ -12,6 +12,7 @@ import (
1212 "github.com/krau/SaveAny-Bot/common"
1313 "github.com/krau/SaveAny-Bot/dao"
1414 "github.com/krau/SaveAny-Bot/types"
15+ "github.com/krau/SaveAny-Bot/userclient"
1516)
1617
1718var (
@@ -50,6 +51,31 @@ func parseLink(ctx *ext.Context, link string) (chatID int64, messageID int, err
5051 return chatID , messageID , nil
5152}
5253
54+ // use passed ctx client to fetch file from message,
55+ //
56+ // if failed try using userclient
57+ func tryFetchFileFromMessage (ctx * ext.Context , chatID int64 , messageID int , fileName string ) (* types.File , bool , error ) {
58+ file , err := FileFromMessage (ctx , chatID , messageID , fileName )
59+ if err == nil {
60+ return file , false , nil
61+ }
62+ if (strings .Contains (err .Error (), "peer not found" ) || strings .Contains (err .Error (), "unexpected message type" )) && userclient .UC != nil {
63+ common .Log .Warnf ("无法获取文件 %d:%d, 尝试使用 userbot: %s" , chatID , messageID , err )
64+ uctx := userclient .GetCtx ()
65+ // TODO: 群组支持
66+ file , err = FileFromMessage (uctx , chatID , messageID , fileName )
67+ if err == nil {
68+ return file , true , nil
69+ }
70+ return nil , true , err
71+ }
72+ return nil , false , err
73+ }
74+
75+ func tryFetchMessage (ctx * ext.Context , chatID int64 , messageID int ) (* tg.Message , error ) {
76+ return GetTGMessage (ctx , chatID , messageID )
77+ }
78+
5379func handleLinkMessage (ctx * ext.Context , update * ext.Update ) error {
5480 common .Log .Trace ("Got link message" )
5581 link := linkRegex .FindString (update .EffectiveMessage .Text )
@@ -70,26 +96,25 @@ func handleLinkMessage(ctx *ext.Context, update *ext.Update) error {
7096 return dispatcher .EndGroups
7197 }
7298
73- // storages := storage.GetUserStorages(user.ChatID)
74- // if len(storages) == 0 {
75- // ctx.Reply(update, ext.ReplyTextString("无可用的存储"), nil)
76- // return dispatcher.EndGroups
77- // }
78-
7999 replied , err := ctx .Reply (update , ext .ReplyTextString ("正在获取文件..." ), nil )
80100 if err != nil {
81101 common .Log .Errorf ("回复失败: %s" , err )
82102 return dispatcher .EndGroups
83103 }
84104
85- file , err := FileFromMessage (ctx , linkChatID , messageID , "" )
105+ file , useUserClient , err := tryFetchFileFromMessage (ctx , linkChatID , messageID , "" )
86106 if err != nil {
87107 common .Log .Errorf ("获取文件失败: %s" , err )
88108 ctx .Reply (update , ext .ReplyTextString ("获取文件失败: " + err .Error ()), nil )
89109 return dispatcher .EndGroups
90110 }
91111 if file .FileName == "" {
92- file .FileName = GenFileNameFromMessage (* update .EffectiveMessage .Message , file )
112+ msg , err := tryFetchMessage (ctx , linkChatID , messageID )
113+ if err != nil {
114+ file .FileName = fmt .Sprintf ("%d_%d" , linkChatID , messageID )
115+ } else {
116+ file .FileName = GenFileNameFromMessage (* msg , file )
117+ }
93118 }
94119
95120 receivedFile := & dao.ReceivedFile {
@@ -99,6 +124,7 @@ func handleLinkMessage(ctx *ext.Context, update *ext.Update) error {
99124 MessageID : messageID ,
100125 ReplyMessageID : replied .ID ,
101126 ReplyChatID : update .GetUserChat ().GetID (),
127+ UseUserClient : useUserClient ,
102128 }
103129 record , err := dao .SaveReceivedFile (receivedFile )
104130 if err != nil {
@@ -116,6 +142,7 @@ func handleLinkMessage(ctx *ext.Context, update *ext.Update) error {
116142 Ctx : ctx ,
117143 Status : types .Pending ,
118144 FileDBID : record .ID ,
145+ UseUserClient : useUserClient ,
119146 File : file ,
120147 StorageName : user .DefaultStorage ,
121148 UserID : user .ChatID ,
0 commit comments