package automation import ( "database/sql/driver" "encoding/json" "fmt" "github.com/gochat/gochat/internal/model" ) // =========================== // BotRule — AgentBot automation rule // =========================== // BotRuleEventType defines the events that can trigger a bot rule. // Reference: Chatwoot automation_rule event_name + AgentBot webhook event types type BotRuleEventType string const ( BotRuleEventConversationCreated BotRuleEventType = "conversation_created" BotRuleEventConversationUpdated BotRuleEventType = "conversation_updated" BotRuleEventConversationOpened BotRuleEventType = "conversation_opened" BotRuleEventConversationResolved BotRuleEventType = "conversation_resolved" BotRuleEventMessageCreated BotRuleEventType = "message_created" BotRuleEventMessageUpdated BotRuleEventType = "message_updated" ) // BotRuleStatus defines the status of a bot rule. type BotRuleStatus string const ( BotRuleStatusActive BotRuleStatus = "active" BotRuleStatusInactive BotRuleStatus = "inactive" ) // BotRule represents an automation rule bound to an AgentBot. // Reference: Chatwoot AutomationRule + AgentBot association pattern // A bot rule is essentially an automation rule that belongs to a bot instead of an account. // It defines trigger conditions and actions that the bot will execute when events match. type BotRule struct { model.Base AgentBotID uint `gorm:"index;not null" json:"agent_bot_id"` AccountID uint `gorm:"index;not null" json:"account_id"` Name string `gorm:"size:255;not null" json:"name"` Description string `gorm:"type:text" json:"description,omitempty"` EventName BotRuleEventType `gorm:"size:100;index;not null" json:"event_name"` Conditions Conditions `gorm:"type:jsonb;default:'[]'" json:"conditions"` Actions Actions `gorm:"type:jsonb;default:'[]'" json:"actions"` Status BotRuleStatus `gorm:"size:20;default:'active'" json:"status"` } func (BotRule) TableName() string { return "bot_rules" } // =========================== // BotTriggerConfig — trigger condition configuration for AgentBot // =========================== // TriggerConditionType defines the type of trigger condition. type TriggerConditionType string const ( TriggerConditionTypeConversationStatus TriggerConditionType = "conversation_status" TriggerConditionTypeMessageContent TriggerConditionType = "message_content" TriggerConditionTypeSenderType TriggerConditionType = "sender_type" TriggerConditionTypePriority TriggerConditionType = "priority" TriggerConditionTypeLabel TriggerConditionType = "label" TriggerConditionTypeAssignee TriggerConditionType = "assignee" TriggerConditionTypeTeam TriggerConditionType = "team" TriggerConditionTypeCustomAttributes TriggerConditionType = "custom_attributes" ) // TriggerOperator defines the comparison operator for conditions. type TriggerOperator string const ( TriggerOperatorEqual TriggerOperator = "equal" TriggerOperatorNotEqual TriggerOperator = "not_equal" TriggerOperatorContains TriggerOperator = "contains" TriggerOperatorDoesNotContain TriggerOperator = "does_not_contain" TriggerOperatorStartsWith TriggerOperator = "starts_with" TriggerOperatorEndsWith TriggerOperator = "ends_with" TriggerOperatorGreaterThan TriggerOperator = "greater_than" TriggerOperatorLessThan TriggerOperator = "less_than" TriggerOperatorIn TriggerOperator = "in" TriggerOperatorNotIn TriggerOperator = "not_in" TriggerOperatorExists TriggerOperator = "exists" TriggerOperatorDoesNotExist TriggerOperator = "does_not_exist" TriggerOperatorChanged TriggerOperator = "changed" TriggerOperatorChangedFrom TriggerOperator = "changed_from" TriggerOperatorChangedTo TriggerOperator = "changed_to" ) // TriggerCondition represents a single trigger condition in the bot trigger config. // Reuses the Condition struct from automation rules for consistency. type TriggerCondition Condition // TriggerConditions is a slice of TriggerCondition that implements GORM JSON column. type TriggerConditions []TriggerCondition func (t TriggerConditions) Value() (driver.Value, error) { if t == nil { return json.Marshal([]TriggerCondition{}) } return json.Marshal(t) } func (t *TriggerConditions) Scan(value interface{}) error { if value == nil { *t = TriggerConditions{} return nil } bytes, ok := value.([]byte) if !ok { return fmt.Errorf("failed to unmarshal TriggerConditions value: %v", value) } return json.Unmarshal(bytes, t) } // BotTriggerConfig represents the trigger condition configuration for an AgentBot. // Reference: Chatwoot automation_rule trigger conditions + AgentBot event subscription // This config defines when and under what conditions a bot should be triggered. type BotTriggerConfig struct { model.Base AgentBotID uint `gorm:"index;not null" json:"agent_bot_id"` AccountID uint `gorm:"index;not null" json:"account_id"` Name string `gorm:"size:255;not null" json:"name"` Description string `gorm:"type:text" json:"description,omitempty"` EventName BotRuleEventType `gorm:"size:100;index;not null" json:"event_name"` Conditions TriggerConditions `gorm:"type:jsonb;default:'[]'" json:"conditions"` QueryOperator string `gorm:"size:10;default:'and'" json:"query_operator"` // "and" or "or" Active bool `gorm:"not null;default:true" json:"active"` } func (BotTriggerConfig) TableName() string { return "bot_trigger_configs" }