dingTalk.py 3.65 KB
Newer Older
taoke committed
1
import requests,time,hmac,hashlib,base64,urllib.parse,json
fanxun committed
2
from config import BaseConfig
taoke committed
3
import datetime
fanxun committed
4 5


taoke committed
6 7 8 9 10 11 12 13 14 15 16

def get_sign(secret):
    timestamp = str(round(time.time() * 1000))
    secret_enc = secret.encode('utf-8')
    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))
    return sign

def dingTalk(secret,webhook,message):
fanxun committed
17
    """发送消息到钉钉群"""
taoke committed
18 19 20
    timestamp = str(round(time.time() * 1000))
    sign=get_sign(secret)
    webhook=webhook+f"&timestamp={timestamp}&sign={sign}"
fanxun committed
21 22 23 24
    data = {'msgtype': 'text', 'text': {"content": message},
            'at': {'isAtAll': True}}
    post_data = json.dumps(data)
    response = requests.post(webhook, headers=BaseConfig.headers, data=post_data)
taoke committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
    return response.text

def dingTalk_link(secret,webhook,message):
    """发送消息到钉钉群"""
    timestamp = str(round(time.time() * 1000))
    sign=get_sign(secret)
    webhook=webhook+f"&timestamp={timestamp}&sign={sign}"
    data = {'msgtype': 'link',
            "link": {
            "text": "这个即将发布的新版本,创始人xx称它为红树林。而在此之前,每当面临重大升级,产品经理们都会取一个应景的代号,这一次,为什么是红树林",
            "title": "Dinding发送消息接口文档",
            "picUrl": "https://aqjg.gyszhjt.com:60028/img/group1/M00/00/03/Cgs5XWCf8QiAeR3KAAA1uImt-QE934.jpg",
            "messageUrl": "https://developers.dingtalk.com/document/app/custom-robot-access"
                     },
            'at': {'isAtAll': True}}
    post_data = json.dumps(data)
    response = requests.post(webhook, headers=BaseConfig.headers, data=post_data)
    return response.text


def dingTalk_markdown(secret,webhook,message):
    """发送消息到钉钉群"""
    timestamp = str(round(time.time() * 1000))
    sign=get_sign(secret)
    webhook=webhook+f"&timestamp={timestamp}&sign={sign}"
taoke committed
50
    now_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M')
taoke committed
51 52 53
    data = {'msgtype': 'markdown',
            "markdown": {
            "title":"接口执行报告",
taoke committed
54
            "text": f"#### 接口自动化测试报告  \n> 本消息由Jenkins构建后自动发送\n> \
taoke committed
55
            ![screenshot](https://aqjg.gyszhjt.com:60028/img/group1/M00/00/03/Cgs5X2CiKqSAPrSCAAEaUuxutsg217.png)\n> ###### {now_time}构建 [斑马信用](http://10.197.236.10:8080/job/bmc/allure/) \n"
taoke committed
56 57 58 59 60 61 62 63 64 65
                        },
            'at': {'isAtAll': False}}
    post_data = json.dumps(data)
    response = requests.post(webhook, headers=BaseConfig.headers, data=post_data)
    return response.text



if __name__ == '__main__':

taoke committed
66 67
    res=dingTalk_markdown(secret="SEC1d08f46da74337cc0e1cd5bb9ad19622d825483343fdfa43ce396881e4745bdb",
                 webhook="https://oapi.dingtalk.com/robot/send?access_token=f9e005c1a984b9607960345d38669337b1115d1141a0294e98666443b312115b",
taoke committed
68 69
                 message="我就是我, @XXX 是不一样的烟火")
    print(res)
taoke committed
70 71
    now_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M')
    print(now_time)
taoke committed
72 73 74 75


    # 自动化测试组 - 技术和思路分享 - ----小帅
    # SEC40a1be4bbd9214e16ba288208fd608b2b590e82e853fa9b24c1850a506c6185b
1  
taoke committed
76 77
    # https: // oapi.dingtalk.com / robot / send?access_token = e830b05eeee88da31972099e403a74d05ec55719360707dc44e532c0d0b49cb6

taoke committed
78 79 80
    # 自动化小组
    # secret = "SEC465015385218e70a94f107a16f72dd33d8fc118c3b2a631e0433685302f2fbb3",
    # webhook = "https://oapi.dingtalk.com/robot/send?access_token=229908a83825ed56abbf728d3382e446a4e8a90e9ad302c37a036bcbccbbf9ee"