slack.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. // Slack Webhooks documentation: https://api.slack.com/messaging/webhooks
  4. package slack // import "miniflux.app/v2/internal/integration/slack"
  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 slackMsgColor = "#5865F2"
  20. type Client struct {
  21. webhookURL string
  22. }
  23. func NewClient(webhookURL string) *Client {
  24. return &Client{webhookURL: webhookURL}
  25. }
  26. func (c *Client) SendSlackMsg(feed *model.Feed, entries model.Entries) error {
  27. for _, entry := range entries {
  28. requestBody, err := json.Marshal(&slackMessage{
  29. Attachments: []slackAttachments{
  30. {
  31. Title: "RSS feed update from Miniflux",
  32. Color: slackMsgColor,
  33. Fields: []slackFields{
  34. {
  35. Title: "Updated feed",
  36. Value: feed.Title,
  37. },
  38. {
  39. Title: "Article title",
  40. Value: entry.Title,
  41. },
  42. {
  43. Title: "Article link",
  44. Value: entry.URL,
  45. },
  46. {
  47. Title: "Author",
  48. Value: entry.Author,
  49. Short: true,
  50. },
  51. {
  52. Title: "Source website",
  53. Value: urllib.RootURL(feed.SiteURL),
  54. Short: true,
  55. },
  56. },
  57. },
  58. },
  59. })
  60. if err != nil {
  61. return fmt.Errorf("slack: unable to encode request body: %v", err)
  62. }
  63. request, err := http.NewRequest(http.MethodPost, c.webhookURL, bytes.NewReader(requestBody))
  64. if err != nil {
  65. return fmt.Errorf("slack: unable to create request: %v", err)
  66. }
  67. request.Header.Set("Content-Type", "application/json")
  68. request.Header.Set("User-Agent", "Miniflux/"+version.Version)
  69. slog.Debug("Sending Slack notification",
  70. slog.String("webhookURL", c.webhookURL),
  71. slog.String("title", feed.Title),
  72. slog.String("entry_url", entry.URL),
  73. )
  74. httpClient := client.NewClientWithOptions(client.Options{Timeout: defaultClientTimeout, BlockPrivateNetworks: !config.Opts.IntegrationAllowPrivateNetworks()})
  75. response, err := httpClient.Do(request)
  76. if err != nil {
  77. return fmt.Errorf("slack: unable to send request: %v", err)
  78. }
  79. defer response.Body.Close()
  80. if response.StatusCode >= 400 {
  81. return fmt.Errorf("slack: unable to send a notification: url=%s status=%d", c.webhookURL, response.StatusCode)
  82. }
  83. }
  84. return nil
  85. }
  86. type slackFields struct {
  87. Title string `json:"title"`
  88. Value string `json:"value"`
  89. Short bool `json:"short,omitempty"`
  90. }
  91. type slackAttachments struct {
  92. Title string `json:"title"`
  93. Color string `json:"color"`
  94. Fields []slackFields `json:"fields"`
  95. }
  96. type slackMessage struct {
  97. Attachments []slackAttachments `json:"attachments"`
  98. }