钉钉机器人自定义通知-python

创建一个钉钉群然后添加一个自定义的机器人

安全设置是必填项
     自定义关键字:报警
     钉钉发送通知时,必须包含 关键子 “报警” 不然会 报 keyword not in content。

钉钉文档: https://ding-doc.dingtalk.com/doc#/serverapi2/qf2nxq

机器人创建成功后,针对文档中发送钉消息类型选择合适的进行触发。

 python 脚本

 body={
         "msgtype": "markdown",
         "markdown": {
             "title":"项目单测情况",
             "text": "#### 杭州天气 @156xxxx8827
" +
              "> 9度,西北风1级,空气良89,相对温度73%

" +
              "> ![screenshot](https://gw.alicdn.com/tfs/TB1ut3xxbsrBKNjSZFpXXcXhFXa-846-786.png)
"  +
              "> ###### 10点20分发布 [天气](http://www.thinkpage.cn/) 
"
         },
       "at": {
           "atMobiles": [
               "156xxxx8827",
               "189xxxx8325"
           ]
       }
    }

 ```
 # -*- coding: utf-8 -*-  linux 和Mac中加上,不然报编码错误 或者是  
  #encoding=utf-8    


 # 字符串连接 用  反斜杠。 
   sendInfo="### **项目警告** 
 
" 
            " **项目构建:**%s  
 
" 
            " **单测成功率**:%s%%   ----->> %s 
 
" 
            " **行覆盖率:** %s%% 
 
"  
            " **类覆盖率:** %s%% 
 
"  
            " ###  [查看详情](http://host/job/%s/) 
" %(str(name),tests[0],tests[1]+"/"+tests[2],covg["lineCoverage"],covg["classCoverage"],str(name))
     把这个拼接的内容,添加到 markdown text 中。
   body["markdown"]["text"]=sendInfo

   print str(sendInfo)

   header001={'Content-Type': "application/json;charset=utf-8"}
   #resp=requests.post(url=dingdingToken,data=json.dumps(body),headers=header001)

结果

钉钉有多种消息内容可自定义。我用的是markdown

python脚本获取参数使用的是 click,通过命令行传递参数

click用法
https://www.cnblogs.com/Zzbj/p/11309130.html
原文地址:https://www.cnblogs.com/trimphNuan/p/11822820.html