37 lines
940 B
Go
37 lines
940 B
Go
package third
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"agent-desk/internal/services"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// TelegramPostWebhook receives incoming Webhook events from Telegram Bot API.
|
|
func TelegramPostWebhook(ctx *gin.Context) {
|
|
channelID := strings.TrimSpace(ctx.Param("channel_id"))
|
|
if channelID == "" {
|
|
channelID = strings.TrimSpace(ctx.Query("channel_id"))
|
|
}
|
|
|
|
secretHeader := ctx.GetHeader("X-Telegram-Bot-Api-Secret-Token")
|
|
|
|
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.TelegramInboundService.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})
|
|
}
|