feat: Add embeds, rework embed struct
This commit is contained in:
parent
712d83c0f0
commit
e6bf12fa35
2 changed files with 129 additions and 54 deletions
106
cmd/embed.go
106
cmd/embed.go
|
@ -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"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Webhook) NewEmbedWithURL(URL string) *Embed {
|
||||||
|
emb := Embed{URL: URL}
|
||||||
w.Embeds = append(w.Embeds, &emb)
|
w.Embeds = append(w.Embeds, &emb)
|
||||||
|
return &emb
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *Webhook) SetAuthor(Name, URL, IconURL string) {
|
func (w *Webhook) NewEmbed() *Embed {
|
||||||
if len(w.Embeds) == 0 {
|
emb := Embed{}
|
||||||
emb := Embed{Author: Author{Name, URL, IconURL}}
|
w.Embeds = append(w.Embeds, &emb)
|
||||||
w.Embeds = append(w.Embeds, &emb)
|
return &emb
|
||||||
} else {
|
|
||||||
w.Embeds[0].Author = Author{Name, URL, IconURL}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *Webhook) SetColor(color string) error {
|
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
|
||||||
}
|
}
|
||||||
|
|
|
@ -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{},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
jsonData, err := json.Marshal(data)
|
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 {
|
if err != nil {
|
||||||
log.Printf("Error while generating JSON for tweet %s: %s", tweet.ID, err.Error())
|
log.Println("Error while sending webhook for tweet %s: %s", tweet.ID, err.Error())
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := http.Post(webhookURL, "application/json", bytes.NewBuffer(jsonData))
|
for _, url := range urlsToAppend {
|
||||||
if err != nil {
|
err := sendRequest(webhookURL, &Webhook{Content: url})
|
||||||
log.Printf("Error while sending webhook for tweet %s: %s", tweet.ID, err.Error())
|
if err != nil {
|
||||||
continue
|
log.Println("Error while sending webhook for tweet %s: %s", tweet.ID, err.Error())
|
||||||
|
continue
|
||||||
|
}
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func sendRequest(url string, data *Webhook) error {
|
||||||
|
jsonData, err := json.Marshal(data)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := http.Post(url, "application/json", bytes.NewBuffer(jsonData))
|
||||||
|
defer resp.Body.Close()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue