diff --git a/plugins/channels/kook/message.go b/plugins/channels/kook/message.go index 2a148b5..902a0da 100644 --- a/plugins/channels/kook/message.go +++ b/plugins/channels/kook/message.go @@ -91,10 +91,20 @@ func (a *Adapter) toUnifiedMessage(data json.RawMessage, frame []byte) (adapter. ct, media := messageKind(ev.Type, content) group := channelIsGroup(ev.ChannelType) + // KOOK temp/临时私聊: for a user→bot DM the event's target_id is the BOT's own id + // (the recipient), while author_id is the peer user. Using target_id as the reply + // target would make the bot reply to itself ("不能是自己", KOOK 40000). So for a + // private (non-group) message we key the conversation by the peer sender, which is + // also the correct reply target and keeps each DM a distinct session. + channelID := ev.TargetID + if group != adapter.ChannelGroup { + channelID = senderID + } + msg := adapter.UnifiedMessage{ Platform: platformName, ChannelType: group, - ChannelID: ev.TargetID, + ChannelID: channelID, SenderID: senderID, SenderName: senderName, Content: content, diff --git a/plugins/channels/kook/message_test.go b/plugins/channels/kook/message_test.go index 4f84881..7bf1b36 100644 --- a/plugins/channels/kook/message_test.go +++ b/plugins/channels/kook/message_test.go @@ -7,6 +7,30 @@ import ( "git.yeij.top/AskaEth/Cyrene_v2/internal/channel/adapter" ) +func TestToUnified_PrivateDmUsesPeerAsChannelID(t *testing.T) { + t.Parallel() + a := New(Config{BotToken: "x"}) + a.setBotIdentity(botIdentity{ID: "bot-1", Username: "Cyrene", Nickname: "Cyrene"}) + // KOOK temp/临时私聊: the user→bot DM event's target_id is the BOT (bot-1), + // author_id is the peer user. ChannelID must be the peer so the reply targets the + // user, not the bot ("不能是自己" otherwise). + data := json.RawMessage(`{ + "channel_type":"PERSON","type":1,"target_id":"bot-1","author_id":"u-1", + "content":"在吗","msg_id":"m-dm","msg_timestamp":1700000000000,"nonce":"", + "extra":{"type":1,"author":{"id":"u-1","username":"alice","nickname":"Alice"}} + }`) + msg, ok := a.toUnifiedMessage(data, nil) + if !ok { + t.Fatal("expected a deliverable private message") + } + if msg.ChannelID != "u-1" { + t.Errorf("private ChannelID = %q, want peer u-1", msg.ChannelID) + } + if msg.SenderID != "u-1" { + t.Errorf("SenderID = %q, want u-1", msg.SenderID) + } +} + func TestToUnified_DropsSelfEcho(t *testing.T) { t.Parallel() a := New(Config{BotToken: "x"})