Skip to content

Commit 59085d4

Browse files
fix(bot): peer ID invalid on first run
1 parent dc2c9d8 commit 59085d4

File tree

2 files changed

+51
-25
lines changed

2 files changed

+51
-25
lines changed

internal/commands/stream.go

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,34 +31,20 @@ func sendLink(ctx *ext.Context, u *ext.Update) error {
3131
if peerChatId.Type != int(storage.TypeUser) {
3232
return dispatcher.EndGroups
3333
}
34-
peer := ctx.PeerStorage.GetPeerById(config.ValueOf.LogChannelID)
35-
switch storage.EntityType(peer.Type) {
36-
case storage.TypeChat:
37-
return dispatcher.EndGroups
38-
case storage.TypeUser:
39-
return dispatcher.EndGroups
40-
}
41-
update, err := ctx.ForwardMessages(
42-
chatId,
43-
config.ValueOf.LogChannelID,
44-
&tg.MessagesForwardMessagesRequest{
45-
FromPeer: &tg.InputPeerChat{ChatID: chatId},
46-
ID: []int{u.EffectiveMessage.ID},
47-
ToPeer: &tg.InputPeerChannel{ChannelID: config.ValueOf.LogChannelID, AccessHash: peer.AccessHash},
48-
},
49-
)
34+
35+
update, err := utils.ForwardMessages(ctx, chatId, config.ValueOf.LogChannelID, u.EffectiveMessage.ID)
5036
if err != nil {
5137
utils.Logger.Sugar().Error(err)
5238
ctx.Reply(u, fmt.Sprintf("Error - %s", err.Error()), nil)
5339
return dispatcher.EndGroups
5440
}
55-
messageID := update.(*tg.Updates).Updates[0].(*tg.UpdateMessageID).ID
41+
messageID := update.Updates[0].(*tg.UpdateMessageID).ID
5642
if err != nil {
5743
utils.Logger.Sugar().Error(err)
5844
ctx.Reply(u, fmt.Sprintf("Error - %s", err.Error()), nil)
5945
return dispatcher.EndGroups
6046
}
61-
doc := update.(*tg.Updates).Updates[1].(*tg.UpdateNewChannelMessage).Message.(*tg.Message).Media
47+
doc := update.Updates[1].(*tg.UpdateNewChannelMessage).Message.(*tg.Message).Media
6248
file, err := utils.FileFromMedia(doc)
6349
if err != nil {
6450
ctx.Reply(u, fmt.Sprintf("Error - %s", err.Error()), nil)
@@ -70,7 +56,6 @@ func sendLink(ctx *ext.Context, u *ext.Update) error {
7056
file.MimeType,
7157
file.ID,
7258
)
73-
7459
hash := utils.GetShortHash(fullHash)
7560
link := fmt.Sprintf("%s/stream/%d?hash=%s", config.ValueOf.Host, messageID, hash)
7661
ctx.Reply(u, link, nil)

internal/utils/helpers.go

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

1619
func 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

Comments
 (0)