

























@@ -300,6 +300,21 @@ function setSlackDmChannelCache(key: string, channelId: string): void {
300300slackDmChannelCache.set(key, channelId);
301301}
302302303+function isSlackUserRecipient(recipient: SlackRecipient): boolean {
304+return recipient.kind === "user" || /^U[A-Z0-9]+$/i.test(recipient.id);
305+}
306+307+function resolveDirectUserPostChannelId(params: {
308+recipient: SlackRecipient;
309+hasMedia: boolean;
310+threadTs?: string;
311+}): string | undefined {
312+if (!isSlackUserRecipient(params.recipient) || params.hasMedia || params.threadTs) {
313+return undefined;
314+}
315+return params.recipient.id;
316+}
317+303318async function resolveChannelId(
304319client: WebClient,
305320recipient: SlackRecipient,
@@ -309,10 +324,9 @@ async function resolveChannelId(
309324// target string had no explicit prefix (parseSlackTarget defaults bare IDs
310325// to "channel"). chat.postMessage tolerates user IDs directly, but
311326// files.uploadV2 → completeUploadExternal validates channel_id against
312-// ^[CGDZ][A-Z0-9]{8,}$ and rejects U-prefixed IDs. Always resolve user
313-// IDs via conversations.open to obtain the DM channel ID.
314-const isUserId = recipient.kind === "user" || /^U[A-Z0-9]+$/i.test(recipient.id);
315-if (!isUserId) {
327+// ^[CGDZ][A-Z0-9]{8,}$ and rejects U-prefixed IDs. Resolve user IDs via
328+// conversations.open only for paths that require the concrete DM channel ID.
329+if (!isSlackUserRecipient(recipient)) {
316330return { channelId: recipient.id };
317331}
318332const cacheKey = createSlackDmCacheKey({
@@ -484,10 +498,17 @@ async function sendMessageSlackQueuedInner(params: {
484498}): Promise<SlackSendResult> {
485499const { opts, cfg, account, token, recipient, blocks, trimmedMessage } = params;
486500const client = opts.client ?? getSlackWriteClient(token);
487-const { channelId } = await resolveChannelId(client, recipient, {
488-accountId: account.accountId,
489- token,
501+const directUserPostChannelId = resolveDirectUserPostChannelId({
502+ recipient,
503+hasMedia: Boolean(opts.mediaUrl),
504+ ...(opts.threadTs ? { threadTs: opts.threadTs } : {}),
490505});
506+const { channelId } = directUserPostChannelId
507+ ? { channelId: directUserPostChannelId }
508+ : await resolveChannelId(client, recipient, {
509+accountId: account.accountId,
510+ token,
511+});
491512if (blocks) {
492513if (opts.mediaUrl) {
493514throw new Error("Slack send does not support blocks with mediaUrl");
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。