-
Notifications
You must be signed in to change notification settings - Fork 736
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(integration-discord): add discord integration
- Loading branch information
1 parent
79ec6ef
commit 64497f4
Showing
26 changed files
with
199 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// Discord Webhooks documentation: https://discord.com/developers/docs/resources/webhook | ||
|
||
package discord // import "miniflux.app/v2/internal/integration/discord" | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"log/slog" | ||
"net/http" | ||
"time" | ||
|
||
"miniflux.app/v2/internal/model" | ||
"miniflux.app/v2/internal/version" | ||
) | ||
|
||
const defaultClientTimeout = 10 * time.Second | ||
const discordMsgColor = 5793266 | ||
|
||
type Client struct { | ||
webhookURL string | ||
} | ||
|
||
func NewClient(webhookURL string) *Client { | ||
return &Client{webhookURL: webhookURL} | ||
} | ||
|
||
func (c *Client) SendDiscordMsg(feed *model.Feed, entries model.Entries) error { | ||
|
||
for _, entry := range entries { | ||
requestBody, err := json.Marshal(&discordMessage{ | ||
Embeds: []discordEmbed{ | ||
{ | ||
Title: entry.Title, | ||
Url: entry.URL, | ||
Description: feed.Title, | ||
Color: discordMsgColor, | ||
Footer: &discordFooter{ | ||
Text: entry.Author + " " + "•" + " " + "Miniflux/" +version.Version, | ||
IconUrl: feed.IconURL, | ||
}, | ||
}, | ||
}, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("discord: unable to encode request body: %v", err) | ||
} | ||
|
||
request, err := http.NewRequest(http.MethodPost, c.webhookURL, bytes.NewReader(requestBody)) | ||
if err != nil { | ||
return fmt.Errorf("discord: unable to create request: %v", err) | ||
} | ||
|
||
request.Header.Set("Content-Type", "application/json") | ||
request.Header.Set("User-Agent", "Miniflux/"+version.Version) | ||
|
||
slog.Debug("Sending Discord notification", | ||
slog.String("webhookURL", c.webhookURL), | ||
slog.String("title", feed.Title), | ||
slog.String("entry_url", entry.URL), | ||
) | ||
|
||
httpClient := &http.Client{Timeout: defaultClientTimeout} | ||
response, err := httpClient.Do(request) | ||
if err != nil { | ||
return fmt.Errorf("discord: unable to send request: %v", err) | ||
} | ||
defer response.Body.Close() | ||
|
||
if response.StatusCode >= 400 { | ||
return fmt.Errorf("discord: unable to send a notification: url=%s status=%d", c.webhookURL, response.StatusCode) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
type discordFooter struct { | ||
Text string `json:"text,omitempty"` | ||
IconUrl string `json:"icon_url,omitempty"` | ||
} | ||
|
||
type discordEmbed struct { | ||
Title string `json:"title,omitempty"` | ||
Url string `json:"url,omitempty"` | ||
Description string `json:"description,omitempty"` | ||
Color int `json:"color,omitempty"` | ||
Footer *discordFooter `json:"footer,omitempty"` | ||
} | ||
|
||
type discordMessage struct { | ||
Username string `json:"username,omitempty"` | ||
AvatarUrl string `json:"avatar_url,omitempty"` | ||
Content string `json:"content,omitempty"` | ||
Embeds []discordEmbed `json:"embeds,omitempty"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.