diff --git a/internal/im/proactive_reply_test.go b/internal/im/proactive_reply_test.go new file mode 100644 index 00000000..d9d19ef5 --- /dev/null +++ b/internal/im/proactive_reply_test.go @@ -0,0 +1,58 @@ +package im + +import ( + "context" + "testing" + + "github.com/gin-gonic/gin" +) + +type recordingReplyAdapter struct { + incoming *IncomingMessage + reply *ReplyMessage +} + +func (a *recordingReplyAdapter) Platform() Platform { return PlatformDingtalk } +func (a *recordingReplyAdapter) VerifyCallback(*gin.Context) error { return nil } +func (a *recordingReplyAdapter) ParseCallback(*gin.Context) (*IncomingMessage, error) { + return nil, nil +} +func (a *recordingReplyAdapter) SendReply(_ context.Context, incoming *IncomingMessage, reply *ReplyMessage) error { + a.incoming = incoming + a.reply = reply + return nil +} +func (a *recordingReplyAdapter) HandleURLVerification(*gin.Context) bool { return false } + +func TestSendReplyToSource(t *testing.T) { + adapter := &recordingReplyAdapter{} + service := &Service{channels: map[string]*channelState{ + "channel-1": { + Adapter: adapter, + Channel: &IMChannel{ID: "channel-1", Platform: "dingtalk", Enabled: true}, + }, + }} + + err := service.SendReplyToSource(context.Background(), ReplyTarget{ + ChannelID: "channel-1", + UserID: "staff-1", + ChatID: "conversation-1", + ChatType: ChatTypeGroup, + }, "技术支持回复内容") + if err != nil { + t.Fatalf("SendReplyToSource: %v", err) + } + if adapter.incoming == nil || adapter.incoming.UserID != "staff-1" || adapter.incoming.ChatID != "conversation-1" || adapter.incoming.ChatType != ChatTypeGroup { + t.Fatalf("incoming = %#v", adapter.incoming) + } + if adapter.reply == nil || adapter.reply.Content != "技术支持回复内容" || !adapter.reply.IsFinal { + t.Fatalf("reply = %#v", adapter.reply) + } +} + +func TestSendReplyToSourceRejectsUnavailableChannel(t *testing.T) { + service := &Service{channels: map[string]*channelState{}} + if err := service.SendReplyToSource(context.Background(), ReplyTarget{ChannelID: "missing"}, "reply"); err == nil { + t.Fatal("expected missing channel error") + } +} diff --git a/internal/im/service.go b/internal/im/service.go index 549117d6..91991e08 100644 --- a/internal/im/service.go +++ b/internal/im/service.go @@ -1253,6 +1253,48 @@ func (s *Service) GetChannelAdapter(channelID string) (Adapter, *IMChannel, bool return cs.Adapter, cs.Channel, true } +// ReplyTarget identifies the original customer conversation for a proactive reply. +type ReplyTarget struct { + ChannelID string + UserID string + ChatID string + ChatType ChatType + ThreadID string +} + +// SendReplyToSource delivers a support reply through the configured platform adapter. +// It intentionally does not depend on a short-lived session webhook; adapters can use +// their authenticated proactive-message API when Extra is empty. +func (s *Service) SendReplyToSource(ctx context.Context, target ReplyTarget, content string) error { + if strings.TrimSpace(target.ChannelID) == "" { + return fmt.Errorf("channel ID is required") + } + content = strings.TrimSpace(content) + if content == "" { + return fmt.Errorf("reply content is required") + } + adapter, channel, ok := s.GetChannelAdapter(target.ChannelID) + if !ok || adapter == nil || channel == nil { + return fmt.Errorf("IM channel %s is not running", target.ChannelID) + } + if !channel.Enabled { + return fmt.Errorf("IM channel %s is disabled", target.ChannelID) + } + chatType := target.ChatType + if chatType == "" { + chatType = ChatTypeDirect + } + incoming := &IncomingMessage{ + Platform: Platform(channel.Platform), + UserID: strings.TrimSpace(target.UserID), + ChatID: strings.TrimSpace(target.ChatID), + ChatType: chatType, + ThreadID: strings.TrimSpace(target.ThreadID), + MessageID: "support:" + uuid.New().String(), + } + return adapter.SendReply(ctx, incoming, &ReplyMessage{Content: content, IsFinal: true}) +} + // GetChannelByID loads a channel from the database. func (s *Service) GetChannelByID(channelID string) (*IMChannel, error) { var ch IMChannel