2025-02-09 16:34:46 +08:00
|
|
|
package telegram
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
2025-02-09 18:51:53 +08:00
|
|
|
"github.com/Vomitblood/cspj-application/server/internal/log_backup"
|
2025-02-09 16:34:46 +08:00
|
|
|
tg "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
2025-02-09 18:51:53 +08:00
|
|
|
"github.com/studio-b12/gowebdav"
|
2025-02-09 16:34:46 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
telegramToken = "7215466800:AAGwjZnXEfbbjQiA0y7qtSzbSZNUWQJnyjo"
|
|
|
|
telegramChatID = 622943829
|
|
|
|
)
|
|
|
|
|
2025-02-09 17:05:57 +08:00
|
|
|
type LogEntry struct {
|
|
|
|
AuditData struct {
|
|
|
|
Messages []string `json:"messages"`
|
|
|
|
} `json:"audit_data"`
|
|
|
|
}
|
|
|
|
|
2025-02-09 20:07:57 +08:00
|
|
|
func Init(webdavClient *gowebdav.Client) *tg.BotAPI {
|
2025-02-09 16:34:46 +08:00
|
|
|
bot, err := tg.NewBotAPI(telegramToken)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Failed to create Telegram bot:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("Telegram bot connected")
|
|
|
|
|
|
|
|
// send init message on startup
|
|
|
|
testMsg := tg.NewMessage(telegramChatID, "I'm in")
|
|
|
|
_, err = bot.Send(testMsg)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Failed to send test message:", err)
|
|
|
|
}
|
|
|
|
|
2025-02-09 20:07:57 +08:00
|
|
|
go handleUpdates(bot, webdavClient)
|
2025-02-09 18:51:53 +08:00
|
|
|
|
|
|
|
return bot
|
|
|
|
}
|
|
|
|
|
|
|
|
// function to handle commands from user on tg
|
|
|
|
func handleUpdates(bot *tg.BotAPI, webdavClient *gowebdav.Client) {
|
|
|
|
u := tg.NewUpdate(0)
|
|
|
|
u.Timeout = 60
|
|
|
|
updates := bot.GetUpdatesChan(u)
|
|
|
|
|
|
|
|
for update := range updates {
|
|
|
|
if update.Message != nil {
|
|
|
|
command := update.Message.Text
|
|
|
|
|
|
|
|
// /backup_logs
|
|
|
|
if command == "/backup_logs" {
|
|
|
|
err := log_backup.BackupLogs(webdavClient)
|
|
|
|
if err != nil {
|
|
|
|
sendTelegramResponse(bot, fmt.Sprintf("Failed to backup logs: %v", err))
|
|
|
|
} else {
|
|
|
|
sendTelegramResponse(bot, "Successfully backed up log files")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-02-09 20:07:57 +08:00
|
|
|
// /restore_logs
|
2025-02-09 18:51:53 +08:00
|
|
|
if command == "/restore_logs" {
|
|
|
|
err := log_backup.RestoreLogs(webdavClient)
|
|
|
|
if err != nil {
|
|
|
|
sendTelegramResponse(bot, fmt.Sprintf("Failed to restore logs: %v", err))
|
|
|
|
} else {
|
|
|
|
sendTelegramResponse(bot, "Successfully restored log files")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func sendTelegramResponse(bot *tg.BotAPI, message string) {
|
|
|
|
msg := tg.NewMessage(telegramChatID, message)
|
|
|
|
_, err := bot.Send(msg)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Failed to send Telegram message:", err)
|
|
|
|
}
|
2025-02-09 16:34:46 +08:00
|
|
|
}
|
|
|
|
|
2025-02-09 17:40:04 +08:00
|
|
|
func SendTelegramAlert(bot *tg.BotAPI, message string) {
|
2025-02-09 17:05:57 +08:00
|
|
|
msg := tg.NewMessage(telegramChatID, fmt.Sprintf("🚨 *WEEWOO ALERT* 🚨\n%s", message))
|
|
|
|
_, err := bot.Send(msg)
|
|
|
|
if err != nil {
|
2025-02-09 17:30:30 +08:00
|
|
|
log.Println("Failed to send Telegram message:", err)
|
2025-02-09 17:05:57 +08:00
|
|
|
}
|
2025-02-09 16:34:46 +08:00
|
|
|
}
|