Repository navigation
Description
What happened
DingTalk 1:1 (direct) messages are received correctly but no reply can ever be delivered. The channel looks healthy the whole time — it connects, flips to operational, and ingests inbound text — so the failure is silent from the user's side: you message the bot, it never answers.
Two defects stack, and fixing the first one alone does not fix the bug:
1. The route is picked by guessing a prefix, and the guess is wrong.
pickDingTalkSendRoute selects the group endpoint when the chatId starts with cid:
const isGroup = targetId.startsWith('cid');Real DingTalk 1:1 conversationId values also start with cid. Since dingTalkPayloadToEvent stamps chatId: payload.conversationId, every direct reply is routed to /v1.0/robot/groupMessages/send.
The doc comment above sendMessage (L377) says the group prefix is cidp, while the code checks cid. The two disagree — and per the experiment below, neither is a reliable discriminator.
2. Even with routing fixed, there is no usable 1:1 address in the event.
The single-chat endpoint addresses recipients by senderStaffId. That field is absent from DingTalkBotMessagePayload, so it is discarded at parse time even though DingTalk sends it. The mapped event carries only conversationId and senderId, and the API accepts neither.
Evidence
Three-way controlled experiment against a live DingTalk Stream connection. Same conversation, same robotCode, same access token, same message text — only the target id varied. Identifiers masked.
| # | Target passed as chatId |
Route taken | Result |
|---|---|---|---|
| A | conversationId — cid…= — what the bridge actually stamps |
group | ❌ HTTP 400 resource.not.found — "robot 不存在;请确认 robotCode 是否正确" |
| B | senderId — $:LWCP_v1:$… |
1:1 | ❌ HTTP 400 staffId.notExisted — "staff 不存在;请确认 userIds 是否正确" |
| C | senderStaffId — 20-digit numeric |
1:1 | ✅ delivered — processQueryKey returned, receipt confirmed in the DingTalk client |
Row A is the current production path. Row C is the fix.
Row A's error message is actively misleading: it blames robotCode, but robotCode is correct (see below). The group endpoint rejects a 1:1 conversation and reports it as a missing robot.
Root cause
The bridge knows whether the conversation is a group and then throws that knowledge away before it is needed.
conversationType ('1' = single, '2' = group) arrives in the payload and is read at L222 to set isGroup on the event. But BotRegistry.sendMessage forwards only (platform, chatId, text, options), and BotSendOptions has no isGroup. By the time sendMessage runs, the authoritative answer is gone — which is exactly what forces the prefix guess. The existing comment concedes this:
The caller (main.ts) already knows whether the bot conversation is a group via the
BotMessageEvent.isGroupflag, but the bridge'ssendMessageonly sees the chatId
Why the tests don't catch it
dingtalk-bridge.test.ts asserts routing with invented fixtures that don't resemble real DingTalk identifiers:
pickDingTalkSendRoute(' cidp-abc ', 'app-key-1', 'hello') // → group
pickDingTalkSendRoute(' user-99 ', 'app-key-1', 'hi') // → 1:1No real 1:1 conversationId looks like user-99 — real ones start with cid and would take the group branch. The fixture encodes the same wrong assumption as the implementation, so the test passes while production is broken.
Two side questions this experiment settled
robotCode= appKey is correct. The raw callback payload'srobotCodematched the app's AppKey exactly, confirming the derivation at L392. Theresource.not.foundmessage in row A is a red herring; do not "fix"robotCodein response to it.- There is an unused second route. The raw payload carries
sessionWebhookplussessionWebhookExpiredTime(~90 minutes). That webhook needs neitherrobotCodenorsenderStaffIdand works for both conversation kinds. The bridge ignores it. It is a viable fallback, though its expiry makes it unsuitable as the only path.
Suggested fix
The repo already has a convention for this, and DingTalk is the one bridge not following it. qq-bridge.ts stamps a synthetic prefix at receive time and decodes it at send time — pickQQSendRoute:
route a send to the right QQ REST endpoint based on the chatId prefix that the receive-side helpers stamp
QQ never guesses from the platform's native id format; it encodes the routing decision it already made. Mirroring that for DingTalk requires no signature change:
- Add
senderStaffId?: stringtoDingTalkBotMessagePayload. - In
dingTalkPayloadToEvent, stamp the route into the chatId — e.g.group:<conversationId>whenconversationType === '2',oto:<senderStaffId>otherwise — driven byconversationType, not by a prefix guess. - Decode that prefix in
pickDingTalkSendRouteand drop thestartsWith('cid')heuristic. - Fall back gracefully when
senderStaffIdis absent (bots in some contexts may not receive it). - Replace the test fixtures with realistic shapes: a
cid…=-style id for both kinds, so a prefix-guessing regression fails the test.
An alternative is to add isGroup?: boolean to BotSendOptions — BotReplyStreamOptions already declares it — but that still leaves defect 2 unaddressed, since senderStaffId would remain uncaptured. The chatId-stamping approach fixes both at once.
I have no strong opinion on which is preferred; I'm reporting rather than claiming this fix.
How to reproduce
- Create a self-built (自建) DingTalk app with a bot, enable Stream mode, and grant the bot message permissions.
- Configure the DingTalk channel in Maka with
appId= AppKey andappSecret= AppSecret. - Start the channel and confirm it reaches
operational. - Send the bot a direct (1:1) message. It is ingested — the inbound event fires.
- Trigger any reply.
- The send fails with HTTP 400
resource.not.found; the user never receives an answer.
Scope and limits of this report
- Group replies were not exercised — the bot was never added to a group. Whether the group endpoint works correctly for genuine group conversations is untested; row A only shows it rejects a 1:1 conversation.
- Reproduced against DingTalk Stream with a personal self-built app on a standard (non-enterprise-verified) account.
- Verified against
upstream/mainat09c73430c. The code path is unchanged since the bridge was introduced.
Environment
- Maka commit:
09c73430c - OS: Windows 11
- Surface: Runtime (
packages/runtime/src/bots/dingtalk-bridge.ts) - Node.js: v22.19.0
Acceptance criteria
- A 1:1 DingTalk message receives a reply end-to-end
- Route selection no longer depends on guessing DingTalk's native id prefixes
-
senderStaffIdis captured from the callback payload - Tests use realistic identifier shapes, so a prefix-guessing regression fails
- Group replies are confirmed still working (not covered by this report)
Found while verifying setup instructions for #3894 (bot platform documentation). All credentials were read from a file outside the repo and never printed; identifiers above are masked.
Activity
fix(runtime): route DingTalk 1:1 replies by staff id
ggbdpq commentedon Sep 9, 2026
take
fix(runtime): surface unroutable DingTalk sends and dedupe routing lore
fix(runtime): route DingTalk direct replies by staff id (apache#5112)