2022-06-30 14:54:58 -07:00
|
|
|
package output
|
|
|
|
|
|
|
|
import (
|
2022-07-12 15:28:31 -07:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2022-06-30 14:54:58 -07:00
|
|
|
"strings"
|
|
|
|
|
2024-04-23 07:15:38 -07:00
|
|
|
"git.jamestombleson.com/jtom38/newsbot-api/internal/database"
|
2022-06-30 14:54:58 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type discordField struct {
|
2022-07-12 15:28:31 -07:00
|
|
|
Name *string `json:"name,omitempty"`
|
|
|
|
Value *string `json:"value,omitempty"`
|
|
|
|
Inline *bool `json:"inline,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type discordFooter struct {
|
|
|
|
Value *string `json:"text,omitempty"`
|
|
|
|
IconUrl *string `json:"icon_url,omitempty"`
|
2022-06-30 14:54:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type discordAuthor struct {
|
2022-07-12 15:28:31 -07:00
|
|
|
Name *string `json:"name,omitempty"`
|
|
|
|
Url *string `json:"url,omitempty"`
|
|
|
|
IconUrl *string `json:"icon_url,omitempty"`
|
2022-06-30 14:54:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type discordImage struct {
|
2022-07-12 15:28:31 -07:00
|
|
|
Url *string `json:"url,omitempty"`
|
2022-06-30 14:54:58 -07:00
|
|
|
}
|
|
|
|
|
2022-07-12 15:28:31 -07:00
|
|
|
type DiscordEmbed struct {
|
|
|
|
Title *string `json:"title,omitempty"`
|
|
|
|
Description *string `json:"description,omitempty"`
|
|
|
|
Url *string `json:"url,omitempty"`
|
|
|
|
Color *int32 `json:"color,omitempty"`
|
|
|
|
//Timestamp time.Time `json:"timestamp,omitempty"`
|
|
|
|
Fields []*discordField `json:"fields,omitempty"`
|
|
|
|
Author discordAuthor `json:"author,omitempty"`
|
|
|
|
Image discordImage `json:"image,omitempty"`
|
|
|
|
Thumbnail discordImage `json:"thumbnail,omitempty"`
|
|
|
|
Footer *discordFooter `json:"footer,omitempty"`
|
2022-06-30 14:54:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Root object for Discord Webhook messages
|
2022-07-12 15:28:31 -07:00
|
|
|
type DiscordMessage struct {
|
|
|
|
Username *string `json:"username,omitempty"`
|
|
|
|
Content *string `json:"content,omitempty"`
|
|
|
|
Embeds *[]DiscordEmbed `json:"embeds,omitempty"`
|
2022-06-30 14:54:58 -07:00
|
|
|
}
|
|
|
|
|
2022-07-12 15:28:31 -07:00
|
|
|
const (
|
|
|
|
DefaultColor = 0
|
|
|
|
YoutubeColor = 16711680
|
|
|
|
TwitchColor = 0
|
|
|
|
RedditColor = 0
|
|
|
|
TwitterColor = 0
|
|
|
|
FfxivColor = 0
|
|
|
|
)
|
|
|
|
|
2022-06-30 14:54:58 -07:00
|
|
|
type Discord struct {
|
|
|
|
Subscriptions []string
|
2022-07-12 15:28:31 -07:00
|
|
|
article database.Article
|
|
|
|
Message *DiscordMessage
|
2022-06-30 14:54:58 -07:00
|
|
|
}
|
|
|
|
|
2022-07-12 15:28:31 -07:00
|
|
|
func NewDiscordWebHookMessage(Article database.Article) Discord {
|
2022-06-30 14:54:58 -07:00
|
|
|
return Discord{
|
|
|
|
article: Article,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-12 15:28:31 -07:00
|
|
|
// Generates the link field to expose in the message
|
|
|
|
func (dwh Discord) getFields() []*discordField {
|
|
|
|
var fields []*discordField
|
|
|
|
|
|
|
|
key := "Link"
|
|
|
|
linkField := discordField{
|
|
|
|
Name: &key,
|
|
|
|
Value: &dwh.article.Url,
|
|
|
|
}
|
|
|
|
|
|
|
|
fields = append(fields, &linkField)
|
|
|
|
|
|
|
|
return fields
|
|
|
|
}
|
|
|
|
|
|
|
|
// This will create the message that will be sent out.
|
|
|
|
func (dwh Discord) GeneratePayload() (*DiscordMessage, error) {
|
|
|
|
|
|
|
|
// Create the embed
|
|
|
|
footerMessage := "Brought to you by Newsbot"
|
|
|
|
footerUrl := ""
|
|
|
|
description := dwh.convertFromHtml(dwh.article.Description)
|
|
|
|
color := dwh.getColor(dwh.article.Url)
|
|
|
|
|
|
|
|
embed := DiscordEmbed{
|
|
|
|
Title: &dwh.article.Title,
|
|
|
|
Description: &description,
|
|
|
|
Image: discordImage{
|
|
|
|
Url: &dwh.article.Thumbnail,
|
|
|
|
},
|
|
|
|
Fields: dwh.getFields(),
|
|
|
|
Footer: &discordFooter{
|
|
|
|
Value: &footerMessage,
|
|
|
|
IconUrl: &footerUrl,
|
2022-06-30 14:54:58 -07:00
|
|
|
},
|
2022-07-12 15:28:31 -07:00
|
|
|
Color: &color,
|
2022-06-30 14:54:58 -07:00
|
|
|
}
|
|
|
|
|
2022-07-12 15:28:31 -07:00
|
|
|
// attach the embed to an array
|
|
|
|
var embedArray []DiscordEmbed
|
|
|
|
embedArray = append(embedArray, embed)
|
|
|
|
|
|
|
|
// create the base message
|
|
|
|
msg := DiscordMessage{
|
|
|
|
Embeds: &embedArray,
|
|
|
|
}
|
|
|
|
|
|
|
|
return &msg, nil
|
2022-06-30 14:54:58 -07:00
|
|
|
}
|
|
|
|
|
2022-07-12 15:28:31 -07:00
|
|
|
func (dwh Discord) SendPayload(Message *DiscordMessage, Url string) error {
|
|
|
|
// Convert the message to a io.reader object
|
|
|
|
buffer := new(bytes.Buffer)
|
|
|
|
json.NewEncoder(buffer).Encode(Message)
|
|
|
|
|
|
|
|
// Send the message
|
|
|
|
resp, err := http.Post(Url, "application/json", buffer)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for 204
|
|
|
|
if resp.StatusCode != 204 {
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
errMsg, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf(string(errMsg))
|
|
|
|
}
|
|
|
|
|
2022-06-30 14:54:58 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dwh Discord) convertFromHtml(body string) string {
|
|
|
|
clean := body
|
|
|
|
clean = strings.ReplaceAll(clean, "<h2>", "**")
|
|
|
|
clean = strings.ReplaceAll(clean, "</h2>", "**")
|
|
|
|
clean = strings.ReplaceAll(clean, "<h3>", "**")
|
|
|
|
clean = strings.ReplaceAll(clean, "</h3>", "**\r\n")
|
|
|
|
clean = strings.ReplaceAll(clean, "<strong>", "**")
|
|
|
|
clean = strings.ReplaceAll(clean, "</strong>", "**\r\n")
|
|
|
|
clean = strings.ReplaceAll(clean, "<ul>", "\r\n")
|
|
|
|
clean = strings.ReplaceAll(clean, "</ul>", "")
|
|
|
|
clean = strings.ReplaceAll(clean, "</li>", "\r\n")
|
|
|
|
clean = strings.ReplaceAll(clean, "<li>", "> ")
|
|
|
|
clean = strings.ReplaceAll(clean, "“", "\"")
|
|
|
|
clean = strings.ReplaceAll(clean, "”", "\"")
|
|
|
|
clean = strings.ReplaceAll(clean, "…", "...")
|
|
|
|
clean = strings.ReplaceAll(clean, "<b>", "**")
|
|
|
|
clean = strings.ReplaceAll(clean, "</b>", "**")
|
|
|
|
clean = strings.ReplaceAll(clean, "<br>", "\r\n")
|
|
|
|
clean = strings.ReplaceAll(clean, "<br/>", "\r\n")
|
|
|
|
clean = strings.ReplaceAll(clean, "\xe2\x96\xa0", "*")
|
|
|
|
clean = strings.ReplaceAll(clean, "\xa0", "\r\n")
|
|
|
|
clean = strings.ReplaceAll(clean, "<p>", "")
|
|
|
|
clean = strings.ReplaceAll(clean, "</p>", "\r\n")
|
|
|
|
return clean
|
|
|
|
}
|
|
|
|
|
2022-07-12 15:28:31 -07:00
|
|
|
func (dwh *Discord) getColor(Url string) int32 {
|
|
|
|
if strings.Contains(Url, "youtube.com") {
|
|
|
|
return YoutubeColor
|
|
|
|
}
|
|
|
|
|
|
|
|
return DefaultColor
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dwh *Discord) convertLinks(body string) string {
|
2022-06-30 14:54:58 -07:00
|
|
|
//items := regexp.MustCompile("<a(.*?)a>")
|
|
|
|
return ""
|
2022-07-12 15:28:31 -07:00
|
|
|
}
|