diff --git a/posthog/tasks/test/test_webhook_message_formatting.py b/posthog/tasks/test/test_webhook_message_formatting.py index 68cb5af69a5..0d880df054b 100644 --- a/posthog/tasks/test/test_webhook_message_formatting.py +++ b/posthog/tasks/test/test_webhook_message_formatting.py @@ -90,16 +90,18 @@ class TestWebhookMessage(BaseTest): def test_get_formatted_message(self) -> None: self.team.slack_incoming_webhook = "https://hooks.slack.com/services/" - event1 = Event.objects.create(team=self.team, distinct_id="2", properties={"$browser": "Chrome"}) + event1 = Event.objects.create( + team=self.team, distinct_id="2", properties={"$browser": "Chrome", "page_title": "Pricing"} + ) action1 = Action.objects.create( team=self.team, name="action1", id=1, - slack_message_format="[user.name] did action from browser [user.browser]", + slack_message_format="[user.name] from [user.browser] on [event.properties.page_title] page with [event.properties.fruit]", ) text, markdown = get_formatted_message(action1, event1, "https://localhost:8000") - self.assertEqual(text, "2 did action from browser Chrome") + self.assertEqual(text, "2 from Chrome on Pricing page with undefined") def test_get_formatted_message_default(self) -> None: """ diff --git a/posthog/tasks/webhooks.py b/posthog/tasks/webhooks.py index 0ff395dde61..40a4262e82f 100644 --- a/posthog/tasks/webhooks.py +++ b/posthog/tasks/webhooks.py @@ -1,3 +1,4 @@ +import json import re from typing import Tuple @@ -57,6 +58,14 @@ def get_value_of_token(action: Action, event: Event, site_url: str, token_parts: elif token_parts[0] == "event": if token_parts[1] == "name": text = markdown = event.event + elif token_parts[1] == "properties" and len(token_parts) > 2: + property_name = token_parts[2] + if property_name in event.properties: + property = event.properties[property_name] + text = markdown = property if isinstance(property, str) else json.dumps(property) + else: + text = markdown = "undefined" + else: raise ValueError return text, markdown