forked from Vu1nT0tal/yarb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
294 lines (248 loc) · 10.9 KB
/
bot.py
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
import time
import json
import yaml
import telegram
import requests
import smtplib
import subprocess
from email.header import Header
from email.mime.text import MIMEText
from pathlib import Path
from datetime import datetime
from pyrate_limiter import Duration, Limiter, RequestRate
from utils import *
__all__ = ["feishuBot", "wecomBot", "dingtalkBot", "qqBot", "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
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)
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
def send(self, text_list: list):
limiter = Limiter(RequestRate(20, Duration.MINUTE)) # 频率限制,20条/分钟
for text in text_list:
with limiter.ratelimit('identity', delay=True):
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, 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 = ''.join(f'- [{title}]({link})\n' for title, link in value.items())
text_list.append([feed, text.strip()])
return text_list
def send(self, text_list: list):
limiter = Limiter(RequestRate(20, Duration.MINUTE)) # 频率限制,20条/分钟
for (feed, text) in text_list:
with limiter.ratelimit('identity', delay=True):
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}'
r = requests.post(url=url, headers=headers, data=json.dumps(data), proxies=self.proxy)
if r.status_code == 200:
console.print('[ ] dingtalkBot 发送成功', style='bold green')
else:
console.print('[-] dingtalkBot 发送失败', style='bold red')
print(r.text)
class qqBot:
"""QQ群机器人
https://github.com/Mrs4s/go-cqhttp
"""
cqhttp_path = Path(__file__).absolute().parent.joinpath('cqhttp')
def __init__(self, group_id: list) -> None:
self.server = 'http://127.0.0.1:5700'
self.group_id = group_id
@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
def send(self, text_list: list):
limiter = Limiter(RequestRate(20, Duration.MINUTE)) # 频率限制,20条/分钟
for text in text_list:
with limiter.ratelimit('identity', delay=True):
print(f'{len(text)} {text[:50]}...{text[-50:]}')
for id in self.group_id:
try:
r = requests.post(f'{self.server}/send_group_msg?group_id={id}&&message={text}')
if r.status_code == 200:
console.print(f'[ ] qqBot 发送成功 {id}', style='bold green')
else:
console.print(f'[-] qqBot 发送失败 {id}', style='bold red')
except Exception as e:
console.print(f'[-] qqBot 发送失败 {id}', style='bold red')
print(e)
def start_server(self, qq_id, qq_passwd, timeout=60):
config_path = self.cqhttp_path.joinpath('config.yml')
with open(config_path, 'r') as f:
data = yaml.load(f, Loader=yaml.FullLoader)
data['account']['uin'] = int(qq_id)
data['account']['password'] = qq_passwd
with open(config_path, 'w ') as f:
yaml.dump(data, f)
subprocess.run('cd cqhttp && ./go-cqhttp -d', shell=True)
timeout = time.time() timeout
while True:
try:
requests.get(self.server)
console.print('[ ] qqBot 启动成功', style='bold green')
return True
except Exception as e:
time.sleep(1)
if time.time() > timeout:
qqBot.kill_server()
console.print('[-] qqBot 启动失败', style='bold red')
return False
@classmethod
def kill_server(cls):
pid_path = cls.cqhttp_path.joinpath('go-cqhttp.pid')
subprocess.run(f'cat {pid_path} | xargs kill', stderr=subprocess.DEVNULL, shell=True)
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
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:
proxy = telegram.utils.request.Request(proxy_url=proxy_url)
self.chat_id = chat_id
self.bot = telegram.Bot(token=key, request=proxy)
def test_connect(self):
try:
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
def send(self, text_list: list):
limiter = Limiter(RequestRate(20, Duration.MINUTE)) # 频率限制,20条/分钟
for text in text_list:
with limiter.ratelimit('identity', delay=True):
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)