render-lab-tasks-intercom 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- render_lab_tasks_intercom-0.1.0/.gitignore +11 -0
- render_lab_tasks_intercom-0.1.0/LICENSE +21 -0
- render_lab_tasks_intercom-0.1.0/PKG-INFO +79 -0
- render_lab_tasks_intercom-0.1.0/README.md +65 -0
- render_lab_tasks_intercom-0.1.0/pyproject.toml +23 -0
- render_lab_tasks_intercom-0.1.0/src/render_lab_tasks_intercom/__init__.py +1 -0
- render_lab_tasks_intercom-0.1.0/src/render_lab_tasks_intercom/_app.py +3 -0
- render_lab_tasks_intercom-0.1.0/src/render_lab_tasks_intercom/add_note.py +18 -0
- render_lab_tasks_intercom-0.1.0/src/render_lab_tasks_intercom/assign_conversation.py +18 -0
- render_lab_tasks_intercom-0.1.0/src/render_lab_tasks_intercom/client.py +192 -0
- render_lab_tasks_intercom-0.1.0/src/render_lab_tasks_intercom/close_conversation.py +18 -0
- render_lab_tasks_intercom-0.1.0/src/render_lab_tasks_intercom/create_ticket.py +18 -0
- render_lab_tasks_intercom-0.1.0/src/render_lab_tasks_intercom/get_contact.py +18 -0
- render_lab_tasks_intercom-0.1.0/src/render_lab_tasks_intercom/get_conversation.py +18 -0
- render_lab_tasks_intercom-0.1.0/src/render_lab_tasks_intercom/py.typed +0 -0
- render_lab_tasks_intercom-0.1.0/src/render_lab_tasks_intercom/reply.py +18 -0
- render_lab_tasks_intercom-0.1.0/src/render_lab_tasks_intercom/retry.py +3 -0
- render_lab_tasks_intercom-0.1.0/src/render_lab_tasks_intercom/search_conversations.py +20 -0
- render_lab_tasks_intercom-0.1.0/src/render_lab_tasks_intercom/tag_user.py +18 -0
- render_lab_tasks_intercom-0.1.0/src/render_lab_tasks_intercom/tasks.py +35 -0
- render_lab_tasks_intercom-0.1.0/src/render_lab_tasks_intercom/types.py +100 -0
- render_lab_tasks_intercom-0.1.0/src/render_lab_tasks_intercom/update_contact.py +18 -0
- render_lab_tasks_intercom-0.1.0/src/render_lab_tasks_intercom/webhooks.py +40 -0
- render_lab_tasks_intercom-0.1.0/tests/test_contracts.py +8 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Render Lab
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: render-lab-tasks-intercom
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: intercom tasks for Render Workflows
|
|
5
|
+
Project-URL: Repository, https://github.com/render-lab/render-tasks-python
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Requires-Python: >=3.12
|
|
9
|
+
Requires-Dist: httpx<0.29,>=0.28
|
|
10
|
+
Requires-Dist: render-lab-tasks-core<0.2,>=0.1.1
|
|
11
|
+
Requires-Dist: render-lab-triggers<0.2,>=0.1.0
|
|
12
|
+
Requires-Dist: render==1.0.1
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
|
|
15
|
+
# render-lab-tasks-intercom
|
|
16
|
+
|
|
17
|
+
Python port of all 10 registered tasks in the pinned TypeScript pack at
|
|
18
|
+
`45f9c2d44bd28e01ae9813e0d2ff56ee6c533816`. Python 3.12+.
|
|
19
|
+
|
|
20
|
+
From the repository, install this pack and its supporting core together:
|
|
21
|
+
|
|
22
|
+
```sh
|
|
23
|
+
pip install render-lab-tasks-intercom==0.1.0
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Import task definitions from `render_lab_tasks_intercom.tasks`. Combine its `app` with
|
|
27
|
+
other pack apps using `Workflows.from_workflows(...)` and call tasks with
|
|
28
|
+
`await ctx.run(task, input)`. Package roots do not register tasks. JSON fields
|
|
29
|
+
retain the TS spelling; Python functions use snake_case.
|
|
30
|
+
|
|
31
|
+
## Tasks
|
|
32
|
+
|
|
33
|
+
| Registered name | Python export (also exposes `_impl`) |
|
|
34
|
+
| --- | --- |
|
|
35
|
+
| `intercom.addNote` | `add_note` |
|
|
36
|
+
| `intercom.assignConversation` | `assign_conversation` |
|
|
37
|
+
| `intercom.closeConversation` | `close_conversation` |
|
|
38
|
+
| `intercom.createTicket` | `create_ticket` |
|
|
39
|
+
| `intercom.getContact` | `get_contact` |
|
|
40
|
+
| `intercom.getConversation` | `get_conversation` |
|
|
41
|
+
| `intercom.reply` | `reply` |
|
|
42
|
+
| `intercom.searchConversations` | `search_conversations` |
|
|
43
|
+
| `intercom.tagUser` | `tag_user` |
|
|
44
|
+
| `intercom.updateContact` | `update_contact` |
|
|
45
|
+
|
|
46
|
+
## Dependencies and retries
|
|
47
|
+
|
|
48
|
+
Raw implementations accept context first and keyword-only `deps`. The `.client`
|
|
49
|
+
module exports typed `Port`, `Deps`, and `Client` classes (also vendor-named aliases).
|
|
50
|
+
Inject a fake port, or pass a caller-owned `httpx.AsyncClient` to `Client` for custom
|
|
51
|
+
transport/configuration. Default dependencies close their HTTP clients after the
|
|
52
|
+
operation. Credentials resolve at first use. HTTP and SDK retries are disabled;
|
|
53
|
+
`retry.py` owns durable retry settings. DTO types live in `.types`.
|
|
54
|
+
|
|
55
|
+
## Environment
|
|
56
|
+
|
|
57
|
+
| Variable | Contract |
|
|
58
|
+
| --- | --- |
|
|
59
|
+
| `INTERCOM_ACCESS_TOKEN` | Required on API calls. |
|
|
60
|
+
|
|
61
|
+
## Verification and limitations
|
|
62
|
+
|
|
63
|
+
The reply and ticket payloads preserve the pinned TS surface. The limited ticket DTO does not expose ticket type configuration; reply does not expose adminId (addNote does). Validate these inherited limitations against your Intercom account before live use.
|
|
64
|
+
|
|
65
|
+
Shared JSON fixtures compare the Python task output with the actual pinned TS
|
|
66
|
+
implementation. REST fixtures also compare HTTP requests; HubSpot and Linear
|
|
67
|
+
fixtures compare source SDK arguments with HTTP payloads/GraphQL variables.
|
|
68
|
+
No live vendor calls were made for this batch. Registered-task parity does not
|
|
69
|
+
claim live readiness or all supporting TS exports. See the root verification
|
|
70
|
+
tracker and docs/porting-batches.md. Licensed under MIT.
|
|
71
|
+
|
|
72
|
+
## Webhook adapter
|
|
73
|
+
|
|
74
|
+
`render_lab_tasks_intercom.webhooks.intercom_adapter(on_event, ...)` returns a
|
|
75
|
+
registration-free adapter for `render_lab_triggers`. The callback receives the
|
|
76
|
+
verified event and returns `{"task": "namespace.task", "args": [event]}` or `None`.
|
|
77
|
+
The signing credential `INTERCOM_CLIENT_SECRET` is read inside verification unless
|
|
78
|
+
provided explicitly. Importing this module does not register tasks. Signature
|
|
79
|
+
checks use the raw body; live delivery remains in the testing backlog.
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# render-lab-tasks-intercom
|
|
2
|
+
|
|
3
|
+
Python port of all 10 registered tasks in the pinned TypeScript pack at
|
|
4
|
+
`45f9c2d44bd28e01ae9813e0d2ff56ee6c533816`. Python 3.12+.
|
|
5
|
+
|
|
6
|
+
From the repository, install this pack and its supporting core together:
|
|
7
|
+
|
|
8
|
+
```sh
|
|
9
|
+
pip install render-lab-tasks-intercom==0.1.0
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
Import task definitions from `render_lab_tasks_intercom.tasks`. Combine its `app` with
|
|
13
|
+
other pack apps using `Workflows.from_workflows(...)` and call tasks with
|
|
14
|
+
`await ctx.run(task, input)`. Package roots do not register tasks. JSON fields
|
|
15
|
+
retain the TS spelling; Python functions use snake_case.
|
|
16
|
+
|
|
17
|
+
## Tasks
|
|
18
|
+
|
|
19
|
+
| Registered name | Python export (also exposes `_impl`) |
|
|
20
|
+
| --- | --- |
|
|
21
|
+
| `intercom.addNote` | `add_note` |
|
|
22
|
+
| `intercom.assignConversation` | `assign_conversation` |
|
|
23
|
+
| `intercom.closeConversation` | `close_conversation` |
|
|
24
|
+
| `intercom.createTicket` | `create_ticket` |
|
|
25
|
+
| `intercom.getContact` | `get_contact` |
|
|
26
|
+
| `intercom.getConversation` | `get_conversation` |
|
|
27
|
+
| `intercom.reply` | `reply` |
|
|
28
|
+
| `intercom.searchConversations` | `search_conversations` |
|
|
29
|
+
| `intercom.tagUser` | `tag_user` |
|
|
30
|
+
| `intercom.updateContact` | `update_contact` |
|
|
31
|
+
|
|
32
|
+
## Dependencies and retries
|
|
33
|
+
|
|
34
|
+
Raw implementations accept context first and keyword-only `deps`. The `.client`
|
|
35
|
+
module exports typed `Port`, `Deps`, and `Client` classes (also vendor-named aliases).
|
|
36
|
+
Inject a fake port, or pass a caller-owned `httpx.AsyncClient` to `Client` for custom
|
|
37
|
+
transport/configuration. Default dependencies close their HTTP clients after the
|
|
38
|
+
operation. Credentials resolve at first use. HTTP and SDK retries are disabled;
|
|
39
|
+
`retry.py` owns durable retry settings. DTO types live in `.types`.
|
|
40
|
+
|
|
41
|
+
## Environment
|
|
42
|
+
|
|
43
|
+
| Variable | Contract |
|
|
44
|
+
| --- | --- |
|
|
45
|
+
| `INTERCOM_ACCESS_TOKEN` | Required on API calls. |
|
|
46
|
+
|
|
47
|
+
## Verification and limitations
|
|
48
|
+
|
|
49
|
+
The reply and ticket payloads preserve the pinned TS surface. The limited ticket DTO does not expose ticket type configuration; reply does not expose adminId (addNote does). Validate these inherited limitations against your Intercom account before live use.
|
|
50
|
+
|
|
51
|
+
Shared JSON fixtures compare the Python task output with the actual pinned TS
|
|
52
|
+
implementation. REST fixtures also compare HTTP requests; HubSpot and Linear
|
|
53
|
+
fixtures compare source SDK arguments with HTTP payloads/GraphQL variables.
|
|
54
|
+
No live vendor calls were made for this batch. Registered-task parity does not
|
|
55
|
+
claim live readiness or all supporting TS exports. See the root verification
|
|
56
|
+
tracker and docs/porting-batches.md. Licensed under MIT.
|
|
57
|
+
|
|
58
|
+
## Webhook adapter
|
|
59
|
+
|
|
60
|
+
`render_lab_tasks_intercom.webhooks.intercom_adapter(on_event, ...)` returns a
|
|
61
|
+
registration-free adapter for `render_lab_triggers`. The callback receives the
|
|
62
|
+
verified event and returns `{"task": "namespace.task", "args": [event]}` or `None`.
|
|
63
|
+
The signing credential `INTERCOM_CLIENT_SECRET` is read inside verification unless
|
|
64
|
+
provided explicitly. Importing this module does not register tasks. Signature
|
|
65
|
+
checks use the raw body; live delivery remains in the testing backlog.
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling>=1.27,<2"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "render-lab-tasks-intercom"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "intercom tasks for Render Workflows"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
license-files = ["LICENSE"]
|
|
12
|
+
requires-python = ">=3.12"
|
|
13
|
+
dependencies = ["render-lab-triggers>=0.1.0,<0.2", "render==1.0.1", "httpx>=0.28,<0.29", "render-lab-tasks-core>=0.1.1,<0.2"]
|
|
14
|
+
|
|
15
|
+
[project.urls]
|
|
16
|
+
Repository = "https://github.com/render-lab/render-tasks-python"
|
|
17
|
+
|
|
18
|
+
[tool.uv.sources]
|
|
19
|
+
render-lab-triggers = { workspace = true }
|
|
20
|
+
render-lab-tasks-core = { workspace = true }
|
|
21
|
+
|
|
22
|
+
[tool.hatch.build.targets.wheel]
|
|
23
|
+
packages = ["src/render_lab_tasks_intercom"]
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Import .tasks explicitly to register durable tasks."""
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
from render import TaskContext
|
|
2
|
+
|
|
3
|
+
from ._app import app
|
|
4
|
+
from .client import Deps, dependencies
|
|
5
|
+
from .retry import INTERCOM_RETRY
|
|
6
|
+
from .types import AddNoteInput, AddNoteResult
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
async def add_note_impl(
|
|
10
|
+
ctx: TaskContext, input: AddNoteInput, *, deps: Deps | None = None
|
|
11
|
+
) -> AddNoteResult:
|
|
12
|
+
async with dependencies(deps) as resolved:
|
|
13
|
+
return await resolved.intercom.add_note(input)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@app.task(name="intercom.addNote", retry=INTERCOM_RETRY)
|
|
17
|
+
async def add_note(ctx: TaskContext, input: AddNoteInput) -> AddNoteResult:
|
|
18
|
+
return await add_note_impl(ctx, input)
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
from render import TaskContext
|
|
2
|
+
|
|
3
|
+
from ._app import app
|
|
4
|
+
from .client import Deps, dependencies
|
|
5
|
+
from .retry import INTERCOM_RETRY
|
|
6
|
+
from .types import AssignConversationInput, Conversation
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
async def assign_conversation_impl(
|
|
10
|
+
ctx: TaskContext, input: AssignConversationInput, *, deps: Deps | None = None
|
|
11
|
+
) -> Conversation:
|
|
12
|
+
async with dependencies(deps) as resolved:
|
|
13
|
+
return await resolved.intercom.assign_conversation(input)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@app.task(name="intercom.assignConversation", retry=INTERCOM_RETRY)
|
|
17
|
+
async def assign_conversation(ctx: TaskContext, input: AssignConversationInput) -> Conversation:
|
|
18
|
+
return await assign_conversation_impl(ctx, input)
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
"""Injected vendor interface and one-attempt async HTTP adapter."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
from collections.abc import AsyncIterator, Mapping
|
|
7
|
+
from contextlib import asynccontextmanager
|
|
8
|
+
from dataclasses import dataclass
|
|
9
|
+
from typing import Any, Protocol
|
|
10
|
+
|
|
11
|
+
import httpx
|
|
12
|
+
from render_lab_tasks_core.http import HttpClient, nullish, pick, required
|
|
13
|
+
|
|
14
|
+
from . import types as t
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def to_conversation(data: Any, fallback: str = "") -> dict[str, Any]:
|
|
18
|
+
data = data or {}
|
|
19
|
+
parts = []
|
|
20
|
+
for part in nullish((data.get("conversation_parts") or {}).get("conversation_parts"), []):
|
|
21
|
+
if part is None or nullish(part.get("body"), "") == "":
|
|
22
|
+
continue
|
|
23
|
+
author = part.get("author") or {}
|
|
24
|
+
parts.append(
|
|
25
|
+
{
|
|
26
|
+
"author": author.get("name") or author.get("type") or author.get("id") or "unknown",
|
|
27
|
+
"body": nullish(part.get("body"), ""),
|
|
28
|
+
}
|
|
29
|
+
)
|
|
30
|
+
contacts = nullish((data.get("contacts") or {}).get("contacts"), [])
|
|
31
|
+
return {
|
|
32
|
+
"id": str(nullish(data.get("id"), fallback)),
|
|
33
|
+
"state": nullish(data.get("state"), "unknown"),
|
|
34
|
+
"contactId": contacts[0].get("id") if contacts else None,
|
|
35
|
+
"parts": parts,
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def to_contact(data: Any, fallback: str = "") -> dict[str, Any]:
|
|
40
|
+
data = data or {}
|
|
41
|
+
return {
|
|
42
|
+
"id": str(nullish(data.get("id"), fallback)),
|
|
43
|
+
"email": data.get("email"),
|
|
44
|
+
"name": data.get("name"),
|
|
45
|
+
"role": nullish(data.get("role"), "unknown"),
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def part_id(data: Any) -> str:
|
|
50
|
+
data = data or {}
|
|
51
|
+
parts = nullish((data.get("conversation_parts") or {}).get("conversation_parts"), [])
|
|
52
|
+
return str(nullish(parts[-1].get("id") if parts else None, nullish(data.get("id"), "")))
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
class Port(Protocol):
|
|
56
|
+
async def add_note(self, input: t.AddNoteInput) -> t.AddNoteResult: ...
|
|
57
|
+
async def assign_conversation(self, input: t.AssignConversationInput) -> t.Conversation: ...
|
|
58
|
+
async def close_conversation(self, input: t.CloseConversationInput) -> t.Conversation: ...
|
|
59
|
+
async def create_ticket(self, input: t.CreateTicketInput) -> t.CreateTicketResult: ...
|
|
60
|
+
async def get_contact(self, input: t.GetContactInput) -> t.Contact: ...
|
|
61
|
+
async def get_conversation(self, input: t.GetConversationInput) -> t.Conversation: ...
|
|
62
|
+
async def reply(self, input: t.ReplyInput) -> t.ReplyResult: ...
|
|
63
|
+
async def search_conversations(
|
|
64
|
+
self, input: t.SearchConversationsInput
|
|
65
|
+
) -> list[t.Conversation]: ...
|
|
66
|
+
async def tag_user(self, input: t.TagUserInput) -> t.TagUserResult: ...
|
|
67
|
+
async def update_contact(self, input: t.UpdateContactInput) -> t.Contact: ...
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
@dataclass(frozen=True)
|
|
71
|
+
class Deps:
|
|
72
|
+
intercom: Port
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
class Client:
|
|
76
|
+
def __init__(self, http: httpx.AsyncClient, env: Mapping[str, str] | None = None) -> None:
|
|
77
|
+
self.env = os.environ if env is None else env
|
|
78
|
+
self.http = HttpClient(
|
|
79
|
+
http,
|
|
80
|
+
"https://api.intercom.io",
|
|
81
|
+
auth=lambda: {
|
|
82
|
+
"authorization": "Bearer " + required(self.env, "INTERCOM_ACCESS_TOKEN"),
|
|
83
|
+
"accept": "application/json",
|
|
84
|
+
},
|
|
85
|
+
label="Intercom API",
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
async def get_conversation(self, input: t.GetConversationInput) -> Any:
|
|
89
|
+
return to_conversation(
|
|
90
|
+
await self.http.call(f"/conversations/{input['conversationId']}"),
|
|
91
|
+
input["conversationId"],
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
async def reply(self, input: t.ReplyInput) -> Any:
|
|
95
|
+
data = await self.http.call(
|
|
96
|
+
f"/conversations/{input['conversationId']}/reply",
|
|
97
|
+
method="POST",
|
|
98
|
+
body={
|
|
99
|
+
"message_type": nullish(input.get("type"), "comment"),
|
|
100
|
+
"type": "admin",
|
|
101
|
+
"body": input["body"],
|
|
102
|
+
},
|
|
103
|
+
)
|
|
104
|
+
return {"conversationId": input["conversationId"], "partId": part_id(data)}
|
|
105
|
+
|
|
106
|
+
async def tag_user(self, input: t.TagUserInput) -> Any:
|
|
107
|
+
await self.http.call(
|
|
108
|
+
f"/contacts/{input['contactId']}/tags", method="POST", body={"id": input["tag"]}
|
|
109
|
+
)
|
|
110
|
+
return pick(input, "contactId", "tag")
|
|
111
|
+
|
|
112
|
+
async def create_ticket(self, input: t.CreateTicketInput) -> Any:
|
|
113
|
+
attrs = {"_default_title_": input["title"]}
|
|
114
|
+
if "description" in input:
|
|
115
|
+
attrs["_default_description_"] = input["description"]
|
|
116
|
+
body: dict[str, Any] = {"ticket_attributes": attrs}
|
|
117
|
+
if input.get("contactId"):
|
|
118
|
+
body["contacts"] = [{"id": input["contactId"]}]
|
|
119
|
+
data = await self.http.call("/tickets", method="POST", body=body)
|
|
120
|
+
return {"ticketId": str(nullish((data or {}).get("id"), ""))}
|
|
121
|
+
|
|
122
|
+
async def search_conversations(self, input: t.SearchConversationsInput) -> Any:
|
|
123
|
+
data = await self.http.call(
|
|
124
|
+
"/conversations/search",
|
|
125
|
+
method="POST",
|
|
126
|
+
body={"query": pick(input, "field", "operator", "value")},
|
|
127
|
+
)
|
|
128
|
+
return [to_conversation(c) for c in nullish((data or {}).get("conversations"), [])]
|
|
129
|
+
|
|
130
|
+
async def assign_conversation(self, input: t.AssignConversationInput) -> Any:
|
|
131
|
+
data = await self.http.call(
|
|
132
|
+
f"/conversations/{input['conversationId']}/parts",
|
|
133
|
+
method="POST",
|
|
134
|
+
body={
|
|
135
|
+
"message_type": "assignment",
|
|
136
|
+
"type": "admin",
|
|
137
|
+
"admin_id": input["adminId"],
|
|
138
|
+
"assignee_id": input["assigneeId"],
|
|
139
|
+
},
|
|
140
|
+
)
|
|
141
|
+
return to_conversation(data, input["conversationId"])
|
|
142
|
+
|
|
143
|
+
async def close_conversation(self, input: t.CloseConversationInput) -> Any:
|
|
144
|
+
data = await self.http.call(
|
|
145
|
+
f"/conversations/{input['conversationId']}/parts",
|
|
146
|
+
method="POST",
|
|
147
|
+
body={"message_type": "close", "type": "admin", "admin_id": input["adminId"]},
|
|
148
|
+
)
|
|
149
|
+
return to_conversation(data, input["conversationId"])
|
|
150
|
+
|
|
151
|
+
async def get_contact(self, input: t.GetContactInput) -> Any:
|
|
152
|
+
return to_contact(
|
|
153
|
+
await self.http.call(f"/contacts/{input['contactId']}"), input["contactId"]
|
|
154
|
+
)
|
|
155
|
+
|
|
156
|
+
async def update_contact(self, input: t.UpdateContactInput) -> Any:
|
|
157
|
+
body = pick(input, "email", "name", "phone", "role")
|
|
158
|
+
if "customAttributes" in input:
|
|
159
|
+
body["custom_attributes"] = input["customAttributes"]
|
|
160
|
+
return to_contact(
|
|
161
|
+
await self.http.call(f"/contacts/{input['contactId']}", method="PUT", body=body),
|
|
162
|
+
input["contactId"],
|
|
163
|
+
)
|
|
164
|
+
|
|
165
|
+
async def add_note(self, input: t.AddNoteInput) -> Any:
|
|
166
|
+
data = await self.http.call(
|
|
167
|
+
f"/conversations/{input['conversationId']}/parts",
|
|
168
|
+
method="POST",
|
|
169
|
+
body={
|
|
170
|
+
"message_type": "note",
|
|
171
|
+
"type": "admin",
|
|
172
|
+
"admin_id": input["adminId"],
|
|
173
|
+
"body": input["body"],
|
|
174
|
+
},
|
|
175
|
+
)
|
|
176
|
+
return {"conversationId": input["conversationId"], "partId": part_id(data)}
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
@asynccontextmanager
|
|
180
|
+
async def dependencies(deps: Deps | None) -> AsyncIterator[Deps]:
|
|
181
|
+
if deps is not None:
|
|
182
|
+
yield deps
|
|
183
|
+
else:
|
|
184
|
+
async with httpx.AsyncClient(
|
|
185
|
+
timeout=30, transport=httpx.AsyncHTTPTransport(retries=0)
|
|
186
|
+
) as http:
|
|
187
|
+
yield Deps(intercom=Client(http))
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
IntercomPort = Port
|
|
191
|
+
IntercomClient = Client
|
|
192
|
+
IntercomDeps = Deps
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
from render import TaskContext
|
|
2
|
+
|
|
3
|
+
from ._app import app
|
|
4
|
+
from .client import Deps, dependencies
|
|
5
|
+
from .retry import INTERCOM_RETRY
|
|
6
|
+
from .types import CloseConversationInput, Conversation
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
async def close_conversation_impl(
|
|
10
|
+
ctx: TaskContext, input: CloseConversationInput, *, deps: Deps | None = None
|
|
11
|
+
) -> Conversation:
|
|
12
|
+
async with dependencies(deps) as resolved:
|
|
13
|
+
return await resolved.intercom.close_conversation(input)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@app.task(name="intercom.closeConversation", retry=INTERCOM_RETRY)
|
|
17
|
+
async def close_conversation(ctx: TaskContext, input: CloseConversationInput) -> Conversation:
|
|
18
|
+
return await close_conversation_impl(ctx, input)
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
from render import TaskContext
|
|
2
|
+
|
|
3
|
+
from ._app import app
|
|
4
|
+
from .client import Deps, dependencies
|
|
5
|
+
from .retry import INTERCOM_RETRY
|
|
6
|
+
from .types import CreateTicketInput, CreateTicketResult
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
async def create_ticket_impl(
|
|
10
|
+
ctx: TaskContext, input: CreateTicketInput, *, deps: Deps | None = None
|
|
11
|
+
) -> CreateTicketResult:
|
|
12
|
+
async with dependencies(deps) as resolved:
|
|
13
|
+
return await resolved.intercom.create_ticket(input)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@app.task(name="intercom.createTicket", retry=INTERCOM_RETRY)
|
|
17
|
+
async def create_ticket(ctx: TaskContext, input: CreateTicketInput) -> CreateTicketResult:
|
|
18
|
+
return await create_ticket_impl(ctx, input)
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
from render import TaskContext
|
|
2
|
+
|
|
3
|
+
from ._app import app
|
|
4
|
+
from .client import Deps, dependencies
|
|
5
|
+
from .retry import INTERCOM_RETRY
|
|
6
|
+
from .types import Contact, GetContactInput
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
async def get_contact_impl(
|
|
10
|
+
ctx: TaskContext, input: GetContactInput, *, deps: Deps | None = None
|
|
11
|
+
) -> Contact:
|
|
12
|
+
async with dependencies(deps) as resolved:
|
|
13
|
+
return await resolved.intercom.get_contact(input)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@app.task(name="intercom.getContact", retry=INTERCOM_RETRY)
|
|
17
|
+
async def get_contact(ctx: TaskContext, input: GetContactInput) -> Contact:
|
|
18
|
+
return await get_contact_impl(ctx, input)
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
from render import TaskContext
|
|
2
|
+
|
|
3
|
+
from ._app import app
|
|
4
|
+
from .client import Deps, dependencies
|
|
5
|
+
from .retry import INTERCOM_RETRY
|
|
6
|
+
from .types import Conversation, GetConversationInput
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
async def get_conversation_impl(
|
|
10
|
+
ctx: TaskContext, input: GetConversationInput, *, deps: Deps | None = None
|
|
11
|
+
) -> Conversation:
|
|
12
|
+
async with dependencies(deps) as resolved:
|
|
13
|
+
return await resolved.intercom.get_conversation(input)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@app.task(name="intercom.getConversation", retry=INTERCOM_RETRY)
|
|
17
|
+
async def get_conversation(ctx: TaskContext, input: GetConversationInput) -> Conversation:
|
|
18
|
+
return await get_conversation_impl(ctx, input)
|
|
File without changes
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
from render import TaskContext
|
|
2
|
+
|
|
3
|
+
from ._app import app
|
|
4
|
+
from .client import Deps, dependencies
|
|
5
|
+
from .retry import INTERCOM_RETRY
|
|
6
|
+
from .types import ReplyInput, ReplyResult
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
async def reply_impl(
|
|
10
|
+
ctx: TaskContext, input: ReplyInput, *, deps: Deps | None = None
|
|
11
|
+
) -> ReplyResult:
|
|
12
|
+
async with dependencies(deps) as resolved:
|
|
13
|
+
return await resolved.intercom.reply(input)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@app.task(name="intercom.reply", retry=INTERCOM_RETRY)
|
|
17
|
+
async def reply(ctx: TaskContext, input: ReplyInput) -> ReplyResult:
|
|
18
|
+
return await reply_impl(ctx, input)
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
from render import TaskContext
|
|
2
|
+
|
|
3
|
+
from ._app import app
|
|
4
|
+
from .client import Deps, dependencies
|
|
5
|
+
from .retry import INTERCOM_RETRY
|
|
6
|
+
from .types import Conversation, SearchConversationsInput
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
async def search_conversations_impl(
|
|
10
|
+
ctx: TaskContext, input: SearchConversationsInput, *, deps: Deps | None = None
|
|
11
|
+
) -> list[Conversation]:
|
|
12
|
+
async with dependencies(deps) as resolved:
|
|
13
|
+
return await resolved.intercom.search_conversations(input)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@app.task(name="intercom.searchConversations", retry=INTERCOM_RETRY)
|
|
17
|
+
async def search_conversations(
|
|
18
|
+
ctx: TaskContext, input: SearchConversationsInput
|
|
19
|
+
) -> list[Conversation]:
|
|
20
|
+
return await search_conversations_impl(ctx, input)
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
from render import TaskContext
|
|
2
|
+
|
|
3
|
+
from ._app import app
|
|
4
|
+
from .client import Deps, dependencies
|
|
5
|
+
from .retry import INTERCOM_RETRY
|
|
6
|
+
from .types import TagUserInput, TagUserResult
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
async def tag_user_impl(
|
|
10
|
+
ctx: TaskContext, input: TagUserInput, *, deps: Deps | None = None
|
|
11
|
+
) -> TagUserResult:
|
|
12
|
+
async with dependencies(deps) as resolved:
|
|
13
|
+
return await resolved.intercom.tag_user(input)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@app.task(name="intercom.tagUser", retry=INTERCOM_RETRY)
|
|
17
|
+
async def tag_user(ctx: TaskContext, input: TagUserInput) -> TagUserResult:
|
|
18
|
+
return await tag_user_impl(ctx, input)
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
from ._app import app
|
|
2
|
+
from .add_note import add_note, add_note_impl
|
|
3
|
+
from .assign_conversation import assign_conversation, assign_conversation_impl
|
|
4
|
+
from .close_conversation import close_conversation, close_conversation_impl
|
|
5
|
+
from .create_ticket import create_ticket, create_ticket_impl
|
|
6
|
+
from .get_contact import get_contact, get_contact_impl
|
|
7
|
+
from .get_conversation import get_conversation, get_conversation_impl
|
|
8
|
+
from .reply import reply, reply_impl
|
|
9
|
+
from .search_conversations import search_conversations, search_conversations_impl
|
|
10
|
+
from .tag_user import tag_user, tag_user_impl
|
|
11
|
+
from .update_contact import update_contact, update_contact_impl
|
|
12
|
+
|
|
13
|
+
__all__ = [
|
|
14
|
+
"app",
|
|
15
|
+
"add_note",
|
|
16
|
+
"add_note_impl",
|
|
17
|
+
"assign_conversation",
|
|
18
|
+
"assign_conversation_impl",
|
|
19
|
+
"close_conversation",
|
|
20
|
+
"close_conversation_impl",
|
|
21
|
+
"create_ticket",
|
|
22
|
+
"create_ticket_impl",
|
|
23
|
+
"get_contact",
|
|
24
|
+
"get_contact_impl",
|
|
25
|
+
"get_conversation",
|
|
26
|
+
"get_conversation_impl",
|
|
27
|
+
"reply",
|
|
28
|
+
"reply_impl",
|
|
29
|
+
"search_conversations",
|
|
30
|
+
"search_conversations_impl",
|
|
31
|
+
"tag_user",
|
|
32
|
+
"tag_user_impl",
|
|
33
|
+
"update_contact",
|
|
34
|
+
"update_contact_impl",
|
|
35
|
+
]
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
"""JSON contracts ported from the pinned TypeScript pack."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import Any, Literal, NotRequired, TypedDict
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class ConversationPart(TypedDict):
|
|
9
|
+
author: str
|
|
10
|
+
body: str
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class Conversation(TypedDict):
|
|
14
|
+
id: str
|
|
15
|
+
state: str
|
|
16
|
+
contactId: str | None
|
|
17
|
+
parts: list[ConversationPart]
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class GetConversationInput(TypedDict):
|
|
21
|
+
conversationId: str
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class ReplyInput(TypedDict):
|
|
25
|
+
conversationId: str
|
|
26
|
+
body: str
|
|
27
|
+
type: NotRequired[Literal["comment"] | Literal["note"]]
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class ReplyResult(TypedDict):
|
|
31
|
+
conversationId: str
|
|
32
|
+
partId: str
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class TagUserInput(TypedDict):
|
|
36
|
+
contactId: str
|
|
37
|
+
tag: str
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class TagUserResult(TypedDict):
|
|
41
|
+
contactId: str
|
|
42
|
+
tag: str
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class CreateTicketInput(TypedDict):
|
|
46
|
+
title: str
|
|
47
|
+
description: str
|
|
48
|
+
contactId: NotRequired[str]
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
class CreateTicketResult(TypedDict):
|
|
52
|
+
ticketId: str
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
class Contact(TypedDict):
|
|
56
|
+
id: str
|
|
57
|
+
email: str | None
|
|
58
|
+
name: str | None
|
|
59
|
+
role: str
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
class SearchConversationsInput(TypedDict):
|
|
63
|
+
field: str
|
|
64
|
+
operator: str
|
|
65
|
+
value: str
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
class AssignConversationInput(TypedDict):
|
|
69
|
+
conversationId: str
|
|
70
|
+
adminId: str
|
|
71
|
+
assigneeId: str
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
class CloseConversationInput(TypedDict):
|
|
75
|
+
conversationId: str
|
|
76
|
+
adminId: str
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
class GetContactInput(TypedDict):
|
|
80
|
+
contactId: str
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
class UpdateContactInput(TypedDict):
|
|
84
|
+
contactId: str
|
|
85
|
+
email: NotRequired[str]
|
|
86
|
+
name: NotRequired[str]
|
|
87
|
+
phone: NotRequired[str]
|
|
88
|
+
role: NotRequired[str]
|
|
89
|
+
customAttributes: NotRequired[dict[str, Any]]
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
class AddNoteInput(TypedDict):
|
|
93
|
+
conversationId: str
|
|
94
|
+
adminId: str
|
|
95
|
+
body: str
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
class AddNoteResult(TypedDict):
|
|
99
|
+
conversationId: str
|
|
100
|
+
partId: str
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
from render import TaskContext
|
|
2
|
+
|
|
3
|
+
from ._app import app
|
|
4
|
+
from .client import Deps, dependencies
|
|
5
|
+
from .retry import INTERCOM_RETRY
|
|
6
|
+
from .types import Contact, UpdateContactInput
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
async def update_contact_impl(
|
|
10
|
+
ctx: TaskContext, input: UpdateContactInput, *, deps: Deps | None = None
|
|
11
|
+
) -> Contact:
|
|
12
|
+
async with dependencies(deps) as resolved:
|
|
13
|
+
return await resolved.intercom.update_contact(input)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@app.task(name="intercom.updateContact", retry=INTERCOM_RETRY)
|
|
17
|
+
async def update_contact(ctx: TaskContext, input: UpdateContactInput) -> Contact:
|
|
18
|
+
return await update_contact_impl(ctx, input)
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"""Registration-free intercom webhook verification and event mapping."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
from collections.abc import Callable
|
|
7
|
+
|
|
8
|
+
from render_lab_tasks_core.signatures import digest, safe_equal
|
|
9
|
+
from render_lab_triggers.types import Adapter, Json, WebhookContext, WebhookDispatch, WebhookRequest
|
|
10
|
+
|
|
11
|
+
type OnEvent = Callable[[dict[str, Json]], WebhookDispatch | None]
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def verify_intercom_signature(secret: str, raw_body: str, signature: str | None) -> bool:
|
|
15
|
+
if not signature:
|
|
16
|
+
return False
|
|
17
|
+
expected = "sha1=" + digest(secret, raw_body, algorithm="sha1", encoding="hex")
|
|
18
|
+
return safe_equal(signature, expected)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def intercom_adapter(on_event: OnEvent, *, secret: str | None = None) -> Adapter:
|
|
22
|
+
def verify(req: WebhookRequest) -> bool:
|
|
23
|
+
key = secret if secret is not None else os.getenv("INTERCOM_CLIENT_SECRET")
|
|
24
|
+
return bool(key) and (
|
|
25
|
+
verify_intercom_signature(
|
|
26
|
+
key or "", req["rawBody"], req["headers"].get("x-hub-signature")
|
|
27
|
+
)
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
def map_event(ctx: WebhookContext) -> WebhookDispatch | None:
|
|
31
|
+
payload = ctx["body"] if ctx["body"] is not None else {}
|
|
32
|
+
fields = payload if isinstance(payload, dict) else {}
|
|
33
|
+
return on_event(
|
|
34
|
+
{
|
|
35
|
+
"topic": (fields.get("topic") if fields.get("topic") is not None else ""),
|
|
36
|
+
"payload": payload,
|
|
37
|
+
}
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
return Adapter(verify, map_event)
|