Files
agent-desk-ng/internal/handlers/third/discord_handler.go

40 lines
998 B
Go

package third
import (
"bytes"
"io"
"net/http"
"strings"
"agent-desk/internal/services"
"github.com/gin-gonic/gin"
)
// DiscordPostWebhook receives incoming Webhook events from Discord.
func DiscordPostWebhook(ctx *gin.Context) {
channelID := strings.TrimSpace(ctx.Param("channel_id"))
if channelID == "" {
channelID = strings.TrimSpace(ctx.Query("channel_id"))
}
secretHeader := ctx.GetHeader("X-Discord-Secret-Token")
if secretHeader == "" {
secretHeader = ctx.GetHeader("X-Webhook-Secret")
}
bodyBytes, err := io.ReadAll(ctx.Request.Body)
if err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"ok": false, "error": "failed to read body"})
return
}
ctx.Request.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
if err := services.DiscordInboundService.HandleWebhook(ctx.Request.Context(), channelID, secretHeader, bodyBytes); err != nil {
ctx.JSON(http.StatusOK, gin.H{"ok": false, "error": err.Error()})
return
}
ctx.JSON(http.StatusOK, gin.H{"ok": true})
}