Skip to content

Commit a729089

Browse files
committed
send demo to common room
1 parent 661467d commit a729089

File tree

2 files changed

+167
-134
lines changed

2 files changed

+167
-134
lines changed

reflex_ui/blocks/demo_form.py

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
based on company size.
66
"""
77

8+
import asyncio
89
import os
910
import urllib.parse
11+
import uuid
12+
from collections.abc import Sequence
1013
from dataclasses import asdict, dataclass
1114
from typing import Any
1215

@@ -18,7 +21,10 @@
1821
import reflex_ui as ui
1922

2023
demo_form_error_message = ClientStateVar.create("demo_form_error_message", "")
24+
is_sending_demo_form = ClientStateVar.create("is_sending_demo_form", False)
2125

26+
COMMONROOM_DESTINATION_ID = os.getenv("COMMONROOM_DESTINATION_ID", "")
27+
COMMONROOM_API_TOKEN = os.getenv("COMMONROOM_API_TOKEN", "")
2228
CAL_REQUEST_DEMO_URL = os.getenv(
2329
"CAL_REQUEST_DEMO_URL", "https://cal.com/team/reflex/reflex-intro"
2430
)
@@ -253,6 +259,7 @@ async def on_submit(self, form_data: dict[str, Any]):
253259
"Please select how did you hear about us"
254260
)
255261
return
262+
yield is_sending_demo_form.push(True)
256263
# Send to PostHog and Slack for all submissions
257264
await self.send_demo_event(form_data)
258265

@@ -285,6 +292,7 @@ async def on_submit(self, form_data: dict[str, Any]):
285292
query_string = urllib.parse.urlencode(params)
286293
cal_url_with_params = f"{CAL_ENTERPRISE_FOLLOW_UP_URL}?{query_string}"
287294

295+
yield is_sending_demo_form.push(False)
288296
yield rx.redirect(cal_url_with_params)
289297

290298
async def send_demo_event(self, form_data: dict[str, Any]):
@@ -312,13 +320,12 @@ async def send_demo_event(self, form_data: dict[str, Any]):
312320
phone_number=form_data.get("phone_number", ""),
313321
)
314322

315-
# Send to PostHog
316-
await self.send_data_to_posthog(demo_event)
317-
318-
try:
319-
await self.send_data_to_slack(demo_event)
320-
except Exception as e:
321-
log(f"Failed to send to Slack: {e}")
323+
# Send data to PostHog, Common Room, and Slack
324+
await asyncio.gather(
325+
self.send_data_to_posthog(demo_event),
326+
self.send_data_to_common_room(demo_event),
327+
self.send_data_to_slack(demo_event),
328+
)
322329

323330
async def send_data_to_posthog(self, event_instance: PosthogEvent):
324331
"""Send data to PostHog using class introspection.
@@ -343,6 +350,41 @@ async def send_data_to_posthog(self, event_instance: PosthogEvent):
343350
except Exception:
344351
log("Error sending data to PostHog")
345352

353+
async def send_data_to_common_room(self, event_instance: DemoEvent):
354+
"""Update CommonRoom with user login information."""
355+
tags: Sequence[str] = [
356+
"Requested Demo",
357+
]
358+
359+
try:
360+
async with httpx.AsyncClient() as client:
361+
await client.post(
362+
f"https://api.commonroom.io/community/v1/source/{COMMONROOM_DESTINATION_ID}/activity",
363+
headers={
364+
"Content-Type": "application/json",
365+
"Authorization": f"Bearer {COMMONROOM_API_TOKEN}",
366+
},
367+
json={
368+
"id": "requested_demo",
369+
"activityType": "requested_demo",
370+
"user": {
371+
"id": str(uuid.uuid4()),
372+
"email": event_instance.company_email,
373+
},
374+
"tags": [
375+
{
376+
"type": "name",
377+
"name": tag,
378+
}
379+
for tag in tags
380+
],
381+
},
382+
)
383+
except Exception as ex:
384+
log(
385+
f"CommonRoom: Failed to identify user with email {event_instance.company_email}, err: {ex}"
386+
)
387+
346388
async def send_data_to_slack(self, event_instance: DemoEvent):
347389
"""Send demo form data to Slack webhook.
348390
@@ -447,7 +489,12 @@ def get_component(cls, **props):
447489
class_name="text-destructive-10 text-sm font-medium",
448490
),
449491
),
450-
ui.button("Submit", type="submit", class_name="w-full"),
492+
ui.button(
493+
"Submit",
494+
type="submit",
495+
class_name="w-full",
496+
loading=is_sending_demo_form.value,
497+
),
451498
on_submit=cls.on_submit,
452499
class_name=ui.cn(
453500
"@container flex flex-col gap-6 p-6",

0 commit comments

Comments
 (0)