diff --git a/plugin-src/host/delivery-service.mjs b/plugin-src/host/delivery-service.mjs index 7e4d22c88..2777e36c2 100644 --- a/plugin-src/host/delivery-service.mjs +++ b/plugin-src/host/delivery-service.mjs @@ -613,7 +613,8 @@ export class DeliveryService { throw deliveryError('capability-unavailable'); } if (account.account?.fingerprint !== expectedFingerprint) throw deliveryError('account-changed'); - const receiptConversation = adapter.channel === 'feishu' && target.kind === 'group' + const receiptConversation = adapter.channel === 'feishu' + && (target.kind === 'group' || typeof target.route.chatId === 'string') ? target.route.chatId : adapter.channel === 'slack' && target.kind === 'conversation' ? target.route.channelId : adapter.channel === 'qq' && target.kind === 'group' ? target.route.groupOpenId : adapter.channel === 'weixin' && target.kind === 'user' diff --git a/src/channels/feishu/feishu-runtime.mjs b/src/channels/feishu/feishu-runtime.mjs index 7b764dd97..232634ec6 100644 --- a/src/channels/feishu/feishu-runtime.mjs +++ b/src/channels/feishu/feishu-runtime.mjs @@ -818,7 +818,11 @@ export class FeishuRuntime { error.code = 'bad-request'; throw error; } - if (typeof receipt !== 'boolean' || (receipt && target.kind !== 'group')) + // A receipt proves the exact conversation: a group chat, or the private + // chat a saved user target names alongside its open_id. + const receiptConversation = target.kind === 'group' ? receiveId + : nonEmptyString(target?.route?.chatId); + if (typeof receipt !== 'boolean' || (receipt && !receiptConversation)) throw Object.assign(new Error('capability-unavailable'), { code: 'capability-unavailable' }); signal?.throwIfAborted(); // Use the same native Markdown element as chat, without opening a stream @@ -846,7 +850,7 @@ export class FeishuRuntime { if (!receipt) return { sent: true }; const messageId = response?.data?.message_id; const conversationId = response?.data?.chat_id; - if (typeof messageId !== 'string' || !messageId || messageId.length > 512 || conversationId !== receiveId) + if (typeof messageId !== 'string' || !messageId || messageId.length > 512 || conversationId !== receiptConversation) throw Object.assign(new Error('send-result-unknown'), { code: 'send-result-unknown' }); return { sent: true, receipt: { version: 1, messageId, conversationId } }; } diff --git a/test/channels/feishu/proactive-markdown.test.mjs b/test/channels/feishu/proactive-markdown.test.mjs index 848088782..84f7976c6 100644 --- a/test/channels/feishu/proactive-markdown.test.mjs +++ b/test/channels/feishu/proactive-markdown.test.mjs @@ -138,3 +138,19 @@ test('opt-in group receipt survives runtime and adapter without changing legacy await assert.rejects(fx.runtime.sendProactiveText(targets[0], text, { receipt: true }), { code: 'capability-unavailable' }); assert.equal(fx.sends.length, before); }); + +test('a saved private-chat target returns a checked receipt for its exact chat (#3842)', async t => { + const fx = await fixture(t); + const privateChat = { targetId: 'owner-dm', kind: 'user', route: { openId: 'ou_fixture_owner', chatId: 'oc_fixtureowner' } }; + targets.push(privateChat); + t.after(() => targets.splice(targets.indexOf(privateChat), 1)); + fx.respondWith(() => ({ code: 0, data: { message_id: 'om_dm_receipt', chat_id: 'oc_fixtureowner' } })); + assert.deepEqual(await fx.runtime.sendProactiveText(privateChat, text, { receipt: true }), { + sent: true, receipt: { version: 1, messageId: 'om_dm_receipt', conversationId: 'oc_fixtureowner' }, + }); + const sent = fx.sends.at(-1); + assert.equal(sent.params.receive_id_type, 'open_id'); + assert.equal(sent.data.receive_id, 'ou_fixture_owner'); + fx.respondWith(() => ({ code: 0, data: { message_id: 'om_dm_receipt', chat_id: 'oc_other' } })); + await assert.rejects(fx.runtime.sendProactiveText(privateChat, text, { receipt: true }), { code: 'send-result-unknown' }); +}); diff --git a/test/delivery-service.test.mjs b/test/delivery-service.test.mjs index ead5d5ac6..4c024c072 100644 --- a/test/delivery-service.test.mjs +++ b/test/delivery-service.test.mjs @@ -393,6 +393,32 @@ test('Weixin checked post requires a current final fence and keeps client acknow assert.equal(sends, 1); }); +test('Feishu checked post receipts a saved private chat by its chat id, never an open_id-only user (#3842)', async () => { + const fx = checkedFixture(); + fx.adapter.describeAccount = async () => ({version: 1, capabilities: ['proactive-text-checked', 'proactive-receipt-checked', 'proactive-fence-checked'], account: {fingerprint: fx.fingerprint}}); + fx.service.registerAdapter(fx.adapter); + const {createHash} = await import('node:crypto'); + const digest = (target) => createHash('sha256').update(JSON.stringify({kind: target.kind, + route: Object.fromEntries(Object.entries(target.route).sort(([a], [b]) => a < b ? -1 : 1))})).digest('hex'); + const privateChat = {targetId: 'owner-dm', kind: 'user', route: {openId: 'ou_owner', chatId: 'oc_ownerdm'}}; + const openIdOnly = {targetId: 'owner-open-id', kind: 'user', route: {openId: 'ou_owner'}}; + await fx.service.createTarget('bot_one', privateChat); + await fx.service.createTarget('bot_one', openIdOnly); + let sends = 0; + fx.adapter.sendText = async (_id, saved, _text, opts) => { + assert.equal(opts.receipt, true); + if (!opts.beforeSend()) throw Object.assign(new Error('cancelled'), {code: 'cancelled'}); + sends++; + return {sent: true, receipt: {version: 1, messageId: 'om_dm', conversationId: saved.route.chatId}}; + }; + const options = {expectedFingerprint: fx.fingerprint, receipt: true, beforeSend: () => true}; + assert.deepEqual(await fx.service.sendChecked('bot_one', 'owner-dm', 'Report', {...options, expectedTargetDigest: digest(privateChat)}), + {sent: true, receipt: {version: 1, messageId: 'om_dm', conversationId: 'oc_ownerdm'}}); + await assert.rejects(fx.service.sendChecked('bot_one', 'owner-open-id', 'Report', {...options, expectedTargetDigest: digest(openIdOnly)}), + {code: 'capability-unavailable'}); + assert.equal(sends, 1); +}); + async function checkedTarget(fx) { const target = {targetId: 'self', kind: 'user', route: {openId: 'ou_self'}}; await fx.service.createTarget('bot_one', target);