From 59508b6176732b3329a2002e6da32affcb47dfb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8C=E1=85=B5=E1=84=8E=E1=85=A1=E1=86=AB=E1=84=80?= =?UTF-8?q?=E1=85=B2?= Date: Mon, 20 Feb 2023 02:45:26 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20get=5Fslack=5Fevent=20=EB=A9=94?= =?UTF-8?q?=EC=84=9C=EB=93=9C=20=EC=BD=94=EB=93=9C=20=EC=A0=95=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/dependency/requests.py | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/app/api/dependency/requests.py b/app/api/dependency/requests.py index 3d561ff..08dce45 100644 --- a/app/api/dependency/requests.py +++ b/app/api/dependency/requests.py @@ -6,19 +6,30 @@ from app.applications.services.slack_services import SLACK_EVENT_HOOKS from app.domains.reactions.entities import SlackEventType + async def get_slack_event(request: Request) -> SLACK_EVENT_HOOKS: - - request_data: dict try: - request_data = await request.json() + request_data: dict = await request.json() + challenge_request = request_data.get('challenge') + event_request = request_data.get('event') except JSONDecodeError: raise HTTPException(status_code=400, detail="request body can not be none") - - if request_data.get('challenge'): + + if challenge_request: return SlackChallengeHook(**request_data) - elif request_data['event'].get('bot_profile'): - return SlackBotDirectMessageHook(**request_data) - elif request_data['event']['type'] in [SlackEventType.APP_MENTION_REACTION.value, SlackEventType.APP_MESSAGE.value]: - return SlackMentionHook(**request_data) + elif event_request: + if event_request.get('bot_profile'): + return SlackBotDirectMessageHook(**request_data) + elif is_slack_mention(event_request['type']): + return SlackMentionHook(**request_data) + else: + return SlackEventHook(**request_data) else: - return SlackEventHook(**request_data) + raise HTTPException(status_code=403, detail="please check slack event") + + +def is_slack_mention(event_type: str) -> bool: + return event_type in [ + SlackEventType.APP_MENTION_REACTION.value, + SlackEventType.APP_MESSAGE.value + ]