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() {
|
func (m *Matrix) PostMessages() {
|
||||||
for entry := range m.feedReader.Feed() {
|
for entry := range m.feedReader.Feed() {
|
||||||
m.logger.Info("received entry", slog.String("title", entry.Title), slog.String("url", entry.URL))
|
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()))
|
m.logger.Error("failed to send message", slog.String("err", err.Error()))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,31 +12,34 @@ type Entry struct {
|
||||||
Title string
|
Title string
|
||||||
Description string
|
Description string
|
||||||
URL string
|
URL string
|
||||||
Comments string
|
CommentsURL string
|
||||||
FeedTitle string
|
FeedTitle string
|
||||||
}
|
}
|
||||||
|
|
||||||
type MinifluxInfo struct {
|
type MinifluxInfo struct {
|
||||||
Endpoint string
|
Endpoint string
|
||||||
ApiKey string
|
ApiKey string
|
||||||
|
Interval time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
type Miniflux struct {
|
type Miniflux struct {
|
||||||
client *client.Client
|
client *client.Client
|
||||||
logger *slog.Logger
|
logger *slog.Logger
|
||||||
feed chan Entry
|
interval time.Duration
|
||||||
|
feed chan Entry
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewMiniflux(mflInfo MinifluxInfo, logger *slog.Logger) *Miniflux {
|
func NewMiniflux(mflInfo MinifluxInfo, logger *slog.Logger) *Miniflux {
|
||||||
return &Miniflux{
|
return &Miniflux{
|
||||||
client: client.New(mflInfo.Endpoint, mflInfo.ApiKey),
|
client: client.New(mflInfo.Endpoint, mflInfo.ApiKey),
|
||||||
logger: logger,
|
interval: mflInfo.Interval,
|
||||||
feed: make(chan Entry),
|
logger: logger,
|
||||||
|
feed: make(chan Entry),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Miniflux) Run() {
|
func (m *Miniflux) Run() {
|
||||||
ticker := time.NewTicker(5 * time.Minute)
|
ticker := time.NewTicker(m.interval)
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ticker.C:
|
case <-ticker.C:
|
||||||
|
@ -48,7 +51,9 @@ func (m *Miniflux) Run() {
|
||||||
|
|
||||||
for _, entry := range entries {
|
for _, entry := range entries {
|
||||||
m.feed <- entry
|
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
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
entries := []Entry{}
|
var entries []Entry
|
||||||
for _, entry := range result.Entries {
|
for _, entry := range result.Entries {
|
||||||
entries = append(entries, Entry{
|
entries = append(entries, Entry{
|
||||||
ID: entry.ID,
|
ID: entry.ID,
|
||||||
Title: entry.Title,
|
Title: entry.Title,
|
||||||
Description: entry.Content,
|
Description: entry.Content,
|
||||||
URL: entry.URL,
|
URL: entry.URL,
|
||||||
Comments: entry.CommentsURL,
|
CommentsURL: entry.CommentsURL,
|
||||||
FeedTitle: entry.Feed.Title,
|
FeedTitle: entry.Feed.Title,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
7
main.go
7
main.go
|
@ -3,6 +3,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
"time"
|
||||||
|
|
||||||
"ewintr.nl/matrix-feedreader/bot"
|
"ewintr.nl/matrix-feedreader/bot"
|
||||||
"golang.org/x/exp/slog"
|
"golang.org/x/exp/slog"
|
||||||
|
@ -12,9 +13,15 @@ func main() {
|
||||||
|
|
||||||
logger := slog.New(slog.NewTextHandler(os.Stderr, nil))
|
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{
|
mflxConfig := bot.MinifluxInfo{
|
||||||
Endpoint: getParam("MINIFLUX_ENDPOINT", "http://localhost:8080"),
|
Endpoint: getParam("MINIFLUX_ENDPOINT", "http://localhost:8080"),
|
||||||
ApiKey: getParam("MINIFLUX_API_KEY", "secret"),
|
ApiKey: getParam("MINIFLUX_API_KEY", "secret"),
|
||||||
|
Interval: interval,
|
||||||
}
|
}
|
||||||
mflx := bot.NewMiniflux(mflxConfig, logger)
|
mflx := bot.NewMiniflux(mflxConfig, logger)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue