代码:
- [root@ip–172–31–46–25 ~]# more tmp.py
- #!/usr/bin/python
- #coding=utf–8
- import urllib
- import urllib2
- import json
- import sys
- import socket
- reload(sys)
- sys.setdefaultencoding(‘utf8’)
- # 获取钉钉消息
- def extractionMessage() :
- #拼接需要发送的消息
- return “##### test1“
- #发送钉钉消息
- def sendDingDingMessage(url, data):
- req = urllib2.Request(url)
- req.add_header(“Content-Type”, “application/json; charset=utf-8”)
- opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())
- response = opener.open(req, json.dumps(data))
- return response.read()
- #主函数
- def main():
- posturl = “https://oapi.dingtalk.com/robot/send?access_token=xxxxxx”
- data = {“msgtype”: “markdown”, “markdown”: {“text”: extractionMessage(),“title”:“Jenkins”,“isAtAll”: “true”}}
- sendDingDingMessage(posturl, data)
- main()
如果需要@某人,需要做如下的改变
- [root@ip–172–31–46–25 ~]# more tmp.py
- #!/usr/bin/python
- #coding=utf–8
- import urllib
- import urllib2
- import json
- import sys
- import socket
- reload(sys)
- sys.setdefaultencoding(‘utf8’)
- # 获取钉钉消息
- def extractionMessage() :
- #拼接需要发送的消息
- return “##### <font color=orange> test1 @15510033357</font>”
- #发送钉钉消息
- def sendDingDingMessage(url, data):
- req = urllib2.Request(url)
- req.add_header(“Content-Type”, “application/json; charset=utf-8”)
- opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())
- response = opener.open(req, json.dumps(data))
- return response.read()
- #主函数
- def main():
- posturl = “https://oapi.dingtalk.com/robot/send?access_token=81e2e9092fd2e965e93bdf54d84f6109ee0ac449b11052227655902d2acf6a5d”
- data = {“msgtype”: “markdown”, “markdown”: {“text”: extractionMessage(),“title”:“Jenkins”},“at”:{“isAtAll”: “false”,“atMobiles”:[“15510033357”]} }
- sendDingDingMessage(posturl, data)
- main()
参数 | 必选 | 类型 | 说明 |
---|---|---|---|
msgtype | true | string | 此消息类型为固定markdown |
title | true | string | 首屏会话透出的展示内容 |
text | true | string | markdown格式的消息 |
atMobiles | Array | 否 | 被@人的手机号(在text内容里要有@手机号) |
isAtAll | bool | 否 | @所有人时:true,否则为:false |
原文地址:https://blog.csdn.net/foolsong/article/details/77888790