Skip to content

Commit

Permalink
Send webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
noahpistilli committed Dec 22, 2023
1 parent 0783ee9 commit f406b39
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
1 change: 1 addition & 0 deletions config-example.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
<accuweather_key>AUTH_KEY_HERE</accuweather_key>
<cloudflare_token>CLOUDFLARE_TOKEN</cloudflare_token>
<cloudflare_zone_name>ZONE_NAME</cloudflare_zone_name>
<discord_webhook></discord_webhook>
</Config>
1 change: 1 addition & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type Config struct {
AccuweatherKey string `xml:"accuweather_key"`
CloudflareToken string `xml:"cloudflare_token"`
CloudflareZoneName string `xml:"cloudflare_zone_name"`
DiscordWebhook string `xml:"discord_webhook"`
}

func GetConfig() Config {
Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,9 @@ func main() {
}()
}

// Finally purge Cloudflare cache
// Finally purge Cloudflare cache and send a webhook
purgeCloudflareCache()
sendWebhook()

wg.Wait()
fmt.Println(time.Since(start))
Expand Down
45 changes: 45 additions & 0 deletions webhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import (
"bytes"
"encoding/json"
"net/http"
"time"
)

func sendWebhook() {
if config.DiscordWebhook != "" {
data := map[string]any{
"username": "Forecast Bot",
"content": "Weather Data has been updated!",
"avatar_url": "https://rc24.xyz/images/logo-small.png",
"attachments": []map[string]any{
{
"fallback": "Weather Data Update",
"color": "#0381D7",
"author_name": "RiiConnect24 Forecast Script",
"author_icon": "https://rc24.xyz/images/webhooks/forecast/profile.png",
"text": "Weather Data has been updated!",
"title": "Update!",
"fields": []map[string]any{
{
"title": "Script",
"value": "Forecast Channel",
"short": false,
},
},
"thumb_url": "https://rc24.xyz/images/webhooks/forecast/accuweather.png",
"footer": "RiiConnect24 Script",
"footer_icon": "https://rc24.xyz/images/logo-small.png",
"ts": int(time.Now().Unix()),
},
},
}

_bytes, err := json.Marshal(data)
checkError(err)

_, err = http.Post(config.DiscordWebhook, "application/json", bytes.NewReader(_bytes))
checkError(err)
}
}

0 comments on commit f406b39

Please sign in to comment.