- Added a new command handler for /watch that allows users to listen to messages from a specified chat and save them according to storage rules. - Introduced filtering options for messages using regular expressions. - Implemented functionality to start and stop watching chats, including error handling for invalid inputs and user settings. - Created a new utility package for message element handling related to the watch feature. - Updated the user model to manage watched chats, including methods to add, remove, and check if a chat is being watched.
54 lines
1.2 KiB
Go
54 lines
1.2 KiB
Go
package user
|
|
|
|
import (
|
|
"github.com/celestix/gotgproto/dispatcher"
|
|
"github.com/celestix/gotgproto/ext"
|
|
"github.com/gotd/td/tg"
|
|
"github.com/krau/SaveAny-Bot/common/utils/tgutil"
|
|
"github.com/krau/SaveAny-Bot/pkg/tfile"
|
|
)
|
|
|
|
type MediaMessageEvent struct {
|
|
Ctx *ext.Context
|
|
ChatID int64 // from witch the media message was sent
|
|
File tfile.TGFileMessage
|
|
}
|
|
|
|
var mediaMessageCh = make(chan MediaMessageEvent, 100)
|
|
|
|
func GetMediaMessageCh() chan MediaMessageEvent {
|
|
return mediaMessageCh
|
|
}
|
|
|
|
func handleMediaMessage(ctx *ext.Context, update *ext.Update) error {
|
|
message := update.EffectiveMessage
|
|
media, ok := message.GetMedia()
|
|
if !ok || media == nil {
|
|
return dispatcher.EndGroups
|
|
}
|
|
support := func() bool {
|
|
switch media.(type) {
|
|
case *tg.MessageMediaDocument, *tg.MessageMediaPhoto:
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}()
|
|
if !support {
|
|
return dispatcher.EndGroups
|
|
}
|
|
file, err := tfile.FromMediaMessage(media, ctx.Raw, message.Message, tfile.WithNameIfEmpty(
|
|
tgutil.GenFileNameFromMessage(*message.Message),
|
|
))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
chatId := update.EffectiveChat().GetID()
|
|
mediaMessageCh <- MediaMessageEvent{
|
|
Ctx: ctx,
|
|
ChatID: chatId,
|
|
File: file,
|
|
}
|
|
return dispatcher.EndGroups
|
|
}
|