fix: improve text message handling by removing duplicates and empty lines

This commit is contained in:
krau
2025-12-21 13:17:10 +08:00
parent c069d1edc7
commit ad7e01f052
2 changed files with 19 additions and 3 deletions
+18 -2
View File
@@ -31,7 +31,23 @@ func handleTextMessage(ctx *ext.Context, u *ext.Update) error {
if len(entityUrls) > 0 { if len(entityUrls) > 0 {
text += "\n" + strings.Join(entityUrls, "\n") text += "\n" + strings.Join(entityUrls, "\n")
} }
ok, pser := parsers.CanHandle(text) // read lines and remove empty lines & duplicates
lines := strings.Split(text, "\n")
seen := make(map[string]struct{})
var processedLines []string
for _, line := range lines {
line = strings.TrimSpace(line)
if line == "" {
continue
}
if _, ok := seen[line]; ok {
continue
}
seen[line] = struct{}{}
processedLines = append(processedLines, line)
}
source := strings.TrimSpace(strings.Join(processedLines, "\n"))
ok, pser := parsers.CanHandle(source)
if !ok { if !ok {
return dispatcher.EndGroups return dispatcher.EndGroups
} }
@@ -40,7 +56,7 @@ func handleTextMessage(ctx *ext.Context, u *ext.Update) error {
return err return err
} }
item, err := pser.Parse(ctx, text) item, err := pser.Parse(ctx, source)
if errors.Is(err, parsers.ErrNoParserFound) { if errors.Is(err, parsers.ErrNoParserFound) {
return dispatcher.EndGroups return dispatcher.EndGroups
} }
+1 -1
View File
@@ -21,7 +21,7 @@ func BuildParsedTextEntity(item parser.Item) (string, []tg.MessageEntityClass, e
styling.Plain(i18n.T(i18nk.BotMsgParseInfoAuthorPrefix, nil)), styling.Plain(i18n.T(i18nk.BotMsgParseInfoAuthorPrefix, nil)),
styling.Code(item.Author), styling.Code(item.Author),
styling.Plain(i18n.T(i18nk.BotMsgParseInfoDescriptionPrefix, nil)), styling.Plain(i18n.T(i18nk.BotMsgParseInfoDescriptionPrefix, nil)),
styling.Code(strutil.Ellipsis(item.Description, 233)), styling.Blockquote(strutil.Ellipsis(item.Description, 233), true),
styling.Plain(i18n.T(i18nk.BotMsgParseInfoFileCountPrefix, nil)), styling.Plain(i18n.T(i18nk.BotMsgParseInfoFileCountPrefix, nil)),
styling.Code(fmt.Sprintf("%d", len(item.Resources))), styling.Code(fmt.Sprintf("%d", len(item.Resources))),
styling.Plain(i18n.T(i18nk.BotMsgParseInfoTotalSizePrefix, nil)), styling.Plain(i18n.T(i18nk.BotMsgParseInfoTotalSizePrefix, nil)),