This commit is contained in:
Rogee
2024-09-02 20:09:57 +08:00
parent 422a5a9cab
commit d3806983a4
2 changed files with 53 additions and 8 deletions

View File

@@ -15,9 +15,10 @@ type ChannelMessage struct {
}
type ChannelMessageMedia struct {
Photo string
Video string
WebPage ChannelMessageMediaWebPage
Photo string
Video string
Document string
WebPage ChannelMessageMediaWebPage
}
type ChannelMessageMediaWebPage struct {
@@ -49,6 +50,11 @@ func (c *ChannelMessage) WithVideo(video string) *ChannelMessage {
return c
}
func (c *ChannelMessage) WithDoc(docID int64, ext string) *ChannelMessage {
c.Medias = append(c.Medias, ChannelMessageMedia{Document: fmt.Sprintf("%d.%s", docID, ext)})
return c
}
func (c *ChannelMessage) WithWebPage(title, url string) *ChannelMessage {
c.Medias = append(c.Medias, ChannelMessageMedia{WebPage: ChannelMessageMediaWebPage{Title: title, URL: url}})
return c

View File

@@ -2,6 +2,7 @@ package internal
import (
"context"
"mime"
"github.com/gotd/td/telegram/downloader"
"github.com/gotd/td/tg"
@@ -24,6 +25,9 @@ func (t *TClient) Channel(ctx context.Context, channel *tg.Channel, cfg *DBChann
request.MinID = cfg.MinID // 提供此ID供新增加的消息
}
request.Limit = 1
request.OffsetID = 3753
messages, err := t.Client.API().MessagesGetHistory(ctx, request)
if err != nil {
return errors.Wrap(err, "messages.getHistory")
@@ -66,16 +70,51 @@ func (t *TClient) Channel(ctx context.Context, channel *tg.Channel, cfg *DBChann
if mediaClass, ok := msg.GetMedia(); ok {
switch mediaClass.(type) {
case *tg.MessageMediaDocument:
if doc, ok := mediaClass.(*tg.MessageMediaDocument).GetDocument(); ok {
t.logger.Warn("document", zap.String("file_name", doc.String()))
if docClass, ok := mediaClass.(*tg.MessageMediaDocument).GetDocument(); ok {
t.logger.Warn("document",
zap.Int("msg_id", msg.ID),
zap.String("file_name", docClass.String()),
)
doc := docClass.(*tg.Document)
if doc.GetSize() > 1024*1024*10 {
t.logger.Warn("document size too large", zap.Int64("size", doc.GetSize()))
return
}
thumbSize := ""
if len(doc.Thumbs) > 1 {
thumbSize = doc.Thumbs[len(doc.Thumbs)-1].GetType()
}
location := &tg.InputDocumentFileLocation{
ID: doc.GetID(),
AccessHash: doc.GetAccessHash(),
FileReference: doc.GetFileReference(),
ThumbSize: thumbSize,
}
exts, err := mime.ExtensionsByType(doc.GetMimeType())
if err != nil {
t.logger.Error("get extension failed", zap.Error(err), zap.String("mime_type", doc.GetMimeType()))
return
}
saveTo := cfg.Asset(doc.GetID(), exts[0])
_, err = downloader.Download(t.Client.API(), location).ToPath(ctx, saveTo)
if err != nil {
t.logger.Error("download failed", zap.Error(err))
return
}
channelMessage.WithDoc(doc.GetID(), exts[0])
t.logger.Info("download photo success", zap.String("location", saveTo))
return
}
return
case *tg.MessageMediaWebPage:
t.logger.Warn("web_page", zap.String("url", mediaClass.(*tg.MessageMediaWebPage).GetWebpage().String()))
if page, ok := mediaClass.(*tg.MessageMediaWebPage).GetWebpage().(*tg.WebPage); ok {
channelMessage.WithWebPage(page.Title, page.URL)
return
}
return
t.logger.Warn("web_page", zap.String("url", mediaClass.(*tg.MessageMediaWebPage).GetWebpage().String()))
case *tg.MessageMediaPhoto:
if photoClass, ok := mediaClass.(*tg.MessageMediaPhoto).GetPhoto(); ok {
photo := photoClass.(*tg.Photo)