-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
427 lines (354 loc) · 15.6 KB
/
bot.py
File metadata and controls
427 lines (354 loc) · 15.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
import time
import json
import telegram
import requests
import smtplib
from email.header import Header
from email.mime.text import MIMEText
from pathlib import Path
from datetime import datetime
from pyrate_limiter import Duration, Rate, InMemoryBucket, Limiter
from utils import *
__all__ = ["feishuBot", "wecomBot", "dingtalkBot", "telegramBot", "mailBot"]
today = datetime.now().strftime("%Y-%m-%d")
class feishuBot:
"""飞书群机器人
https://open.feishu.cn/document/ukTMukTMukTM/ucTM5YjL3ETO24yNxkjN
"""
def __init__(self, key, proxy_url='') -> None:
self.key = key
self.proxy = {'http': proxy_url, 'https': proxy_url} if proxy_url else {
'http': None, 'https': None}
@staticmethod
def parse_results(results: list):
text_list = []
for result in results:
(feed, value), = result.items()
text = f'[ {feed} ]\n\n'
for title, link in value.items():
text += f'{title}\n{link}\n\n'
text_list.append(text.strip())
return text_list
async def send(self, text_list: list):
for text in text_list:
print(f'{len(text)} {text[:50]}...{text[-50:]}')
data = {"msg_type": "text", "content": {"text": text}}
headers = {'Content-Type': 'application/json'}
url = f'https://open.feishu.cn/open-apis/bot/v2/hook/{self.key}'
r = requests.post(url=url, headers=headers,
data=json.dumps(data), proxies=self.proxy)
if r.status_code == 200:
console.print('[+] feishuBot 发送成功', style='bold green')
else:
console.print('[-] feishuBot 发送失败', style='bold red')
print(r.text)
async def send_markdown(self, text):
# TODO 富文本
data = {"msg_type": "text", "content": {"text": text}}
self.send(data)
class wecomBot:
"""企业微信群机器人
https://developer.work.weixin.qq.com/document/path/91770
"""
def __init__(self, key, proxy_url='') -> None:
self.key = key
self.proxy = {'http': proxy_url, 'https': proxy_url} if proxy_url else {
'http': None, 'https': None}
@staticmethod
def parse_results(results: list):
text_list = []
for result in results:
(feed, value), = result.items()
text = f'## {feed}\n'
for title, link in value.items():
text += f'- [{title}]({link})\n'
text_list.append(text.strip())
return text_list
async def send(self, text_list: list):
rates = [Rate(20, Duration.MINUTE)] # 频率限制,20条/分钟
bucket = InMemoryBucket(rates)
limiter = Limiter(bucket, max_delay=Duration.MINUTE.value)
for text in text_list:
limiter.try_acquire('identity')
print(f'{len(text)} {text[:50]}...{text[-50:]}')
data = {"msgtype": "markdown", "markdown": {"content": text}}
headers = {'Content-Type': 'application/json'}
url = f'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key={self.key}'
r = requests.post(url=url, headers=headers, data=json.dumps(data), proxies=self.proxy)
if r.status_code == 200:
console.print('[+] wecomBot 发送成功', style='bold green')
else:
console.print('[-] wecomBot 发送失败', style='bold red')
print(r.text)
class dingtalkBot:
"""钉钉群机器人
https://open.dingtalk.com/document/robots/custom-robot-access
"""
def __init__(self, key, secret='', proxy_url='') -> None:
self.key = key
self.secret = secret
self.proxy = {'http': proxy_url, 'https': proxy_url} if proxy_url else {
'http': None, 'https': None}
@staticmethod
def parse_results(results: list):
text_list = []
for result in results:
(feed, value), = result.items()
text = ''.join(
f'- [{title}]({link})\n' for title, link in value.items())
text_list.append([feed, text.strip()])
return text_list
def _get_sign(self):
"""计算钉钉机器人签名"""
import hmac
import hashlib
import base64
import urllib.parse
timestamp = str(round(time.time() * 1000))
secret_enc = self.secret.encode('utf-8')
string_to_sign = '{}\n{}'.format(timestamp, self.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_plus(base64.b64encode(hmac_code))
return timestamp, sign
async def send(self, text_list: list):
rates = [Rate(20, Duration.MINUTE)] # 频率限制,20条/分钟
bucket = InMemoryBucket(rates)
limiter = Limiter(bucket, max_delay=Duration.MINUTE.value)
for (feed, text) in text_list:
limiter.try_acquire('identity')
text = f'## {feed}\n{text}'
text += f"\n\n <!-- Powered by Yarb. -->"
print(f'{len(text)} {text[:50]}...{text[-50:]}')
data = {"msgtype": "markdown", "markdown": {
"title": feed, "text": text}}
headers = {'Content-Type': 'application/json'}
url = f'https://oapi.dingtalk.com/robot/send?access_token={self.key}'
# 如果设置了secret,添加签名
if self.secret:
timestamp, sign = self._get_sign()
url = f'{url}×tamp={timestamp}&sign={sign}'
console.print(f'[*] dingtalkBot 使用签名验证', style='bold blue')
r = requests.post(url=url, headers=headers,
data=json.dumps(data), proxies=self.proxy)
if r.status_code == 200:
resp = r.json()
if resp.get('errcode') == 0:
console.print('[+] dingtalkBot 发送成功', style='bold green')
else:
console.print(f"[-] dingtalkBot 发送失败: {resp.get('errmsg')}", style='bold red')
else:
console.print('[-] dingtalkBot 发送失败', style='bold red')
print(r.text)
class mailBot:
"""邮件机器人
"""
def __init__(self, sender, passwd, receiver: str, fromwho='', server='') -> None:
self.sender = sender
self.receiver = receiver
self.fromwho = fromwho or sender
server = server or self.get_server(sender)
self.smtp = smtplib.SMTP_SSL(server)
self.smtp.login(sender, passwd)
def get_server(self, sender: str):
key = sender.rstrip('.com').split('@')[-1]
server = {
'qq': 'smtp.qq.com',
'foxmail': 'smtp.qq.com',
'163': 'smtp.163.com',
'sina': 'smtp.sina.com',
'gmail': 'smtp.gmail.com',
'outlook': 'smtp.live.com',
}
return server.get(key, f'smtp.{key}.com')
@staticmethod
def parse_results(results: list):
text = f'<html><head><h1>每日安全资讯({today})</h1></head><body>'
for result in results:
(feed, value), = result.items()
text += f'<h3>{feed}</h3><ul>'
for title, link in value.items():
text += f'<li><a href="{link}">{title}</a></li>'
text += '</ul>'
text += '<br><br><b>如不需要,可直接回复本邮件退订。</b></body></html>'
print(text)
return text
async def send(self, text: str):
print(f'{len(text)} {text[:50]}...{text[-50:]}')
msg = MIMEText(text, 'html')
msg['Subject'] = Header(f'每日安全资讯({today})')
msg['From'] = self.fromwho
msg['To'] = self.receiver
try:
self.smtp.sendmail(
self.sender, self.receiver.split(','), msg.as_string())
console.print('[+] mailBot 发送成功', style='bold green')
except Exception as e:
console.print('[+] mailBot 发送失败', style='bold red')
print(e)
class telegramBot:
"""Telegram机器人
https://core.telegram.org/bots/api
"""
def __init__(self, key, chat_id: list, proxy_url='') -> None:
self.key = key
self.proxy = {'http': proxy_url, 'https': proxy_url} if proxy_url else {
'http': None, 'https': None}
proxy = telegram.request.HTTPXRequest(proxy_url=None)
self.chat_id = chat_id
self.bot = telegram.Bot(token=key, request=proxy)
async def test_connect(self):
try:
await self.bot.get_me()
return True
except Exception as e:
console.print('[-] telegramBot 连接失败', style='bold red')
return False
@staticmethod
def parse_results(results: list):
text_list = []
for result in results:
(feed, value), = result.items()
text = f'<b>{feed}</b>\n'
for idx, (title, link) in enumerate(value.items()):
text += f'{idx+1}. <a href="{link}">{title}</a>\n'
text_list.append(text.strip())
return text_list
async def send(self, text_list: list):
rates = [Rate(20, Duration.MINUTE)] # 频率限制,20条/分钟
bucket = InMemoryBucket(rates)
limiter = Limiter(bucket, max_delay=Duration.MINUTE.value)
for text in text_list:
limiter.try_acquire('identity')
print(f'{len(text)} {text[:50]}...{text[-50:]}')
for id in self.chat_id:
try:
self.bot.send_message(chat_id=id, text=text, parse_mode='HTML')
console.print(f'[+] telegramBot 发送成功 {id}', style='bold green')
except Exception as e:
console.print(f'[-] telegramBot 发送失败 {id}', style='bold red')
print(e)
class wechatAppBot:
"""企业微信应用推送
https://developer.work.weixin.qq.com/document/path/90236
"""
def __init__(self, corpid, corpsecret, agentid, proxy_url=''):
self.corpid = corpid
self.corpsecret = corpsecret
self.agentid = agentid
self.proxy = {'http': proxy_url, 'https': proxy_url} if proxy_url else {
'http': None, 'https': None}
self.access_token = None
self.token_expire_time = 0
def _get_access_token(self):
"""获取access_token"""
import time
current_time = time.time()
# 如果token未过期,直接返回
if self.access_token and current_time < self.token_expire_time:
return self.access_token
# 获取新token
url = f'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={self.corpid}&corpsecret={self.corpsecret}'
try:
r = requests.get(url, proxies=self.proxy, timeout=10)
if r.status_code == 200:
resp = r.json()
if resp.get('errcode') == 0:
self.access_token = resp.get('access_token')
# token有效期7200秒,提前300秒刷新
self.token_expire_time = current_time + resp.get('expires_in', 7200) - 300
return self.access_token
else:
# 打印错误信息用于调试
print(f"[-] 企业微信获取token失败: errcode={resp.get('errcode')}, errmsg={resp.get('errmsg')}")
return None
else:
print(f"[-] 企业微信获取token失败: HTTP {r.status_code}")
return None
except Exception as e:
print(f"[-] 企业微信获取token异常: {e}")
return None
async def send(self, text: str):
"""发送文本消息(不记录日志)"""
try:
access_token = self._get_access_token()
if not access_token:
print("[-] 企业微信推送失败: 无法获取access_token")
return False
url = f'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={access_token}'
# 企业微信应用消息格式
data = {
"touser": "@all",
"msgtype": "text",
"agentid": self.agentid,
"text": {
"content": text
}
}
headers = {'Content-Type': 'application/json'}
r = requests.post(url, headers=headers, data=json.dumps(data, ensure_ascii=False), proxies=self.proxy, timeout=10)
if r.status_code == 200:
resp = r.json()
if resp.get('errcode') == 0:
return True
else:
print(f"[-] 企业微信推送失败: errcode={resp.get('errcode')}, errmsg={resp.get('errmsg')}")
return False
else:
print(f"[-] 企业微信推送失败: HTTP {r.status_code}, {r.text[:200]}")
return False
except Exception as e:
print(f"[-] 企业微信推送异常: {e}")
return False
class dingtalkAISummaryBot:
"""钉钉AI总结推送(不记录日志)"""
def __init__(self, access_token, secret='', proxy_url=''):
self.access_token = access_token
self.secret = secret
self.proxy = {'http': proxy_url, 'https': proxy_url} if proxy_url else {
'http': None, 'https': None}
def _get_sign(self):
"""计算钉钉机器人签名"""
import hmac
import hashlib
import base64
import urllib.parse
timestamp = str(round(time.time() * 1000))
secret_enc = self.secret.encode('utf-8')
string_to_sign = '{}\n{}'.format(timestamp, self.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_plus(base64.b64encode(hmac_code))
return timestamp, sign
async def send(self, text: str):
"""发送文本消息(不记录日志)"""
try:
if not self.access_token:
print("[-] 钉钉推送失败: access_token为空")
return False
data = {"msgtype": "markdown", "markdown": {
"title": "AI安全资讯总结",
"text": text
}}
headers = {'Content-Type': 'application/json'}
url = f'https://oapi.dingtalk.com/robot/send?access_token={self.access_token}'
# 如果设置了secret,添加签名
if self.secret:
timestamp, sign = self._get_sign()
url = f'{url}×tamp={timestamp}&sign={sign}'
r = requests.post(url, headers=headers, data=json.dumps(data, ensure_ascii=False), proxies=self.proxy, timeout=10)
if r.status_code == 200:
resp = r.json()
if resp.get('errcode') == 0:
return True
else:
print(f"[-] 钉钉推送失败: errcode={resp.get('errcode')}, errmsg={resp.get('errmsg')}")
return False
else:
print(f"[-] 钉钉推送失败: HTTP {r.status_code}, {r.text[:200]}")
return False
except Exception as e:
print(f"[-] 钉钉推送异常: {e}")
import traceback
traceback.print_exc()
return False