-
Notifications
You must be signed in to change notification settings - Fork 3.4k
feat(data-warehouse): add Intercom and HubSpot as self-driving inbox sources #72535
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1bc3a54
feat(data-warehouse): add Intercom and HubSpot as self-driving inbox …
Gilbert09 32258dd
chore: update OpenAPI generated types
Gilbert09 9bccb0a
test(mcp): update unit test snapshots
tests-posthog[bot] 44e35e0
chore(signals): format migration 0065
Gilbert09 b92a121
chore: update OpenAPI generated types
tests-posthog[bot] 6bb7a0e
chore(signals): fix markdown emphasis in adding-inbox-sources skill
Gilbert09 8117b0f
Merge branch 'master' into tom/inbox-oauth-sources
Gilbert09 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| """Signal emitter for hubspot `tickets` (record kind: ticket). | ||
|
|
||
| HubSpot is an OAuth-connected source, but its `tickets` table is flat, so the shared factory | ||
| applies. `hs_object_id` is the record id, `subject`/`content` the text, `createdate` an ISO | ||
| string. Only the ticket properties the user selected during setup are synced; the defaults | ||
| include subject/content/createdate. | ||
| """ | ||
|
|
||
| from products.signals.backend.emission._common import make_flat_emitter | ||
| from products.signals.backend.emission._prompts import TICKET_ACTIONABILITY_PROMPT, TICKET_SUMMARIZATION_PROMPT | ||
| from products.signals.backend.emission.fetchers.data_warehouse import data_warehouse_record_fetcher | ||
| from products.signals.backend.emission.registry import SignalSourceTableConfig | ||
|
|
||
| HUBSPOT_FIELDS = ( | ||
| "hs_object_id", | ||
| "subject", | ||
| "content", | ||
| "hs_ticket_priority", | ||
| "hs_pipeline_stage", | ||
| "hs_ticket_category", | ||
| "createdate", | ||
| ) | ||
|
|
||
| HUBSPOT_CONFIG = SignalSourceTableConfig( | ||
| source_product="hubspot", | ||
| source_type="ticket", | ||
| emitter=make_flat_emitter( | ||
| source_product="hubspot", | ||
| source_type="ticket", | ||
| id_field="hs_object_id", | ||
| title_field="subject", | ||
| body_field="content", | ||
| extra_fields=("hs_ticket_priority", "hs_pipeline_stage", "hs_ticket_category", "createdate"), | ||
| ), | ||
| record_fetcher=data_warehouse_record_fetcher, | ||
| partition_field="createdate", | ||
| partition_field_is_datetime_string=True, | ||
| fields=HUBSPOT_FIELDS, | ||
| max_records=200, | ||
| first_sync_lookback_days=1, | ||
| actionability_prompt=TICKET_ACTIONABILITY_PROMPT, | ||
| summarization_prompt=TICKET_SUMMARIZATION_PROMPT, | ||
| description_summarization_threshold_chars=2000, | ||
| ) |
103 changes: 103 additions & 0 deletions
103
products/signals/backend/emission/intercom_conversations.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| """Signal emitter for intercom `conversations` (record kind: ticket). | ||
|
|
||
| Intercom is OAuth-connected and its `conversations` table isn't flat, so this is a bespoke | ||
| emitter (like Jira's): | ||
| - The opening message isn't a top-level column — it lives in the `source` JSON blob | ||
| (`source.body`, HTML). `title` is often null, so we fall back to the stripped source body. | ||
| The rest of the thread is in `conversation_parts` (a separate table we don't sync). | ||
| - `created_at` is a Unix epoch (seconds), so the partition cursor wraps it in | ||
| `fromUnixTimestamp(...)`. Verify the stored type on the first real sync. | ||
| """ | ||
|
|
||
| import re | ||
| from typing import Any | ||
|
|
||
| from structlog import get_logger | ||
|
|
||
| from products.signals.backend.emission._prompts import TICKET_ACTIONABILITY_PROMPT, TICKET_SUMMARIZATION_PROMPT | ||
| from products.signals.backend.emission.fetchers.data_warehouse import data_warehouse_record_fetcher | ||
| from products.signals.backend.emission.registry import SignalEmitterOutput, SignalSourceTableConfig | ||
|
|
||
| logger = get_logger(__name__) | ||
|
|
||
| INTERCOM_IGNORED_STATES = ("closed",) | ||
|
|
||
| FIELDS = ( | ||
| "id", | ||
| "title", | ||
| "state", | ||
| "priority", | ||
| "admin_assignee_id", | ||
| "created_at", | ||
| "JSONExtractString(source, 'body') AS source_body", | ||
| ) | ||
|
|
||
| _TAG_RE = re.compile(r"<[^>]+>") | ||
|
|
||
|
|
||
| def _strip_html(raw: Any) -> str: | ||
| if not raw or not isinstance(raw, str): | ||
| return "" | ||
| return _TAG_RE.sub(" ", raw).replace(" ", " ").strip() | ||
|
|
||
|
|
||
| def intercom_conversation_emitter(team_id: int, record: dict[str, Any]) -> SignalEmitterOutput | None: | ||
| try: | ||
| conversation_id = record["id"] | ||
| except KeyError as e: | ||
| msg = f"Intercom conversation record missing required field {e}" | ||
| logger.exception(msg, record=record, team_id=team_id, signals_type="data-import-signals") | ||
| raise ValueError(msg) from e | ||
| if not conversation_id: | ||
| msg = f"Intercom conversation record has empty id: {conversation_id!r}" | ||
| logger.exception(msg, record=record, team_id=team_id, signals_type="data-import-signals") | ||
| raise ValueError(msg) | ||
|
|
||
| title = record.get("title") | ||
| body = _strip_html(record.get("source_body")) | ||
| # Prefer an explicit subject; otherwise use the opening message. Skip conversations with no | ||
| # readable text (auto-created/empty ones can't produce a useful signal). | ||
| description = title or body | ||
| if not description: | ||
| logger.info( | ||
| "Ignoring Intercom conversation without text", | ||
| team_id=team_id, | ||
| signals_type="data-import-signals", | ||
| ) | ||
| return None | ||
| if title and body: | ||
| description = f"{title}\n{body}" | ||
|
|
||
| return SignalEmitterOutput( | ||
| source_product="intercom", | ||
| source_type="ticket", | ||
| source_id=str(conversation_id), | ||
| description=description, | ||
| weight=1.0, | ||
| extra={ | ||
| "state": record.get("state") or None, | ||
| "priority": record.get("priority") or None, | ||
| "admin_assignee_id": ( | ||
| str(record["admin_assignee_id"]) if record.get("admin_assignee_id") is not None else None | ||
| ), | ||
| "created_at": str(record["created_at"]) if record.get("created_at") is not None else None, | ||
| }, | ||
| ) | ||
|
|
||
|
|
||
| INTERCOM_CONFIG = SignalSourceTableConfig( | ||
| source_product="intercom", | ||
| source_type="ticket", | ||
| emitter=intercom_conversation_emitter, | ||
| record_fetcher=data_warehouse_record_fetcher, | ||
| # created_at is a Unix epoch (seconds). Wrap so the cursor compares as a datetime; verify the | ||
| # stored column type on the first real sync (it may already be a DateTime). | ||
| partition_field="fromUnixTimestamp(toUInt32(created_at))", | ||
| fields=FIELDS, | ||
| where_clause=f"state NOT IN ({', '.join(repr(s) for s in INTERCOM_IGNORED_STATES)})", | ||
| max_records=200, | ||
| first_sync_lookback_days=1, | ||
| actionability_prompt=TICKET_ACTIONABILITY_PROMPT, | ||
| summarization_prompt=TICKET_SUMMARIZATION_PROMPT, | ||
| description_summarization_threshold_chars=2000, | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.