From 3d84b0753269cc9a6534c32e2771878b8d2300be Mon Sep 17 00:00:00 2001 From: Frédéric Guillot Date: Sat, 9 Sep 2023 16:16:45 -0700 Subject: Add builtin Matrix client and send HTML formatted messages to Matrix - Add builtin Matrix client - Remove dependency on `gomatrix` client - Send HTML formatted messages to Matrix --- internal/integration/matrixbot/matrixbot.go | 45 ++++++++++++----------------- 1 file changed, 19 insertions(+), 26 deletions(-) (limited to 'internal/integration/matrixbot/matrixbot.go') diff --git a/internal/integration/matrixbot/matrixbot.go b/internal/integration/matrixbot/matrixbot.go index 7b3198ed..8b2c6961 100644 --- a/internal/integration/matrixbot/matrixbot.go +++ b/internal/integration/matrixbot/matrixbot.go @@ -5,46 +5,39 @@ package matrixbot // import "miniflux.app/v2/internal/integration/matrixbot" import ( "fmt" + "strings" - "miniflux.app/v2/internal/logger" "miniflux.app/v2/internal/model" - - "github.com/matrix-org/gomatrix" ) // PushEntry pushes entries to matrix chat using integration settings provided -func PushEntries(entries model.Entries, serverURL, botLogin, botPassword, chatID string) error { - bot, err := gomatrix.NewClient(serverURL, "", "") +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 fmt.Errorf("matrixbot: bot creation failed: %w", err) + return err } - resp, err := bot.Login(&gomatrix.ReqLogin{ - Type: "m.login.password", - User: botLogin, - Password: botPassword, - }) - + loginResponse, err := client.Login(discovery.HomeServerInformation.BaseURL, matrixUsername, matrixPassword) if err != nil { - logger.Debug("matrixbot: login failed: %w", err) - return fmt.Errorf("matrixbot: login failed, please check your credentials or turn on debug mode") + return err } - bot.SetCredentials(resp.UserID, resp.AccessToken) - defer func() { - bot.Logout() - bot.ClearCredentials() - }() + var textMessages []string + var formattedTextMessages []string - message := "" for _, entry := range entries { - message = message + entry.Title + " " + entry.URL + "\n" + textMessages = append(textMessages, fmt.Sprintf(`[%s] %s - %s`, feed.Title, entry.Title, entry.URL)) + formattedTextMessages = append(formattedTextMessages, fmt.Sprintf(`
  • %s: %s
  • `, feed.Title, entry.URL, entry.Title)) } - if _, err = bot.SendText(chatID, message); err != nil { - logger.Debug("matrixbot: sending message failed: %w", err) - return fmt.Errorf("matrixbot: sending message failed, turn on debug mode for more informations") - } + _, err = client.SendFormattedTextMessage( + discovery.HomeServerInformation.BaseURL, + loginResponse.AccessToken, + matrixRoomID, + strings.Join(textMessages, "\n"), + "", + ) - return nil + return err } -- cgit v1.2.3