init: Initial commit
This commit is contained in:
54
app/controllers/webhook_receive.go
Normal file
54
app/controllers/webhook_receive.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"git.secnex.io/secnex/taro-bot/repositories"
|
||||
"git.secnex.io/secnex/taro-bot/services"
|
||||
"git.secnex.io/secnex/taro-bot/utils"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func WebhookReceiveController(c *fiber.Ctx) error {
|
||||
id := c.Params("id")
|
||||
if id == "" {
|
||||
return utils.NewErrorResponse(fiber.StatusBadRequest, &fiber.Map{
|
||||
"message": "ID is required",
|
||||
}).Send(c)
|
||||
}
|
||||
|
||||
token := c.Query("token")
|
||||
if token == "" {
|
||||
return utils.NewErrorResponse(fiber.StatusUnauthorized, &fiber.Map{
|
||||
"message": "Token is required",
|
||||
}).Send(c)
|
||||
}
|
||||
|
||||
webhook, err := repositories.GetWebhookByID(uuid.MustParse(id))
|
||||
if err != nil {
|
||||
return utils.NewErrorResponse(fiber.StatusInternalServerError, &fiber.Map{
|
||||
"message": "Failed to get webhook",
|
||||
}).Send(c)
|
||||
}
|
||||
|
||||
hashedToken := webhook.Token
|
||||
valid, err := utils.Verify(token, hashedToken)
|
||||
if err != nil {
|
||||
return utils.NewErrorResponse(fiber.StatusInternalServerError, &fiber.Map{
|
||||
"message": "Failed to verify token",
|
||||
}).Send(c)
|
||||
}
|
||||
if !valid {
|
||||
return utils.NewErrorResponse(fiber.StatusUnauthorized, &fiber.Map{
|
||||
"message": "Invalid token",
|
||||
}).Send(c)
|
||||
}
|
||||
|
||||
var body utils.HTTPBody
|
||||
if err := c.BodyParser(&body); err != nil {
|
||||
return utils.NewErrorResponse(fiber.StatusBadRequest, &fiber.Map{
|
||||
"message": "Invalid request body",
|
||||
}).Send(c)
|
||||
}
|
||||
|
||||
return services.ExecuteWebhook(webhook.ChannelID, body).Send(c)
|
||||
}
|
||||
Reference in New Issue
Block a user