From 12cd485899f55058339faeca0f8523fbb0d2b334 Mon Sep 17 00:00:00 2001 From: Alexander Neonxp Kiryukhin Date: Tue, 17 Dec 2024 20:51:07 +0300 Subject: Обновил публикацию в телеграм MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/app.go | 52 ++++++++++++++++++++-------------------------------- 1 file changed, 20 insertions(+), 32 deletions(-) (limited to 'app/app.go') diff --git a/app/app.go b/app/app.go index 756bad2..7baa73a 100644 --- a/app/app.go +++ b/app/app.go @@ -1,25 +1,21 @@ package app import ( - "bytes" "context" + "fmt" "log/slog" "os" "strings" - "text/template" "time" - "git.neonxp.ru/posse/templates" - tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" "github.com/microcosm-cc/bluemonday" "github.com/mmcdole/gofeed" ) type App struct { - config *Config - telegram *tgbotapi.BotAPI - templates *template.Template + config *Config + telegram *tgbotapi.BotAPI } func New(cfg *Config) (*App, error) { @@ -28,15 +24,9 @@ func New(cfg *Config) (*App, error) { return nil, err } - tpl, err := template.ParseFS(templates.Templates, "*.gotmpl") - if err != nil { - return nil, err - } - return &App{ - config: cfg, - telegram: bot, - templates: tpl, + config: cfg, + telegram: bot, }, nil } @@ -85,8 +75,6 @@ func (a *App) iteration() error { } func (a *App) processItem(item *gofeed.Item) error { - buf := bytes.NewBufferString("") - p := bluemonday.Policy{} p.AllowStandardURLs() p.AllowElements("b", "i", "code", "u", "strike", "pre", "br", "a") @@ -98,26 +86,19 @@ func (a *App) processItem(item *gofeed.Item) error { s = strings.ReplaceAll(s, "", "") s = strings.ReplaceAll(s, "", "") + s = strings.ReplaceAll(s, "

", "\n") s = strings.ReplaceAll(s, "
", "\n") - s = strings.ReplaceAll(s, "<p>", "") - s = strings.ReplaceAll(s, "</p>", "") - item.Content = p.Sanitize(s) + s = p.Sanitize(s) + s = strings.Trim(s, "\n\t ") - if err := a.templates.ExecuteTemplate(buf, "telegram", item); err != nil { - return err - } - str := "" - str2 := buf.String() - for str != str2 { - str = str2 - str2 = strings.ReplaceAll(str, "\n\n", "\n") - str2 = strings.ReplaceAll(str2, " ", " ") - str2 = strings.Trim(str2, " \t\n") - } + str := fmt.Sprintf("%s\n%s", item.Title, s) for _, group := range a.config.Telegram.TargetGroups { switch { case item.Image != nil: - msg := tgbotapi.NewPhoto(group, tgbotapi.FileURL(item.Image.URL)) + msg := tgbotapi.NewPhoto( + group, + tgbotapi.FileURL(item.Image.URL), + ) msg.ParseMode = tgbotapi.ModeHTML msg.Caption = str if _, err := a.telegram.Send(msg); err != nil { @@ -130,6 +111,13 @@ func (a *App) processItem(item *gofeed.Item) error { return err } } + msg := tgbotapi.NewMessage( + group, + fmt.Sprintf("Оригинал: %s", item.Link), + ) + if _, err := a.telegram.Send(msg); err != nil { + return err + } } return nil -- cgit v1.2.3