Merge branch '4-send-logs-to-discord-channel-for-monitoring-purposes' into 'develop'
Send SN posts as embeds to Discord Closes #4 See merge request ekzyis/hnbot!3
This commit is contained in:
commit
073924066b
|
@ -1 +1,2 @@
|
||||||
SN_AUTH_COOKIE=
|
SN_AUTH_COOKIE=
|
||||||
|
DISCORD_WEBHOOK=
|
||||||
|
|
|
@ -0,0 +1,63 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/joho/godotenv"
|
||||||
|
"github.com/namsral/flag"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
DiscordWebhook string
|
||||||
|
)
|
||||||
|
|
||||||
|
type DiscordEmbedFooter struct {
|
||||||
|
Text string `json:"text"`
|
||||||
|
IconUrl string `json:"icon_url"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type DiscordEmbed struct {
|
||||||
|
Title string `json:"title"`
|
||||||
|
Url string `json:"url"`
|
||||||
|
Color int `json:"color"`
|
||||||
|
Footer DiscordEmbedFooter `json:"footer"`
|
||||||
|
Timestamp string `json:"timestamp"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type DiscordWebhookPayload struct {
|
||||||
|
Embeds []DiscordEmbed `json:"embeds"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
err := godotenv.Load()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("Error loading .env file")
|
||||||
|
}
|
||||||
|
flag.StringVar(&DiscordWebhook, "DISCORD_WEBHOOK", "", "Webhook to send logs to discord")
|
||||||
|
flag.Parse()
|
||||||
|
if DiscordWebhook == "" {
|
||||||
|
log.Fatal("DISCORD_WEBHOOK not set")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func SendEmbedToDiscord(embed DiscordEmbed) {
|
||||||
|
bodyJSON, err := json.Marshal(
|
||||||
|
DiscordWebhookPayload{
|
||||||
|
Embeds: []DiscordEmbed{embed},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("Error during json.Marshal:", err)
|
||||||
|
}
|
||||||
|
req, err := http.NewRequest("POST", DiscordWebhook, bytes.NewBuffer(bodyJSON))
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
client := &http.Client{}
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Discord webhook error:", err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
}
|
22
sn.go
22
sn.go
|
@ -172,6 +172,7 @@ func PostStoryToStackerNews(story *Story) {
|
||||||
|
|
||||||
log.Println("Created new post on SN")
|
log.Println("Created new post on SN")
|
||||||
log.Printf("id=%d title='%s' url=%s\n", parentId, story.Title, story.Url)
|
log.Printf("id=%d title='%s' url=%s\n", parentId, story.Title, story.Url)
|
||||||
|
SendStackerNewsEmbedToDiscord(story.Title, parentId)
|
||||||
|
|
||||||
comment := fmt.Sprintf(
|
comment := fmt.Sprintf(
|
||||||
"This link was posted by [%s](%s) %s on [HN](%s). It received %d points and %d comments.",
|
"This link was posted by [%s](%s) %s on [HN](%s). It received %d points and %d comments.",
|
||||||
|
@ -184,6 +185,10 @@ func PostStoryToStackerNews(story *Story) {
|
||||||
CommentStackerNewsPost(comment, parentId)
|
CommentStackerNewsPost(comment, parentId)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func StackerNewsItemLink(id int) string {
|
||||||
|
return fmt.Sprintf("https://stacker.news/items/%d", id)
|
||||||
|
}
|
||||||
|
|
||||||
func CommentStackerNewsPost(text string, parentId int) {
|
func CommentStackerNewsPost(text string, parentId int) {
|
||||||
body := GraphQLPayload{
|
body := GraphQLPayload{
|
||||||
Query: `
|
Query: `
|
||||||
|
@ -203,3 +208,20 @@ func CommentStackerNewsPost(text string, parentId int) {
|
||||||
log.Println("Commented post on SN")
|
log.Println("Commented post on SN")
|
||||||
log.Printf("text='%s' parentId=%d\n", text, parentId)
|
log.Printf("text='%s' parentId=%d\n", text, parentId)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func SendStackerNewsEmbedToDiscord(title string, id int) {
|
||||||
|
Timestamp := time.Now().Format(time.RFC3339)
|
||||||
|
url := StackerNewsItemLink(id)
|
||||||
|
color := 0xffc107
|
||||||
|
embed := DiscordEmbed{
|
||||||
|
Title: title,
|
||||||
|
Url: url,
|
||||||
|
Color: color,
|
||||||
|
Footer: DiscordEmbedFooter{
|
||||||
|
Text: "Stacker News",
|
||||||
|
IconUrl: "https://stacker.news/favicon.png",
|
||||||
|
},
|
||||||
|
Timestamp: Timestamp,
|
||||||
|
}
|
||||||
|
SendEmbedToDiscord(embed)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue