diff --git a/.sampo/changesets/quiet-beacon-push.md b/.sampo/changesets/quiet-beacon-push.md new file mode 100644 index 000000000..8f80163e8 --- /dev/null +++ b/.sampo/changesets/quiet-beacon-push.md @@ -0,0 +1,5 @@ +--- +pypi/posthog: minor +--- + +Add `posthog.push.send()`, `AsyncPosthog.push.send()` and the standalone `Push` and `AsyncPush` classes to send transactional push notifications from a PostHog push template to every device of one person. Read what happened to a send with `push.get()`. Both use the client's `secret_key` (a project secret API key with the `transactional_push:write` scope), `app_host`, and email request timeout and retries. diff --git a/posthog/async_client.py b/posthog/async_client.py index 7401e3740..e0f5df213 100644 --- a/posthog/async_client.py +++ b/posthog/async_client.py @@ -94,6 +94,7 @@ ) from .consumer import AI_MAX_MSG_SIZE, MAX_MSG_SIZE from .emails import AsyncEmails, _client_emails, _validate_options +from .push import AsyncPush from .feature_flag_evaluations import ( FeatureFlagEvaluations, _EvaluatedFlagRecord, @@ -392,6 +393,27 @@ def emails(self) -> AsyncEmails: ) return self._emails + @property + def push(self) -> AsyncPush: + """ + Send transactional push notifications from PostHog push templates. + + Needs ``secret_key``, a project secret API key with the ``transactional_push:write`` + scope. Sends go where ``emails`` sends go and use the same timeout and + retries. A disabled or shut-down client sends nothing. See ``posthog.push`` + for setup. + + Examples: + ```python + await client.push.send( + template_id="0190f6d2-7c1e-7b3a-9f5e-1c2d3e4f5a6b", + distinct_id="user_123", + template_data={"order_id": "1042"}, + ) + ``` + """ + return AsyncPush._over(self.emails) + def _admit_email_send(self) -> Optional[Callable[[], None]]: # Shutdown waits for in-flight operations before it closes the email transport. if not self._accepting or self._closed: diff --git a/posthog/client.py b/posthog/client.py index 7d4de2c8a..c8b727f05 100644 --- a/posthog/client.py +++ b/posthog/client.py @@ -95,6 +95,7 @@ tag as _context_tag, ) from posthog.emails import Emails, _client_emails, _no_op, _validate_options +from posthog.push import Push from posthog.exception_capture import ExceptionCapture from posthog._logging import _configure_posthog_logging from posthog.exception_utils import ( @@ -1836,6 +1837,27 @@ def emails(self) -> Emails: ) return self._emails + @property + def push(self) -> Push: + """ + Send transactional push notifications from PostHog push templates. + + Needs ``secret_key``, a project secret API key with the ``transactional_push:write`` + scope. Sends go where ``emails`` sends go and use the same timeout and + retries. A disabled or shut-down client sends nothing. See ``posthog.push`` + for setup. + + Examples: + ```python + client.push.send( + template_id="0190f6d2-7c1e-7b3a-9f5e-1c2d3e4f5a6b", + distinct_id="user_123", + template_data={"order_id": "1042"}, + ) + ``` + """ + return Push._over(self.emails) + @property def queue(self) -> Queue: """The analytics lane's queue (kept for backwards compatibility).""" diff --git a/posthog/emails.py b/posthog/emails.py index c180a9224..cecd47be2 100644 --- a/posthog/emails.py +++ b/posthog/emails.py @@ -1455,6 +1455,33 @@ def _deliver(self, prepared: _PreparedSend, secret_key: str) -> SendEmailResult: prepared.max_retries, ) + def _push_call( + self, + method: str, + path: str, + parse: Callable[[int, bytes, Optional[str]], _ResultT], + not_done: str, + key_needed: str, + *, + body: Optional[bytes] = None, + idempotency_key: Optional[str] = None, + max_retries: Optional[int] = None, + ) -> _ResultT: + # posthog.push calls through this, so it gets the same host, key, timeout and retries. + finish = self._start(not_done) + try: + headers = _headers( + self._secret_key(key_needed), + self.migrated_from, + has_body=body is not None, + idempotency_key=idempotency_key, + ) + return self._call( + self._posthog_request(method, path, body, headers), parse, max_retries + ) + finally: + finish() + def _upload(self, prepared: _PreparedSend, secret_key: str) -> None: headers = _headers(secret_key, self.migrated_from, has_body=True) uploads = self._call( @@ -1676,6 +1703,33 @@ async def _deliver( prepared.max_retries, ) + async def _push_call( + self, + method: str, + path: str, + parse: Callable[[int, bytes, Optional[str]], _ResultT], + not_done: str, + key_needed: str, + *, + body: Optional[bytes] = None, + idempotency_key: Optional[str] = None, + max_retries: Optional[int] = None, + ) -> _ResultT: + # posthog.push calls through this, so it gets the same host, key, timeout and retries. + finish = self._start(not_done) + try: + headers = _headers( + self._secret_key(key_needed), + self.migrated_from, + has_body=body is not None, + idempotency_key=idempotency_key, + ) + return await self._call( + self._posthog_request(method, path, body, headers), parse, max_retries + ) + finally: + finish() + async def _upload(self, prepared: _PreparedSend, secret_key: str) -> None: headers = _headers(secret_key, self.migrated_from, has_body=True) uploads = await self._call( diff --git a/posthog/push.py b/posthog/push.py new file mode 100644 index 000000000..4ba9de8a7 --- /dev/null +++ b/posthog/push.py @@ -0,0 +1,493 @@ +"""Transactional push notifications, sent from a push template in PostHog. + +Setup: + +1. In PostHog, open the push notification under Workflows, make it sendable + from the API, and copy its ID. ``template_id`` is that UUID. +2. Create a project secret API key (``phs_...``) with the + ``transactional_push:write`` scope in your project settings. +3. Send from the client you already use, or from :class:`Push` in a process + that only sends notifications: + +```python +from posthog import Posthog + +posthog = Posthog("phc_...", secret_key="phs_...", host="https://eu.i.posthog.com") +result = posthog.push.send( + template_id="