2023-06-19 23:42:47 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2022-10-14 17:18:44 +02:00
|
|
|
|
2023-08-11 04:46:45 +02:00
|
|
|
package matrixbot // import "miniflux.app/v2/internal/integration/matrixbot"
|
2022-10-14 17:18:44 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-09-10 01:16:45 +02:00
|
|
|
"strings"
|
2022-10-14 17:18:44 +02:00
|
|
|
|
2023-08-11 04:46:45 +02:00
|
|
|
"miniflux.app/v2/internal/model"
|
2022-10-14 17:18:44 +02:00
|
|
|
)
|
|
|
|
|
2024-04-10 08:27:01 +02:00
|
|
|
// PushEntries pushes entries to matrix chat using integration settings provided
|
2023-09-10 01:16:45 +02:00
|
|
|
func PushEntries(feed *model.Feed, entries model.Entries, matrixBaseURL, matrixUsername, matrixPassword, matrixRoomID string) error {
|
|
|
|
client := NewClient(matrixBaseURL)
|
|
|
|
discovery, err := client.DiscoverEndpoints()
|
2022-10-14 17:18:44 +02:00
|
|
|
if err != nil {
|
2023-09-10 01:16:45 +02:00
|
|
|
return err
|
2022-10-14 17:18:44 +02:00
|
|
|
}
|
|
|
|
|
2023-09-10 01:16:45 +02:00
|
|
|
loginResponse, err := client.Login(discovery.HomeServerInformation.BaseURL, matrixUsername, matrixPassword)
|
2022-10-14 17:18:44 +02:00
|
|
|
if err != nil {
|
2023-09-10 01:16:45 +02:00
|
|
|
return err
|
2022-10-14 17:18:44 +02:00
|
|
|
}
|
|
|
|
|
2023-09-10 01:16:45 +02:00
|
|
|
var textMessages []string
|
|
|
|
var formattedTextMessages []string
|
2022-10-14 17:18:44 +02:00
|
|
|
|
|
|
|
for _, entry := range entries {
|
2023-09-10 01:16:45 +02:00
|
|
|
textMessages = append(textMessages, fmt.Sprintf(`[%s] %s - %s`, feed.Title, entry.Title, entry.URL))
|
2024-02-29 00:27:39 +01:00
|
|
|
formattedTextMessages = append(formattedTextMessages, fmt.Sprintf(`<li><strong>%s</strong>: <a href=%q>%s</a></li>`, feed.Title, entry.URL, entry.Title))
|
2022-10-14 17:18:44 +02:00
|
|
|
}
|
|
|
|
|
2023-09-10 01:16:45 +02:00
|
|
|
_, err = client.SendFormattedTextMessage(
|
|
|
|
discovery.HomeServerInformation.BaseURL,
|
|
|
|
loginResponse.AccessToken,
|
|
|
|
matrixRoomID,
|
|
|
|
strings.Join(textMessages, "\n"),
|
|
|
|
"<ul>"+strings.Join(formattedTextMessages, "\n")+"</ul>",
|
|
|
|
)
|
2022-10-14 17:18:44 +02:00
|
|
|
|
2023-09-10 01:16:45 +02:00
|
|
|
return err
|
2022-10-14 17:18:44 +02:00
|
|
|
}
|