discord-tweeter/cmd/webhook.go
2023-08-29 18:00:00 +02:00

48 lines
1.2 KiB
Go

package cmd
import (
"bytes"
"encoding/json"
ts "github.com/n0madic/twitter-scraper"
"log"
"net/http"
)
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 {
// Temporarily hardcoded
data := Webhook{
Content: tweet.PermanentURL,
Mention: &Mention{Parse: []string{"roles"}},
Embeds: []*Embed{},
}
jsonData, err := json.Marshal(data)
if err != nil {
log.Printf("Error while generating JSON for tweet %s: %s", tweet.ID, err.Error())
continue
}
resp, err := http.Post(webhookURL, "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()
}
}