Github Actions 카카오워크 알림 전송

카카오워크에서 봇에 대한 생성 과정은 다음으로 이루어져있습니다. 제가 원하는 것은 github에서 수행한 배포 과정에 대한 결과를 카카오워크에 채팅을 보내는 것이기에 알림형을 선택하게 되었습니다.

etc-image-0

 

해당 이미지는 카카오 워크 공식문서에서 참조하였습니다.
https://docs.kakaoi.ai/kakao_work/botdevguide/#%EC%95%8C%EB%A6%BC%ED%98%95


관리자 사이트에서 개발자에 대한 권한을 부여합니다. 그리고나서 개발자는 봇을 생성하면 App Key가 발급이 됩니다.
잘 작동하는지 테스트하기 위해 다음 요청을 보냈고 이에 대한 응답으로 user_id가 필요하다는 에러가 반환되었습니다.

curl -X POST https://api.kakaowork.com/v1/conversations.open \
-H "Content-type: application/json" \
-H "Authorization: Bearer {APP_KEY}" 

 

100 98 100 98 0 0 2850 0 --:--:-- --:--:-- --:--:-- 2969{"error":{"code":"missing_parameter","message":"user_id or user_ids must needed"},"success":false}

 

유저 조회

우선 초대하고자 하는 user_id에 대한 정보를 얻어야 합니다. users.list api를 호출하여 특정 워크스페이스에 속한 전체 멤버 목록과 상세 정보를 얻을수 있습니다.

 

전체 유저 목록 조회


curl -X GET https://api.kakaowork.com/v1/users.list \
     -H "Authorization: Bearer {APP_KEY}"

하지만 이러한 방식은 특정 유저 id를 얻기 위해서는 별도의 필터 처리를 필요로 했습니다. 만일 이메일을 알고있다면 아래 API를 호출하여 user_id를 얻을수 있습니다.

이메일을 통한 특정 user_id 조회

curl -X GET https://api.kakaowork.com/v1/users.find_by_email?email={email} \
     -H "Authorization: Bearer {APP_KEY}"

 

100 348 100 348 0 0 10832 0 --:--:-- --:--:-- --:--:-- 11600{"success":true,"user":{"avatar_url":null,"department":null,"display_name":"홍길동","emails":[],"id":"11111","mobiles":[],"name":"홍길동","nickname":null,"position":null,"responsibility":null,"space_id":"111111","status":"activated","tels":[],"vacation_end_time":null,"vacation_start_time":null,"work_end_time":null,"work_start_time":null}}

 

저에 대한 user_id를 얻은 이후에는 그룹 채팅방에 초대하고자 하는 인원 user_id를 얻은 후에 봇 그룹 채팅방 생성하는 API를 호출하면 됩니다.

 

그룹 채팅방

봇 그룹 채팅방 생성 생성

curl -X POST https://api.kakaowork.com/v1/conversations.open \
     -H "Authorization: Bearer {app_key}" \
     -H "Content-Type: application/json" \
     -d '{ "user_ids": [111, 112, 113], "conversation_name": "github actions" }'


그룹이 생성한 이후에는 group_id에 대한 값이 출력이 됩니다. group_id는 그룹 메시지 전송에 필수로 필요합니다. 공식 문서에서 제공하는 api를 실행해본 결과 그룹 채팅방에 메시지가 가는 것을 확인할 수 있습니다.

그룹 메시지 전송

   curl -v -g -X POST https://api.kakaowork.com/v1/messages.send \
   -H "Authorization: Bearer {APP_KEY}" \
   -H "Content-Type: application/json" \
   -d '{"conversation_id":"{group_id}", "text": "github actions 수행",
      "blocks": [
                    {"type": "header", "text": "Github Actions 배포 결과", "style": "white"},
                    {"type": "text", "text": "브랜치",
                      "inlines":

                        [{"type": "styled", "text": "브랜치 : ", "bold": true },

                        {"type": "styled", "text": "${{ github.ref_name }}" }]},

                    {"type": "text", "text": "작업자",

                      "inlines":

                        [{"type": "styled", "text": "작업수행자 : ", "bold": true},

                        {"type": "styled", "text": "${{ github.actor }}" }]},

                    {"type": "text", "text": "결과",

                      "inlines":

                        [{"type": "styled", "text": "수행결과 : ","bold": true},

                        {"type": "styled", "text": "깃 수행 결과 "}]},

                    {"type": "text", "text": "github link",

                      "inlines":

                        [{"type": "styled", "text": "Githublink "},

                        {"type": "link", "text": "Repository", "url": "${{ github.repositoryUrl }}" }]},

                    {"type": "description", "term": "날짜",

                      "content": {"type": "text","text": "2020년 9월 16일 7시"},

                      "accent": true}

      ]}'

 

