- 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.
111 lines
3.8 KiB
Go
111 lines
3.8 KiB
Go
package handlers
|
|
|
|
import (
|
|
"regexp"
|
|
"strings"
|
|
|
|
"github.com/celestix/gotgproto/dispatcher"
|
|
"github.com/celestix/gotgproto/ext"
|
|
"github.com/charmbracelet/log"
|
|
"github.com/krau/SaveAny-Bot/client/bot/handlers/utils/msgelem"
|
|
"github.com/krau/SaveAny-Bot/common/utils/tgutil"
|
|
"github.com/krau/SaveAny-Bot/database"
|
|
)
|
|
|
|
func handleWatchCmd(ctx *ext.Context, update *ext.Update) error {
|
|
logger := log.FromContext(ctx)
|
|
args := strings.Split(string(update.EffectiveMessage.Text), " ")
|
|
if len(args) < 2 {
|
|
ctx.Reply(update, ext.ReplyTextString(msgelem.WatchHelpText), nil)
|
|
return dispatcher.EndGroups
|
|
}
|
|
userChatID := update.GetUserChat().GetID()
|
|
user, err := database.GetUserByChatID(ctx, userChatID)
|
|
if err != nil {
|
|
logger.Errorf("获取用户失败: %s", err)
|
|
ctx.Reply(update, ext.ReplyTextString("获取用户失败"), nil)
|
|
return dispatcher.EndGroups
|
|
}
|
|
if user.DefaultStorage == "" {
|
|
ctx.Reply(update, ext.ReplyTextString("请先设置默认存储, 使用 /storage 命令"), nil)
|
|
return dispatcher.EndGroups
|
|
}
|
|
chatArg := args[1]
|
|
chatID, err := tgutil.ParseChatID(ctx, chatArg)
|
|
if err != nil {
|
|
ctx.Reply(update, ext.ReplyTextString("无效的ID或用户名: "+err.Error()), nil)
|
|
return dispatcher.EndGroups
|
|
}
|
|
watching, err := user.WatchingChat(ctx, chatID)
|
|
if err != nil {
|
|
logger.Errorf("Failed to check if user is watching chat %d: %s", chatID, err)
|
|
return dispatcher.EndGroups
|
|
}
|
|
if watching {
|
|
ctx.Reply(update, ext.ReplyTextString("已经在监听此聊天"), nil)
|
|
return dispatcher.EndGroups
|
|
}
|
|
filter := ""
|
|
if len(args) > 2 {
|
|
filterArg := strings.Join(args[2:], " ")
|
|
filterType := strings.Split(filterArg, ":")[0]
|
|
filterData := strings.Split(filterArg, ":")[1]
|
|
if filterType == "" || filterData == "" {
|
|
ctx.Reply(update, ext.ReplyTextString("过滤器格式错误, 请使用 <过滤器类型>:<表达式>"), nil)
|
|
return dispatcher.EndGroups
|
|
}
|
|
switch filterType {
|
|
case "msgre":
|
|
_, err := regexp.Compile(filterData)
|
|
if err != nil {
|
|
ctx.Reply(update, ext.ReplyTextString("正则表达式格式错误: "+err.Error()), nil)
|
|
return dispatcher.EndGroups
|
|
}
|
|
filter = filterType + ":" + filterData
|
|
default:
|
|
ctx.Reply(update, ext.ReplyTextString("不支持的过滤器类型, 请参阅文档"), nil)
|
|
return dispatcher.EndGroups
|
|
}
|
|
}
|
|
if err := user.WatchChat(ctx, database.WatchChat{
|
|
UserID: user.ID,
|
|
ChatID: chatID,
|
|
Filter: filter,
|
|
}); err != nil {
|
|
logger.Errorf("Failed to watch chat %d: %s", chatID, err)
|
|
ctx.Reply(update, ext.ReplyTextString("监听聊天失败: "+err.Error()), nil)
|
|
return dispatcher.EndGroups
|
|
}
|
|
ctx.Reply(update, ext.ReplyTextString("已开始监听聊天: "+chatArg), nil)
|
|
return dispatcher.EndGroups
|
|
}
|
|
|
|
func handleUnwatchCmd(ctx *ext.Context, update *ext.Update) error {
|
|
logger := log.FromContext(ctx)
|
|
args := strings.Split(string(update.EffectiveMessage.Text), " ")
|
|
if len(args) < 2 {
|
|
ctx.Reply(update, ext.ReplyTextString("请提供要取消监听的聊天ID或用户名"), nil)
|
|
return dispatcher.EndGroups
|
|
}
|
|
userChatID := update.GetUserChat().GetID()
|
|
user, err := database.GetUserByChatID(ctx, userChatID)
|
|
if err != nil {
|
|
logger.Errorf("获取用户失败: %s", err)
|
|
ctx.Reply(update, ext.ReplyTextString("获取用户失败"), nil)
|
|
return dispatcher.EndGroups
|
|
}
|
|
chatArg := args[1]
|
|
chatID, err := tgutil.ParseChatID(ctx, chatArg)
|
|
if err != nil {
|
|
ctx.Reply(update, ext.ReplyTextString("无效的ID或用户名: "+err.Error()), nil)
|
|
return dispatcher.EndGroups
|
|
}
|
|
if err := user.UnwatchChat(ctx, chatID); err != nil {
|
|
logger.Errorf("Failed to unwatch chat %d: %s", chatID, err)
|
|
ctx.Reply(update, ext.ReplyTextString("取消监听聊天失败: "+err.Error()), nil)
|
|
return dispatcher.EndGroups
|
|
}
|
|
ctx.Reply(update, ext.ReplyTextString("已取消监听聊天: "+chatArg), nil)
|
|
return dispatcher.EndGroups
|
|
}
|