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} +