парсер
This commit is contained in:
59
tg_parser/internal/adapter/telegram/resolve.go
Normal file
59
tg_parser/internal/adapter/telegram/resolve.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package telegram
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/TelegramExchange/tgex-parser/internal/domain"
|
||||
"github.com/google/uuid"
|
||||
"github.com/gotd/td/tg"
|
||||
)
|
||||
|
||||
func (t *Telegram) ParseChannelMeta(ctx context.Context, username string) (domain.Channel, error) {
|
||||
resolved, err := t.API().ContactsResolveUsername(ctx, &tg.ContactsResolveUsernameRequest{
|
||||
Username: username,
|
||||
})
|
||||
if err != nil {
|
||||
return domain.Channel{}, fmt.Errorf("resolve username: %w", err)
|
||||
}
|
||||
|
||||
if len(resolved.Chats) == 0 {
|
||||
return domain.Channel{}, fmt.Errorf("no chats in resolve result")
|
||||
}
|
||||
|
||||
ch, ok := resolved.Chats[0].(*tg.Channel)
|
||||
if !ok {
|
||||
return domain.Channel{}, fmt.Errorf("not a channel")
|
||||
}
|
||||
|
||||
req := []tg.InputDialogPeerClass{
|
||||
&tg.InputDialogPeer{
|
||||
Peer: &tg.InputPeerChannel{
|
||||
ChannelID: ch.ID,
|
||||
AccessHash: ch.AccessHash,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
dialogs, err := t.API().MessagesGetPeerDialogs(ctx, req)
|
||||
if err != nil {
|
||||
return domain.Channel{}, fmt.Errorf("peer dialogs: %w", err)
|
||||
}
|
||||
|
||||
pts := 0
|
||||
if len(dialogs.Dialogs) > 0 {
|
||||
d, ok := dialogs.Dialogs[0].(*tg.Dialog)
|
||||
if ok {
|
||||
pts = d.Pts
|
||||
}
|
||||
}
|
||||
|
||||
return domain.Channel{
|
||||
ID: uuid.New(),
|
||||
TelegramID: domain.ChatIDFromChannelID(ch.ID),
|
||||
Username: username,
|
||||
Title: ch.Title,
|
||||
AccessHash: ch.AccessHash,
|
||||
Pts: pts,
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user