slack.go 2.6 KB

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