humble-bot/error/notifydiscord.go

62 lines
1.8 KiB
Go
Raw Normal View History

2024-10-29 11:15:55 +01:00
/*
* Copyright (c) 2023 Laura Kalb <dev@lauka.net>
* The code of this project is available under the MIT license. See the LICENSE file for more info.
*
*/
package error
import (
"codeberg.org/lauralani/humble-bot/misc"
"codeberg.org/lauralani/humble-bot/models"
"encoding/json"
"fmt"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
"io/ioutil"
"net/http"
"net/url"
)
func NotifyDiscord(message models.DiscordMessage) {
_, urlerr := url.ParseRequestURI(viper.GetString("errorhandling.discordhook"))
if urlerr != nil || message.Content == "" {
log.Error().Str("integration", "discord").Str("config", "errorhandling.discordhook").Msgf("Tried to send discord error message, but" +
"message was empty or discord hook url in config is invalid. Message: %q", message.Content)
return
}
url := "https://discord.com/api/webhooks/1146044096029405226/GJ-KfB7NIgAhuf8soP_DvxbOui5E8yLrR4hzAZDgL6BLTxlqymsGTA0Q1weM90fMjLUa"
method := "POST"
payload, err := json.Marshal(message)
if err != nil {
log.Error().Str("func", "NotifyDiscord").Msgf("Can't marshal disocrd message: %q", err)
}
client := misc.CustomHttpClient()
req, err := http.NewRequest(method, url, payload)
if err != nil {
fmt.Println(err)
return
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Cookie", "__cfruid=52458531a20bc4c09fc012c806f897199e3e5bed-1693308636; __dcfduid=797cba3c465f11ee9d809a4b39b1053e; __sdcfduid=797cba3c465f11ee9d809a4b39b1053e004a6a0e48439b5049639654379f965153994920b12b1713669e63cabceb2f7a; _cfuvid=OdkJt_LIxefbXs_wHs.2k94vJD0kWpNzWTxAGVu2tjQ-1693308636963-0-604800000")
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(body))
}
}