telegram-bot-manager/routers/api/v1/notification/notification.go

73 lines
1.8 KiB
Go

package notification
import (
"jannex/telegram-bot-manager/modules/database"
"jannex/telegram-bot-manager/modules/logger"
"jannex/telegram-bot-manager/modules/structs"
"jannex/telegram-bot-manager/modules/telegram"
"jannex/telegram-bot-manager/modules/utils"
"strings"
"git.ex.umbach.dev/Alex/roese-utils/rslogger"
"git.ex.umbach.dev/Alex/roese-utils/rsutils"
"github.com/gofiber/fiber/v2"
)
func SendNotification(c *fiber.Ctx) error {
// swagger:operation POST /v1/notification notification sendNotification
// ---
// summary: Send notification to users
// consumes:
// - application/json
// produces:
// - application/json
// parameters:
// - name: body
// in: body
// description: Notification body
// required: true
// schema:
// "$ref": "#/definitions/NotificationBody"
// responses:
// '200':
// description: OK
// '400':
// description: Bad request
var body structs.NotificationBody
if err := rsutils.BodyParserHelper(c, &body); err != nil {
return c.SendStatus(fiber.StatusBadRequest)
}
var foundVerifiedUsers []structs.VerifiedUser
// get all verified users by body userIds
database.DB.Where("user_id IN ?", body.UserIds).Find(&foundVerifiedUsers)
formattedMessage := utils.GetNotificationIconByType(body.Type) + " " + body.Title
var notifiedUsers []string
// send message to all verified users
for _, user := range foundVerifiedUsers {
if user.Filter == "" {
continue
}
filter := strings.Split(user.Filter, ",")
if !utils.IsNotificationTypeInFilter(filter, body.Type) {
continue
}
telegram.SendNotification(int64(user.ChatId), formattedMessage)
notifiedUsers = append(notifiedUsers, user.UserId)
}
logger.AddSystemLog(rslogger.LogTypeInfo, "Sent notification: %s to %d users: %v", formattedMessage, len(notifiedUsers), notifiedUsers)
return c.SendStatus(fiber.StatusOK)
}