다양한 활동/인턴

[인턴] Slack Bot 만들기 (4) : Slack API 호출 규격

토끼개발자 조르디 2023. 8. 30. 14:27

SLACK API 호출 규격

플랫폼 서버에서 SLACK API를 호출하여 해당 채널에 메시지를 다이렉트로 전송

 

POST https://api.slack.com/api/chat.postMessage

  • headers  {"Authorization": f"Bearer {SLACK_BOT_TOKEN}", "Content-Type": "application/json"}
    • SLACK_BOT_TOKEN = ""
    • 만든 슬랙봇의 토큰을 입력

 

  • Request body (application/json)
{
    "channel": CHANNEL_ID, 
    "text": message, 
    "blocks": [{....}]
}
  • channel (string) : 메시지 전송 대상 채널 ID
  • text (string) : 메시지 보낼 내용
  • blocks (list) : 메시지 디자인 + 내용

 

  • payload 예시
     
payload = {
    "channel": CHANNEL_ID,
    "text": message,
    "blocks": [
        {
            "type": "section",
            "text": {
                "type": "mrkdwn",
                "text": "Hello, this is a block message!"
            }
        }
    ]
}

 

 

  • Response body (application/json)
  • Success
    • status code : 200
{
    'ok': True, 
    'channel': '', 
    'ts': '', 
    'message': {
        'bot_id': '', 
        'type': 'message', 
        'text': 'Hello, this is a block message!', 
        'user': '', 
        'ts': '', 
        'app_id': '', 
        'blocks': [
            {
                'type': 'section', 
                'block_id': 'HjEj', 
                'text': {
                    'type': 'mrkdwn', 
                    'text': 'Hello, this is a block message!', 
                    'verbatim': False
                }
            }
        ], 
        'team': '', 
        'bot_profile': {
            'id': '', 
            'app_id': '', 
            'name': '', 
            'icons': {
                'image_36': 'https://a.slack-edge.com/80588/img/plugins/app/bot_36.png', 
                'image_48': 'https://a.slack-edge.com/80588/img/plugins/app/bot_48.png', 
                'image_72': 'https://a.slack-edge.com/80588/img/plugins/app/service_72.png'
                }, 
            'deleted': False, 
            'updated': , 
            'team_id': ''
        },
        'warning': 'missing_charset', 
        'response_metadata': {
            'warnings': ['missing_charset']
        }
    }
}

 

  • Error
# 채널ID가 올바르지 않은 경우
{
    'ok': False, 
    'error': 'channel_not_found', 
    'warning': 'missing_charset', 
    'response_metadata': {'warnings': ['missing_charset']}
}
# 토큰이 유효하지 않는 경우
{
    'ok': False, 
    'error': 'invalid_auth', 
    'warning': 'missing_charset', 
    'response_metadata': {'warnings': ['missing_charset']}
}