: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
feat(slack): classify untagged posts that name posthog (#121076)
Vojta Bartoš7350fb96c5b5
diff --git a/products/slack_app/backend/api.py b/products/slack_app/backend/api.pyindex e3dd516d62db..bd5891864408 100644--- a/products/slack_app/backend/api.py+++ b/products/slack_app/backend/api.py@@ -2568,6 +2568,15 @@ def _route_untagged_question(if _us_should_handle_instead(slack_team_id, [SLACK_INTEGRATION_KIND], can_defer, incoming_host):return _proxy_event_and_return_route(request, other_domain, {ALREADY_EMITTED_HEADER: "1"})+ # Most top-level posts are chatter, so the flag drops them before the author's mode read and+ # user resolution. The flag is rolled out per organization, and a workspace can connect+ # projects from several organizations, so each project keeps its own answer.+ enabled_integration_ids = {+ candidate.id for candidate in workspace_result.candidates if is_slack_app_unprompted_answers_enabled(candidate)+ }+ if not enabled_integration_ids:+ return ROUTE_HANDLED_LOCALLY+# One indexed read, ahead of user resolution, which can call Slack's users.info for every author.if resolve_untagged_question_mode(slack_team_id, slack_user_id) == UntaggedFollowupMode.NEVER:return ROUTE_HANDLED_LOCALLY@@ -2586,9 +2595,7 @@ def _route_untagged_question(target = resolution.integration or (candidates[0] if len(candidates) == 1 else None)if target is None or SlackIntegration(target).missing_scopes(REQUIRED_SLACK_SCOPES):return ROUTE_HANDLED_LOCALLY- # Checked on the project that will answer: a workspace can connect projects from several- # organizations, and the flag is rolled out per organization.- if not is_slack_app_unprompted_answers_enabled(target):+ if target.id not in enabled_integration_ids:return ROUTE_HANDLED_LOCALLYlogger.info(diff --git a/products/slack_app/backend/logic/untagged_question.py b/products/slack_app/backend/logic/untagged_question.pyindex 816521bf96c4..4848d4ddefdd 100644--- a/products/slack_app/backend/logic/untagged_question.py+++ b/products/slack_app/backend/logic/untagged_question.py@@ -5,17 +5,25 @@ from typing import AnyUNTAGGED_QUESTION_MIN_CHARS = 12UNTAGGED_QUESTION_MAX_CHARS = 2000-_QUESTION_OPENER_PATTERN = re.compile(- r"^(how|what|why|when|where|which|who|whose|is|are|was|were|can|could|does|do|did|should|would|will|has|have|any|anyone|anybody)\b",- re.IGNORECASE,-)+UNTAGGED_QUESTION_MIN_WORDS = 3+# A message for another bot or a typed Slack command, such as "/remind" or "!deploy".+_COMMAND_PATTERN = re.compile(r"^[/!]\S")+_POSTHOG_PATTERN = re.compile(r"\bpost\s?hog\b", re.IGNORECASE)+# Slack wraps links, channels and tags in angle brackets. A PostHog URL alone does not name PostHog.+_SLACK_MARKUP_PATTERN = re.compile(r"<[^>]*>")+++def mentions_posthog(text: str) -> bool:+ """Whether the words of a post name PostHog, outside links."""+ return bool(_POSTHOG_PATTERN.search(_SLACK_MARKUP_PATTERN.sub(" ", text)))def is_untagged_question_candidate(event: dict[str, Any]) -> bool:"""Whether a top-level post nobody tagged the app in could be a question for PostHog.Text-only checks, because every top-level post in every channel the app is in comes through- here. The classifier makes the real decision later, on the few posts that pass.+ here. The post must name PostHog, so the classifier sees what people ask about PostHog in any+ phrasing, with or without a ``?``. The classifier makes the real decision later."""# ``group`` is a private channel. Group DMs (``mpim``) are private conversations between people.if event.get("channel_type") not in ("channel", "group"):@@ -27,4 +35,8 @@ def is_untagged_question_candidate(event: dict[str, Any]) -> bool:# the whole channel addresses them, not PostHog.if "<@" in text or "<!" in text:return False- return "?" in text or bool(_QUESTION_OPENER_PATTERN.match(text))+ if len(text.split()) < UNTAGGED_QUESTION_MIN_WORDS:+ return False+ if _COMMAND_PATTERN.match(text):+ return False+ return mentions_posthog(text)diff --git a/products/slack_app/backend/tests/logic/test_untagged_question.py b/products/slack_app/backend/tests/logic/test_untagged_question.pynew file mode 100644index 000000000000..c9a56fffee84--- /dev/null+++ b/products/slack_app/backend/tests/logic/test_untagged_question.py@@ -0,0 +1,25 @@+from django.test import SimpleTestCase++from parameterized import parameterized++from products.slack_app.backend.logic.untagged_question import is_untagged_question_candidate+++class TestIsUntaggedQuestionCandidate(SimpleTestCase):+ @parameterized.expand(+ [+ ("a question that names PostHog", "how many signups does PostHog show for last week?", True),+ ("a request without a question word", "need signups by country from posthog for last month", True),+ ("a statement that names PostHog", "the Post Hog funnel looks off since the deploy", True),+ ("a question that does not name PostHog", "how many people signed up last week?", False),+ ("PostHog only in a link", "how many signups here <https://us.posthog.com/project/2/insights/abc>", False),+ ("a command", "/remind me to check posthog signups tomorrow", False),+ ("a command for another bot", "!deploy posthog to staging now", False),+ ("too few words", "posthog???????????", False),+ ("too short", "posthog?", False),+ ]+ )+ def test_the_word_and_length_checks(self, _name, text, expected):+ event = {"channel_type": "channel", "text": text}++ assert is_untagged_question_candidate(event) is expecteddiff --git a/products/slack_app/backend/tests/test_thread_message_routing.py b/products/slack_app/backend/tests/test_thread_message_routing.pyindex 768943194ad2..ee34be18cf46 100644--- a/products/slack_app/backend/tests/test_thread_message_routing.py+++ b/products/slack_app/backend/tests/test_thread_message_routing.py@@ -127,7 +127,7 @@ class TestRouteThreadMessage(TestCase):"channel_type": "channel","ts": "2000.0000","thread_ts": None,- "text": "How many people signed up last week?",+ "text": "How many people signed up last week in PostHog?",}return self._make_event(**{**defaults, **overrides})@@ -162,7 +162,22 @@ class TestRouteThreadMessage(TestCase):[("an untagged question", {}, True, None, False, True),("a private channel", {"channel_type": "group"}, True, None, False, True),- ("a statement", {"text": "Shipped the new export filter today"}, True, None, False, False),+ (+ "a request without a question word",+ {"text": "need signups by country from PostHog for last month"},+ True,+ None,+ False,+ True,+ ),+ (+ "a question that does not name PostHog",+ {"text": "How many people signed up last week?"},+ True,+ None,+ False,+ False,+ ),("a question to a person", {"text": "<@U_ALICE> how many signups last week?"}, True, None, False, False),("a group DM", {"channel_type": "mpim"}, True, None, False, False),("the flag is off", {}, False, None, False, False),