Webhook added for kubewatch notifcation

This commit is contained in:
nomadics9 2024-12-24 01:48:23 +03:00
parent 8e0f4451ea
commit 547506036e
2 changed files with 43 additions and 1 deletions

1
.gitignore vendored
View file

@ -1,2 +1,3 @@
.env .env
.botenv .botenv
api-server

43
main.go
View file

@ -80,7 +80,6 @@ func bumpVersionTv(repoPath string) {
if err := scanner.Err(); err != nil { if err := scanner.Err(); err != nil {
fmt.Errorf("error reading %s: %w", filePath, err) fmt.Errorf("error reading %s: %w", filePath, err)
} }
// Write the updated content back to the file
if err := os.WriteFile(filePath, []byte(updatedContent), 0644); err != nil { if err := os.WriteFile(filePath, []byte(updatedContent), 0644); err != nil {
fmt.Errorf("failed to write to %s: %w", filePath, err) fmt.Errorf("failed to write to %s: %w", filePath, err)
} }
@ -89,6 +88,46 @@ func bumpVersionTv(repoPath string) {
} }
func notifyHandler(c *gin.Context) {
var data struct {
Name string `json:"name"`
Image string `json:"image"`
}
if err := c.ShouldBindJSON(&data); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid JSON"})
return
}
if data.Name == "" {
data.Name = "unknown"
}
if data.Image == "" {
data.Image = "unknown"
}
godotenv.Load(".botenv")
botToken := os.Getenv("BOT_TOKEN")
chatid := os.Getenv("CHAT_ID")
telegramToken := botToken
chatID := chatid
message := fmt.Sprintf("Service '%s' was updated to image '%s'", data.Name, data.Image)
telegramAPI := fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", telegramToken)
resp, err := http.PostForm(telegramAPI, url.Values{
"chat_id": {chatID},
"text": {message},
})
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to send Telegram message"})
return
}
defer resp.Body.Close()
c.JSON(http.StatusOK, gin.H{"message": "Notification sent"})
}
func main() { func main() {
router := gin.Default() router := gin.Default()
@ -119,6 +158,8 @@ func main() {
c.String(http.StatusOK, "Bumped AlaskarTV!") c.String(http.StatusOK, "Bumped AlaskarTV!")
}) })
router.POST("/notify", notifyHandler)
fmt.Println("Webhook server running on port 8080") fmt.Println("Webhook server running on port 8080")
router.Run(":8080") router.Run(":8080")
} }