python 编写的钉钉机器人自动发消息

用python实现的钉钉机器人发消息

 1 # coding:utf-8
 2 
 3 import json
 4 
 5 import urllib.request
 6 
 7 # 1、构建url
 8 
 9 url = "机器人的tooken地址"
10 # url为机器人的webhook
11 
12 # 2、构建一下请求头部
13 
14 header = {
15 
16     "Content-Type": "application/json",
17 
18     "Charset": "UTF-8"
19 
20 }
21 
22 # 3、构建请求数据
23 
24 data = {
25     "msgtype": "text",
26     "text": {
27         "content": "【你要发送的消息内容】  "
28     },
29     "at": {
30          "isAtAll": True     #@全体成员(在此可设置@特定某人)
31     }
32 }
33 
34 #4、对请求的数据进行json封装
35 sendData = json.dumps(data)#将字典类型数据转化为json格式
36 sendData = sendData.encode("utf-8") # python3的Request要求data为byte类型
37 #5、发送请求
38 request = urllib.request.Request(url=url, data=sendData, headers=header)
39 
40 #6、将请求发回的数据构建成为文件格式
41 
42 opener = urllib.request.urlopen(request)
43 #7、打印返回的结果
44 print(opener.read())
45  
View Code

猜你喜欢

转载自www.cnblogs.com/sniepr/p/12308318.html