본문 바로가기
실무/슬랙 봇 만들기

파이썬 슬랙 봇 만들기(3-4): 파일 전송하기 > 견적서 생성 모달 창 만들기

by davi_kr 2023. 4. 18.
반응형

이전 포스팅 -

2023.04.18 - [파이썬/파이썬 활용] - 파이썬 슬랙 봇 만들기(3-3): 파일 전송하기 > 모달 드롭다운 만들기

 

이번 포스팅에선 지난 시간에 이어서 모달로 견적서 정보 받는 화면을 만들어보겠습니다.

여기서 견적서 생성하기를 눌러주면 이제 엑셀 파일을 편집해서 슬랙으로 띄워주기만 하면 됩니다.

[코드 리뷰]

코드가 아주 깁니다... 마음의 준비를 하고 보세요.

@app.view("view_1")
def handle_submission(ack, body, client, view, logger, payload):
    ack()
    selected = body["view"]["state"]["values"]["input_request_type"][
        "select_office_request_type"
    ]["selected_option"]["value"]

if selected == "견적서 출력":
        client.views_open(
            trigger_id=body["trigger_id"],
            view={
                "type": "modal",
                "callback_id": "view_4",
                "title": {"type": "plain_text", "text": "견적서 출력"},
                "submit": {"type": "plain_text", "text": "견적서 생성하기"},
                "blocks": [
                    {
                        "type": "input",
                        "block_id": "making_estimate1",
                        "element": {
                            "type": "static_select",
                            "placeholder": {
                                "type": "plain_text",
                                "text": "인입 경로를 선택해주세요.",
                            },
                            "options": [
                                {
                                    "text": {
                                        "type": "plain_text",
                                        "text": "1회",
                                    },
                                    "value": "1회",
                                },
                                {
                                    "text": {
                                        "type": "plain_text",
                                        "text": "2회",
                                    },
                                    "value": "2회",
                                },
                                {
                                    "text": {
                                        "type": "plain_text",
                                        "text": "3회",
                                    },
                                    "value": "3회",
                                },
                                {
                                    "text": {
                                        "type": "plain_text",
                                        "text": "4회",
                                    },
                                    "value": "4회",
                                },
                            ],
                            "action_id": "input_request_route",
                        },
                        "label": {"type": "plain_text", "text": "인입 경로"},
                    },
                    {
                        "type": "input",
                        "block_id": "making_estimate2",
                        "element": {
                            "type": "plain_text_input",
                            "action_id": "input_company_name",
                        },
                        "label": {"type": "plain_text", "text": "회사명/고객명"},
                    },
                    {
                        "type": "input",
                        "block_id": "making_estimate3",
                        "element": {
                            "type": "plain_text_input",
                            "action_id": "input_company_email",
                        },
                        "label": {"type": "plain_text", "text": "회사 이메일(없으면 '-'라고 적기)"},
                    },
                    {
                        "type": "input",
                        "block_id": "making_estimate4",
                        "element": {
                            "type": "radio_buttons",
                            "options": [
                                {
                                    "text": {
                                        "type": "plain_text",
                                        "text": "매주",
                                    },
                                    "value": "매주",
                                },
                                {
                                    "text": {
                                        "type": "plain_text",
                                        "text": "격주",
                                    },
                                    "value": "격주",
                                },
                            ],
                            "action_id": "input_week_period",
                        },
                        "label": {"type": "plain_text", "text": "매주/격주 여부"},
                    },
                    {
                        "type": "input",
                        "block_id": "making_estimate5",
                        "element": {
                            "type": "static_select",
                            "placeholder": {
                                "type": "plain_text",
                                "text": "주당 횟수를 선택해주세요.",
                            },
                            "options": [
                                {
                                    "text": {
                                        "type": "plain_text",
                                        "text": "1회",
                                    },
                                    "value": "1회",
                                },
                                {
                                    "text": {
                                        "type": "plain_text",
                                        "text": "2회",
                                    },
                                    "value": "2회",
                                },
                                {
                                    "text": {
                                        "type": "plain_text",
                                        "text": "3회",
                                    },
                                    "value": "3회",
                                },
                                {
                                    "text": {
                                        "type": "plain_text",
                                        "text": "4회",
                                    },
                                    "value": "4회",
                                },
                                {
                                    "text": {
                                        "type": "plain_text",
                                        "text": "5회",
                                    },
                                    "value": "5회",
                                },
                                {
                                    "text": {
                                        "type": "plain_text",
                                        "text": "6회",
                                    },
                                    "value": "6회",
                                },
                                {
                                    "text": {
                                        "type": "plain_text",
                                        "text": "7회",
                                    },
                                    "value": "7회",
                                },
                            ],
                            "action_id": "input_times_per_week",
                        },
                        "label": {"type": "plain_text", "text": "주당 청소 횟수"},
                    },
                    {
                        "type": "input",
                        "block_id": "making_estimate6",
                        "element": {
                            "type": "radio_buttons",
                            "options": [
                                {
                                    "text": {
                                        "type": "plain_text",
                                        "text": "2시간",
                                    },
                                    "value": "2시간",
                                },
                                {
                                    "text": {
                                        "type": "plain_text",
                                        "text": "3시간",
                                    },
                                    "value": "3시간",
                                },
                                {
                                    "text": {
                                        "type": "plain_text",
                                        "text": "4시간",
                                    },
                                    "value": "4시간",
                                },
                            ],
                            "action_id": "input_times_per_service",
                        },
                        "label": {"type": "plain_text", "text": "1회당 청소 시간"},
                    },
                ],
            },
        )

엄청 길어서 당황스러우셨을 수 있는데요.

사실상 구조만 알면 되고 나머지 값을 입력하는 필드들은 따지자면 이전에 했던거랑 다 같은 방식이라 참고만 하시면 될 것 같습니다.

(인입 경로에 1회, 2회 이런 식으로 적혀있는 건.. 제가 복사해서 만들다가 그냥 올려서 그런거라 무시하셔도 좋습니다. 다른 값을 넣으실 거면 text와 value 값을 수정하시면 됩니다.)

[핵심 정리]

중요한 것 위주로 적어보자면

selected = body["view"]["state"]["values"]["input_request_type"][

"select_office_request_type"

]["selected_option"]["value"]는 이전에 모달에서 선택한 값을 기준으로 정해지고 여기서는 '견적서 출력'을 의미합니다.

그리고 if문에서 견적서 출력이 맞으면 그 아래 코드들을 실행합니다.

이전에 했던 것처럼 모달 창을 다시 띄워줍니다.

이번에는 단순 드롭다운만 있지는 않은게 특징이라서 각각을 간단히만 짚어보자면

1️⃣ 인입 경로는 드롭다운

2️⃣ 회사명/고객명은 일반 텍스트 박스

3️⃣ 매주/격주 여부는 라디오 버튼으로 이해하시면 됩니다.

 

수량이 많아져서 어지러워 보일 뿐.. 그냥 이전에 했던 거랑 완전 동일합니다.

위치만 잘 적어주시면 되는 부분이고 혹시 이게 어렵다고 하시면

https://app.slack.com/block-kit-builder/

이쪽에서 직접 편집해보시면 이해가 더 편하실 거예요. 꼭 해봅시길 권해드립니다.

이 다음에는 view_4와 연결되는 견적서 생성 및 파일 올리기를 다뤄보겠습니다.

이번 포스팅은 여기까지입니다!

반응형

댓글