Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

webhooks/notion: Register native Notion webhook.

sathwikshetty332c0d9a830041
diff --git a/zerver/lib/integrations.py b/zerver/lib/integrations.py
index 3b0705ed905..d4fe58c5c8d 100644
--- a/zerver/lib/integrations.py
+++ b/zerver/lib/integrations.py
@@ -216,7 +216,7 @@ class Integration:
self.doc = doc
def is_enabled_in_catalog(self) -> bool:
- return self.name != "intercom"
+ return self.name not in ("intercom", "notion")
def get_logo_path(self, fallback_logo_path: str | None = None) -> str:
paths_to_check = [
@@ -769,6 +769,7 @@ INCOMING_WEBHOOK_INTEGRATIONS: list[IncomingWebhookIntegration] = [
[WebhookScreenshotConfig("incident_activated_new_default_payload.json")],
display_name="New Relic",
),
+ IncomingWebhookIntegration("notion", ["productivity", "project-management"]),
IncomingWebhookIntegration(
"opencollective",
["financial"],
@@ -1128,7 +1129,7 @@ INTEGRATIONS_MISSING_SCREENSHOT_CONFIG = (
# so the screenshot config is commented out.
{"beeminder"}
# Disabled integrations that are in the process of being added or rewritten.
- | {"intercom"}
+ | {"intercom", "notion"}
# Integrations that call external API endpoints.
| {"slack"}
# Integrations that require screenshots of message threads - support is yet to be added
diff --git a/zerver/webhooks/notion/__init__.py b/zerver/webhooks/notion/__init__.py
new file mode 100644
index 00000000000..e69de29bb2d
diff --git a/zerver/webhooks/notion/fixtures/verification.json b/zerver/webhooks/notion/fixtures/verification.json
new file mode 100644
index 00000000000..4d4e0f0fa91
--- /dev/null
+++ b/zerver/webhooks/notion/fixtures/verification.json
@@ -0,0 +1,3 @@
+{
+ "verification_token": "secret_tMrlL1qK5vuQAh1b6cZGhFChZTSYJlce98V0pYn7yBl"
+}
diff --git a/zerver/webhooks/notion/tests.py b/zerver/webhooks/notion/tests.py
new file mode 100644
index 00000000000..22520ad6a84
--- /dev/null
+++ b/zerver/webhooks/notion/tests.py
@@ -0,0 +1,12 @@
+from zerver.lib.test_classes import WebhookTestCase
+
+
+class NotionWebhookTest(WebhookTestCase):
+ def test_verification_request(self) -> None:
+ expected_topic = "Verification"
+ expected_message = """
+Notion webhook has been successfully configured.
+Your verification token is: `secret_tMrlL1qK5vuQAh1b6cZGhFChZTSYJlce98V0pYn7yBl`
+Please copy this token and paste it into your Notion webhook configuration to complete the setup.
+""".strip()
+ self.check_webhook("verification", expected_topic, expected_message)
diff --git a/zerver/webhooks/notion/view.py b/zerver/webhooks/notion/view.py
new file mode 100644
index 00000000000..c022b73c40b
--- /dev/null
+++ b/zerver/webhooks/notion/view.py
@@ -0,0 +1,61 @@
+from collections.abc import Callable
+
+from django.http import HttpRequest
+from django.http.response import HttpResponse
+
+from zerver.decorator import webhook_view
+from zerver.lib.exceptions import UnsupportedWebhookEventTypeError
+from zerver.lib.response import json_success
+from zerver.lib.typed_endpoint import JsonBodyPayload, typed_endpoint
+from zerver.lib.validator import WildValue, check_none_or, check_string
+from zerver.lib.webhooks.common import check_send_webhook_message, get_setup_webhook_message
+from zerver.models import UserProfile
+
+NOTION_VERIFICATION_TOKEN_MESSAGE = """
+{setup_message}
+Your verification token is: `{token}`
+Please copy this token and paste it into your Notion webhook configuration to complete the setup.
+""".strip()
+
+
+def handle_verification_request(payload: WildValue) -> tuple[str, str]:
+ verification_token = payload["verification_token"].tame(check_string)
+ setup_message = get_setup_webhook_message("Notion")
+ body = NOTION_VERIFICATION_TOKEN_MESSAGE.format(
+ setup_message=setup_message, token=verification_token
+ )
+ return ("Verification", body)
+
+
+EVENT_TO_FUNCTION_MAPPER: dict[str, Callable[[WildValue], tuple[str, str]]] = {
+ "verification": handle_verification_request,
+}
+
+
+def is_verification(payload: WildValue) -> bool:
+ return payload.get("verification_token").tame(check_none_or(check_string)) is not None
+
+
+ALL_EVENT_TYPES = list(EVENT_TO_FUNCTION_MAPPER.keys())
+
+
+@webhook_view("Notion", all_event_types=ALL_EVENT_TYPES)
+@typed_endpoint
+def api_notion_webhook(
+ request: HttpRequest,
+ user_profile: UserProfile,
+ *,
+ payload: JsonBodyPayload[WildValue],
+) -> HttpResponse:
+ if is_verification(payload):
+ event_type = "verification"
+ else:
+ event_type = payload.get("type").tame(check_string) # nocoverage
+
+ handler = EVENT_TO_FUNCTION_MAPPER.get(event_type)
+ if handler is None:
+ raise UnsupportedWebhookEventTypeError(event_type)
+ topic_name, body = handler(payload)
+
+ check_send_webhook_message(request, user_profile, topic_name, body, event_type)
+ return json_success(request)