feat(webhook): align chatwoot ingress routes

This commit is contained in:
2026-06-04 23:20:58 +08:00
parent 6aa62c6d7b
commit 9e3f561bed
12 changed files with 467 additions and 120 deletions
+20 -13
View File
@@ -2,6 +2,7 @@ package webhook
import (
"encoding/json"
"fmt"
"io"
"net/http"
@@ -10,7 +11,9 @@ import (
channelprovider "github.com/gochat/gochat/internal/channel/provider"
telegramchannel "github.com/gochat/gochat/internal/channel/telegram"
"github.com/gochat/gochat/internal/model"
channelmodel "github.com/gochat/gochat/internal/model/channel"
applogger "github.com/gochat/gochat/pkg/logger"
"gorm.io/gorm"
)
// TelegramWebhookHandler processes incoming Telegram webhook HTTP requests via Gin.
@@ -32,17 +35,19 @@ import (
type TelegramWebhookHandler struct {
provider *channelprovider.TelegramProvider
telWebhook *telegramchannel.WebhookHandler
// inboxRepo would be injected here for inbox lookup in production
db *gorm.DB
}
// NewTelegramWebhookHandler creates a Telegram webhook handler for Gin integration.
func NewTelegramWebhookHandler(
provider *channelprovider.TelegramProvider,
telWebhook *telegramchannel.WebhookHandler,
db *gorm.DB,
) *TelegramWebhookHandler {
return &TelegramWebhookHandler{
provider: provider,
telWebhook: telWebhook,
db: db,
}
}
@@ -123,18 +128,20 @@ func (h *TelegramWebhookHandler) HandleTelegramWebhook(c *gin.Context) {
// In production: queries GORM for Inbox with channel_type=telegram matching the bot_token.
// Reference: Chatwoot: Inbox.find_by(channel: telegram, bot_token: token)
func (h *TelegramWebhookHandler) lookupInbox(botToken string) (*model.Inbox, error) {
// TODO: Implement with GORM repository
// Placeholder — production would use:
// var ch channelmodel.ChannelTelegram
// db.Where("bot_token = ?", botToken).First(&ch)
// var inbox model.Inbox
// db.Where("id = ?", ch.InboxID).First(&inbox)
//
// For now, return a placeholder inbox
inbox := &model.Inbox{
ChannelType: "telegram",
if h.db == nil {
return nil, fmt.Errorf("telegram webhook database is not configured")
}
return inbox, nil
var channel channelmodel.ChannelTelegram
if err := h.db.Where("bot_token = ?", botToken).First(&channel).Error; err != nil {
return nil, fmt.Errorf("telegram channel not found for bot_token: %w", err)
}
var inbox model.Inbox
if err := h.db.Where("id = ? AND channel_type = ?", channel.InboxID, "telegram").First(&inbox).Error; err != nil {
return nil, fmt.Errorf("telegram inbox not found for channel inbox_id=%d: %w", channel.InboxID, err)
}
return &inbox, nil
}
// maskBotToken masks a bot token for safe logging (show first 8 chars only).
@@ -143,4 +150,4 @@ func maskBotToken(token string) string {
return token[:8] + "..."
}
return token
}
}