@@ -7,15 +7,18 @@ import (
77 "context"
88 "errors"
99 "fmt"
10+ "math/rand"
1011
1112 "github.com/celestix/gotgproto"
13+ "github.com/celestix/gotgproto/ext"
14+ "github.com/celestix/gotgproto/storage"
1215 "github.com/gotd/td/tg"
1316 "go.uber.org/zap"
1417)
1518
1619func GetTGMessage (ctx context.Context , client * gotgproto.Client , messageID int ) (* tg.Message , error ) {
1720 inputMessageID := tg .InputMessageClass (& tg.InputMessageID {ID : messageID })
18- channel , err := GetChannelById (ctx , client )
21+ channel , err := GetLogChannelPeer (ctx , client . API (), client . PeerStorage )
1922 if err != nil {
2023 return nil , err
2124 }
@@ -89,18 +92,56 @@ func FileFromMessage(ctx context.Context, client *gotgproto.Client, messageID in
8992 // TODO: add photo support
9093}
9194
92- func GetChannelById (ctx context.Context , client * gotgproto.Client ) (* tg.InputChannel , error ) {
93- channel := & tg.InputChannel {}
95+ func GetLogChannelPeer (ctx context.Context , api * tg.Client , peerStorage * storage.PeerStorage ) (* tg.InputChannel , error ) {
96+ cachedInputPeer := peerStorage .GetInputPeerById (config .ValueOf .LogChannelID )
97+
98+ switch peer := cachedInputPeer .(type ) {
99+ case * tg.InputPeerEmpty :
100+ break
101+ case * tg.InputPeerChannel :
102+ return & tg.InputChannel {
103+ ChannelID : peer .ChannelID ,
104+ AccessHash : peer .AccessHash ,
105+ }, nil
106+ default :
107+ return nil , errors .New ("unexpected type of input peer" )
108+ }
94109 inputChannel := & tg.InputChannel {
95110 ChannelID : config .ValueOf .LogChannelID ,
96111 }
97- channels , err := client . API () .ChannelsGetChannels (ctx , []tg.InputChannelClass {inputChannel })
112+ channels , err := api .ChannelsGetChannels (ctx , []tg.InputChannelClass {inputChannel })
98113 if err != nil {
99114 return nil , err
100115 }
101116 if len (channels .GetChats ()) == 0 {
102117 return nil , errors .New ("no channels found" )
103118 }
104- channel = channels .GetChats ()[0 ].(* tg.Channel ).AsInput ()
105- return channel , nil
119+ channel , ok := channels .GetChats ()[0 ].(* tg.Channel )
120+ if ! ok {
121+ return nil , errors .New ("type assertion to *tg.Channel failed" )
122+ }
123+ // Bruh, I literally have to call library internal functions at this point
124+ peerStorage .AddPeer (channel .GetID (), channel .AccessHash , storage .TypeChannel , "" )
125+ return channel .AsInput (), nil
126+ }
127+
128+ func ForwardMessages (ctx * ext.Context , fromChatId , toChatId int64 , messageID int ) (* tg.Updates , error ) {
129+ fromPeer := ctx .PeerStorage .GetInputPeerById (fromChatId )
130+ if fromPeer .Zero () {
131+ return nil , fmt .Errorf ("fromChatId: %d is not a valid peer" , fromChatId )
132+ }
133+ toPeer , err := GetLogChannelPeer (ctx , ctx .Raw , ctx .PeerStorage )
134+ if err != nil {
135+ return nil , err
136+ }
137+ update , err := ctx .Raw .MessagesForwardMessages (ctx , & tg.MessagesForwardMessagesRequest {
138+ RandomID : []int64 {rand .Int63 ()},
139+ FromPeer : fromPeer ,
140+ ID : []int {messageID },
141+ ToPeer : & tg.InputPeerChannel {ChannelID : toPeer .ChannelID , AccessHash : toPeer .AccessHash },
142+ })
143+ if err != nil {
144+ return nil , err
145+ }
146+ return update .(* tg.Updates ), nil
106147}
0 commit comments