Skip to content

feat(slack): refetch unfurl attachments for inbound messages with URLs#415

Draft
sentry-junior[bot] wants to merge 1 commit into
mainfrom
feat/slack-unfurl-fetch
Draft

feat(slack): refetch unfurl attachments for inbound messages with URLs#415
sentry-junior[bot] wants to merge 1 commit into
mainfrom
feat/slack-unfurl-fetch

Conversation

@sentry-junior
Copy link
Copy Markdown
Contributor

@sentry-junior sentry-junior Bot commented May 25, 2026

Fixes #416

Problem

Slack generates link unfurl previews asynchronously. The initial message event arrives with an empty attachments array; the actual unfurl data lands later via a message_changed event. The existing message_changed handler only fires for newly-added bot mentions, so unfurl data was silently dropped and Junior never saw it — even when the preview was visible in the Slack UI.

Fix

Add maybeRefetchSlackUnfurlAttachments (slack/unfurl-fetch.ts) called in reply-executor.ts before prepareTurnState finalises the user turn text.

When the inbound message.raw has no attachment data and the text contains a URL, the helper retries conversations.replies with short delays (400 ms → 800 ms → 1.3 s). On a match, fetched attachments are merged back into the raw object so the existing appendSlackLegacyAttachmentText path renders them as normal [attachment] … lines. The call is a no-op for messages without URLs.

Verification

  • 10 unit tests: skip-when-has-attachments, skip-when-no-URL, skip-when-missing-ids, enrich-on-first-retry, enrich-on-second-retry, fallback-when-all-retries-miss, fallback-on-API-error, threadTs-fallback, raw-fields-preserved.
  • pnpm typecheck clean.

Action taken on behalf of Sergiy Dybskiy.

Slack delivers unfurl previews asynchronously — the initial message event
arrives with empty `attachments`, and the actual unfurl data comes later
via a `message_changed` event. The existing `message_changed` handler only
triggers on newly-added bot mentions, so unfurl data was silently dropped.

Add `maybeRefetchSlackUnfurlAttachments` in `slack/unfurl-fetch.ts` that,
when the inbound `message.raw` has no attachment data but the message text
contains a URL, retries `conversations.replies` with short delays (400 ms,
800 ms, 1.3 s) to give Slack time to generate the preview. On success, the
fetched `attachments` are merged back into the raw object so that the
existing `appendSlackLegacyAttachmentText` path renders them into the user
turn text as normal `[attachment] ...` lines.

The call is gated on: missing raw attachments AND a URL in the text AND
known channel/message identifiers — so messages without URLs incur no
extra API call.

Co-authored-by: Sergiy Dybskiy <sergiy@sentry.io>
Co-authored-by: claude-opus-4-5 <noreply>
@vercel
Copy link
Copy Markdown

vercel Bot commented May 25, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
junior-docs Ready Ready Preview, Comment May 25, 2026 4:16pm

Request Review

@dcramer
Copy link
Copy Markdown
Member

dcramer commented May 25, 2026

I'm not really sure this is a good idea. Whats the issue? Having arbitrary "try to process the message again" delays is going to cause all sorts of problems.

@dcramer dcramer requested a review from sergical May 25, 2026 19:17
@sergical
Copy link
Copy Markdown
Member

@dcramer we have the gibpotato app that unfurls discord links and adds content as attachments.

for some reason even when the message preview before sending already includes the unfurled content preview of the attachments, junior fails to see them when posted.

beyond discord unfurl in our usecase, i feel like theres other usecases where this async attachment context might be helpful?

i could be wrong and this could be not the right solution to a problem (or not big enough of a problem to solve for)

Context thread on slack: https://sentry.slack.com/archives/C0B2KJG9B5Z/p1779722822970109

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Junior can't see Slack unfurl previews on inbound messages

2 participants