diff --git a/app/app.go b/app/app.go index 405a4e9..28fefe0 100644 --- a/app/app.go +++ b/app/app.go @@ -5,6 +5,8 @@ import ( "context" "log/slog" "os" + "regexp" + "strings" "text/template" "time" @@ -52,7 +54,6 @@ func (a *App) Run(ctx context.Context) error { } func (a *App) iteration() error { - seq, err := a.readSeqFile() if os.IsNotExist(err) { seq = "" @@ -81,11 +82,13 @@ func (a *App) iteration() error { func (a *App) processItem(item *gofeed.Item) error { buf := bytes.NewBufferString("") + item.Content = trimHtml(item.Description) if err := a.templates.ExecuteTemplate(buf, "telegram", item); err != nil { return err } + str := buf.String() for _, group := range a.config.Telegram.TargetGroups { - msg := tgbotapi.NewMessage(group, buf.String()) + msg := tgbotapi.NewMessage(group, str) msg.ParseMode = tgbotapi.ModeHTML if _, err := a.telegram.Send(msg); err != nil { return err @@ -105,7 +108,7 @@ func (a *App) readSeqFile() (string, error) { } func (a *App) writeSeqFile(seqVal string) error { - return os.WriteFile(a.config.RSS.SeqFile, []byte(seqVal), 0644) + return os.WriteFile(a.config.RSS.SeqFile, []byte(seqVal), 0o644) } func (a *App) findNewItems(from string) ([]*gofeed.Item, error) { @@ -124,3 +127,22 @@ func (a *App) findNewItems(from string) ([]*gofeed.Item, error) { return out, nil } + +func trimHtml(src string) string { + // Convert all HTML tags to lowercase + re, _ := regexp.Compile("\\<[\\S\\s]+?\\>") + src = re.ReplaceAllStringFunc(src, strings.ToLower) + // Remove STYLE + re, _ = regexp.Compile("\\") + src = re.ReplaceAllString(src, "") + // Remove SCRIPT + re, _ = regexp.Compile("\\") + src = re.ReplaceAllString(src, "") + // Remove all HTML code in angle brackets and replace them with newline characters + re, _ = regexp.Compile("\\<[\\S\\s]+?\\>") + src = re.ReplaceAllString(src, "\n") + // Remove consecutive newlines + re, _ = regexp.Compile("\\s{2,}") + src = re.ReplaceAllString(src, "\n") + return strings.TrimSpace(src) +} diff --git a/templates/telegram.gotmpl b/templates/telegram.gotmpl index cd98d7a..d217731 100644 --- a/templates/telegram.gotmpl +++ b/templates/telegram.gotmpl @@ -1,5 +1,5 @@ {{define "telegram"}} -{{.Title}} +{{.Title}} {{.Content}}