Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cdp): add discord template #25649

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
4c0ba1b
add discord template
MarconLP Oct 17, 2024
a9ecdbb
add test
MarconLP Oct 17, 2024
7c98298
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 17, 2024
cdf47a1
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 17, 2024
f9122a3
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 17, 2024
7ba9e77
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 17, 2024
9731570
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 17, 2024
c29dd27
Update UI snapshots for `chromium` (2)
github-actions[bot] Oct 17, 2024
b0f7b8d
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 17, 2024
0633da5
Update UI snapshots for `chromium` (2)
github-actions[bot] Oct 17, 2024
d32cbb2
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 17, 2024
01af678
Update UI snapshots for `chromium` (2)
github-actions[bot] Oct 17, 2024
6592c23
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 17, 2024
b90aad1
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 17, 2024
c93b4e5
Update UI snapshots for `chromium` (2)
github-actions[bot] Oct 17, 2024
3639e05
Update UI snapshots for `chromium` (2)
github-actions[bot] Oct 17, 2024
ca78056
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 17, 2024
f1427ed
only allow discord webhooks and add test
MarconLP Oct 18, 2024
75a64f2
make destination free
MarconLP Oct 18, 2024
159180d
resolve conflict
MarconLP Oct 18, 2024
c477760
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 18, 2024
4d4ecca
resolve merge conflict
MarconLP Oct 18, 2024
431307f
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added frontend/public/services/discord.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions posthog/cdp/templates/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from .clearbit.template_clearbit import template as clearbit
from .posthog.template_posthog import template as posthog, TemplatePostHogMigrator
from .aws_kinesis.template_aws_kinesis import template as aws_kinesis
from .discord.template_discord import template as discord
from .salesforce.template_salesforce import template_create as salesforce_create, template_update as salesforce_update
from .mailjet.template_mailjet import (
template_create_contact as mailjet_create_contact,
Expand Down Expand Up @@ -43,6 +44,7 @@
braze,
clearbit,
customerio,
discord,
engage,
gleap,
google_ads,
Expand Down
66 changes: 66 additions & 0 deletions posthog/cdp/templates/discord/template_discord.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
from posthog.cdp.templates.hog_function_template import HogFunctionTemplate, HogFunctionSubTemplate, SUB_TEMPLATE_COMMON

template: HogFunctionTemplate = HogFunctionTemplate(
status="free",
id="template-discord",
name="Discord",
description="Sends a message to a discord channel",
icon_url="/static/services/discord.png",
category=["Customer Success"],
hog="""
if (not match(inputs.webhookUrl, '^https://discord.com/api/webhooks/.*')) {
throw Error('Invalid url');
}

let res := fetch(inputs.webhookUrl, {
'body': {
'content': inputs.content
},
'method': 'POST',
'headers': {
'Content-Type': 'application/json'
}
});

if (res.status >= 400) {
throw Error(f'Failed to post message to Discord: {res.status}: {res.body}');
}
""".strip(),
inputs_schema=[
{
"key": "webhookUrl",
"type": "string",
"label": "Webhook URL",
"description": "See this page on how to generate a Webhook URL: https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks",
"secret": False,
"required": True,
},
{
"key": "content",
"type": "string",
"label": "Content",
"description": "(see https://support.discord.com/hc/en-us/articles/210298617-Markdown-Text-101-Chat-Formatting-Bold-Italic-Underline)",
"default": "**{person.name}** triggered event: '{event.event}'",
"secret": False,
"required": True,
},
],
sub_templates=[
HogFunctionSubTemplate(
id="early_access_feature_enrollment",
name="Post to Discord on feature enrollment",
description="Posts a message to Discord when a user enrolls or un-enrolls in an early access feature",
filters=SUB_TEMPLATE_COMMON["early_access_feature_enrollment"].filters,
inputs={
"content": "**{person.name}** {event.properties.$feature_enrollment ? 'enrolled in' : 'un-enrolled from'} the early access feature for '{event.properties.$feature_flag}'"
},
),
HogFunctionSubTemplate(
id="survey_response",
name="Post to Discord on survey response",
description="Posts a message to Discord when a user responds to a survey",
filters=SUB_TEMPLATE_COMMON["survey_response"].filters,
inputs={"content": "**{person.name}** responded to survey **{event.properties.$survey_name}**"},
),
],
)
50 changes: 50 additions & 0 deletions posthog/cdp/templates/discord/test_template_discord.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import pytest
from inline_snapshot import snapshot
from posthog.cdp.templates.helpers import BaseHogFunctionTemplateTest
from posthog.cdp.templates.discord.template_discord import template as template_discord


class TestTemplateDiscord(BaseHogFunctionTemplateTest):
template = template_discord

def _inputs(self, **kwargs):
inputs = {
"webhookUrl": "https://discord.com/api/webhooks/00000000000000000/xxxxxxxxxxxxxx",
"content": "**[email protected]** triggered event: '$pageview'",
}
inputs.update(kwargs)
return inputs

def test_function_works(self):
self.run_function(inputs=self._inputs())

assert self.get_mock_fetch_calls()[0] == snapshot(
(
"https://discord.com/api/webhooks/00000000000000000/xxxxxxxxxxxxxx",
{
"method": "POST",
"headers": {
"Content-Type": "application/json",
},
"body": {
"content": "**[email protected]** triggered event: '$pageview'",
},
},
)
)

def test_only_allow_teams_url(self):
for url, allowed in [
["https://discord.com/api/webhooks/abc", True],
["https://webhook.site/def", False],
["https://webhook.site/def#https://discord.com/api/webhooks/abc", False],
]:
if allowed:
self.run_function(inputs=self._inputs(webhookUrl=url))
assert len(self.get_mock_fetch_calls()) == 1
else:
with pytest.raises(Exception) as e:
self.run_function(inputs=self._inputs(webhookUrl=url))
assert (
e.value.message == "Invalid url" # type: ignore[attr-defined]
)