본문 바로가기

Ops/Slack

파이썬 (python) 으로 슬랙 (slack) 테스트 메시지 보내기 v2 (attachment)

반응형

How to send test message to slack on Python v2 (attachment)

 

 

 

이번에는 간단하게 slack 에서 제공하는 공식 가이드에 나와 있는 내용을 그대로 사용하여 어떻게 메시지가 오는지를 간략하게 소개하려고 합니다. 공식 가이드 페이지는 아래의 링크에서 확인할 수 있습니다.

 

공식 가이드 페이지

 

import requests

__slack_url__ = "https://hooks.slack.com/services/abcdefghijklmnopqrstuvwxyz"


def message_to_slack():
    payload = {
        "attachments": [
            {
                "mrkdwn_in": ["text"],
                "color": "#36a64f",
                "pretext": "Optional pre-text that appears above the attachment block",
                "author_name": "author_name",
                "author_link": "http://flickr.com/bobby/",
                "author_icon": "https://placeimg.com/16/16/people",
                "title": "title",
                "title_link": "https://api.slack.com/",
                "text": "Optional `text` that appears within the attachment",
                "fields": [
                    {
                        "title": "A field's title",
                        "value": "This field's value",
                        "short": "false"
                    },
                    {
                        "title": "A short field's title",
                        "value": "A short field's value",
                        "short": "true"
                    },
                    {
                        "title": "A second short field's title",
                        "value": "A second short field's value",
                        "short": "true"
                    }
                ],
                "thumb_url": "http://placekitten.com/g/200/200",
                "footer": "footer",
                "footer_icon": "https://platform.slack-edge.com/img/default_application_icon.png",
                "ts": 123456789
            }
        ]
    }
    requests.post(__slack_url__, json=payload)
    return

if __name__ == '__main__':
    message_to_slack()

     

가이드 페이지에 나와 있는 코드를 그대로 복붙한 내용인데요, 보통은(?) 에러가 나지 않아야 하지만 저는 에러가 발생해서 다음의 사항을 수정했습니다.

 

    as-is to-be
    "short" : false "short" : "false"
    "short" : true "short" : "true"

네, 그냥 " 로 묶은게 전부입니다. 허허허,,,

 

 

해당 코드를 실행하면 슬랙 채널로 다음과 같은 메시지가 전송되는 것을 확인할 수 있습니다! (이미지의 경우는 보낼 때마다 바뀌더라구요~) atachment를 사용하실 분들은 참고하시면 좋을 것 같습니다.

 

 

 


by mkdir-chandler

 

 

 

 

 

728x90
반응형