-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: feature/cafeteria-1-view 파일로 aws-sandol-api 수정
feature/cafeteria-1-view 브랜치에서 개발한 cafeteria-view 관련 코드를 aws-sandol-api 폴더에 적용합니다.
- Loading branch information
1 parent
a22703c
commit a91e6c3
Showing
24 changed files
with
2,888 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from .utils import make_meal_cards | ||
from .settings import HELP, CAFETERIA_WEB |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from flask import Flask | ||
|
||
app = Flask(__name__) | ||
|
||
|
||
@app.route("/") | ||
def root(): | ||
return "Hello Sandol" | ||
|
||
|
||
app.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
""" | ||
This module provides the Kakao package. | ||
""" | ||
from .skill import SimpleImageResponse, SimpleTextResponse | ||
from .skill import BasicCard, CommerceCard, Carousel | ||
from .input import Payload, Params | ||
from .response import KakaoResponse | ||
from .customerror import InvalidActionError, InvalidLinkError, InvalidTypeError | ||
|
||
__all__ = [ | ||
"SimpleImageResponse", | ||
"SimpleTextResponse", | ||
"BasicCard", | ||
"CommerceCard", | ||
"InvalidActionError", | ||
"InvalidLinkError", | ||
"InvalidTypeError", | ||
"KakaoResponse", | ||
"Payload", | ||
"Params", | ||
"Carousel", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
from typing import Callable, Optional | ||
from .base import BaseModel | ||
|
||
from .common import Action, Button, Buttons, QuickReplies | ||
from .skill import SimpleTextResponse, TextCard | ||
from .response import KakaoResponse | ||
|
||
from .input import Payload | ||
|
||
|
||
class AI(BaseModel): | ||
def __init__( | ||
self, | ||
payload: Payload, | ||
callback: Optional[Callable] = None, | ||
): | ||
self.payload = payload | ||
self.callback = callback | ||
self._answer: Optional[str] = None | ||
self.buttons = Buttons() | ||
self.buttons.max_buttons = 20 | ||
self.ButtonWrapper = TextCard | ||
|
||
@staticmethod | ||
def create_dict_with_non_none_values( | ||
base: Optional[dict] = None, **kwargs): | ||
""" | ||
Dict 객체를 생성합니다. | ||
kwargs의 값이 None이 아닌 경우 base에 추가합니다. | ||
Args: | ||
base (dict): 기본 딕셔너리, 없는 경우 빈 딕셔너리 | ||
**kwargs: 추가할 딕셔너리 | ||
Returns: | ||
dict: base와 kwargs를 합친 딕셔너리 | ||
""" | ||
if base is None: | ||
base = {} | ||
base.update({k: v for k, v in kwargs.items() if v is not None}) | ||
return base | ||
|
||
@property | ||
def callback_url(self): | ||
return self.payload.user_request.callback_url | ||
|
||
@classmethod | ||
def from_dict( | ||
cls, | ||
user_request_dict: dict, | ||
callback: Optional[Callable] = None): | ||
payload = Payload.from_dict(user_request_dict) | ||
if not payload.user_request.callback_url: | ||
raise ValueError("Callback_url must be set") | ||
return cls(payload=payload, callback=callback) | ||
|
||
def validate(self): | ||
if not self.callback_url: | ||
raise ValueError("Callback_url must be set") | ||
|
||
if not self.callback: | ||
raise ValueError("Callback must be set") | ||
|
||
def set_answer(self, answer: str): | ||
self._answer = answer | ||
|
||
def add_button(self, label: str): | ||
button = Button(label=label, action=Action.MESSAGE, messageText=label) | ||
self.buttons.add_button(button) | ||
|
||
@property | ||
def answer(self): | ||
return self._answer | ||
|
||
def prepare_callback_message( | ||
self, | ||
context: Optional[dict] = None, | ||
data: Optional[dict] = None): | ||
""" | ||
카카오 서버에 전달할 콜백 사용 여부를 나타내는 JSON 데이터를 준비합니다. | ||
Args: | ||
context (Optional[dict], optional): 콜백 사용과 관련된 컨텍스트 데이터. | ||
data (Optional[dict], optional): 콜백 사용과 관련된 데이터. | ||
Returns: | ||
dict: 콜백 사용 여부를 나타내는 JSON 형식의 데이터 | ||
""" | ||
response = { | ||
"version": "2.0", | ||
"useCallback": True, | ||
} | ||
|
||
response = self.create_dict_with_non_none_values( | ||
response, context=context, data=data) | ||
return response | ||
|
||
def render(self, to_json=False): | ||
real_response = KakaoResponse() | ||
if len(self.buttons) > 3: | ||
response = SimpleTextResponse( | ||
self.answer) | ||
real_response.add_skill(response) | ||
quick_replies = QuickReplies.from_buttons(self.buttons) | ||
real_response.add_quick_replies(quick_replies) | ||
return response.get_dict() | ||
|
||
elif len(self.buttons) == 0: | ||
response = SimpleTextResponse(self.answer) | ||
return response.get_dict() | ||
|
||
response = SimpleTextResponse(self.answer) | ||
response_card = self.ButtonWrapper( | ||
"Buttons", | ||
buttons=self.buttons, | ||
) | ||
multi_response = real_response+response+response_card | ||
if to_json: | ||
return multi_response.get_json() | ||
return multi_response.get_dict() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from abc import ABC, abstractmethod | ||
from typing import Optional | ||
|
||
|
||
class BaseModel(ABC): | ||
"""카카오 라이브러리의 대부분의 클래스의 부모 클래스 | ||
카카오 라이브러리의 대부분의 클래스는 이 클래스를 상속받아 구현됩니다. | ||
create_dict_with_non_none_values 메서드를 통해 None이 아닌 값만을 가진 dict를 생성할 수 있습니다. | ||
render 메서드를 통해 객체를 카카오톡 응답 형식에 알맞게 dict로 변환하도록 구현해야 합니다. | ||
validate 메서드를 통해 카카오톡 응답 규칙에 맞는지 검증하도록 구현해야 합니다. | ||
""" | ||
@staticmethod | ||
def create_dict_with_non_none_values( | ||
base: Optional[dict] = None, **kwargs): | ||
"""value가 None이 아닌 key-value 쌍을 base에 추가해 dict를 생성합니다. | ||
카카오톡 서버로 반환 시 None인 값을 제외하고 반환하기 위해 사용합니다. | ||
Args: | ||
base (dict): 기본 딕셔너리, 없는 경우 빈 딕셔너리 | ||
**kwargs: 추가할 딕셔너리 | ||
Returns: | ||
dict: base와 kwargs를 합친 딕셔너리 | ||
""" | ||
if base is None: | ||
base = {} | ||
base.update({k: v for k, v in kwargs.items() if v is not None}) | ||
return base | ||
|
||
@abstractmethod | ||
def render(self): | ||
"""객체를 카카오톡 응답 형식에 알맞게 dict로 변환 | ||
변환된 dict는 각 객체가 타깃으로 하는 | ||
카카오톡 응답 형식의 상세 필드를 key로 가집니다. | ||
Returns: | ||
dict | ||
""" | ||
|
||
@abstractmethod | ||
def validate(self): | ||
"""카카오톡 응답 규칙에 알맞은지 객체를 검증 | ||
응답 규칙에 맞지 않을 경우 예외를 발생시키도록 구현해야 합니다. | ||
""" |
Oops, something went wrong.