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(messaging): actually send the message #25830

Closed
wants to merge 38 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
bc5e8a0
feat(messaging): actually send the message
mariusandra Oct 25, 2024
c5aa62b
Update query snapshots
github-actions[bot] Oct 25, 2024
c360b17
fix typo
mariusandra Oct 25, 2024
a742658
fix another flaker
mariusandra Oct 25, 2024
c60c167
Merge branch 'messaging-part-4' of github.com:PostHog/posthog into me…
mariusandra Oct 25, 2024
f012afe
remove broadcast code
mariusandra Oct 25, 2024
56bc999
remove broadcast code
mariusandra Oct 25, 2024
2594667
Revert "remove broadcast code"
mariusandra Oct 25, 2024
a59821a
Update query snapshots
github-actions[bot] Oct 25, 2024
acca2b8
Update query snapshots
github-actions[bot] Oct 25, 2024
0c573dd
Update UI snapshots for `chromium` (2)
github-actions[bot] Oct 25, 2024
dfd226f
Update UI snapshots for `chromium` (2)
github-actions[bot] Oct 25, 2024
7b3cc3b
Merge branch 'master' into messaging-part-4
mariusandra Oct 25, 2024
6c0903c
no change
mariusandra Oct 25, 2024
77dfb8b
fixes
mariusandra Oct 25, 2024
7b8a4c8
maybe not
mariusandra Oct 25, 2024
1e5341e
Update query snapshots
github-actions[bot] Oct 25, 2024
739d8bb
Update query snapshots
github-actions[bot] Oct 25, 2024
26292c5
test calling sendEmail for providers on invocation test
mariusandra Oct 25, 2024
57345ed
test email provider import
mariusandra Oct 25, 2024
800739a
test calling imported code
mariusandra Oct 25, 2024
4c43ca0
fistbump
mariusandra Oct 25, 2024
7d65366
Merge branch 'messaging-part-4' of github.com:PostHog/posthog into me…
mariusandra Oct 25, 2024
63964f7
Merge branch 'master' into messaging-part-4
mariusandra Oct 25, 2024
13693da
Revert "Update query snapshots"
mariusandra Oct 25, 2024
384beee
Revert "Update query snapshots"
mariusandra Oct 25, 2024
8776a7b
Revert "Update query snapshots"
mariusandra Oct 25, 2024
e1c3154
Merge branch 'messaging-part-4' into messaging-part-5
mariusandra Oct 25, 2024
ccbdc8c
Revert "remove broadcast code"
mariusandra Oct 25, 2024
48dd38e
Update query snapshots
github-actions[bot] Oct 25, 2024
bb75a1e
Update query snapshots
github-actions[bot] Oct 25, 2024
0d01164
Update query snapshots
github-actions[bot] Oct 25, 2024
2631623
Merge branch 'master' into messaging-part-5
mariusandra Oct 26, 2024
2dc8805
Update query snapshots
github-actions[bot] Oct 26, 2024
ebe75b2
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 26, 2024
9cfc283
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 26, 2024
fb7be49
Update query snapshots
github-actions[bot] Oct 26, 2024
b0be5d3
Fixes
benjackwhite Oct 30, 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
3 changes: 3 additions & 0 deletions frontend/src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1760,6 +1760,9 @@ const api = {
async update(id: HogFunctionType['id'], data: Partial<HogFunctionType>): Promise<HogFunctionType> {
return await new ApiRequest().hogFunction(id).update({ data })
},
async sendBroadcast(id: HogFunctionType['id']): Promise<HogFunctionType> {
return await new ApiRequest().hogFunction(id).withAction('broadcast').create()
},
async logs(
id: HogFunctionType['id'],
params: LogEntryRequestParams = {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export function HogFunctionConfiguration({ templateId, id }: HogFunctionConfigur
templateHasChanged,
forcedSubTemplateId,
type,
broadcastLoading,
} = useValues(logic)
const {
submitConfiguration,
Expand All @@ -80,6 +81,7 @@ export function HogFunctionConfiguration({ templateId, id }: HogFunctionConfigur
setConfigurationValue,
deleteHogFunction,
setSubTemplateId,
sendBroadcast,
} = useActions(logic)

if (loading && !loaded) {
Expand Down Expand Up @@ -210,8 +212,6 @@ export function HogFunctionConfiguration({ templateId, id }: HogFunctionConfigur
{template && <DestinationTag status={template.status} />}
</div>

<HogFunctionStatusIndicator hogFunction={hogFunction} />

{showEnabled && <HogFunctionStatusIndicator hogFunction={hogFunction} />}
{showEnabled && (
<LemonField name="enabled">
Expand Down Expand Up @@ -493,6 +493,37 @@ export function HogFunctionConfiguration({ templateId, id }: HogFunctionConfigur
)}

{!id || id === 'new' ? <HogFunctionTestPlaceholder /> : <HogFunctionTest id={id} />}

{type === 'broadcast' ? (
id && id !== 'new' ? (
<HogFunctionTestPlaceholder
title="Send broadcast"
description={
<div className="space-y-2">
<LemonButton
type="primary"
onClick={sendBroadcast}
loading={personsCountLoading || broadcastLoading}
>
Send to {personsCount} emails
</LemonButton>
<div>
<strong>Please note:</strong> Clicking the button above will
synchronously send to all the e-mails. While this is fine for
testing with small lists, please don't use this for production
usecases yet.
</div>
</div>
}
/>
) : (
<HogFunctionTestPlaceholder
title="Send broadcast"
description="Save to send your broadcast"
/>
)
) : null}

<div className="flex gap-2 justify-end">{saveButtons}</div>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ export const hogFunctionConfigurationLogic = kea<hogFunctionConfigurationLogicTy
setUnsavedConfiguration: (configuration: HogFunctionConfigurationType | null) => ({ configuration }),
persistForUnload: true,
setSampleGlobalsError: (error) => ({ error }),
sendBroadcast: true,
}),
reducers({
showSource: [
Expand Down Expand Up @@ -402,6 +403,22 @@ export const hogFunctionConfigurationLogic = kea<hogFunctionConfigurationLogicTy
},
},
],

broadcast: [
false,
{
sendBroadcast: async () => {
const id = values.hogFunction?.id
if (!id) {
lemonToast.error('No broadcast to send')
return false
}
await api.hogFunctions.sendBroadcast(id)
lemonToast.success('Broadcast sent!')
return true
},
},
],
})),
forms(({ values, props, asyncActions }) => ({
configuration: {
Expand Down
31 changes: 31 additions & 0 deletions posthog/api/hog_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from posthog.cdp.templates import HOG_FUNCTION_TEMPLATES_BY_ID
from posthog.cdp.validation import compile_hog, generate_template_bytecode, validate_inputs, validate_inputs_schema
from posthog.constants import AvailableFeature
from posthog.hogql_queries.actors_query_runner import ActorsQueryRunner
from posthog.models.activity_logging.activity_log import log_activity, changes_between, Detail
from posthog.models.hog_functions.hog_function import HogFunction, HogFunctionState
from posthog.plugins.plugin_server_api import create_hog_invocation_test
Expand Down Expand Up @@ -305,6 +306,36 @@ def invocations(self, request: Request, *args, **kwargs):

return Response(res.json())

@action(detail=True, methods=["POST"])
def broadcast(self, request: Request, *args, **kwargs):
hog_function = self.get_object()
actors_query = {
"kind": "ActorsQuery",
"properties": hog_function.filters.get("properties", None),
"select": ["id", "any(pdi.distinct_id)", "properties", "created_at"],
}

response = ActorsQueryRunner(query=actors_query, team=self.team).calculate()

for result in response.results:
globals = {
"person": {
"id": str(result[0]),
"distinct_id": str(result[1]),
"properties": json.loads(result[2]),
"created_at": result[3].isoformat(),
}
}
create_hog_invocation_test(
team_id=hog_function.team_id,
hog_function_id=hog_function.id,
globals=globals,
configuration=HogFunctionSerializer(hog_function).data,
mock_async_functions=False,
)

return Response({"success": True})

def perform_create(self, serializer):
serializer.save()
log_activity(
Expand Down
2 changes: 1 addition & 1 deletion posthog/api/test/__snapshots__/test_api_docs.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@
'/home/runner/work/posthog/posthog/posthog/api/survey.py: Warning [SurveyViewSet > SurveySerializer]: unable to resolve type hint for function "get_conditions". Consider using a type hint or @extend_schema_field. Defaulting to string.',
'/home/runner/work/posthog/posthog/posthog/api/web_experiment.py: Warning [WebExperimentViewSet]: could not derive type of path parameter "project_id" because model "posthog.models.web_experiment.WebExperiment" contained no such field. Consider annotating parameter with @extend_schema. Defaulting to "string".',
'Warning: encountered multiple names for the same choice set (HrefMatchingEnum). This may be unwanted even though the generated schema is technically correct. Add an entry to ENUM_NAME_OVERRIDES to fix the naming.',
'Warning: enum naming encountered a non-optimally resolvable collision for fields named "kind". The same name has been used for multiple choice sets in multiple components. The collision was resolved with "KindCfaEnum". add an entry to ENUM_NAME_OVERRIDES to fix the naming.',
'Warning: enum naming encountered a non-optimally resolvable collision for fields named "kind". The same name has been used for multiple choice sets in multiple components. The collision was resolved with "Kind069Enum". add an entry to ENUM_NAME_OVERRIDES to fix the naming.',
'Warning: enum naming encountered a non-optimally resolvable collision for fields named "kind". The same name has been used for multiple choice sets in multiple components. The collision was resolved with "KindCfaEnum". add an entry to ENUM_NAME_OVERRIDES to fix the naming.',
'Warning: enum naming encountered a non-optimally resolvable collision for fields named "type". The same name has been used for multiple choice sets in multiple components. The collision was resolved with "TypeF73Enum". add an entry to ENUM_NAME_OVERRIDES to fix the naming.',
'Warning: encountered multiple names for the same choice set (EffectivePrivilegeLevelEnum). This may be unwanted even though the generated schema is technically correct. Add an entry to ENUM_NAME_OVERRIDES to fix the naming.',
'Warning: encountered multiple names for the same choice set (MembershipLevelEnum). This may be unwanted even though the generated schema is technically correct. Add an entry to ENUM_NAME_OVERRIDES to fix the naming.',
Expand Down
4 changes: 2 additions & 2 deletions posthog/cdp/templates/_internal/template_broadcast.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
status="beta",
type="broadcast",
id="template-new-broadcast",
name="Hello !",
description="This is a broadcast",
name="Broadcast message",
description="Send an email to a person",
icon_url="/static/hedgehog/explorer-hog.png",
category=["Email Marketing"],
hog="""import('provider/email').sendEmail(inputs.email)""".strip(),
Expand Down