aboutsummaryrefslogtreecommitdiff
path: root/internal/integration/matrixbot/matrixbot.go
blob: 8b2c6961019a08a3fafde566083f4b96f411232f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package matrixbot // import "miniflux.app/v2/internal/integration/matrixbot"

import (
	"fmt"
	"strings"

	"miniflux.app/v2/internal/model"
)

// PushEntry pushes entries to matrix chat using integration settings provided
func PushEntries(feed *model.Feed, entries model.Entries, matrixBaseURL, matrixUsername, matrixPassword, matrixRoomID string) error {
	client := NewClient(matrixBaseURL)
	discovery, err := client.DiscoverEndpoints()
	if err != nil {
		return err
	}

	loginResponse, err := client.Login(discovery.HomeServerInformation.BaseURL, matrixUsername, matrixPassword)
	if err != nil {
		return err
	}

	var textMessages []string
	var formattedTextMessages []string

	for _, entry := range entries {
		textMessages = append(textMessages, fmt.Sprintf(`[%s] %s - %s`, feed.Title, entry.Title, entry.URL))
		formattedTextMessages = append(formattedTextMessages, fmt.Sprintf(`<li><strong>%s</strong>: <a href="%s">%s</a></li>`, feed.Title, entry.URL, entry.Title))
	}

	_, err = client.SendFormattedTextMessage(
		discovery.HomeServerInformation.BaseURL,
		loginResponse.AccessToken,
		matrixRoomID,
		strings.Join(textMessages, "\n"),
		"<ul>"+strings.Join(formattedTextMessages, "\n")+"</ul>",
	)

	return err
}