integration.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package integration // import "miniflux.app/v2/internal/integration"
  4. import (
  5. "miniflux.app/v2/internal/config"
  6. "miniflux.app/v2/internal/integration/apprise"
  7. "miniflux.app/v2/internal/integration/espial"
  8. "miniflux.app/v2/internal/integration/instapaper"
  9. "miniflux.app/v2/internal/integration/linkding"
  10. "miniflux.app/v2/internal/integration/matrixbot"
  11. "miniflux.app/v2/internal/integration/notion"
  12. "miniflux.app/v2/internal/integration/nunuxkeeper"
  13. "miniflux.app/v2/internal/integration/pinboard"
  14. "miniflux.app/v2/internal/integration/pocket"
  15. "miniflux.app/v2/internal/integration/readwise"
  16. "miniflux.app/v2/internal/integration/shaarli"
  17. "miniflux.app/v2/internal/integration/shiori"
  18. "miniflux.app/v2/internal/integration/telegrambot"
  19. "miniflux.app/v2/internal/integration/wallabag"
  20. "miniflux.app/v2/internal/integration/webhook"
  21. "miniflux.app/v2/internal/logger"
  22. "miniflux.app/v2/internal/model"
  23. )
  24. // SendEntry sends the entry to third-party providers when the user click on "Save".
  25. func SendEntry(entry *model.Entry, integration *model.Integration) {
  26. if integration.PinboardEnabled {
  27. logger.Debug("[Integration] Sending entry #%d %q for user #%d to Pinboard", entry.ID, entry.URL, integration.UserID)
  28. client := pinboard.NewClient(integration.PinboardToken)
  29. err := client.CreateBookmark(
  30. entry.URL,
  31. entry.Title,
  32. integration.PinboardTags,
  33. integration.PinboardMarkAsUnread,
  34. )
  35. if err != nil {
  36. logger.Error("[Integration] UserID #%d: %v", integration.UserID, err)
  37. }
  38. }
  39. if integration.InstapaperEnabled {
  40. logger.Debug("[Integration] Sending entry #%d %q for user #%d to Instapaper", entry.ID, entry.URL, integration.UserID)
  41. client := instapaper.NewClient(integration.InstapaperUsername, integration.InstapaperPassword)
  42. if err := client.AddURL(entry.URL, entry.Title); err != nil {
  43. logger.Error("[Integration] UserID #%d: %v", integration.UserID, err)
  44. }
  45. }
  46. if integration.WallabagEnabled {
  47. logger.Debug("[Integration] Sending entry #%d %q for user #%d to Wallabag", entry.ID, entry.URL, integration.UserID)
  48. client := wallabag.NewClient(
  49. integration.WallabagURL,
  50. integration.WallabagClientID,
  51. integration.WallabagClientSecret,
  52. integration.WallabagUsername,
  53. integration.WallabagPassword,
  54. integration.WallabagOnlyURL,
  55. )
  56. if err := client.CreateEntry(entry.URL, entry.Title, entry.Content); err != nil {
  57. logger.Error("[Integration] UserID #%d: %v", integration.UserID, err)
  58. }
  59. }
  60. if integration.NotionEnabled {
  61. logger.Debug("[Integration] Sending entry #%d %q for user #%d to Notion", entry.ID, entry.URL, integration.UserID)
  62. client := notion.NewClient(
  63. integration.NotionToken,
  64. integration.NotionPageID,
  65. )
  66. if err := client.UpdateDocument(entry.URL, entry.Title); err != nil {
  67. logger.Error("[Integration] UserID #%d: %v", integration.UserID, err)
  68. }
  69. }
  70. if integration.NunuxKeeperEnabled {
  71. logger.Debug("[Integration] Sending entry #%d %q for user #%d to NunuxKeeper", entry.ID, entry.URL, integration.UserID)
  72. client := nunuxkeeper.NewClient(
  73. integration.NunuxKeeperURL,
  74. integration.NunuxKeeperAPIKey,
  75. )
  76. if err := client.AddEntry(entry.URL, entry.Title, entry.Content); err != nil {
  77. logger.Error("[Integration] UserID #%d: %v", integration.UserID, err)
  78. }
  79. }
  80. if integration.EspialEnabled {
  81. logger.Debug("[Integration] Sending entry #%d %q for user #%d to Espial", entry.ID, entry.URL, integration.UserID)
  82. client := espial.NewClient(
  83. integration.EspialURL,
  84. integration.EspialAPIKey,
  85. )
  86. if err := client.CreateLink(entry.URL, entry.Title, integration.EspialTags); err != nil {
  87. logger.Error("[Integration] Unable to send entry #%d to Espial for user #%d: %v", entry.ID, integration.UserID, err)
  88. }
  89. }
  90. if integration.PocketEnabled {
  91. logger.Debug("[Integration] Sending entry #%d %q for user #%d to Pocket", entry.ID, entry.URL, integration.UserID)
  92. client := pocket.NewClient(config.Opts.PocketConsumerKey(integration.PocketConsumerKey), integration.PocketAccessToken)
  93. if err := client.AddURL(entry.URL, entry.Title); err != nil {
  94. logger.Error("[Integration] UserID #%d: %v", integration.UserID, err)
  95. }
  96. }
  97. if integration.LinkdingEnabled {
  98. logger.Debug("[Integration] Sending entry #%d %q for user #%d to Linkding", entry.ID, entry.URL, integration.UserID)
  99. client := linkding.NewClient(
  100. integration.LinkdingURL,
  101. integration.LinkdingAPIKey,
  102. integration.LinkdingTags,
  103. integration.LinkdingMarkAsUnread,
  104. )
  105. if err := client.CreateBookmark(entry.URL, entry.Title); err != nil {
  106. logger.Error("[Integration] UserID #%d: %v", integration.UserID, err)
  107. }
  108. }
  109. if integration.ReadwiseEnabled {
  110. logger.Debug("[Integration] Sending entry #%d %q for user #%d to Readwise Reader", entry.ID, entry.URL, integration.UserID)
  111. client := readwise.NewClient(
  112. integration.ReadwiseAPIKey,
  113. )
  114. if err := client.CreateDocument(entry.URL); err != nil {
  115. logger.Error("[Integration] UserID #%d: %v", integration.UserID, err)
  116. }
  117. }
  118. if integration.ShioriEnabled {
  119. logger.Debug("[Integration] Sending entry #%d %q for user #%d to Shiori", entry.ID, entry.URL, integration.UserID)
  120. client := shiori.NewClient(
  121. integration.ShioriURL,
  122. integration.ShioriUsername,
  123. integration.ShioriPassword,
  124. )
  125. if err := client.CreateBookmark(entry.URL, entry.Title); err != nil {
  126. logger.Error("[Integration] Unable to send entry #%d to Shiori for user #%d: %v", entry.ID, integration.UserID, err)
  127. }
  128. }
  129. if integration.ShaarliEnabled {
  130. logger.Debug("[Integration] Sending entry #%d %q for user #%d to Shaarli", entry.ID, entry.URL, integration.UserID)
  131. client := shaarli.NewClient(
  132. integration.ShaarliURL,
  133. integration.ShaarliAPISecret,
  134. )
  135. if err := client.CreateLink(entry.URL, entry.Title); err != nil {
  136. logger.Error("[Integration] Unable to send entry #%d to Shaarli for user #%d: %v", entry.ID, integration.UserID, err)
  137. }
  138. }
  139. if integration.WebhookEnabled {
  140. logger.Debug("[Integration] Sending entry #%d %q for user #%d to Webhook URL: %s", entry.ID, entry.URL, integration.UserID, integration.WebhookURL)
  141. webhookClient := webhook.NewClient(integration.WebhookURL, integration.WebhookSecret)
  142. if err := webhookClient.SendSaveEntryWebhookEvent(entry); err != nil {
  143. logger.Error("[Integration] UserID #%d: %v", integration.UserID, err)
  144. }
  145. }
  146. }
  147. // PushEntries pushes a list of entries to activated third-party providers during feed refreshes.
  148. func PushEntries(feed *model.Feed, entries model.Entries, userIntegrations *model.Integration) {
  149. if userIntegrations.MatrixBotEnabled {
  150. logger.Debug("[Integration] Sending %d entries for user #%d to Matrix", len(entries), userIntegrations.UserID)
  151. err := matrixbot.PushEntries(feed, entries, userIntegrations.MatrixBotURL, userIntegrations.MatrixBotUser, userIntegrations.MatrixBotPassword, userIntegrations.MatrixBotChatID)
  152. if err != nil {
  153. logger.Error("[Integration] push entries to matrix bot failed: %v", err)
  154. }
  155. }
  156. if userIntegrations.WebhookEnabled {
  157. logger.Debug("[Integration] Sending %d entries for user #%d to Webhook URL: %s", len(entries), userIntegrations.UserID, userIntegrations.WebhookURL)
  158. webhookClient := webhook.NewClient(userIntegrations.WebhookURL, userIntegrations.WebhookSecret)
  159. if err := webhookClient.SendNewEntriesWebhookEvent(feed, entries); err != nil {
  160. logger.Error("[Integration] sending entries to webhook failed: %v", err)
  161. }
  162. }
  163. // Integrations that only support sending individual entries
  164. if userIntegrations.TelegramBotEnabled || userIntegrations.AppriseEnabled {
  165. for _, entry := range entries {
  166. if userIntegrations.TelegramBotEnabled {
  167. logger.Debug("[Integration] Sending entry %q for user #%d to Telegram", entry.URL, userIntegrations.UserID)
  168. if err := telegrambot.PushEntry(
  169. feed,
  170. entry,
  171. userIntegrations.TelegramBotToken,
  172. userIntegrations.TelegramBotChatID,
  173. userIntegrations.TelegramBotTopicID,
  174. userIntegrations.TelegramBotDisableWebPagePreview,
  175. userIntegrations.TelegramBotDisableNotification,
  176. ); err != nil {
  177. logger.Error("[Integration] %v", err)
  178. }
  179. }
  180. if userIntegrations.AppriseEnabled {
  181. logger.Debug("[Integration] Sending entry %q for user #%d to Apprise", entry.URL, userIntegrations.UserID)
  182. appriseServiceURLs := userIntegrations.AppriseURL
  183. if feed.AppriseServiceURLs != "" {
  184. appriseServiceURLs = feed.AppriseServiceURLs
  185. }
  186. client := apprise.NewClient(
  187. userIntegrations.AppriseServicesURL,
  188. appriseServiceURLs,
  189. )
  190. if err := client.SendNotification(entry); err != nil {
  191. logger.Error("[Integration] %v", err)
  192. }
  193. }
  194. }
  195. }
  196. }