парсер
This commit is contained in:
46
tg_parser/internal/adapter/telegram/history.go
Normal file
46
tg_parser/internal/adapter/telegram/history.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package telegram
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/TelegramExchange/tgex-parser/internal/domain"
|
||||
"github.com/gotd/td/tg"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func (t *Telegram) GetChannelHistory(ctx context.Context, channel domain.Channel, limit int) ([]domain.Post, error) {
|
||||
log.Info().Msg(channel.String())
|
||||
|
||||
req := &tg.MessagesGetHistoryRequest{
|
||||
Peer: &tg.InputPeerChannel{
|
||||
ChannelID: channel.ChannelID(),
|
||||
AccessHash: channel.AccessHash,
|
||||
},
|
||||
Limit: limit,
|
||||
}
|
||||
|
||||
resp, err := t.API().MessagesGetHistory(ctx, req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("resp: %w", err)
|
||||
}
|
||||
|
||||
messages, ok := resp.(*tg.MessagesChannelMessages)
|
||||
if !ok {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
placements := make([]domain.Post, 0, len(messages.Messages))
|
||||
|
||||
for _, raw := range messages.Messages {
|
||||
m, ok := raw.(*tg.Message)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
p := domain.NewPost(channel, m.ID, m.Message, m.Views)
|
||||
placements = append(placements, p)
|
||||
}
|
||||
|
||||
return placements, nil
|
||||
}
|
||||
Reference in New Issue
Block a user