-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathDtalkRobot.py
More file actions
146 lines (117 loc) · 4.6 KB
/
DtalkRobot.py
File metadata and controls
146 lines (117 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
钉钉群自定义机器人
author:疯狂的技术宅
github:https://github.com/magician000
学习python时做的练习,纯粹为了娱乐
如果存在bug请自行修改,不提供任何支持
官方文档
https://open-doc.dingtalk.com/docs/doc.htm?spm=a219a.7629140.0.0.z5MWoh&treeId=257&articleId=105735&docType=1
这个接口的消息格式命名风格不统一,坑爹呢?
所以不要迷信大公司就怎样规范。
"""
import urllib.parse
import urllib.request
import json
import time
# 代理,一般不使用
if False:
proxy_support = urllib.request.ProxyHandler({'http': '192.168.1.60:808', 'https': '192.168.1.60:808'})
# proxy_support = urllib.request.ProxyHandler({'sock5': '192.168.1.60:1080'})
opener = urllib.request.build_opener(proxy_support)
urllib.request.install_opener(opener)
# 自定义机器人的封装类
class DtalkRobot(object):
def __init__(self, webhook):
super(DtalkRobot, self).__init__()
self.webhook = webhook
# text类型
def sendText_webhook(self, webhook, msg, isAtAll=False, atMobiles=[]):
data = {"msgtype": "text", "text": {"content": msg}, "at": {"atMobiles": atMobiles, "isAtAll": isAtAll}}
return self.post_webhook(webhook, data)
# markdown类型
def sendMarkdown_webhook(self, webhook, title, text):
data = {"msgtype": "markdown", "markdown": {"title": title, "text": text}}
return self.post_webhook(webhook, data)
# link类型
def sendLink(self, title, text, messageUrl, picUrl=""):
data = {"msgtype": "link",
"link": {"text": text, "title": title, "picUrl": picUrl, "messageUrl": messageUrl}}
return self.post(data)
# ActionCard类型
def sendActionCard(self, actionCard):
data = actionCard.getData()
return self.post(data)
# FeedCard类型
def sendFeedCard(self, links):
data = {"feedCard": {"links": links}, "msgtype": "feedCard"}
return self.post(data)
def post(self, data):
post_data = json.JSONEncoder().encode(data).encode(encoding='UTF8')
req = urllib.request.Request(self.webhook, post_data)
req.add_header('Content-Type', 'application/json')
content = urllib.request.urlopen(req).read().decode('UTF-8')
return content
def post_webhook(self, webhook, data):
post_data = json.JSONEncoder().encode(data).encode(encoding='UTF8')
req = urllib.request.Request(webhook, post_data)
req.add_header('Content-Type', 'application/json')
content = urllib.request.urlopen(req).read().decode('UTF-8')
# print(content)
return content
# ActionCard类型消息结构
class ActionCard(object):
"""docstring for ActionCard"""
title = ""
text = ""
singleTitle = ""
singleURL = ""
btnOrientation = 0
hideAvatar = 0
btns = []
def __init__(self, arg=""):
super(ActionCard, self).__init__()
self.arg = arg
def putBtn(self, title, actionURL):
self.btns.append({"title": title, "actionURL": actionURL})
def getData(self):
data = {"actionCard": {"title": self.title, "text": self.text, "hideAvatar": self.hideAvatar,
"btnOrientation": self.btnOrientation, "singleTitle": self.singleTitle,
"singleURL": self.singleURL, "btns": self.btns}, "msgtype": "actionCard"}
return data
# FeedCard类型消息格式
class FeedLink(object):
"""docstring for FeedLink"""
title = ""
picUrl = ""
messageUrl = ""
def __init__(self, arg=""):
super(FeedLink, self).__init__()
self.arg = arg
def getData(self):
data = {"title": self.title, "picURL": self.picUrl, "messageURL": self.messageUrl}
return data
if __name__ == "__main__":
webhook = "https://oapi.dingtalk.com/robot/send?access_token=72f6252257f43f402f743b7d698fc21d21939717783a20689e1c1292c20d1903"
robot = DtalkRobot(webhook)
# print(robot.sendText("xxxx:[" + time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) + "]", False,
# ["13912345678 "]))
print(robot.sendMarkdown('报单回报', """# LAST_HEDGE (OI805)
1. 21:00:00.224/6298/0|-2/
1. 21:00:01.412/6296/0
## LAST_HEDGE (TA805)
1. 21:00:00.224/5692/0|-6/
1. 21:00:01.427/5690/0
### LAST_HEDGE (TA805)
- 21:00:00.224/5692/0|-6/
- 21:00:01.427/5690/0
#### LAST_HEDGE (TA805)
- 21:00:00.224/5692/0|-6/
- 21:00:01.427/5690/0
##### LAST_HEDGE (TA805)
- 21:00:00.224/5692/0|-6/
- 21:00:01.427/5690/0
###### LAST_HEDGE (i1805)
- 21:00:00.427/514.5/0|1/
- 21:00:01.412/514/0"""))