钉钉自定义机器人+加签

钉钉机器人增加了 加签的功能 python版本

def send_ding(content, webhook_addrs=None):
    """
    钉钉机器人 通知
    :param content:
    :return:
    """
    secret = "SEC19dd84be8c7f3cf71761974f1ed3ef77c1451906ae8ba18acf56103ee"
    timestamp = "%d" % (time.time()*1000)
    sign = timestamp+'\n'+secret
    j = hmac.new(secret.encode(), msg=sign.encode(), digestmod=hashlib.sha256)
    sign = parse.quote(base64.b64encode(j.digest()), safe='')
    if not webhook_addrs:
        access_token = "98975521a1b23c02cdd1a9a08c69511ddf5fa9d2ae387813d1c5348fb"
        url = "https://oapi.dingtalk.com/robot/send?access_token=%s&sign=%s×tamp=%s" % \
              (access_token, sign, timestamp)
        webhook_addrs = [url]
    for url in webhook_addrs:
        data = {
            "msgtype": "markdown",
            "markdown": {
                "title": "业务告警",
                "text": content
            },
            "at": {"atMobiles": ["18501376614"],
            "isAtAll": False
        }}
        post(url, data=data)




def post(url, param=None, data=None, headers=None):
    """
    post 请求
    """
    try:
        if param:
            url = url + "?" + urlencode(param)
        if data:
            data = json.dumps(data)
        head = {"content-type": "application/json"}
        if headers:
            head.update(headers)
        response = session.post(url, data=data, headers=head, timeout=TIMEOUT)
        log.log_info("post请求{}".format({"请求url": url, "请求data": data, "响应": response.text}))
        return response.text
    except Exception as e:
        log.log_error("post请求错误{}".format({"请求url": url, "请求data": data, "错误": e}), exc_info=True)
        return False

你可能感兴趣的:(python)