Files
gochat/internal/model/channel/telegram.go
T
2026-06-04 15:44:48 +08:00

80 lines
3.1 KiB
Go

package channel
// ChannelTelegram represents a Telegram bot channel configuration.
// Reference: Chatwoot app/models/channel/telegram.rb
//
// Chatwoot's ChannelTelegram model attributes:
// - account_id: FK to Account
// - bot_token: Telegram Bot API token (from @BotFather)
// - bot_name: Bot display name (validated via getMe API)
// - welcome_message: Auto-sent to new contacts
// - reauthorization_required: Flag when webhook/bot fails
//
// Chatwoot lifecycle hooks:
// - before_validation :ensure_valid_bot_token → calls getMe API
// - before_save :setup_telegram_webhook → calls setWebhook API
// - after_destroy :delete_telegram_webhook → calls deleteWebhook API
//
// gochat enhancements beyond Chatwoot:
// - webhook_url: Stores the configured webhook URL for verification
// - account_id: Explicit FK (Chatwoot uses polymorphic through Inbox)
// - welcome_message: Moved to channel model (Chatwoot uses inbox settings)
// - reauthorization_required: Tracks when bot needs re-auth
import (
"github.com/gochat/gochat/internal/model"
)
// ChannelTelegram stores Telegram bot configuration for a channel.
type ChannelTelegram struct {
model.Base // ID, CreatedAt, UpdatedAt, DeletedAt (soft delete via GORM)
// Account foreign key
AccountID uint `gorm:"index;not null" json:"account_id"`
// Inbox foreign key — links to the Inbox that wraps this channel
InboxID uint `gorm:"index" json:"inbox_id,omitempty"`
// Bot token obtained from @BotFather (format: N:XXXXX)
// Reference: Chatwoot EDITABLE_ATTRS = [:bot_token]
BotToken string `gorm:"type:varchar(255);not null;unique" json:"bot_token"`
// Bot display name — validated and set via getMe API
// Reference: Chatwoot sets this during ensure_valid_bot_token
BotName string `gorm:"type:varchar(255)" json:"bot_name,omitempty"`
// Webhook URL configured via setWebhook API
// gochat extension: stored for verification/debugging
WebhookURL string `gorm:"type:varchar(512)" json:"webhook_url,omitempty"`
// Welcome message sent to new contacts
// Reference: Chatwoot uses inbox.welcome_message, gochat puts it on the channel model
WelcomeMessage string `gorm:"type:text" json:"welcome_message,omitempty"`
// Flag indicating the bot token needs reauthorization
// Reference: Chatwoot reauthorization_required attribute
// Set when webhook delivery fails or getMe validation fails
ReauthorizationRequired bool `gorm:"default:false" json:"reauthorization_required,omitempty"`
}
// TableName returns the GORM table name for ChannelTelegram.
// GetChannelBase returns a ChannelBase populated from ChannelTelegram fields.
// Required by the Channelable interface.
func (ct *ChannelTelegram) GetChannelBase() ChannelBase {
return ChannelBase{
ID: ct.ID,
AccountID: ct.AccountID,
InboxID: ct.InboxID,
ChannelType: ct.GetChannelType(),
CreatedAt: ct.CreatedAt,
UpdatedAt: ct.UpdatedAt,
DeletedAt: ct.DeletedAt,
}
}
// GetChannelType returns the channel type identifier.
func (ct *ChannelTelegram) GetChannelType() string { return "telegram" }
func (ChannelTelegram) TableName() string {
return "channel_telegrams"
}