package cmd import ( "bytes" "encoding/json" ts "github.com/imperatrona/twitter-scraper" "log" "net/http" "strings" //"strconv" ) const ( BaseURL = "https://twitter.com/" 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 sendToWebhook(webhookURL string, tweets []*ts.Tweet) { for _, tweet := range tweets { data := Webhook{ Content: "<" + tweet.PermanentURL + ">", Mention: &Mention{Parse: []string{"roles"}}, } 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 • ", 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) 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 }