matrixbot.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package matrixbot // import "miniflux.app/v2/internal/integration/matrixbot"
  4. import (
  5. "fmt"
  6. "miniflux.app/v2/internal/logger"
  7. "miniflux.app/v2/internal/model"
  8. "github.com/matrix-org/gomatrix"
  9. )
  10. // PushEntry pushes entries to matrix chat using integration settings provided
  11. func PushEntries(entries model.Entries, serverURL, botLogin, botPassword, chatID string) error {
  12. bot, err := gomatrix.NewClient(serverURL, "", "")
  13. if err != nil {
  14. return fmt.Errorf("matrixbot: bot creation failed: %w", err)
  15. }
  16. resp, err := bot.Login(&gomatrix.ReqLogin{
  17. Type: "m.login.password",
  18. User: botLogin,
  19. Password: botPassword,
  20. })
  21. if err != nil {
  22. logger.Debug("matrixbot: login failed: %w", err)
  23. return fmt.Errorf("matrixbot: login failed, please check your credentials or turn on debug mode")
  24. }
  25. bot.SetCredentials(resp.UserID, resp.AccessToken)
  26. defer func() {
  27. bot.Logout()
  28. bot.ClearCredentials()
  29. }()
  30. message := ""
  31. for _, entry := range entries {
  32. message = message + entry.Title + " " + entry.URL + "\n"
  33. }
  34. if _, err = bot.SendText(chatID, message); err != nil {
  35. logger.Debug("matrixbot: sending message failed: %w", err)
  36. return fmt.Errorf("matrixbot: sending message failed, turn on debug mode for more informations")
  37. }
  38. return nil
  39. }