2022-07-01 12:07:15 +02:00
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
from rest_framework import viewsets
|
2024-08-23 15:06:27 +02:00
|
|
|
from posthog.api.utils import action
|
2022-07-01 12:07:15 +02:00
|
|
|
from rest_framework.exceptions import AuthenticationFailed
|
|
|
|
from rest_framework.request import Request
|
|
|
|
from rest_framework.response import Response
|
|
|
|
|
|
|
|
from ee.tasks.slack import handle_slack_event
|
|
|
|
from posthog.api.integration import IntegrationSerializer
|
2023-10-26 12:38:15 +02:00
|
|
|
from posthog.models.integration import (
|
|
|
|
Integration,
|
|
|
|
SlackIntegration,
|
|
|
|
SlackIntegrationError,
|
|
|
|
)
|
2022-07-01 12:07:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
class PublicIntegrationViewSet(viewsets.GenericViewSet):
|
|
|
|
queryset = Integration.objects.all()
|
|
|
|
serializer_class = IntegrationSerializer
|
|
|
|
|
2024-01-10 15:23:18 +01:00
|
|
|
authentication_classes = []
|
|
|
|
permission_classes = []
|
2022-07-01 12:07:15 +02:00
|
|
|
|
|
|
|
@action(methods=["POST"], detail=False, url_path="slack/events")
|
|
|
|
def slack_events(self, request: Request, *args: Any, **kwargs: Any) -> Response:
|
|
|
|
try:
|
|
|
|
SlackIntegration.validate_request(request)
|
|
|
|
except SlackIntegrationError:
|
|
|
|
raise AuthenticationFailed()
|
|
|
|
|
|
|
|
if request.data["type"] == "url_verification":
|
|
|
|
return Response({"challenge": request.data["challenge"]})
|
|
|
|
|
|
|
|
handle_slack_event(request.data)
|
|
|
|
|
|
|
|
return Response({"status": "ok"})
|