128 lines
4.3 KiB
Go
128 lines
4.3 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
|
|
}
|
|
type BootstrapCandidate struct {
|
|
TenantKey, Code, StaffID, UnionID, DisplayName, Department string
|
|
DetectedAt 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
|
|
}
|
|
type Controller struct {
|
|
repo ControllerRepository
|
|
now func() time.Time
|
|
}
|
|
|
|
func NewController(repo ControllerRepository) *Controller {
|
|
return &Controller{repo: repo, now: time.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
|
|
}
|
|
if len(cmd.Args) == 1 && len(cmd.Args[0]) == 6 && allDigits(cmd.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()})
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return "身份已验证,请回到后台确认首位管理员。", nil
|
|
}
|
|
if len(cmd.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
|
|
}
|
|
action := strings.ToLower(cmd.Args[0])
|
|
if action == "language" {
|
|
if len(cmd.Args) != 2 || !validLocale(cmd.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 {
|
|
return "", err
|
|
}
|
|
return "✅ 回答语言已设置为 " + cmd.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(cmd.Args) != 2 {
|
|
return "请在命令后发送钉钉AI表格链接。", nil
|
|
}
|
|
link, e := ParseNotableLink(cmd.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 {
|
|
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 != ""
|
|
}
|