discord-tweeter/cmd/webhook.go

106 lines
2.7 KiB
Go
Raw Permalink Normal View History

2023-08-29 18:00:00 +02:00
package cmd
import (
"bytes"
"encoding/json"
ts "github.com/imperatrona/twitter-scraper"
2023-08-29 18:00:00 +02:00
"log"
"net/http"
2023-10-17 00:10:00 +02:00
"strings"
//"strconv"
)
const (
BaseURL = "https://twitter.com/"
BaseIcon = "https://abs.twimg.com/icons/apple-touch-icon-192x192.png"
2023-08-29 18:00:00 +02:00
)
type Webhook struct {
Content string `json:"content"`
Name string `json:"username,omitempty"`
Avatar string `json:"avatar_url,omitempty"`
Mention *Mention `json:"allowed_mentions,omitempty"`
Embeds []*Embed `json:"embeds,omitempty"`
}
type Mention struct {
Parse []string `json:"parse,omitempty"`
Roles []string `json:"roles,omitempty"`
Users []string `json:"users,omitempty"`
RepliedUser bool `json:"replied_user,omitempty"`
}
func sendToWebhook(webhookURL string, tweets []*ts.Tweet) {
for _, tweet := range tweets {
data := Webhook{
2023-10-17 00:10:00 +02:00
Content: "<" + tweet.PermanentURL + ">",
2023-08-29 18:00:00 +02:00
Mention: &Mention{Parse: []string{"roles"}},
}
2023-10-17 00:10:00 +02:00
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))
2023-08-29 18:00:00 +02:00
}
2023-10-17 00:10:00 +02:00
err := sendRequest(webhookURL, &data)
2023-08-29 18:00:00 +02:00
if err != nil {
2023-10-17 00:10:00 +02:00
log.Println("Error while sending webhook for tweet %s: %s", tweet.ID, err.Error())
2023-08-29 18:00:00 +02:00
continue
}
2023-10-17 00:10:00 +02:00
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)
if err != nil {
return err
}
resp, err := http.Post(url, "application/json", bytes.NewBuffer(jsonData))
if err != nil {
return err
2023-08-29 18:00:00 +02:00
}
2024-04-27 22:20:03 +02:00
defer resp.Body.Close()
2023-10-17 00:10:00 +02:00
return nil
2023-08-29 18:00:00 +02:00
}