From 91e986b47586a65ca5ab9c3ccb6d7585ccbf4e94 Mon Sep 17 00:00:00 2001 From: bumpsoo Date: Tue, 2 Jul 2024 09:23:19 +0900 Subject: [PATCH] =?UTF-8?q?slack=20=EB=A9=94=EC=8B=9C=EC=A7=80=20=EB=B3=80?= =?UTF-8?q?=ED=99=98=ED=95=98=EB=8A=94=20=EB=B6=80=EB=B6=84=20=EC=9E=91?= =?UTF-8?q?=EC=84=B1=EC=A4=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 6 +++--- menu.py | 4 ---- slack.py | 35 ++++++++++++++++++++++++++++++++++- 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/main.py b/main.py index c7556c2..503eed6 100644 --- a/main.py +++ b/main.py @@ -50,10 +50,10 @@ def lambda_handler(event: Dict[str, Any], context: Any) -> Dict[str, str]: return PARAM_ERROR menus = menu.menu(param.date) menus_without_img = [ x for x in menus if x.menu_image is None ] - if len(menus) == 0: + if param.count < MAX_RETRY and (len(menus) == 0 or len(menus_without_img) > 0): return retry(param, lambda_arn, schedule_role_arn) - menus_without_img: List[menu.Menu] = [ x for x in menus if x.menu_image is None ] - if len(menus_without_img) > 0 and param.count < MAX_RETRY: + if len(menus) == 0: + # 슬랙 메시지 전송( 오늘의 메뉴 존재 X ) return retry(param, lambda_arn, schedule_role_arn) # Extract relevant data for your Slack message (adjust as needed) message_content: str = f"*Important Data from External API*\n" diff --git a/menu.py b/menu.py index dfce087..6c91342 100644 --- a/menu.py +++ b/menu.py @@ -27,10 +27,6 @@ class Menu: if self.menu_image: self.menu_image = f"https://store.shinsegaefood.com/{self.menu_image}" - def to_slack_block(self, date: str) -> Dict[str, Any]: - blocks = [slack.markdown(f'*{date} 일자 메뉴*')] - return {} - # date should be this form 20240601 def menu(date: str) -> List[Menu]: menus: List[Menu] = [] diff --git a/slack.py b/slack.py index 400763c..925e37c 100644 --- a/slack.py +++ b/slack.py @@ -1,4 +1,5 @@ -from typing import Any, Dict +from typing import Any, Dict, List +from menu import Menu def markdown(text: str) -> Dict[str, Any]: return { @@ -8,3 +9,35 @@ def markdown(text: str) -> Dict[str, Any]: 'text': text } } + +def img(text: str, img_url: str) -> Dict[str, Any]: + return { + 'type': 'image', + 'title': { + 'type': 'plain_text', + 'text': text + }, + 'image_url': img_url, + 'alt_text': text + } + +def menu_to_str(m: Menu) -> str: + ret = '\n'.join([ + f'*식사 시간*: {m.meal_time}', + f'*식사 종류*: {m.meal_type}', + f'*대표 메뉴*: {m.rep_menu}', + f'*상세 메뉴*: {m.detailed_menu}', + f'*칼로리*: {m.calory}', + ]) + return ret + +def for_slack(date: str, data: List[Menu]) -> Dict[str, Any]: + blocks = [markdown(f'*{date} 일자 메뉴*')] + for each in data: + blocks.append(markdown(menu_to_str(each))) + if each.menu_image: + blocks.append(img(each.rep_menu, each.menu_image)) + else: + blocks.append(markdown('이미지가 존재하지 않습니다')) + return {'blocks': blocks} +