forked from SunRed/discord-tweeter
* Update golang version to 1.24 * Update multiarch Dockerfile to be more ISA agnostic * Refactor existing code and properly structure project into modules * Get rid of global variables except where necessary (go:embed) * Add default values to Config * Add webserver with templates to finally correctly serve videos and gifs * Add tiny caching library to decrease api load and improve latency * Improve Webhook data preparation by filtering out redundant links from the tweet text and properly attaching videos and gifs in separate webhook request by utilising new webserver * Improve tests for filter function * Improve bake definition for easier CI integration
109 lines
2.9 KiB
Go
109 lines
2.9 KiB
Go
package tweeter
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"log"
|
|
"net/http"
|
|
"strings"
|
|
|
|
ts "github.com/imperatrona/twitter-scraper"
|
|
//"strconv"
|
|
)
|
|
|
|
const (
|
|
BaseIcon = "https://abs.twimg.com/icons/apple-touch-icon-192x192.png"
|
|
)
|
|
|
|
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 (app App) SendToWebhook(tweets []*ts.Tweet) {
|
|
for _, tweet := range tweets {
|
|
webhooksToSend := []*Webhook{}
|
|
data := Webhook{
|
|
Mention: &Mention{Parse: []string{"roles"}},
|
|
}
|
|
webhooksToSend = append(webhooksToSend, &data)
|
|
|
|
if len(tweet.Videos) > 0 {
|
|
webhooksToSend = append(webhooksToSend, &Webhook{
|
|
Content: "[\u2800](" + app.config.HostURL + "/video/" + tweet.ID + ")",
|
|
Mention: &Mention{Parse: []string{"roles"}},
|
|
})
|
|
}
|
|
|
|
mainEmbed := data.NewEmbedWithURL(tweet.PermanentURL)
|
|
mainEmbed.SetAuthor(tweet.Name+" (@"+tweet.Username+")", app.config.HostURL+"/tweet/"+tweet.ID, app.config.HostURL+"/avatar/"+tweet.Username)
|
|
mainEmbed.SetColor("#26a7de")
|
|
mainEmbed.SetTimestamp(tweet.TimeParsed)
|
|
//mainEmbed.SetFooter("Twitter", BaseIcon)
|
|
//mainEmbed.SetFooter("Twitter • <t:" + strconv.FormatInt((tweet.Timestamp), 10) + ":R>", BaseIcon)
|
|
|
|
tweetText := tweet.Text
|
|
for i, photo := range tweet.Photos {
|
|
embed := mainEmbed
|
|
if i > 0 {
|
|
embed = data.NewEmbedWithURL(tweet.PermanentURL)
|
|
}
|
|
embed.SetImage(photo.URL)
|
|
tweetText = strings.ReplaceAll(tweetText, photo.URL, "")
|
|
}
|
|
for _, gif := range tweet.GIFs {
|
|
//embed := mainEmbed
|
|
//if i > 0 {
|
|
// embed = data.NewEmbedWithURL(tweet.PermanentURL)
|
|
//}
|
|
//embed.SetImage(gif.Preview)
|
|
tweetText = strings.ReplaceAll(tweetText, gif.Preview, "")
|
|
tweetText = strings.ReplaceAll(tweetText, gif.URL, "")
|
|
}
|
|
for _, video := range tweet.Videos {
|
|
//embed := mainEmbed
|
|
//if i > 0 {
|
|
// embed = data.NewEmbedWithURL(tweet.PermanentURL)
|
|
//}
|
|
//embed.SetImage(video.Preview)
|
|
tweetText = strings.ReplaceAll(tweetText, video.Preview, "")
|
|
tweetText = strings.ReplaceAll(tweetText, video.URL, "")
|
|
}
|
|
|
|
mainEmbed.SetText(strings.TrimSpace(tweetText))
|
|
|
|
for _, data := range webhooksToSend {
|
|
err := sendRequest(app.config.Webhook, data)
|
|
if err != nil {
|
|
log.Printf("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
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
return nil
|
|
}
|