Python 釘釘機(jī)器人消息通知

#!/usr/bin/env python
# encoding: utf8
# Author: zepinglai
#python 3.8 

import time
import hmac
import hashlib
import base64
import urllib.parse
import requests
import json


def send_msg_text(access_token,secret,content):
    secret_enc = secret.encode('utf-8')
    timestamp = str(round(time.time() * 1000))
    string_to_sign = '{}\n{}'.format(timestamp, secret)
    string_to_sign_enc = string_to_sign.encode('utf-8')
    hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
    sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))

    webhook = "https://oapi.dingtalk.com/robot/send?access_token=%s&timestamp=%s&sign=%s" %(access_token,timestamp,sign)
    header = {
            "Content-Type": "application/json",
            "Charset": "UTF-8"
    }
    message = {
                "msgtype": "text",
                "text": {
                    "content": content
                },
                "at": {
                    "atMobiles": [""],
                    "isAtAll": False
                }
    } 

    message_json = json.dumps(message)
    res = requests.post(url=webhook,data=message_json,headers=header)
    return res.text


def send_msg_markdown(access_token,secret,title,text):
    secret_enc = secret.encode('utf-8')
    timestamp = str(round(time.time() * 1000))
    string_to_sign = '{}\n{}'.format(timestamp, secret)
    string_to_sign_enc = string_to_sign.encode('utf-8')
    hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
    sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))

    webhook = "https://oapi.dingtalk.com/robot/send?access_token=%s&timestamp=%s&sign=%s" %(access_token,timestamp,sign)
    header = {
            "Content-Type": "application/json",
            "Charset": "UTF-8"
    }
    message = {
                "msgtype": "markdown",
                "markdown": {
                    "title": title ,
                    "text": text
                },
                "at": {
                    "atMobiles": [""],
                    "isAtAll": False
                }
    } 

    message_json = json.dumps(message)
    res = requests.post(url=webhook,data=message_json,headers=header)
    return res.text


if __name__=="__main__":

    # 釘釘機(jī)器人信息
    access_token = 'ze938fcf8f80a90464b26b25d43e99fe7c291c9b8320f360c8e83wwwlinxuhubcn'
    secret = 'SECzz525afc9bb354d424f2361b4d563e26bd9e564bd095ca5a4wwwlinxuhubcn'
    
    # text
    content = '你好, 澤澤!'
    print(send_msg_text(access_token,secret,content))


    # markdown
    title = "項(xiàng)目發(fā)布通知"
    text = "### 發(fā)布成功 \n\n>項(xiàng)目: java-demo\n\n>環(huán)境: prd\n\n>日期時(shí)間: 2020-09-05 11:56"
    print(send_msg_markdown(access_token,secret,title,text))

image.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容