diff --git a/main.go b/main.go index 3204222..04f74c1 100644 --- a/main.go +++ b/main.go @@ -49,6 +49,8 @@ HOST=127.0.0.1 PORT=8080 LOG_MANAGER_SERVER_URL=http://localhost:50110 +TELEGRAM_BOT_MANAGER_ENABLED=true +TELEGRAM_BOT_MANAGER_SERVER_URL=http://localhost:50056 # Folder paths FOLDER_GROUPTASKS_GROUPS=./groupTasks/groups/ diff --git a/modules/config/config.go b/modules/config/config.go index e376891..922ef54 100644 --- a/modules/config/config.go +++ b/modules/config/config.go @@ -10,14 +10,16 @@ import ( var Cfg Config type Config struct { - Debug bool - ColorizedOutput bool - Host string - Port string - LogManagerServerUrl string - FolderPaths FolderPaths - MariaDB MariaDB - InvexAPI InvexAPI + Debug bool + ColorizedOutput bool + Host string + Port string + LogManagerServerUrl string + TelegramBotManagerEnabled bool + TelegramBotManagerServerUrl string + FolderPaths FolderPaths + MariaDB MariaDB + InvexAPI InvexAPI } type FolderPaths struct { @@ -49,11 +51,13 @@ func LoadConfig() { } Cfg = Config{ - Debug: os.Getenv("DEBUG") == "true", - ColorizedOutput: os.Getenv("COLORIZED_OUTPUT") == "true", - Host: os.Getenv("HOST"), - Port: os.Getenv("PORT"), - LogManagerServerUrl: os.Getenv("LOG_MANAGER_SERVER_URL"), + Debug: os.Getenv("DEBUG") == "true", + ColorizedOutput: os.Getenv("COLORIZED_OUTPUT") == "true", + Host: os.Getenv("HOST"), + Port: os.Getenv("PORT"), + LogManagerServerUrl: os.Getenv("LOG_MANAGER_SERVER_URL"), + TelegramBotManagerEnabled: os.Getenv("TELEGRAM_BOT_MANAGER_ENABLED") == "true", + TelegramBotManagerServerUrl: os.Getenv("TELEGRAM_BOT_MANAGER_SERVER_URL"), FolderPaths: FolderPaths{ GroupTasksGroups: os.Getenv("FOLDER_GROUPTASKS_GROUPS"), GroupTasksRunningTasks: os.Getenv("FOLDER_GROUPTASKS_RUNNINGTASKS"), diff --git a/modules/notification/notification.go b/modules/notification/notification.go index 94cc20f..be5b8c7 100644 --- a/modules/notification/notification.go +++ b/modules/notification/notification.go @@ -1,7 +1,9 @@ package notification import ( + "jannex/admin-dashboard-backend/modules/config" "jannex/admin-dashboard-backend/modules/database" + "jannex/admin-dashboard-backend/modules/requestclient" "jannex/admin-dashboard-backend/modules/structs" "jannex/admin-dashboard-backend/modules/utils" "jannex/admin-dashboard-backend/socketclients" @@ -102,6 +104,14 @@ func AddNotification(c *fiber.Ctx, body structs.AddNotificationRequest) error { }) } + if config.Cfg.TelegramBotManagerEnabled { + requestclient.TelegramBotManagerRequestClient(structs.TelegramBotManagerRequestBody{ + UserIds: userIds, + Title: body.Title, + Type: body.Type, + }) + } + return c.SendStatus(fiber.StatusOK) } diff --git a/modules/requestclient/requestclient.go b/modules/requestclient/requestclient.go index 0032a22..fcfbedd 100644 --- a/modules/requestclient/requestclient.go +++ b/modules/requestclient/requestclient.go @@ -1,8 +1,10 @@ package requestclient import ( + "encoding/json" "jannex/admin-dashboard-backend/modules/config" "jannex/admin-dashboard-backend/modules/logger" + "jannex/admin-dashboard-backend/modules/structs" "strconv" "git.ex.umbach.dev/Alex/roese-utils/rslogger" @@ -46,3 +48,34 @@ func InvexApiRequestClient(requestMethod string, url string) (statusCode int, bo return code, body, nil } + +func TelegramBotManagerRequestClient(telegramBotManagerRequestBody structs.TelegramBotManagerRequestBody) { + a := fiber.AcquireAgent() + + req := a.Request() + req.Header.SetMethod(fiber.MethodPost) + req.SetRequestURI(config.Cfg.TelegramBotManagerServerUrl + "/v1/notification") + req.Header.SetContentType("application/json") + + reqestBodyBytes, err := json.Marshal(telegramBotManagerRequestBody) + + if err != nil { + log.Error().Msgf("Failed to marshal request body, err: %s", err) + logger.AddSystemLog(rslogger.LogTypeError, "TelegramBotManagerRequestClient failed to marshal request body, err: %s", err.Error()) + return + } + + req.SetBody(reqestBodyBytes) + + if err = a.Parse(); err != nil { + log.Error().Msgf("Failed to parse request, err: %s", err) + logger.AddSystemLog(rslogger.LogTypeError, "TelegramBotManagerRequestClient failed to parse request, err: %s", err.Error()) + return + } + + code, _, _ := a.Bytes() + + if code != 200 { + logger.AddSystemLog(rslogger.LogTypeError, "TelegramBotManagerRequestClient err: %s statusCode: %s", "failed to send notification", strconv.Itoa(code)) + } +} diff --git a/modules/structs/notification.go b/modules/structs/notification.go index 16586c7..c44cc85 100644 --- a/modules/structs/notification.go +++ b/modules/structs/notification.go @@ -28,3 +28,9 @@ type NotificationsResponse struct { Notifications []Notification TotalPages int } + +type TelegramBotManagerRequestBody struct { + UserIds []string + Type uint8 + Title string +}