Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(slack-app): unfurl links in replies our bot user posts (#122123)

Vojta Bartoš64dbadd1b348
diff --git a/products/slack_app/backend/slack_link_unfurl.py b/products/slack_app/backend/slack_link_unfurl.py
index c067b6d6f2d9..7adc6e8347f1 100644
--- a/products/slack_app/backend/slack_link_unfurl.py
+++ b/products/slack_app/backend/slack_link_unfurl.py
@@ -66,6 +66,7 @@ from products.slack_app.backend.logic.link_unfurl_charts import (
from products.slack_app.backend.models import SlackThreadTaskMapping
from products.slack_app.backend.services.followup_invite import build_followup_invite
from products.slack_app.backend.services.slack_messages import UNFURL_OPT_OUT_PARAM
+from products.slack_app.backend.services.slack_user_info import get_cached_bot_user_id
from products.slack_app.backend.temporal.slack_app_unfurl_charts import (
SLACK_APP_UNFURL_CHARTS_WORKFLOW_TIMEOUT,
SlackAppUnfurlChartsWorkflow,
@@ -1319,9 +1320,10 @@ def add_artifact_image_to_unfurl(
def _bot_reply_actor(integration: Integration, event: dict) -> str | None:
"""The Slack user steering the run in this thread, when that run was told to share chart links.
- Slack reports every bot-posted message under one placeholder author, so a thread whose run
- shares links is the signal that a placeholder-authored message is our reply, written for that
- person with their PostHog access. The run state holds the decision, so no flag call here."""
+ Slack credits a message our app posts to our bot user, and some bot-posted messages to the
+ placeholder author. Neither resolves to a PostHog user, so a thread whose run shares links is
+ the signal that such a message is our reply, written for that person with their PostHog
+ access. The run state holds the decision, so no flag call here."""
if not event.get("thread_ts"):
return None
actors = (
@@ -1361,7 +1363,8 @@ def handle_posthog_link_unfurl(event: dict, integration: Integration) -> None:
# Imported here to avoid circular import with api (api imports this module at load time).
from products.slack_app.backend.api import SLACK_PLACEHOLDER_USER_ID, resolve_slack_user
- bot_reply_actor = _bot_reply_actor(integration, event) if slack_user_id == SLACK_PLACEHOLDER_USER_ID else None
+ bot_authors = {SLACK_PLACEHOLDER_USER_ID, get_cached_bot_user_id(slack, integration)}
+ bot_reply_actor = _bot_reply_actor(integration, event) if slack_user_id in bot_authors else None
if bot_reply_actor:
slack_user_id = bot_reply_actor
# Everything downstream, the chart workflow included, reads the sharer from the event.
diff --git a/products/slack_app/backend/tests/test_link_unfurl.py b/products/slack_app/backend/tests/test_link_unfurl.py
index b01737128538..25cdad2f365d 100644
--- a/products/slack_app/backend/tests/test_link_unfurl.py
+++ b/products/slack_app/backend/tests/test_link_unfurl.py
@@ -566,10 +566,12 @@ class TestHandlePosthogLinkUnfurl(APIBaseTest):
@parameterized.expand(
[
- # (run told to link, thread has a run, author resolved)
- ("linking_run", True, True, "U_ACTOR"),
- ("run_not_told_to_link", False, True, "U00"),
- ("no_run_in_thread", True, False, "U00"),
+ # (event author, run told to link, thread has a run, author resolved)
+ ("placeholder_linking_run", "U00", True, True, "U_ACTOR"),
+ ("placeholder_run_not_told_to_link", "U00", False, True, "U00"),
+ ("placeholder_no_run_in_thread", "U00", True, False, "U00"),
+ ("our_bot_linking_run", "U_OUR_BOT", True, True, "U_ACTOR"),
+ ("our_bot_no_run_in_thread", "U_OUR_BOT", True, False, "U_OUR_BOT"),
]
)
@patch("products.slack_app.backend.api.resolve_slack_user")
@@ -577,16 +579,18 @@ class TestHandlePosthogLinkUnfurl(APIBaseTest):
def test_bot_reply_link_unfurls_for_the_thread_actor(
self,
_name: str,
+ author: str,
run_links: bool,
has_run: bool,
expected_resolved: str,
mock_slack_integration_class: MagicMock,
mock_resolve: MagicMock,
) -> None:
- # Slack reports a bot-posted message under a placeholder author that resolves to no PostHog
- # user, so without this path a chart link in the agent's answer never unfurls. The
- # attribution stays narrow: a thread whose run was told to share links, and the actor is
- # the one the run recorded rather than the mapping's stale column.
+ # Slack credits a bot-posted message to our bot user or to a placeholder author, and neither
+ # resolves to a PostHog user, so without this path a chart link in the agent's answer never
+ # unfurls. The attribution stays narrow: a thread whose run was told to share links, and the
+ # actor is the one the run recorded rather than the mapping's stale column.
+ write_auth_state_ok(self.integration.id, bot_user_id="U_OUR_BOT")
mock_resolve.side_effect = lambda _slack, _integration, uid, *_a, **_k: (
MagicMock(user=self.user) if uid == "U_ACTOR" else None
)
@@ -623,7 +627,7 @@ class TestHandlePosthogLinkUnfurl(APIBaseTest):
"channel": "C1",
"message_ts": "123.456",
"thread_ts": "100.000",
- "user": "U00",
+ "user": author,
"links": [{"url": url}],
},
self.integration,