feat: Add embeds, rework embed struct

This commit is contained in:
Manuel 2023-10-17 00:10:00 +02:00
parent 712d83c0f0
commit e6bf12fa35
Signed by: Manuel
GPG key ID: 4085037435E1F07A
2 changed files with 129 additions and 54 deletions

View file

@ -4,18 +4,21 @@ import (
"errors" "errors"
"strconv" "strconv"
"strings" "strings"
"time"
) )
type Embed struct { type Embed struct {
Author Author `json:"author"` Author Author `json:"author,omitempty"`
Title string `json:"title"` Title string `json:"title,omitempty"`
URL string `json:"url"` URL string `json:"url,omitempty"`
Description string `json:"description"` Description string `json:"description,omitempty"`
Color int64 `json:"color"` Timestamp string `json:"timestamp,omitempty"`
Fields []Field `json:"fields"` Color int64 `json:"color,omitempty"`
Fields []Field `json:"fields,omitempty"`
Thumbnail Image `json:"thumbnail,omitempty"` Thumbnail Image `json:"thumbnail,omitempty"`
Image Image `json:"image,omitempty"` Image Image `json:"image,omitempty"`
Footer Footer `json:"footer"` Video Video `json:"video,omitempty"`
Footer Footer `json:"footer,omitempty"`
} }
type Author struct { type Author struct {
@ -39,60 +42,75 @@ type Image struct {
URL string `json:"url"` URL string `json:"url"`
} }
func (w *Webhook) NewEmbed(Title, Description, URL string) { type Video struct {
emb := Embed{Title: Title, Description: Description, URL: URL} URL string `json:"url"`
w.Embeds = append(w.Embeds, &emb)
} }
func (w *Webhook) SetAuthor(Name, URL, IconURL string) { func (w *Webhook) NewEmbedWithURL(URL string) *Embed {
if len(w.Embeds) == 0 { emb := Embed{URL: URL}
emb := Embed{Author: Author{Name, URL, IconURL}}
w.Embeds = append(w.Embeds, &emb) w.Embeds = append(w.Embeds, &emb)
} else { return &emb
w.Embeds[0].Author = Author{Name, URL, IconURL}
}
} }
func (w *Webhook) SetColor(color string) error { func (w *Webhook) NewEmbed() *Embed {
emb := Embed{}
w.Embeds = append(w.Embeds, &emb)
return &emb
}
func (e *Embed) SetTitle(Title string) *Embed {
e.Title = Title
return e
}
func (e *Embed) SetText(Description string) *Embed {
e.Description = Description
return e
}
func (e *Embed) SetAuthor(Name, URL, IconURL string) *Embed {
e.Author = Author{Name, URL, IconURL}
return e
}
func (e *Embed) SetColor(color string) (*Embed, error) {
color = strings.Replace(color, "0x", "", -1) color = strings.Replace(color, "0x", "", -1)
color = strings.Replace(color, "0X", "", -1) color = strings.Replace(color, "0X", "", -1)
color = strings.Replace(color, "#", "", -1) color = strings.Replace(color, "#", "", -1)
colorInt, err := strconv.ParseInt(color, 16, 64) colorInt, err := strconv.ParseInt(color, 16, 64)
if err != nil { if err != nil {
return errors.New("Invalid hex code passed") return nil, errors.New("Invalid hex code passed")
} }
w.Embeds[0].Color = colorInt e.Color = colorInt
return nil return e, nil
} }
func (w *Webhook) SetThumbnail(URL string) error { func (e *Embed) SetThumbnail(URL string) *Embed {
if len(w.Embeds) < 1 { e.Thumbnail = Image{URL}
return errors.New("Invalid Embed passed in, Webhook.Embeds must have at least one Embed element") return e
}
w.Embeds[0].Thumbnail = Image{URL}
return nil
} }
func (w *Webhook) SetImage(URL string) error { func (e *Embed) SetImage(URL string) *Embed {
if len(w.Embeds) < 1 { e.Image = Image{URL}
return errors.New("Invalid Embed passed in, Webhook.Embeds must have at least one Embed element") return e
}
w.Embeds[0].Image = Image{URL}
return nil
} }
func (w *Webhook) SetFooter(Text, IconURL string) error { func (e *Embed) SetVideo(URL string) *Embed {
if len(w.Embeds) < 1 { e.Video = Video{URL}
return errors.New("Invalid Embed passed in, Webhook.Embeds must have at least one Embed element") return e
}
w.Embeds[0].Footer = Footer{Text, IconURL}
return nil
} }
func (w *Webhook) AddField(Name, Value string, Inline bool) error { func (e *Embed) SetFooter(Text, IconURL string) *Embed {
if len(w.Embeds) < 1 { e.Footer = Footer{Text, IconURL}
return errors.New("Invalid Embed passed in, Webhook.Embeds must have at least one Embed element") return e
} }
w.Embeds[0].Fields = append(w.Embeds[0].Fields, Field{Name, Value, Inline})
return nil func (e *Embed) SetTimestamp(timestamp time.Time) *Embed {
e.Timestamp = timestamp.Format(time.RFC3339)
return e
}
func (e *Embed) AddField(Name, Value string, Inline bool) *Embed {
e.Fields = append(e.Fields, Field{Name, Value, Inline})
return e
} }

View file

@ -6,6 +6,13 @@ import (
ts "github.com/n0madic/twitter-scraper" ts "github.com/n0madic/twitter-scraper"
"log" "log"
"net/http" "net/http"
"strings"
//"strconv"
)
const (
BaseURL = "https://twitter.com/"
BaseIcon = "https://abs.twimg.com/icons/apple-touch-icon-192x192.png"
) )
type Webhook struct { type Webhook struct {
@ -25,24 +32,74 @@ type Mention struct {
func sendToWebhook(webhookURL string, tweets []*ts.Tweet) { func sendToWebhook(webhookURL string, tweets []*ts.Tweet) {
for _, tweet := range tweets { for _, tweet := range tweets {
// Temporarily hardcoded
data := Webhook{ data := Webhook{
Content: tweet.PermanentURL, Content: "<" + tweet.PermanentURL + ">",
Mention: &Mention{Parse: []string{"roles"}}, Mention: &Mention{Parse: []string{"roles"}},
Embeds: []*Embed{},
} }
urlsToAppend := []string{}
userUrl := BaseURL + tweet.Username
mainEmbed := data.NewEmbedWithURL(tweet.PermanentURL)
mainEmbed.SetAuthor(tweet.Name+" (@"+tweet.Username+")", userUrl, "https://unavatar.io/twitter/"+tweet.Username)
mainEmbed.SetText(tweet.Text)
mainEmbed.SetColor("#26a7de")
mainEmbed.SetFooter("Twitter", BaseIcon)
mainEmbed.SetTimestamp(tweet.TimeParsed)
//mainEmbed.SetFooter("Twitter • <t:" + strconv.FormatInt((tweet.Timestamp), 10) + ":R>", BaseIcon)
for i, photo := range tweet.Photos {
embed := mainEmbed
if i > 0 {
embed = data.NewEmbedWithURL(tweet.PermanentURL)
}
embed.SetImage(photo.URL)
}
for i, gif := range tweet.GIFs {
embed := mainEmbed
if i > 0 {
embed = data.NewEmbedWithURL(tweet.PermanentURL)
}
embed.SetImage(gif.Preview)
}
for i, video := range tweet.Videos {
embed := mainEmbed
if i > 0 {
embed = data.NewEmbedWithURL(tweet.PermanentURL)
}
// Video embeds are not supported right now
embed.SetImage(video.Preview)
embed.SetVideo(video.URL) // This has sadly no effect
urlsToAppend = append(urlsToAppend, strings.Replace(tweet.PermanentURL, "twitter", "fxtwitter", 1))
}
err := sendRequest(webhookURL, &data)
if err != nil {
log.Println("Error while sending webhook for tweet %s: %s", tweet.ID, err.Error())
continue
}
for _, url := range urlsToAppend {
err := sendRequest(webhookURL, &Webhook{Content: url})
if err != nil {
log.Println("Error while sending webhook for tweet %s: %s", tweet.ID, err.Error())
continue
}
}
}
}
func sendRequest(url string, data *Webhook) error {
jsonData, err := json.Marshal(data) jsonData, err := json.Marshal(data)
if err != nil { if err != nil {
log.Printf("Error while generating JSON for tweet %s: %s", tweet.ID, err.Error()) return err
continue
} }
resp, err := http.Post(webhookURL, "application/json", bytes.NewBuffer(jsonData)) resp, err := http.Post(url, "application/json", bytes.NewBuffer(jsonData))
if err != nil {
log.Printf("Error while sending webhook for tweet %s: %s", tweet.ID, err.Error())
continue
}
defer resp.Body.Close() defer resp.Body.Close()
if err != nil {
return err
} }
return nil
} }