: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(subscriptions): remove redundant text from slack reports (#122741)
Matt Pua7a3d9ebc458f
diff --git a/products/subscriptions/backend/logic/slack_subscriptions.py b/products/subscriptions/backend/logic/slack_subscriptions.pyindex 39ad3e47ffee..a105141a4ea8 100644--- a/products/subscriptions/backend/logic/slack_subscriptions.py+++ b/products/subscriptions/backend/logic/slack_subscriptions.py@@ -132,7 +132,6 @@ def _subscription_title(if is_new_subscription:return (f"This channel has been subscribed to {display_name} on PostHog! 🎉\n\n"- f"This subscription is {subscription.summary}.\n"f"The next one will be sent on {next_delivery_date_display(subscription)}")return f"Your subscription to {display_name} is ready! 🎉"@@ -164,10 +163,6 @@ def _subscription_header_blocks("type": "context","elements": [{"type": "plain_text", "text": f"{resource_icon} {resource_info.kind}: {resource_info.name}"}],},- {- "type": "context",- "elements": [{"type": "plain_text", "text": f"🕒 {subscription.summary.removeprefix('sent ')}"}],- },]@@ -324,7 +319,7 @@ def _prepare_slack_gallery(unattached_assets.append(asset)continuefile_uploads.append({"content": content, "filename": asset.filename, "title": _insight_name(asset)})- if asset.insight:+ if subscription.dashboard_id and asset.insight:if insight_links:insight_links.append({"type": "text", "text": " · "})insight_links.append({"type": "link", "url": asset.insight.url, "text": _insight_name(asset)})@@ -381,7 +376,7 @@ def _prepare_slack_gallery()-def _blocks_for_asset(asset: ExportedAsset, resource_url: str) -> list[dict]:+def _blocks_for_asset(asset: ExportedAsset, resource_url: str, *, include_insight_link: bool = True) -> list[dict]:if _has_asset_failed(asset):details = failed_asset_details(asset)support_url = subscription_support_url(resource_url)@@ -447,8 +442,9 @@ def _blocks_for_asset(asset: ExportedAsset, resource_url: str) -> list[dict]:image_url = DEBUG_PLACEHOLDER_IMAGE_URLblocks = []- if link := _insight_link_block(asset):- blocks.append(link)+ if include_insight_link:+ if link := _insight_link_block(asset):+ blocks.append(link)blocks.append({"type": "image", "image_url": image_url, "alt_text": alt_text})return blocks@@ -508,7 +504,11 @@ def _prepare_slack_message(notice = summary_skipped_over_budget_message(f"<{billing_url}|Billing settings>")blocks.append({"type": "context", "elements": [{"type": "mrkdwn", "text": f"_{notice}_"}]})- blocks.extend(_blocks_for_asset(first_asset, resource_url=resource_info.url))+ blocks.extend(+ _blocks_for_asset(+ first_asset, resource_url=resource_info.url, include_insight_link=bool(subscription.dashboard_id)+ )+ )if other_assets:blocks.append(@@ -523,7 +523,9 @@ def _prepare_slack_message(# Prepare additional messages for threadthread_messages = []for asset in other_assets:- asset_blocks = _blocks_for_asset(asset, resource_url=resource_info.url)+ asset_blocks = _blocks_for_asset(+ asset, resource_url=resource_info.url, include_insight_link=bool(subscription.dashboard_id)+ )thread_messages.append({"blocks": asset_blocks})if total_asset_count > len(assets):diff --git a/products/subscriptions/backend/temporal/ai_subscription/test/test_delivery.py b/products/subscriptions/backend/temporal/ai_subscription/test/test_delivery.pyindex 07462bcbad59..f263ee19a858 100644--- a/products/subscriptions/backend/temporal/ai_subscription/test/test_delivery.py+++ b/products/subscriptions/backend/temporal/ai_subscription/test/test_delivery.py@@ -424,21 +424,19 @@ class TestChartsOnSlackMessages:_mock_subscription(), "A short report.", delivery_id=_DELIVERY_ID, charts=[_CHART])- assert [block["type"] for block in message.blocks[:6]] == [+ assert [block["type"] for block in message.blocks[:5]] == ["divider","header","context",- "context","markdown","image",]- assert message.blocks[4]["text"] == "A short report."+ assert message.blocks[3]["text"] == "A short report."assert message.blocks[1]["text"]["text"] == "Your subscription to Weekly report is ready! 🎉"assert message.blocks[2]["elements"][0]["text"] == "🤖 AI prompt: Weekly report"- assert message.blocks[3]["elements"][0]["text"] == "🕒 every week on Monday"- assert message.blocks[5]["image_url"] == _CHART["image_url"]- assert message.blocks[5]["alt_text"] == "signups by day"- assert message.blocks[5]["title"] == {"type": "plain_text", "text": "signups by day"}+ assert message.blocks[4]["image_url"] == _CHART["image_url"]+ assert message.blocks[4]["alt_text"] == "signups by day"+ assert message.blocks[4]["title"] == {"type": "plain_text", "text": "signups by day"}def test_an_untitled_chart_posts_no_title_block(self) -> None:message = _build_ai_slack_message(@@ -448,8 +446,8 @@ class TestChartsOnSlackMessages:charts=[{"title": "", "image_url": "https://ph.test/img.png"}],)- assert "title" not in message.blocks[5]- assert message.blocks[5]["alt_text"] == "Chart"+ assert "title" not in message.blocks[4]+ assert message.blocks[4]["alt_text"] == "Chart"def test_a_chartless_report_posts_no_image_blocks(self) -> None:message = _build_message("A short report.")@@ -562,7 +560,7 @@ class TestBuildAISlackMessage:def test_complex_findings_keep_native_markdown(self, content: str) -> None:markdown = f"## Main result\n\n{content}"message = _build_message(markdown)- assert message.blocks[4] == {"type": "markdown", "text": markdown}+ assert message.blocks[3] == {"type": "markdown", "text": markdown}def test_single_section_report_has_no_thread_messages(self) -> None:message = _build_message("A short report.")@@ -583,7 +581,7 @@ class TestBuildAISlackMessage:)def test_preserves_report_markdown(self, markdown: str) -> None:message = _build_message(markdown)- assert message.blocks[4] == {"type": "markdown", "text": markdown or "_No report content was generated._"}+ assert message.blocks[3] == {"type": "markdown", "text": markdown or "_No report content was generated._"}assert message.thread_messages == []def test_long_report_overflows_into_thread(self) -> None:@@ -613,8 +611,8 @@ class TestBuildAISlackMessage:charts=[_CHART],)- assert [block["type"] for block in message.blocks] == ["divider", "header", "context", "context", "markdown"]- assert message.blocks[4]["text"] == "A short report."+ assert [block["type"] for block in message.blocks] == ["divider", "header", "context", "markdown"]+ assert message.blocks[3]["text"] == "A short report."def test_with_images_delivery_keeps_charts_without_posthog_controls(self) -> None:subscription = _mock_subscription()@@ -637,11 +635,10 @@ class TestBuildAISlackMessage:"divider","header","context",- "context","markdown","image",]- assert message.blocks[5]["image_url"] == _CHART["image_url"]+ assert message.blocks[4]["image_url"] == _CHART["image_url"]class TestBuildAITeamsCard:diff --git a/products/subscriptions/backend/tests/logic/test_slack_subscriptions.py b/products/subscriptions/backend/tests/logic/test_slack_subscriptions.pyindex 6d11ea434f23..a9e1bf61b56d 100644--- a/products/subscriptions/backend/tests/logic/test_slack_subscriptions.py+++ b/products/subscriptions/backend/tests/logic/test_slack_subscriptions.py@@ -94,22 +94,9 @@ class TestSlackSubscriptionsTasks(APIBaseTest):"text": {"type": "plain_text", "text": "Your subscription to My Test subscription is ready! 🎉"},},{"type": "context", "elements": [{"type": "plain_text", "text": "📊 Insight: My Test subscription"}]},- {- "type": "context",- "elements": [{"type": "plain_text", "text": f"🕒 {self.subscription.summary.removeprefix('sent ')}"}],- },- {- "type": "rich_text",- "elements": [- {- "type": "rich_text_section",- "elements": [{"type": "link", "url": self.insight.url, "text": "My Test subscription"}],- }- ],- },{"type": "image",- "image_url": post_message_calls[0].kwargs["blocks"][5]["image_url"],+ "image_url": post_message_calls[0].kwargs["blocks"][3]["image_url"],"alt_text": "My Test subscription",},{"type": "divider"},@@ -157,13 +144,13 @@ class TestSlackSubscriptionsTasks(APIBaseTest):"new_without_title",None,True,- "This channel has been subscribed to the Insight *My Test subscription* on PostHog! 🎉\n\nThis subscription is sent every day.\nThe next one will be sent on Wednesday February 02, 2022",+ "This channel has been subscribed to the Insight *My Test subscription* on PostHog! 🎉\n\nThe next one will be sent on Wednesday February 02, 2022",),("new_with_title","Weekly KPI Report",True,- "This channel has been subscribed to *Weekly KPI Report* (Insight: My Test subscription) on PostHog! 🎉\n\nThis subscription is sent every day.\nThe next one will be sent on Wednesday February 02, 2022",+ "This channel has been subscribed to *Weekly KPI Report* (Insight: My Test subscription) on PostHog! 🎉\n\nThe next one will be sent on Wednesday February 02, 2022",),("recurring_with_failed_export",@@ -224,10 +211,6 @@ class TestSlackSubscriptionsTasks(APIBaseTest):first_call["blocks"][1]["text"]["text"] == "⚠️ Your subscription to My Test subscription needs attention")assert first_call["blocks"][2]["elements"][0]["text"] == "📊 Insight: My Test subscription"- assert (- first_call["blocks"][3]["elements"][0]["text"]- == f"🕒 {self.subscription.summary.removeprefix('sent ')}"- )if has_failed_thread_export:thread_call = mock_slack_integration.client.chat_postMessage.call_args_list[1].kwargserror_card = thread_call["blocks"][0]@@ -263,10 +246,6 @@ class TestSlackSubscriptionsTasks(APIBaseTest):"text": {"type": "plain_text", "text": "Your subscription to private dashboard is ready! 🎉"},},{"type": "context", "elements": [{"type": "plain_text", "text": "📊 Dashboard: private dashboard"}]},- {- "type": "context",- "elements": [{"type": "plain_text", "text": f"🕒 {self.subscription.summary.removeprefix('sent ')}"}],- },{"type": "rich_text","elements": [@@ -278,7 +257,7 @@ class TestSlackSubscriptionsTasks(APIBaseTest):},{"type": "image",- "image_url": post_message_calls[0].kwargs["blocks"][5]["image_url"],+ "image_url": post_message_calls[0].kwargs["blocks"][4]["image_url"],"alt_text": "My Test subscription",},{@@ -970,7 +949,8 @@ class TestSlackPostAllInMainMessage(APIBaseTest):delivery_config={"post_all_insights_in_main_message": post_all_in_main},)- def test_prepare_slack_gallery_builds_comment_and_uploads(self) -> None:+ @parameterized.expand([("insight", False), ("dashboard", True)])+ def test_prepare_slack_gallery_builds_comment_and_uploads(self, _name: str, is_dashboard: bool) -> None:subscription = create_subscription(team=self.team,created_by=self.user,@@ -979,6 +959,9 @@ class TestSlackPostAllInMainMessage(APIBaseTest):insight=self.insight,delivery_config={"post_all_insights_in_main_message": True},)+ if is_dashboard:+ subscription.insight = None+ subscription.dashboard = Dashboard.objects.create(team=self.team, name="Test dashboard")ok = ExportedAsset.objects.create(team=self.team, export_format="image/png", content=b"PNGBYTES", insight_id=self.insight.id)@@ -990,7 +973,7 @@ class TestSlackPostAllInMainMessage(APIBaseTest):assert len(gallery.file_uploads) == 1 # failed asset excludedassert gallery.file_uploads[0]["content"] == b"PNGBYTES"links = [block for block in gallery.blocks if block["type"] == "rich_text"]- assert links == [+ expected_links = [{"type": "rich_text","elements": [@@ -1001,6 +984,7 @@ class TestSlackPostAllInMainMessage(APIBaseTest):],}]+ assert links == (expected_links if is_dashboard else [])assert "View in PostHog" in gallery.initial_comment# Failed assets are listed by name with a "Could not generate" noticeassert "Could not generate" in gallery.initial_comment