python3判断正式和测试参数来发送钉钉通知

post by rocdk890 / 2021-12-29 17:52 Wednesday linux技术
  最近搞了几个脚本,但配置钉钉报警通知时,老是要把加签这几个参数改来改去,对于我这样的懒人来说,这不能忍受啊,直接就整了个判断,测试脚本的时候就把通知发送到我测试的钉钉机器人那边,测试完成后再改成正式的钉钉机器人通知就好了.

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
@IDE     :PyCharm 
@Author  :rocdk890
@Date    :2021/12/29 17:32 
'''
import sys
import time
import hmac
import hashlib
import base64
import urllib.parse
import requests
import json

project = "dev"
if project == 'dev':
    timestamp = str(round(time.time() * 1000))
    secret = '*****' #改成你自己的
    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(base64.b64encode(hmac_code))
    ddtoken = "*******" #改成你自己的
elif project == 'pro':
    timestamp = str(round(time.time() * 1000))
    secret = '*******' #改成你自己的
    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(base64.b64encode(hmac_code))
    ddtoken = "*********" #改成你自己的
else:
    print("请检查project参数是否正确!")
    sys.exit(0)

def dingding_robot(data):
    headers = {'Content-Type': 'application/json'}
    webhook = 'https://oapi.dingtalk.com/robot/send?access_token=' + ddtoken + "&timestamp=" + timestamp + "&sign=" + sign
    r = requests.post(webhook, headers=headers, data=json.dumps(data))
    r.encoding = 'utf-8'
    return (r.text)

def get_msg(text_content):
    text = {
        "msgtype": "text",
        "text": {
            "content": text_content
        },
    }
    return (text)

if __name__ == '__main__':
    text_content = "测试"
    data = get_msg(text_content)
    dingding_robot(data)

夜空- 本站版权
1、本站所有主题由该文章作者发表,该文章作者与夜空享有文章相关版权
2、其他单位或个人使用、转载或引用本文时必须同时征得该文章作者和夜空的同意
3、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责
4、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意
5、原文链接:blog.slogra.com/post-784.html

标签: linux python 通知 python3 dingding

评论: