Compare commits

..

3 Commits

Author SHA1 Message Date
Space-Banane
df078e2aa7 feat: update README and docker-compose for Kuma monitoring configuration 2026-01-27 17:42:54 +01:00
Space-Banane
51beafed0f fix: log message for unconfigured Kuma push monitor 2026-01-27 17:40:02 +01:00
Space-Banane
7005d4588d feat: add Kuma push monitoring functionality with retry logic 2026-01-27 17:32:30 +01:00
3 changed files with 59 additions and 1 deletions

View File

@@ -28,6 +28,9 @@ Create a `.env` file in the project root with your Discord bot token:
``` ```
DISCORD_BOT_TOKEN=your_discord_bot_token_here DISCORD_BOT_TOKEN=your_discord_bot_token_here
# Optional: Set Kuma monitoring variables:
# KUMA_PUSH_URL=https://your-uptime-kuma-instance.com/api/push/
# KUMA_PUSH_INTERVAL=60
``` ```
### 3. Run with Docker Compose ### 3. Run with Docker Compose

53
bot.go
View File

@@ -4,11 +4,14 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"log"
"net/http" "net/http"
"os" "os"
"os/signal" "os/signal"
"strconv"
"strings" "strings"
"syscall" "syscall"
"time"
"thoughtful-dcbob/commands" "thoughtful-dcbob/commands"
@@ -51,6 +54,22 @@ func main() {
// continue running; commands may be registered later // continue running; commands may be registered later
} }
// Start Kuma push monitor if configured
kumaPushURL, ok := os.LookupEnv("KUMA_PUSH_URL")
if ok {
intervalStr, ok := os.LookupEnv("KUMA_PUSH_INTERVAL")
interval := 60 // default to 60 seconds
if ok && intervalStr != "" {
if parsed, err := strconv.Atoi(intervalStr); err == nil && parsed > 0 {
interval = parsed
}
}
go startKumaPush(kumaPushURL, interval)
log.Printf("Kuma push monitor started (URL: %s, Interval: %ds)", kumaPushURL, interval)
} else {
log.Println("Kuma push monitor not configured.")
}
// Wait here until CTRL-C or other term signal is received. // Wait here until CTRL-C or other term signal is received.
fmt.Println("Bot is now running. Press CTRL-C to exit.") fmt.Println("Bot is now running. Press CTRL-C to exit.")
sc := make(chan os.Signal, 1) sc := make(chan os.Signal, 1)
@@ -61,6 +80,40 @@ func main() {
dg.Close() dg.Close()
} }
// startKumaPush periodically sends GET requests to the Kuma push URL
func startKumaPush(url string, intervalSeconds int) {
ticker := time.NewTicker(time.Duration(intervalSeconds) * time.Second)
defer ticker.Stop()
for range ticker.C {
pushToKuma(url)
}
}
// pushToKuma sends a GET request to the Kuma push URL with retry logic
func pushToKuma(url string) {
client := &http.Client{Timeout: 10 * time.Second}
// First attempt
resp, err := client.Get(url)
if err != nil {
log.Printf("Kuma push failed: %v (retrying in 15s)", err)
time.Sleep(15 * time.Second)
// Retry attempt
resp, err = client.Get(url)
if err != nil {
log.Printf("Kuma push retry failed: %v", err)
return
}
defer resp.Body.Close()
log.Printf("Kuma push retry succeeded: status %d", resp.StatusCode)
return
}
defer resp.Body.Close()
log.Printf("Kuma push: status %d", resp.StatusCode)
}
// This function will be called (due to AddHandler above) every time a new // This function will be called (due to AddHandler above) every time a new
// message is created on any channel that the authenticated bot has access to. // message is created on any channel that the authenticated bot has access to.
// //

View File

@@ -7,4 +7,6 @@ services:
command: sh -c "git pull && if [ ! -f .env ]; then echo \".env file not found! Please create one based on .env.example and set your DISCORD_BOT_TOKEN.\"; exit 1; fi && go mod tidy && go run ." command: sh -c "git pull && if [ ! -f .env ]; then echo \".env file not found! Please create one based on .env.example and set your DISCORD_BOT_TOKEN.\"; exit 1; fi && go mod tidy && go run ."
env_file: env_file:
- .env - .env
# Optional: Set Kuma monitoring variables in .env file:
# KUMA_PUSH_URL=https://your-uptime-kuma-instance.com/api/push/xxxxx
# KUMA_PUSH_INTERVAL=60