mirror of
https://github.com/krau/SaveAny-Bot.git
synced 2026-09-05 23:56:50 +08:00
feat: enhance text message handling and parser configuration
This commit is contained in:
@@ -20,7 +20,16 @@ import (
|
|||||||
func handleTextMessage(ctx *ext.Context, u *ext.Update) error {
|
func handleTextMessage(ctx *ext.Context, u *ext.Update) error {
|
||||||
logger := log.FromContext(ctx)
|
logger := log.FromContext(ctx)
|
||||||
text := u.EffectiveMessage.Text
|
text := u.EffectiveMessage.Text
|
||||||
item, err := parsers.ParseWithContext(ctx, text)
|
ok, pser := parsers.CanHandle(text)
|
||||||
|
if !ok {
|
||||||
|
return dispatcher.EndGroups
|
||||||
|
}
|
||||||
|
msg, err := ctx.Reply(u, ext.ReplyTextString("正在解析..."), nil)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
item, err := pser.Parse(ctx, text)
|
||||||
if errors.Is(err, parsers.ErrNoParserFound) {
|
if errors.Is(err, parsers.ErrNoParserFound) {
|
||||||
return dispatcher.EndGroups
|
return dispatcher.EndGroups
|
||||||
}
|
}
|
||||||
@@ -29,7 +38,7 @@ func handleTextMessage(ctx *ext.Context, u *ext.Update) error {
|
|||||||
ctx.Reply(u, ext.ReplyTextString("Failed to parse text: "+err.Error()), nil)
|
ctx.Reply(u, ext.ReplyTextString("Failed to parse text: "+err.Error()), nil)
|
||||||
return dispatcher.EndGroups
|
return dispatcher.EndGroups
|
||||||
}
|
}
|
||||||
logger.Debug("Parsed item from text message", "text", text, "item", item)
|
logger.Debug("Parsed item from text message", "title", item.Title, "url", item.URL)
|
||||||
userID := u.GetUserChat().GetID()
|
userID := u.GetUserChat().GetID()
|
||||||
markup, err := msgelem.BuildAddSelectStorageKeyboard(storage.GetUserStorages(ctx, userID), tcbdata.Add{
|
markup, err := msgelem.BuildAddSelectStorageKeyboard(storage.GetUserStorages(ctx, userID), tcbdata.Add{
|
||||||
TaskType: tasktype.TaskTypeParseditem,
|
TaskType: tasktype.TaskTypeParseditem,
|
||||||
@@ -46,14 +55,11 @@ func handleTextMessage(ctx *ext.Context, u *ext.Update) error {
|
|||||||
ctx.Reply(u, ext.ReplyTextString("Failed to build parsed text entity: "+err.Error()), nil)
|
ctx.Reply(u, ext.ReplyTextString("Failed to build parsed text entity: "+err.Error()), nil)
|
||||||
return dispatcher.EndGroups
|
return dispatcher.EndGroups
|
||||||
}
|
}
|
||||||
ctx.SendMessage(userID, &tg.MessagesSendMessageRequest{
|
ctx.EditMessage(userID, &tg.MessagesEditMessageRequest{
|
||||||
Message: text,
|
Message: text,
|
||||||
ReplyMarkup: markup,
|
ReplyMarkup: markup,
|
||||||
Entities: entities,
|
Entities: entities,
|
||||||
ReplyTo: &tg.InputReplyToMessage{
|
ID: msg.ID,
|
||||||
ReplyToMsgID: u.EffectiveMessage.ID,
|
|
||||||
ReplyToPeerID: u.GetUserChat().AsInputPeer(),
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
|
|
||||||
return dispatcher.EndGroups
|
return dispatcher.EndGroups
|
||||||
@@ -80,7 +86,7 @@ func handleSilentSaveText(ctx *ext.Context, u *ext.Update) error {
|
|||||||
ctx.Reply(u, ext.ReplyTextString("Failed to parse text: "+err.Error()), nil)
|
ctx.Reply(u, ext.ReplyTextString("Failed to parse text: "+err.Error()), nil)
|
||||||
return dispatcher.EndGroups
|
return dispatcher.EndGroups
|
||||||
}
|
}
|
||||||
logger.Debug("Parsed item from text message", "text", text, "item", item)
|
logger.Debug("Parsed item from text message", "title", item.Title, "url", item.URL)
|
||||||
userID := u.GetUserChat().GetID()
|
userID := u.GetUserChat().GetID()
|
||||||
text, entities, err := msgelem.BuildParsedTextEntity(*item)
|
text, entities, err := msgelem.BuildParsedTextEntity(*item)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
+34
-22
@@ -6,32 +6,16 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/krau/SaveAny-Bot/config"
|
"github.com/krau/SaveAny-Bot/config"
|
||||||
|
"github.com/krau/SaveAny-Bot/parsers/kemono"
|
||||||
"github.com/krau/SaveAny-Bot/parsers/twitter"
|
"github.com/krau/SaveAny-Bot/parsers/twitter"
|
||||||
"github.com/krau/SaveAny-Bot/pkg/parser"
|
"github.com/krau/SaveAny-Bot/pkg/parser"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
parsers []parser.Parser
|
parsers []parser.Parser
|
||||||
parsersMu sync.Mutex
|
parsersMu sync.Mutex
|
||||||
doConfig sync.Once
|
doConfig sync.Once
|
||||||
)
|
configParsers = func() {
|
||||||
|
|
||||||
func AddParser(p ...parser.Parser) {
|
|
||||||
parsersMu.Lock()
|
|
||||||
defer parsersMu.Unlock()
|
|
||||||
parsers = append(parsers, p...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
AddParser(new(twitter.TwitterParser))
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
ErrNoParserFound = fmt.Errorf("no parser found for the given URL")
|
|
||||||
)
|
|
||||||
|
|
||||||
func ParseWithContext(ctx context.Context, url string) (*parser.Item, error) {
|
|
||||||
doConfig.Do(func() {
|
|
||||||
if len(parsers) == 0 {
|
if len(parsers) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -45,7 +29,25 @@ func ParseWithContext(ctx context.Context, url string) (*parser.Item, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
func AddParser(p ...parser.Parser) {
|
||||||
|
parsersMu.Lock()
|
||||||
|
defer parsersMu.Unlock()
|
||||||
|
parsers = append(parsers, p...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
AddParser(new(twitter.TwitterParser), new(kemono.KemonoParser))
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrNoParserFound = fmt.Errorf("no parser found for the given URL")
|
||||||
|
)
|
||||||
|
|
||||||
|
func ParseWithContext(ctx context.Context, url string) (*parser.Item, error) {
|
||||||
|
doConfig.Do(configParsers)
|
||||||
ch := make(chan *parser.Item, 1)
|
ch := make(chan *parser.Item, 1)
|
||||||
errCh := make(chan error, 1)
|
errCh := make(chan error, 1)
|
||||||
|
|
||||||
@@ -74,3 +76,13 @@ func ParseWithContext(ctx context.Context, url string) (*parser.Item, error) {
|
|||||||
return nil, ctx.Err()
|
return nil, ctx.Err()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func CanHandle(url string) (bool, parser.Parser) {
|
||||||
|
doConfig.Do(configParsers)
|
||||||
|
for _, pser := range parsers {
|
||||||
|
if pser.CanHandle(url) {
|
||||||
|
return true, pser
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user