윈도우에서 curl를 수행하면 다음 에러가 발생할 수 있습니다. 동일 요청에 대해서 POSTNAM 또는 리눅스에서 요청을 수행했을 때 정상적으로 요청이 이뤄지게 됩니다. 이는 윈도우에서 쌍따옴표에 대한 처리가 되지 않기 때문입니다.

 

{ "error": { "code": "bad_request", "message": "bad_request from ingress" } }

 

만일 윈도우에서 해당 curl를 사용하기 위해서는 다음으로 변경해줘야 한다.

curl -v -g -X POST https://api.kakaowork.com/v1/messages.send \
           -H "Authorization: Bearer  {APP_KEY}" \
           -H "Content-Type: application/json" \
           -d "{'conversation_id':'group_id','text':'카카오워크에 오신걸 환영합니다.'}"

 

이후 기존 github action에서 해당 코드를 추가해주었습니다. 해당 작업에서 시크릿을 가져올 때 "${{ secrets.APP_KEY}}" 형태로 값을 불러와야지 curl 요청을 보낼때 bad request for ingress 요청이 발생하지 않습니다.

 

steps:
    - name: get currrent date
      id: date
      run: echo "::set-output name=date::$(TZ='Asia/Seoul' date '+%Y-%m-%d %H:%M')"

    - name: send notifictaion to kakao work
      run: |
        curl -v -g -X POST https://api.kakaowork.com/v1/messages.send \
             -H "Authorization: Bearer ${{ secrets.APP_KEY }}" \
             -H "Content-Type: application/json" \
             -d '{"conversation_id": ${{ secrets.CONVENTION_ID }},"text": "github actions 수행",
                  "blocks": [
                    {"type": "header", "text": "Github Actions 배포 결과", "style": "white"},
                    {"type": "text", "text": "브랜치",
                      "inlines":
                        [{"type": "styled", "text": "브랜치 : ", "bold": true },
                        {"type": "styled", "text": "${{ github.ref_name }}" }]},
                    {"type": "text", "text": "작업자",
                      "inlines":
                        [{"type": "styled", "text": "작업수행자 : ", "bold": true},
                        {"type": "styled", "text": "${{ github.actor }}" }]},
                    {"type": "text", "text": "결과",
                      "inlines":
                        [{"type": "styled", "text": "수행결과 : ","bold": true},
                        {"type": "styled", "text": "${{ needs.build.result }}"}]},
                    {"type": "text", "text": "github link",
                      "inlines":
                        [{"type": "styled", "text": "Githublink "},
                        {"type": "link", "text": "Repository", "url": " ${{ github.server_url }}/${{ github.repository }}" }]},
                    {"type": "description", "term": "날짜",
                      "content": {"type": "text","text": "${{ steps.date.outputs.date }}"},
                      "accent": true}
                  ]}' > /dev/null

 

Ref
https://docs.kakaoi.ai/kakao_work/botdevguide/#%EC%95%8C%EB%A6%BC%ED%98%95
https://docs.kakaoi.ai/kakao_work/webapireference/commonguide/
https://blog.naver.com/techshare/221178764058
https://docs.kakaoi.ai/kakao_work/botdevguide/#%EC%95%8C%EB%A6%BC%ED%98%95

'CI&CD' 카테고리의 다른 글

Github Actions 코드 개선하기  (2) 2024.06.10
Github Actions CI&CD 파이프라인  (0) 2024.06.09
[Github Actions] 다수의 설정 한번에 주입  (0) 2024.06.08
[ArgoCD] 설치하기  (0) 2023.08.14
[ArgoCD] 슬랙 설정하기  (0) 2023.08.14