formatted message with comments and configurable interval
This commit is contained in:
parent
ee9cd0e19c
commit
9af87b102e
|
@ -110,9 +110,13 @@ func (m *Matrix) InviteHandler() (event.Type, mautrix.EventHandler) {
|
|||
func (m *Matrix) PostMessages() {
|
||||
for entry := range m.feedReader.Feed() {
|
||||
m.logger.Info("received entry", slog.String("title", entry.Title), slog.String("url", entry.URL))
|
||||
formattedReply := format.RenderMarkdown(fmt.Sprintf("%s - %s - %s", entry.FeedTitle, entry.Title, entry.URL), true, false)
|
||||
message := fmt.Sprintf(`%s: [%s](%s)`, entry.FeedTitle, entry.Title, entry.URL)
|
||||
if entry.CommentsURL != "" {
|
||||
message += fmt.Sprintf(" - [Comments](%s)", entry.CommentsURL)
|
||||
}
|
||||
formattedMessage := format.RenderMarkdown(message, true, false)
|
||||
|
||||
if _, err := m.client.SendMessageEvent(id.RoomID(m.config.RoomID), event.EventMessage, &formattedReply); err != nil {
|
||||
if _, err := m.client.SendMessageEvent(id.RoomID(m.config.RoomID), event.EventMessage, &formattedMessage); err != nil {
|
||||
m.logger.Error("failed to send message", slog.String("err", err.Error()))
|
||||
return
|
||||
}
|
||||
|
|
|
@ -12,31 +12,34 @@ type Entry struct {
|
|||
Title string
|
||||
Description string
|
||||
URL string
|
||||
Comments string
|
||||
CommentsURL string
|
||||
FeedTitle string
|
||||
}
|
||||
|
||||
type MinifluxInfo struct {
|
||||
Endpoint string
|
||||
ApiKey string
|
||||
Interval time.Duration
|
||||
}
|
||||
|
||||
type Miniflux struct {
|
||||
client *client.Client
|
||||
logger *slog.Logger
|
||||
interval time.Duration
|
||||
feed chan Entry
|
||||
}
|
||||
|
||||
func NewMiniflux(mflInfo MinifluxInfo, logger *slog.Logger) *Miniflux {
|
||||
return &Miniflux{
|
||||
client: client.New(mflInfo.Endpoint, mflInfo.ApiKey),
|
||||
interval: mflInfo.Interval,
|
||||
logger: logger,
|
||||
feed: make(chan Entry),
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Miniflux) Run() {
|
||||
ticker := time.NewTicker(5 * time.Minute)
|
||||
ticker := time.NewTicker(m.interval)
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
|
@ -48,7 +51,9 @@ func (m *Miniflux) Run() {
|
|||
|
||||
for _, entry := range entries {
|
||||
m.feed <- entry
|
||||
m.MarkRead(entry.ID)
|
||||
if err := m.MarkRead(entry.ID); err != nil {
|
||||
m.logger.Error("error marking entry as read", slog.String("error", err.Error()))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -64,14 +69,14 @@ func (m *Miniflux) Unread() ([]Entry, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
entries := []Entry{}
|
||||
var entries []Entry
|
||||
for _, entry := range result.Entries {
|
||||
entries = append(entries, Entry{
|
||||
ID: entry.ID,
|
||||
Title: entry.Title,
|
||||
Description: entry.Content,
|
||||
URL: entry.URL,
|
||||
Comments: entry.CommentsURL,
|
||||
CommentsURL: entry.CommentsURL,
|
||||
FeedTitle: entry.Feed.Title,
|
||||
})
|
||||
}
|
||||
|
|
7
main.go
7
main.go
|
@ -3,6 +3,7 @@ package main
|
|||
import (
|
||||
"os"
|
||||
"os/signal"
|
||||
"time"
|
||||
|
||||
"ewintr.nl/matrix-feedreader/bot"
|
||||
"golang.org/x/exp/slog"
|
||||
|
@ -12,9 +13,15 @@ func main() {
|
|||
|
||||
logger := slog.New(slog.NewTextHandler(os.Stderr, nil))
|
||||
|
||||
interval, err := time.ParseDuration(getParam("MINIFLUX_INTERVAL", "10m"))
|
||||
if err != nil {
|
||||
logger.Error("error parsing interval: %v", slog.String("error", err.Error()))
|
||||
os.Exit(1)
|
||||
}
|
||||
mflxConfig := bot.MinifluxInfo{
|
||||
Endpoint: getParam("MINIFLUX_ENDPOINT", "http://localhost:8080"),
|
||||
ApiKey: getParam("MINIFLUX_API_KEY", "secret"),
|
||||
Interval: interval,
|
||||
}
|
||||
mflx := bot.NewMiniflux(mflxConfig, logger)
|
||||
|
||||
|
|
Loading…
Reference in New Issue