feat: keep table binding messages out of QA
This commit is contained in:
@@ -17,12 +17,19 @@ 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
|
||||
@@ -32,25 +39,51 @@ type Controller struct {
|
||||
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, ok := ParseCommand(in.Text)
|
||||
if !ok {
|
||||
return "", nil
|
||||
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 len(cmd.Args) == 1 && len(cmd.Args[0]) == 6 && allDigits(cmd.Args[0]) {
|
||||
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: cmd.Args[0], StaffID: in.StaffID, UnionID: in.UnionID, DisplayName: in.DisplayName, Department: in.Department, DetectedAt: c.now()})
|
||||
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 len(cmd.Args) == 0 {
|
||||
if explicit && len(args) == 0 {
|
||||
admin, err := c.repo.IsAdministrator(ctx, in.TenantKey, in.StaffID)
|
||||
if err != nil {
|
||||
return "", err
|
||||
@@ -60,15 +93,17 @@ func (c *Controller) Handle(ctx context.Context, in SettingInput) (string, error
|
||||
}
|
||||
return "⚙️ 我的设置:/setting language <auto|zh-CN|en|ja|ko|es>", nil
|
||||
}
|
||||
action := strings.ToLower(cmd.Args[0])
|
||||
if action == "" {
|
||||
action = strings.ToLower(args[0])
|
||||
}
|
||||
if action == "language" {
|
||||
if len(cmd.Args) != 2 || !validLocale(cmd.Args[1]) {
|
||||
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, cmd.Args[1]); err != nil {
|
||||
if err := c.repo.SetPreference(ctx, in.TenantKey, defaultChannel(in.Channel), in.StaffID, args[1]); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return "✅ 回答语言已设置为 " + cmd.Args[1] + "。", nil
|
||||
return "✅ 回答语言已设置为 " + args[1] + "。", nil
|
||||
}
|
||||
admin, err := c.repo.IsAdministrator(ctx, in.TenantKey, in.StaffID)
|
||||
if err != nil {
|
||||
@@ -87,16 +122,21 @@ func (c *Controller) Handle(ctx context.Context, in SettingInput) (string, error
|
||||
return "✅ 当前群已保存为技术支持群(待后台验证)。", nil
|
||||
}
|
||||
case "question", "faq":
|
||||
if len(cmd.Args) != 2 {
|
||||
return "请在命令后发送钉钉AI表格链接。", nil
|
||||
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(cmd.Args[1])
|
||||
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:
|
||||
|
||||
Reference in New Issue
Block a user