1. 程式人生 > 其它 >Linux 傳送https POST請求sample ===Slack 頻道中傳送訊息通知

Linux 傳送https POST請求sample ===Slack 頻道中傳送訊息通知

jasqia@jasqia0121mac ~ %

curl -i -k -H "Content-type: application/json" -X POST -d '{
"blocks":[
{
"type":"section",
"text":{
"type":"mrkdwn",
"text":"*Welcome to the imagination world*"
}
},
{
"type":"section",
"block_id":"section567",
"text":{
"type":"mrkdwn",
"text":"http://www.google.com "
},
"accessory":{
"type":"image",
"image_url":"https://files.slack.com/files-pri/T28JEQ9RV-F024A8BTXE3/icon.jpeg",
"alt_text":"Say Hi"
}
}
]
}' https://hooks.slack.com/services/T28JEQ9RV/B024MUM8URK/ccc

  

and if you use Python language, it is quit easy

import json
import requests


def alert2Slack(webhook, message):
    headers = {
        "Content-Type": "application/json",
        "charset": "utf-8"
    }

    url = webhook
    value = message

    response = requests.request("POST", url, data=json.dumps(value, ensure_ascii=False).encode('utf-8'),
                                headers=headers)
    return response


if __name__ == "__main__":
    webhook = "https://hooks.slack.com/services/xxxx"

    message = {
        "blocks": [
            {
                "type": "section",
                "text": {
                    "type": "mrkdwn",
                    "text": "*Welcome to the imagination world*"
                }
            },
            {
                "type": "section",
                "block_id": "section567",
                "text": {
                    "type": "mrkdwn",
                    "text": "\n *The event Time is:*  " + "\n *The issue website is:* xxx" +
                            "\n @jasmine.qian" + "\n *The log information is as the following:* http://www.google.com "

                },
                "accessory": {
                    "type": "image",
                    "image_url": "https://files.slack.com/files-pri/T28JEQ9RV-F024A8BTXE3/icon.jpeg",
                    "alt_text": "Say Hi"
                }
            }
        ]
    }

    alert2Slack(webhook, message)