Files
tg_exporter/internal/channel_message.go
2024-09-03 09:55:51 +08:00

93 lines
2.0 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 {
MsgID int `json:"msg_id,omitempty"`
AssetID int64 `json:"asset_id,omitempty"`
Photo *string `json:"photo,omitempty"`
Video *string `json:"video,omitempty"`
Document *ChannelMessageDocument `json:"document,omitempty"`
WebPage *ChannelMessageMediaWebPage `json:"web_page,omitempty"`
}
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 = ChannelMessageMedia{
MsgID: c.ID,
AssetID: assetID,
Photo: lo.ToPtr(fmt.Sprintf("%d.%s", assetID, strings.Trim(ext, "."))),
}
return c
}
func (c *ChannelMessage) WithDocument(assetID int64, d ChannelMessageDocument) *ChannelMessage {
c.Medias = ChannelMessageMedia{
MsgID: c.ID,
AssetID: assetID,
Document: lo.ToPtr(d),
}
return c
}
func (c *ChannelMessage) WithWebPage(assetID int64, title, url string) *ChannelMessage {
c.Medias = ChannelMessageMedia{
MsgID: c.ID,
AssetID: assetID,
WebPage: lo.ToPtr(ChannelMessageMediaWebPage{Title: title, URL: url}),
}
return c
}
func (c *ChannelMessage) GetMedias() string {
b, _ := json.Marshal([]ChannelMessageMedia{c.Medias})
return string(b)
}