Files
rogee aeddedf2a3 Reorganize repo: backend/, deploy/, docs/ layout + AGENTS.md
Restructure the monorepo into clear top-level directories:
- backend/: Go module root (cmd, internal, pkg, configs, migrations,
  docs/swagger, scripts, tests, go.mod, Makefile, .air.toml)
- deploy/: Docker (Dockerfile, docker-compose*), quickstart, fluentd
- docs/: project documentation + reports/ (moved from repo root)
- AGENTS.md: new AI coding-agent guide at repo root

Update all references to the new layout:
- Dockerfile: COPY backend/go.mod, COPY backend/ (context = repo root)
- docker-compose files: context ../.., dockerfile deploy/docker/Dockerfile,
  env_file ../../.env, volume mounts ../../backend:/app
- deploy/quickstart/compose.yaml: dockerfile deploy/docker/Dockerfile
- CI: working-directory: backend for go commands, file deploy/docker/Dockerfile,
  coverage path backend/coverage.out, health_check backend/scripts/
- backend/Makefile: docker target uses -f ../deploy/docker/Dockerfile ../
- README: architecture tree, quickstart, config paths updated

Move root stray scripts (rename_models.*, run_m11_tests.sh, verify_build.sh,
gorm_bool_main.go) to backend/scripts/legacy/. All moves via git mv to
preserve history. Build, vet, SQLite tests, and docker compose config verified.
2026-07-07 14:44:12 +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"
}