init: Initial commit
This commit is contained in:
119
app/controllers/message.go
Normal file
119
app/controllers/message.go
Normal file
@@ -0,0 +1,119 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"git.secnex.io/secnex/masterlog"
|
||||
"git.secnex.io/secnex/taro-bot/repositories"
|
||||
"git.secnex.io/secnex/taro-bot/utils"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
type MessageRequest struct {
|
||||
EventType string `json:"type"`
|
||||
ChannelID string `json:"channelId"`
|
||||
Text string `json:"text"`
|
||||
Recipient Recipient `json:"recipient"`
|
||||
MembersAdded []Member `json:"membersAdded"`
|
||||
MembersRemoved []Member `json:"membersRemoved"`
|
||||
ChannelData ChannelData `json:"channelData"`
|
||||
}
|
||||
|
||||
type Member struct {
|
||||
ID string `json:"id"`
|
||||
}
|
||||
|
||||
type Recipient struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type ChannelData struct {
|
||||
TeamsTeamID string `json:"teamsTeamId"`
|
||||
TeamsChannelID string `json:"teamsChannelId"`
|
||||
Tenant Tenant `json:"tenant"`
|
||||
Team Team `json:"team"`
|
||||
EventType string `json:"eventType"`
|
||||
Settings Settings `json:"settings"`
|
||||
}
|
||||
|
||||
type Tenant struct {
|
||||
ID string `json:"id"`
|
||||
}
|
||||
|
||||
type Team struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
AADGroupId string `json:"aadGroupId"`
|
||||
}
|
||||
|
||||
type Settings struct {
|
||||
SelectedChannel SelectedChannel `json:"selectedChannel"`
|
||||
}
|
||||
|
||||
type SelectedChannel struct {
|
||||
ID string `json:"id"`
|
||||
}
|
||||
|
||||
func MessageController(c *fiber.Ctx) error {
|
||||
var request MessageRequest
|
||||
if err := json.Unmarshal(c.Body(), &request); err != nil {
|
||||
return utils.NewErrorResponse(fiber.StatusBadRequest, &fiber.Map{
|
||||
"message": "Invalid request body",
|
||||
}).Send(c)
|
||||
}
|
||||
|
||||
masterlog.Debug("Message received", map[string]interface{}{"type": request.EventType, "text": request.Text, "channelId": request.ChannelID, "recipient": request.Recipient.ID, "membersAdded": len(request.MembersAdded), "membersRemoved": len(request.MembersRemoved)})
|
||||
|
||||
tenantId := request.ChannelData.Tenant.ID
|
||||
teamId := request.ChannelData.Team.ID
|
||||
if request.ChannelID == "emulator" {
|
||||
tenantId = "4ef9262f-f8db-453d-be35-920132ca874d"
|
||||
teamId = "405148b9-752f-4c29-867c-4670081382e1"
|
||||
}
|
||||
|
||||
switch request.EventType {
|
||||
case "message":
|
||||
message := request.Text
|
||||
masterlog.Debug("Message received", map[string]interface{}{"message": message})
|
||||
case "conversationUpdate":
|
||||
botIsAdded := false
|
||||
botIsRemoved := false
|
||||
for _, member := range request.MembersAdded {
|
||||
if member.ID == request.Recipient.ID {
|
||||
botIsAdded = true
|
||||
break
|
||||
}
|
||||
}
|
||||
for _, member := range request.MembersRemoved {
|
||||
if member.ID == request.Recipient.ID {
|
||||
botIsRemoved = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if botIsAdded {
|
||||
masterlog.Debug("Bot added to conversation", map[string]interface{}{"channelId": request.ChannelID, "tenantId": tenantId, "teamId": teamId, "eventType": request.ChannelData.EventType, "selectedChannelId": request.ChannelData.Settings.SelectedChannel.ID})
|
||||
channel, err := repositories.UpsertChannel(request.ChannelData.Team.Name, tenantId, teamId)
|
||||
if err != nil {
|
||||
masterlog.Error("Failed to create team", map[string]interface{}{"error": err})
|
||||
}
|
||||
masterlog.Debug("Channel created", map[string]interface{}{"channelId": channel.ID})
|
||||
}
|
||||
if botIsRemoved {
|
||||
err := repositories.DeleteTeamByExternalID(teamId)
|
||||
if err != nil {
|
||||
masterlog.Error("Failed to delete team", map[string]interface{}{"error": err})
|
||||
}
|
||||
masterlog.Debug("Team deleted", map[string]interface{}{"teamId": teamId})
|
||||
}
|
||||
}
|
||||
|
||||
return utils.NewHTTPResponse(fiber.StatusOK, &fiber.Map{
|
||||
"message": "Message received",
|
||||
}, "", nil, nil).Send(c)
|
||||
}
|
||||
|
||||
func MessageHeadController(c *fiber.Ctx) error {
|
||||
masterlog.Debug("Message head received")
|
||||
return utils.NewHTTPResponse(fiber.StatusNoContent, nil, "", nil, nil).Send(c)
|
||||
}
|
||||
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