Files
dobotai-platform/internal/settings/controller.go
T

174 lines
6.2 KiB
Go

package settings
import (
"context"
"errors"
"fmt"
"strings"
"time"
)
type ResourceBinding struct {
Kind, TenantKey, ConversationID, ConversationTitle string
BaseID, SheetID, ViewID, OperatorID string
CreatedByStaffID string
FieldMapping map[string]string
ValidationCode string
Activate bool
}
type BootstrapCandidate struct {
TenantKey, Code, StaffID, UnionID, DisplayName, Department string
DetectedAt time.Time
}
type SettingSession struct {
TenantKey, StaffID, ConversationID, ConversationType, PendingAction string
ExpiresAt time.Time
}
type SettingInput struct{ TenantKey, Channel, StaffID, UnionID, DisplayName, Department, Text, ConversationType, ConversationID, ConversationTitle string }
type ControllerRepository interface {
IsAdministrator(context.Context, string, string) (bool, error)
SetPreference(context.Context, string, string, string, string) error
SaveBinding(context.Context, ResourceBinding) error
DetectBootstrapCandidate(context.Context, BootstrapCandidate) error
GetSettingSession(context.Context, string, string) (SettingSession, bool, error)
SaveSettingSession(context.Context, SettingSession) error
DeleteSettingSession(context.Context, string, string) error
}
type Controller struct {
repo ControllerRepository
now func() time.Time
}
func NewController(repo ControllerRepository) *Controller {
return &Controller{repo: repo, now: time.Now}
}
func (c *Controller) ShouldHandle(ctx context.Context, tenant, staff, text string) bool {
if _, ok := ParseCommand(text); ok {
return true
}
if c == nil || c.repo == nil {
return false
}
s, ok, err := c.repo.GetSettingSession(ctx, tenant, staff)
return err == nil && ok && s.ExpiresAt.After(c.now())
}
func (c *Controller) Handle(ctx context.Context, in SettingInput) (string, error) {
if c == nil || c.repo == nil {
return "", errors.New("settings repository is required")
}
cmd, explicit := ParseCommand(in.Text)
var action string
var args []string
if explicit {
args = cmd.Args
} else {
s, ok, err := c.repo.GetSettingSession(ctx, in.TenantKey, in.StaffID)
if err != nil {
return "", err
}
if !ok || !s.ExpiresAt.After(c.now()) {
return "", nil
}
if strings.TrimSpace(in.Text) == "取消" {
_ = c.repo.DeleteSettingSession(ctx, in.TenantKey, in.StaffID)
return "已取消设置。", nil
}
action = s.PendingAction
args = []string{action, strings.TrimSpace(in.Text)}
}
if explicit && cmd.Name == "/help" {
return "设置回答语言:/setting language <auto|zh-CN|en|ja|ko|es>\n例如:/setting language en", nil
}
if explicit && len(args) == 1 && len(args[0]) == 6 && allDigits(args[0]) {
if strings.TrimSpace(in.ConversationType) != "direct" {
return "管理员绑定码只能私聊机器人使用。", nil
}
err := c.repo.DetectBootstrapCandidate(ctx, BootstrapCandidate{TenantKey: in.TenantKey, Code: args[0], StaffID: in.StaffID, UnionID: in.UnionID, DisplayName: in.DisplayName, Department: in.Department, DetectedAt: c.now()})
if err != nil {
return "", err
}
return "身份已验证,请回到后台确认首位管理员。", nil
}
if explicit && len(args) == 0 {
admin, err := c.repo.IsAdministrator(ctx, in.TenantKey, in.StaffID)
if err != nil {
return "", err
}
if admin {
return "⚙️ 我的设置:/setting language <auto|zh-CN|en|ja|ko|es>\n🛠 系统管理:support、question、faq", nil
}
return "⚙️ 我的设置:/setting language <auto|zh-CN|en|ja|ko|es>", nil
}
if action == "" {
action = strings.ToLower(args[0])
}
if action == "language" {
if len(args) != 2 || !validLocale(args[1]) {
return "可选语言:auto、zh-CN、en、ja、ko、es。", nil
}
if err := c.repo.SetPreference(ctx, in.TenantKey, defaultChannel(in.Channel), in.StaffID, args[1]); err != nil {
return "", err
}
return "✅ 回答语言已设置为 " + args[1] + "。", nil
}
admin, err := c.repo.IsAdministrator(ctx, in.TenantKey, in.StaffID)
if err != nil {
return "", err
}
if !admin {
return "你没有系统管理权限。", nil
}
switch action {
case "support":
if in.ConversationType != "group" || strings.TrimSpace(in.ConversationID) == "" {
return "请在目标技术支持群中@机器人发送 /setting support。", nil
}
err = c.repo.SaveBinding(ctx, ResourceBinding{Kind: "support_group", TenantKey: in.TenantKey, ConversationID: in.ConversationID, ConversationTitle: in.ConversationTitle, CreatedByStaffID: in.StaffID})
if err == nil {
return "✅ 当前群已保存为技术支持群(待后台验证)。", nil
}
case "question", "faq":
if len(args) != 2 {
err = c.repo.SaveSettingSession(ctx, SettingSession{TenantKey: in.TenantKey, StaffID: in.StaffID, ConversationID: in.ConversationID, ConversationType: in.ConversationType, PendingAction: action, ExpiresAt: c.now().Add(10 * time.Minute)})
if err != nil {
return "", err
}
return "请在10分钟内发送钉钉AI表格中的任意记录链接,发送“取消”退出。", nil
}
link, e := ParseNotableLink(args[1])
if e != nil {
return "AI表格链接无法识别。", nil
}
kind := action + "_table"
err = c.repo.SaveBinding(ctx, ResourceBinding{Kind: kind, TenantKey: in.TenantKey, BaseID: link.BaseID, SheetID: link.SheetID, ViewID: link.ViewID, OperatorID: in.UnionID, CreatedByStaffID: in.StaffID})
if err == nil {
_ = c.repo.DeleteSettingSession(ctx, in.TenantKey, in.StaffID)
return fmt.Sprintf("✅ %s已解析并保存为待验证配置。", map[string]string{"question": "问题AI表格", "faq": "FAQ表格"}[action]), nil
}
default:
return "未知设置项。发送 /setting 查看菜单。", nil
}
return "", err
}
func validLocale(s string) bool {
switch s {
case "auto", "zh-CN", "en", "ja", "ko", "es":
return true
}
return false
}
func defaultChannel(s string) string {
if strings.TrimSpace(s) == "" {
return "dingtalk"
}
return strings.TrimSpace(s)
}
func allDigits(s string) bool {
for _, r := range s {
if r < '0' || r > '9' {
return false
}
}
return s != ""
}