feat: sync DingTalk QA to Notable question table

This commit is contained in:
姚章浩
2026-07-23 14:38:50 +08:00
parent 5e0694835a
commit 0a3f62618e
22 changed files with 4351 additions and 77 deletions
+19 -10
View File
@@ -179,9 +179,11 @@ func (h *Handler) Handle(ctx context.Context, cb BotCallback) error {
return errors.New("dingtalk handler: sender staffId/senderId is required")
}
senderName := strings.TrimSpace(cb.SenderNick)
triggerPerson := strings.TrimSpace(cb.SenderNick)
senderName := triggerPerson
department := ""
region := ""
actorUnionID := ""
if h.directory != nil {
profile, _ := h.directory.ResolveUser(ctx, h.cfg.TenantKey, externalUserID, senderName)
if name := strings.TrimSpace(profile.Name); name != "" {
@@ -189,18 +191,25 @@ func (h *Handler) Handle(ctx context.Context, cb BotCallback) error {
department = strings.TrimSpace(profile.DepartmentName)
region = strings.TrimSpace(profile.Region)
}
actorUnionID = strings.TrimSpace(profile.UnionID)
}
// Trigger person snapshot falls back to official name when callback nick is blank.
if triggerPerson == "" {
triggerPerson = senderName
}
req := core.QARequest{
TenantKey: h.cfg.TenantKey,
Channel: h.cfg.Channel,
ExternalUserID: externalUserID,
ExternalMessageID: strings.TrimSpace(cb.MsgID),
SenderName: senderName,
Department: department,
Region: region,
MessageType: strings.ToLower(strings.TrimSpace(cb.Msgtype)),
ConversationTitle: strings.TrimSpace(cb.ConversationTitle),
TenantKey: h.cfg.TenantKey,
Channel: h.cfg.Channel,
ExternalUserID: externalUserID,
ExternalMessageID: strings.TrimSpace(cb.MsgID),
SenderName: senderName,
TriggerPersonSnapshot: triggerPerson,
ActorUnionIDSnapshot: actorUnionID,
Department: department,
Region: region,
MessageType: strings.ToLower(strings.TrimSpace(cb.Msgtype)),
ConversationTitle: strings.TrimSpace(cb.ConversationTitle),
}
switch cb.ConversationType {
case conversationTypeGroup:
+133
View File
@@ -222,6 +222,8 @@ func TestHandleDirectUsesDirectoryProfile(t *testing.T) {
}
}
// TestHandleDirectoryFailureFallsBackToSenderNick validates that when directory
// resolution fails, SenderName falls back to the callback SenderNick.
func TestHandleDirectoryFailureFallsBackToSenderNick(t *testing.T) {
qa := &fakeQA{result: core.QAResult{Answer: "ok"}}
reply := &fakeReply{}
@@ -1874,3 +1876,134 @@ func TestHandlePassesConversationTitleToQARequest(t *testing.T) {
}
})
}
// TestActorUnionIDSnapshotPropagation verifies the actor_union_id_snapshot
// is populated from the directory profile UnionID when available.
func TestActorUnionIDSnapshotPropagation(t *testing.T) {
t.Run("resolved profile with union ID captures snapshot", func(t *testing.T) {
qa := &fakeQA{result: core.QAResult{Answer: "ok"}}
reply := &fakeReply{}
profiles := &fakeDirectory{profile: directory.Profile{
Name: "张三", UnionID: "union-abc-123", DepartmentName: "技术部", Region: "APAC",
}}
h, err := NewHandlerWithOptions(TenantConfig{TenantKey: "tenant-a", Channel: "dingtalk"}, qa, reply, HandlerOptions{Directory: profiles})
if err != nil {
t.Fatalf("NewHandlerWithOptions: %v", err)
}
data := directTextCallback()
data.SenderNick = "钉钉昵称"
if err := h.Handle(context.Background(), data); err != nil {
t.Fatalf("Handle: %v", err)
}
if qa.lastReq.ActorUnionIDSnapshot != "union-abc-123" {
t.Fatalf("ActorUnionIDSnapshot = %q, want union-abc-123", qa.lastReq.ActorUnionIDSnapshot)
}
if qa.lastReq.SenderName != "张三" {
t.Fatalf("SenderName = %q, want 张三", qa.lastReq.SenderName)
}
if qa.lastReq.TriggerPersonSnapshot != "钉钉昵称" {
t.Fatalf("TriggerPersonSnapshot = %q, want 钉钉昵称", qa.lastReq.TriggerPersonSnapshot)
}
})
t.Run("blank profile union leaves snapshot empty", func(t *testing.T) {
qa := &fakeQA{result: core.QAResult{Answer: "ok"}}
reply := &fakeReply{}
profiles := &fakeDirectory{profile: directory.Profile{
Name: "张三", UnionID: "", DepartmentName: "技术部", Region: "APAC",
}}
h, err := NewHandlerWithOptions(TenantConfig{TenantKey: "tenant-a", Channel: "dingtalk"}, qa, reply, HandlerOptions{Directory: profiles})
if err != nil {
t.Fatalf("NewHandlerWithOptions: %v", err)
}
data := directTextCallback()
data.SenderNick = "钉钉昵称"
if err := h.Handle(context.Background(), data); err != nil {
t.Fatalf("Handle: %v", err)
}
if qa.lastReq.ActorUnionIDSnapshot != "" {
t.Fatalf("ActorUnionIDSnapshot = %q, want empty", qa.lastReq.ActorUnionIDSnapshot)
}
})
t.Run("directory failure leaves union snapshot empty", func(t *testing.T) {
qa := &fakeQA{result: core.QAResult{Answer: "ok"}}
reply := &fakeReply{}
profiles := &fakeDirectory{err: errors.New("directory unavailable")}
h, err := NewHandlerWithOptions(TenantConfig{TenantKey: "tenant-a", Channel: "dingtalk"}, qa, reply, HandlerOptions{Directory: profiles})
if err != nil {
t.Fatalf("NewHandlerWithOptions: %v", err)
}
data := directTextCallback()
data.SenderNick = "钉钉昵称"
if err := h.Handle(context.Background(), data); err != nil {
t.Fatalf("Handle: %v", err)
}
if qa.lastReq.ActorUnionIDSnapshot != "" {
t.Fatalf("ActorUnionIDSnapshot = %q, want empty on directory failure", qa.lastReq.ActorUnionIDSnapshot)
}
})
t.Run("no directory leaves union snapshot empty", func(t *testing.T) {
qa := &fakeQA{result: core.QAResult{Answer: "ok"}}
reply := &fakeReply{}
h := newTestHandler(t, qa, reply)
data := directTextCallback()
data.SenderNick = "钉钉昵称"
if err := h.Handle(context.Background(), data); err != nil {
t.Fatalf("Handle: %v", err)
}
if qa.lastReq.ActorUnionIDSnapshot != "" {
t.Fatalf("ActorUnionIDSnapshot = %q, want empty without directory", qa.lastReq.ActorUnionIDSnapshot)
}
})
}
// TestTriggerPersonSnapshotRegression proves the trigger_person_snapshot
// contract: the callback SenderNick becomes TriggerPersonSnapshot, the
// directory official Name remains SenderName, and a blank callback nick
// falls back to the official name.
func TestTriggerPersonSnapshotRegression(t *testing.T) {
t.Run("callback nick becomes trigger person snapshot", func(t *testing.T) {
qa := &fakeQA{result: core.QAResult{Answer: "ok"}}
reply := &fakeReply{}
profiles := &fakeDirectory{profile: directory.Profile{Name: "张三", DepartmentName: "技术部", Region: "APAC"}}
h, err := NewHandlerWithOptions(TenantConfig{TenantKey: "tenant-a", Channel: "dingtalk"}, qa, reply, HandlerOptions{Directory: profiles})
if err != nil {
t.Fatalf("NewHandlerWithOptions: %v", err)
}
data := directTextCallback()
data.SenderNick = "钉钉昵称"
if err := h.Handle(context.Background(), data); err != nil {
t.Fatalf("Handle: %v", err)
}
if qa.lastReq.TriggerPersonSnapshot != "钉钉昵称" {
t.Fatalf("TriggerPersonSnapshot = %q, want 钉钉昵称", qa.lastReq.TriggerPersonSnapshot)
}
if qa.lastReq.SenderName != "张三" {
t.Fatalf("SenderName = %q, want 张三", qa.lastReq.SenderName)
}
})
t.Run("blank callback nick falls back to official name", func(t *testing.T) {
qa := &fakeQA{result: core.QAResult{Answer: "ok"}}
reply := &fakeReply{}
profiles := &fakeDirectory{profile: directory.Profile{Name: "张三", DepartmentName: "技术部", Region: "APAC"}}
h, err := NewHandlerWithOptions(TenantConfig{TenantKey: "tenant-a", Channel: "dingtalk"}, qa, reply, HandlerOptions{Directory: profiles})
if err != nil {
t.Fatalf("NewHandlerWithOptions: %v", err)
}
data := directTextCallback()
data.SenderNick = ""
if err := h.Handle(context.Background(), data); err != nil {
t.Fatalf("Handle: %v", err)
}
if qa.lastReq.TriggerPersonSnapshot != "张三" {
t.Fatalf("TriggerPersonSnapshot = %q, want 张三 on blank nick", qa.lastReq.TriggerPersonSnapshot)
}
if qa.lastReq.SenderName != "张三" {
t.Fatalf("SenderName = %q, want 张三", qa.lastReq.SenderName)
}
})
}