summaryrefslogtreecommitdiff
path: root/app/app.go
diff options
context:
space:
mode:
authorAlexander Neonxp Kiryukhin <i@neonxp.ru>2024-12-17 20:51:07 +0300
committerAlexander Neonxp Kiryukhin <i@neonxp.ru>2024-12-17 20:51:07 +0300
commit12cd485899f55058339faeca0f8523fbb0d2b334 (patch)
treed059341d5fb86d5f8ef80c366f167fd448651425 /app/app.go
parent70565ecac31f7bb5c1d6c559ed18ff9f9c56ac46 (diff)
Обновил публикацию в телеграмHEADmaster
Diffstat (limited to 'app/app.go')
-rw-r--r--app/app.go52
1 files changed, 20 insertions, 32 deletions
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, "</h1>", "</b>")
s = strings.ReplaceAll(s, "<h2", "<b")
s = strings.ReplaceAll(s, "</h2>", "</b>")
+ s = strings.ReplaceAll(s, "</p>", "\n")
s = strings.ReplaceAll(s, "<br />", "\n")
- s = strings.ReplaceAll(s, "&lt;p&gt;", "")
- s = strings.ReplaceAll(s, "&lt;/p&gt;", "")
- 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("<b>%s</b>\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