diff --git a/src/error_handler.go b/src/error_handler.go index 4f0b05e..56c0368 100644 --- a/src/error_handler.go +++ b/src/error_handler.go @@ -67,6 +67,30 @@ func ErrorCaptureHandler(client *raven.Client, errorsStacktrace bool) ErrorHandl "endpoint": c.Request.RequestURI, } + var ( + ok bool + conn Connection + ) + + connection, ok := c.Get("connection") + if ok { + conn = connection.(Connection) + } + + b, ok := c.Get("bot") + if ok { + tags["bot"] = b.(Bot).Token + conn = *getConnectionById(b.(Bot).ConnectionID) + } + + if conn.APIURL != "" { + tags["crm"] = conn.APIURL + } + + if conn.ClientID != "" { + tags["clientID"] = conn.ClientID + } + if recovery != nil { stacktrace := raven.NewStacktrace(4, 3, nil) recStr := fmt.Sprint(recovery) diff --git a/src/routing.go b/src/routing.go index 9024953..35120f4 100644 --- a/src/routing.go +++ b/src/routing.go @@ -406,17 +406,7 @@ func updateBots(conn *Connection, hashSettings string) { } func telegramWebhookHandler(c *gin.Context) { - token := c.Param("token") - b, err := getBotByToken(token) - if err != nil { - c.Error(err) - return - } - - if b.ID == 0 { - c.AbortWithStatus(http.StatusOK) - return - } + b := c.MustGet("bot").(Bot) conn := getConnectionById(b.ConnectionID) if !conn.Active { @@ -559,17 +549,7 @@ func telegramWebhookHandler(c *gin.Context) { } func mgWebhookHandler(c *gin.Context) { - clientID := c.GetHeader("Clientid") - if clientID == "" { - c.AbortWithStatus(http.StatusBadRequest) - return - } - - conn := getConnection(clientID) - if !conn.Active { - c.AbortWithStatus(http.StatusBadRequest) - return - } + conn := c.MustGet("connection").(Connection) var msg v1.WebhookRequest if err := c.ShouldBindJSON(&msg); err != nil { diff --git a/src/run.go b/src/run.go index 74fd3db..a7b3c50 100644 --- a/src/run.go +++ b/src/run.go @@ -1,6 +1,7 @@ package main import ( + "net/http" "os" "os/signal" "syscall" @@ -92,8 +93,8 @@ func setup() *gin.Engine { r.POST("/delete-bot/", checkBotForRequest(), deleteBotHandler) r.POST("/set-lang/", checkBotForRequest(), setLangBotHandler) r.POST("/actions/activity", activityHandler) - r.POST("/telegram/:token", telegramWebhookHandler) - r.POST("/webhook/", mgWebhookHandler) + r.POST("/telegram/:token", checkBotForWebhook(), telegramWebhookHandler) + r.POST("/webhook/", checkConnectionForWebhook(), mgWebhookHandler) return r } @@ -147,3 +148,40 @@ func checkConnectionForRequest() gin.HandlerFunc { c.Set("connection", conn) } } + +func checkConnectionForWebhook() gin.HandlerFunc { + return func(c *gin.Context) { + clientID := c.GetHeader("Clientid") + if clientID == "" { + c.AbortWithStatus(http.StatusBadRequest) + return + } + + conn := getConnection(clientID) + if !conn.Active { + c.AbortWithStatus(http.StatusBadRequest) + return + } + + c.Set("connection", *conn) + } +} + +func checkBotForWebhook() gin.HandlerFunc { + return func(c *gin.Context) { + token := c.Param("token") + + b, err := getBotByToken(token) + if err != nil { + c.Error(err) + return + } + + if b.ID == 0 { + c.AbortWithStatus(http.StatusOK) + return + } + + c.Set("bot", *b) + } +}