discord.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. // Discord Webhooks documentation: https://discord.com/developers/docs/resources/webhook
  4. package discord // import "miniflux.app/v2/internal/integration/discord"
  5. import (
  6. "bytes"
  7. "encoding/json"
  8. "fmt"
  9. "log/slog"
  10. "net/http"
  11. "time"
  12. "miniflux.app/v2/internal/config"
  13. "miniflux.app/v2/internal/http/client"
  14. "miniflux.app/v2/internal/model"
  15. "miniflux.app/v2/internal/urllib"
  16. "miniflux.app/v2/internal/version"
  17. )
  18. const defaultClientTimeout = 10 * time.Second
  19. const discordMsgColor = 5793266
  20. type Client struct {
  21. webhookURL string
  22. }
  23. func NewClient(webhookURL string) *Client {
  24. return &Client{webhookURL: webhookURL}
  25. }
  26. func (c *Client) SendDiscordMsg(feed *model.Feed, entries model.Entries) error {
  27. for _, entry := range entries {
  28. requestBody, err := json.Marshal(&discordMessage{
  29. Embeds: []discordEmbed{
  30. {
  31. Title: "RSS feed update from Miniflux",
  32. Color: discordMsgColor,
  33. Fields: []discordFields{
  34. {
  35. Name: "Updated feed",
  36. Value: feed.Title,
  37. },
  38. {
  39. Name: "Article link",
  40. Value: "[" + entry.Title + "]" + "(" + entry.URL + ")",
  41. },
  42. {
  43. Name: "Author",
  44. Value: entry.Author,
  45. Inline: true,
  46. },
  47. {
  48. Name: "Source website",
  49. Value: urllib.RootURL(feed.SiteURL),
  50. Inline: true,
  51. },
  52. },
  53. },
  54. },
  55. })
  56. if err != nil {
  57. return fmt.Errorf("discord: unable to encode request body: %v", err)
  58. }
  59. request, err := http.NewRequest(http.MethodPost, c.webhookURL, bytes.NewReader(requestBody))
  60. if err != nil {
  61. return fmt.Errorf("discord: unable to create request: %v", err)
  62. }
  63. request.Header.Set("Content-Type", "application/json")
  64. request.Header.Set("User-Agent", "Miniflux/"+version.Version)
  65. slog.Debug("Sending Discord notification",
  66. slog.String("webhookURL", c.webhookURL),
  67. slog.String("title", feed.Title),
  68. slog.String("entry_url", entry.URL),
  69. )
  70. httpClient := client.NewClientWithOptions(client.Options{Timeout: defaultClientTimeout, BlockPrivateNetworks: !config.Opts.IntegrationAllowPrivateNetworks()})
  71. response, err := httpClient.Do(request)
  72. if err != nil {
  73. return fmt.Errorf("discord: unable to send request: %v", err)
  74. }
  75. defer response.Body.Close()
  76. if response.StatusCode >= 400 {
  77. return fmt.Errorf("discord: unable to send a notification: url=%s status=%d", c.webhookURL, response.StatusCode)
  78. }
  79. }
  80. return nil
  81. }
  82. type discordFields struct {
  83. Name string `json:"name"`
  84. Value string `json:"value"`
  85. Inline bool `json:"inline,omitempty"`
  86. }
  87. type discordEmbed struct {
  88. Title string `json:"title"`
  89. Color int `json:"color"`
  90. Fields []discordFields `json:"fields"`
  91. }
  92. type discordMessage struct {
  93. Embeds []discordEmbed `json:"embeds"`
  94. }