discord.go 2.6 KB

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