feat: add DingTalk directory-backed session titles
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
"time"
|
||||
|
||||
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/core"
|
||||
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/directory"
|
||||
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/weknora"
|
||||
)
|
||||
|
||||
@@ -32,17 +33,18 @@ type TenantConfig struct {
|
||||
}
|
||||
|
||||
type BotCallback struct {
|
||||
ConversationType string
|
||||
ConversationID string
|
||||
Msgtype string
|
||||
MsgID string
|
||||
SenderNick string
|
||||
SenderStaffID string
|
||||
SenderID string
|
||||
IsInAtList bool
|
||||
SessionWebhook string
|
||||
Text string
|
||||
DownloadCode string
|
||||
ConversationType string
|
||||
ConversationID string
|
||||
ConversationTitle string
|
||||
Msgtype string
|
||||
MsgID string
|
||||
SenderNick string
|
||||
SenderStaffID string
|
||||
SenderID string
|
||||
IsInAtList bool
|
||||
SessionWebhook string
|
||||
Text string
|
||||
DownloadCode string
|
||||
}
|
||||
|
||||
type QAAsker interface {
|
||||
@@ -73,6 +75,10 @@ type ImageDownloader interface {
|
||||
Download(ctx context.Context, downloadCode string) (core.ImageInput, error)
|
||||
}
|
||||
|
||||
type UserDirectory interface {
|
||||
ResolveUser(ctx context.Context, tenantKey, staffID, fallbackName string) (directory.Profile, error)
|
||||
}
|
||||
|
||||
// FinalCardSender sends an AI card as the final reply. Implementations must
|
||||
// not log err, outTrackID, or content at any level.
|
||||
type FinalCardSender interface {
|
||||
@@ -89,6 +95,7 @@ type StreamingCardSender interface {
|
||||
type HandlerOptions struct {
|
||||
ImageDownloader ImageDownloader
|
||||
CardSender FinalCardSender
|
||||
Directory UserDirectory
|
||||
}
|
||||
|
||||
type Handler struct {
|
||||
@@ -97,6 +104,7 @@ type Handler struct {
|
||||
reply MarkdownSender
|
||||
downloader ImageDownloader
|
||||
card FinalCardSender
|
||||
directory UserDirectory
|
||||
now func() time.Time
|
||||
cardUpdateInterval time.Duration
|
||||
finalDeliveryTimeout time.Duration
|
||||
@@ -137,6 +145,7 @@ func NewHandlerWithOptions(cfg TenantConfig, qa QAAsker, reply MarkdownSender, o
|
||||
reply: reply,
|
||||
downloader: opts.ImageDownloader,
|
||||
card: opts.CardSender,
|
||||
directory: opts.Directory,
|
||||
now: time.Now,
|
||||
cardUpdateInterval: 500 * time.Millisecond,
|
||||
finalDeliveryTimeout: defaultHTTPTimeout,
|
||||
@@ -170,13 +179,28 @@ 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)
|
||||
department := ""
|
||||
region := ""
|
||||
if h.directory != nil {
|
||||
profile, _ := h.directory.ResolveUser(ctx, h.cfg.TenantKey, externalUserID, senderName)
|
||||
if name := strings.TrimSpace(profile.Name); name != "" {
|
||||
senderName = name
|
||||
department = strings.TrimSpace(profile.DepartmentName)
|
||||
region = strings.TrimSpace(profile.Region)
|
||||
}
|
||||
}
|
||||
|
||||
req := core.QARequest{
|
||||
TenantKey: h.cfg.TenantKey,
|
||||
Channel: h.cfg.Channel,
|
||||
ExternalUserID: externalUserID,
|
||||
ExternalMessageID: strings.TrimSpace(cb.MsgID),
|
||||
SenderName: cb.SenderNick,
|
||||
SenderName: senderName,
|
||||
Department: department,
|
||||
Region: region,
|
||||
MessageType: strings.ToLower(strings.TrimSpace(cb.Msgtype)),
|
||||
ConversationTitle: strings.TrimSpace(cb.ConversationTitle),
|
||||
}
|
||||
switch cb.ConversationType {
|
||||
case conversationTypeGroup:
|
||||
@@ -187,6 +211,7 @@ func (h *Handler) Handle(ctx context.Context, cb BotCallback) error {
|
||||
req.ExternalChatID = strings.TrimSpace(cb.ConversationID)
|
||||
default:
|
||||
req.ConversationType = core.ConversationDirect
|
||||
req.ConversationTitle = ""
|
||||
}
|
||||
|
||||
switch strings.ToLower(strings.TrimSpace(cb.Msgtype)) {
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"time"
|
||||
|
||||
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/core"
|
||||
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/directory"
|
||||
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/weknora"
|
||||
)
|
||||
|
||||
@@ -31,6 +32,17 @@ func (f *fakeQA) RecordDelivery(_ context.Context, update core.DeliveryUpdate) e
|
||||
return f.deliveryErr
|
||||
}
|
||||
|
||||
type fakeDirectory struct {
|
||||
profile directory.Profile
|
||||
err error
|
||||
calls int
|
||||
}
|
||||
|
||||
func (f *fakeDirectory) ResolveUser(context.Context, string, string, string) (directory.Profile, error) {
|
||||
f.calls++
|
||||
return f.profile, f.err
|
||||
}
|
||||
|
||||
type fakeReply struct {
|
||||
called bool
|
||||
lastURL string
|
||||
@@ -192,6 +204,42 @@ func TestHandleDirectTextCallsQAAndRepliesAnswer(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleDirectUsesDirectoryProfile(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 profiles.calls != 1 || qa.lastReq.SenderName != "张三" || qa.lastReq.Department != "技术支持部" || qa.lastReq.Region != "APAC" {
|
||||
t.Fatalf("calls=%d request=%+v", profiles.calls, qa.lastReq)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleDirectoryFailureFallsBackToSenderNick(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.SenderName != "回调昵称" || qa.lastReq.Department != "" || qa.lastReq.Region != "" {
|
||||
t.Fatalf("request=%+v", qa.lastReq)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleDirectTextFallsBackToSenderID(t *testing.T) {
|
||||
qa := &fakeQA{result: core.QAResult{Answer: "ok"}}
|
||||
reply := &fakeReply{}
|
||||
@@ -1796,3 +1844,33 @@ func TestDeliveryAuditEventStreamingProductionPath(t *testing.T) {
|
||||
}
|
||||
requireDelivery(t, qa.deliveries, core.DeliverySent, core.DeliveryMethodCard, "event-track-1", "")
|
||||
}
|
||||
|
||||
func TestHandlePassesConversationTitleToQARequest(t *testing.T) {
|
||||
qa := &fakeQA{result: core.QAResult{Answer: "ok"}}
|
||||
reply := &fakeReply{}
|
||||
h := newTestHandler(t, qa, reply)
|
||||
|
||||
t.Run("group conversation title propagated", func(t *testing.T) {
|
||||
cb := groupTextCallback()
|
||||
cb.ConversationTitle = "项目讨论群"
|
||||
if err := h.Handle(context.Background(), cb); err != nil {
|
||||
t.Fatalf("Handle: %v", err)
|
||||
}
|
||||
if qa.lastReq.ConversationTitle != "项目讨论群" {
|
||||
t.Fatalf("ConversationTitle = %q, want 项目讨论群", qa.lastReq.ConversationTitle)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("direct conversation title is empty", func(t *testing.T) {
|
||||
qa2 := &fakeQA{result: core.QAResult{Answer: "ok"}}
|
||||
h2 := newTestHandler(t, qa2, &fakeReply{})
|
||||
cb := directTextCallback()
|
||||
cb.ConversationTitle = "should not appear"
|
||||
if err := h2.Handle(context.Background(), cb); err != nil {
|
||||
t.Fatalf("Handle: %v", err)
|
||||
}
|
||||
if qa2.lastReq.ConversationTitle != "" {
|
||||
t.Fatalf("ConversationTitle = %q, want empty for direct", qa2.lastReq.ConversationTitle)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -69,16 +69,17 @@ func BotCallbackFromSDK(data *chatbot.BotCallbackDataModel) (BotCallback, error)
|
||||
}
|
||||
msgtype := strings.ToLower(strings.TrimSpace(data.Msgtype))
|
||||
cb := BotCallback{
|
||||
ConversationType: data.ConversationType,
|
||||
ConversationID: data.ConversationId,
|
||||
Msgtype: msgtype,
|
||||
MsgID: data.MsgId,
|
||||
SenderNick: data.SenderNick,
|
||||
SenderStaffID: data.SenderStaffId,
|
||||
SenderID: data.SenderId,
|
||||
IsInAtList: data.IsInAtList,
|
||||
SessionWebhook: data.SessionWebhook,
|
||||
Text: data.Text.Content,
|
||||
ConversationType: data.ConversationType,
|
||||
ConversationID: data.ConversationId,
|
||||
ConversationTitle: data.ConversationTitle,
|
||||
Msgtype: msgtype,
|
||||
MsgID: data.MsgId,
|
||||
SenderNick: data.SenderNick,
|
||||
SenderStaffID: data.SenderStaffId,
|
||||
SenderID: data.SenderId,
|
||||
IsInAtList: data.IsInAtList,
|
||||
SessionWebhook: data.SessionWebhook,
|
||||
Text: data.Text.Content,
|
||||
}
|
||||
if msgtype == "richtext" {
|
||||
rtMsgtype, rtText, rtCode := parseRichText(data.Content)
|
||||
|
||||
@@ -542,6 +542,46 @@ func TestBotCallbackFromSDKRichText(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestBotCallbackFromSDKConversationTitle(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
t.Run("conversation title survives conversion", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
data := &chatbot.BotCallbackDataModel{
|
||||
ConversationType: "2",
|
||||
ConversationId: "cid123",
|
||||
Msgtype: "text",
|
||||
MsgId: "msg-1",
|
||||
ConversationTitle: "项目讨论群",
|
||||
Text: chatbot.BotCallbackDataTextModel{Content: "hello"},
|
||||
}
|
||||
cb, err := BotCallbackFromSDK(data)
|
||||
if err != nil {
|
||||
t.Fatalf("BotCallbackFromSDK() error = %v", err)
|
||||
}
|
||||
if cb.ConversationTitle != "项目讨论群" {
|
||||
t.Fatalf("ConversationTitle = %q, want 项目讨论群", cb.ConversationTitle)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("conversation title empty for direct", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
data := &chatbot.BotCallbackDataModel{
|
||||
ConversationType: "1",
|
||||
Msgtype: "text",
|
||||
MsgId: "msg-2",
|
||||
Text: chatbot.BotCallbackDataTextModel{Content: "hi"},
|
||||
}
|
||||
cb, err := BotCallbackFromSDK(data)
|
||||
if err != nil {
|
||||
t.Fatalf("BotCallbackFromSDK() error = %v", err)
|
||||
}
|
||||
if cb.ConversationTitle != "" {
|
||||
t.Fatalf("ConversationTitle = %q, want empty for direct", cb.ConversationTitle)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestNewStreamClientValidation(t *testing.T) {
|
||||
t.Parallel()
|
||||
h := mustTestHandler(t)
|
||||
|
||||
Reference in New Issue
Block a user