Files
tg_exporter/internal/channel_message.go
2024-09-02 23:39:22 +08:00

89 lines
1.8 KiB
Go

package internal
import (
"encoding/json"
"fmt"
"strings"
"time"
"github.com/samber/lo"
)
type ChannelMessage struct {
ID int
GroupID int64
Message string
Medias []ChannelMessageMedia
PublishAt time.Time
}
type ChannelMessageMedia struct {
Photo *string
Video *string
Document *ChannelMessageDocument
WebPage *ChannelMessageMediaWebPage
}
type ChannelMessageDocument struct {
Ext string
Filename string
MimeType string
Size int64
Video *ChannelMessageDocumentVideo
}
type ChannelMessageDocumentVideo struct {
Duration float64
Width int
Height int
}
type ChannelMessageMediaWebPage struct {
Title string
URL string
}
func NewChannelMessage(id, ts int) *ChannelMessage {
return &ChannelMessage{ID: id, PublishAt: time.Unix(int64(ts), 0)}
}
func (c *ChannelMessage) WithGroupID(groupID int64) *ChannelMessage {
c.GroupID = groupID
return c
}
func (c *ChannelMessage) WithMessage(message string) *ChannelMessage {
c.Message = message
return c
}
func (c *ChannelMessage) WithPhoto(assetID int64, ext string) *ChannelMessage {
c.Medias = append(c.Medias, ChannelMessageMedia{
Photo: lo.ToPtr(fmt.Sprintf("%d.%s", assetID, strings.Trim(ext, "."))),
})
return c
}
func (c *ChannelMessage) WithVideo(video string) *ChannelMessage {
c.Medias = append(c.Medias, ChannelMessageMedia{Video: lo.ToPtr(video)})
return c
}
func (c *ChannelMessage) WithDocument(d ChannelMessageDocument) *ChannelMessage {
c.Medias = append(c.Medias, ChannelMessageMedia{
Document: lo.ToPtr(d),
})
return c
}
func (c *ChannelMessage) WithWebPage(title, url string) *ChannelMessage {
c.Medias = append(c.Medias, ChannelMessageMedia{
WebPage: lo.ToPtr(ChannelMessageMediaWebPage{Title: title, URL: url}),
})
return c
}
func (c *ChannelMessage) GetMedia() string {
b, _ := json.Marshal(c.Medias)
return string(b)
}