-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
293 lines (238 loc) · 9.47 KB
/
bot.py
File metadata and controls
293 lines (238 loc) · 9.47 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
from typing import Union
import requests
from pyrogram import Client
from pyrogram.types import Message
import re
import logging
import config
from datetime import datetime
# 配置日志记录
logging.basicConfig(filename='app.log', filemode='w', format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# 设置日志等级
logging.getLogger().setLevel(logging.INFO)
api_id = config.API_ID
api_hash = config.API_HASH
bot_token = config.BOT_TOKEN
group_chat_id = config.GROUP_CHAT_ID
ql_url = config.QL_URL
client_id = config.CLIENT_ID
client_secret = config.CLIENT_SECRET
enable_proxy = config.ENABLE_PROXY
proxy_scheme = config.PROXY_SCHEME
proxy_hostname = config.PROXY_HOSTNAME
proxy_port = config.PROXY_PORT
need_notify = config.NEED_NOTIFY
user_id = config.USER_ID
def make_request(req_type, url, params=None, headers=None, isFile=False) -> Union[str, dict]:
"""函数化一个接口请求"""
if req_type == "get":
response = requests.get(url, params=params, headers=headers)
elif req_type == "post":
response = requests.post(url, data=params, headers=headers)
elif req_type == "put":
response = requests.put(url, json=params, headers=headers)
elif req_type == "delete":
response = requests.delete(url, headers=headers)
else:
raise ValueError("Invalid request type. Supported types are 'get', 'post', 'put', and 'delete'.")
if isFile:
return response.text
else:
return response.json()
def extract_key_value(text):
"""分解抛出的变量和值"""
pattern = r'export (\w+)=(".*")'
matches = re.findall(pattern, text)
result = []
if matches:
for match in matches:
key, value = match
result.append({'key': key, 'value': value})
return result
def extract_key_value_with_title(text):
"""
分解抛出的变量和值,同时解析标题,去除图标。
"""
result = {}
# 修改标题的正则表达式,移除前导表情符号
title_pattern = r'[\u4e00-\u9fa5\w\s]+·' # 匹配文字、数字和点号
title_match = re.search(title_pattern, text)
if title_match:
result['title'] = title_match.group(0).replace('·', '').strip() # 去掉末尾的点号并去除多余空格
# 解析变量和值
pattern = r'export (\w+)=(".*?")'
matches = re.findall(pattern, text)
if matches:
result['variables'] = [{'key': key, 'value': value} for key, value in matches]
else:
result['variables'] = []
return result
def get_ql_toke():
"""获取青龙的token"""
resp = make_request('get', f'{ql_url}/open/auth/token',
{'client_id': client_id, 'client_secret': client_secret})
if resp['code'] != 200:
_token = -1
else:
_token = resp['data']['token']
return _token
def get_qlva_config(_token=-1):
resp = make_request('get', f'{ql_url}/open/configs/qlva.sh',
headers={'Authorization': f'Bearer {_token}'})
if resp['code'] == 404:
print('没有该文件')
return []
return extract_key_value(resp['data'])
def update_qlva_config(_token=-1, content=''):
resp = make_request("post", f"{ql_url}/open/configs/save",
{'name': 'qlva.sh', 'content': content},
headers={'Authorization': f'Bearer {_token}'})
logging.info(f'update {resp["message"]} \n{content}')
def parse_config(config_string):
"""分解farker脚本库spy的文档"""
config_list = []
items = config_string.split("- Container:")
for item in items[1:]:
obj = {}
lines = item.strip().split("\n")
for line in lines:
if line.strip() != "":
if ": " in line:
key, value = line.strip().split(": ")
if key == "Container":
obj[key] = [value.strip()]
elif key in ["Env", "Script"]:
obj[key] = value.strip()
elif key in ["KeyWord", "Name"]:
obj[key] = [value.strip()]
else:
obj[key] = int(value.strip()) if value.strip().isdigit() else value.strip()
else:
obj['ScriptEnv'] = line.strip()
config_list.append(obj)
return config_list
def get_corns(_token=-1):
resp = make_request('get', f'{ql_url}/open/crons', headers={'Authorization': f'Bearer {_token}'})
# 处理响应,这里使用示例中的数据
# data = resp['data']['data']
# 将数据写入JSON文件
# file_path = 'data.json' # 设置要写入的JSON文件路径
# with open(file_path, 'w') as file:
# json.dump(data, file, indent=4)
return resp['data']['data']
# searchValue: 店铺抽奖
# page: 1
# size: 20
# filters: {}
# queryString: {"filters":null,"sorts":null,"filterRelation":"and"}
# t: 1733450195718
def search_corns_by_name(_name, _token=-1):
# 获取当前时间的时间戳
current_time = int(datetime.now().timestamp() * 1000) # 转为毫秒级时间戳
resp = make_request('get', f'{ql_url}/open/crons', headers={'Authorization': f'Bearer {_token}'}, params={'searchValue': _name,},)
# logging.info(f'搜索结果{resp}')
return resp['data']['data']
def run_crons(_token=-1, ids=None):
if ids is None:
ids = []
resp = make_request('put', f'{ql_url}/open/crons/run', headers={'Authorization': f'Bearer {_token}'},
params=ids)
logging.info(f"任务运行结果{resp}")
def find_config_by_env(configs, env_value, env_key="Env"):
"""在数组对象中找到匹配的对象"""
matching_configs = []
for config in configs:
if env_key in config and env_value in config[env_key]:
matching_configs.append(config)
return matching_configs
def match_script_id(_values, _send_msg, crons, token):
matching_scripts = find_config_by_env(_values, _send_msg[0]['key'])
# print(f'获取到到脚本{crons}')
scipy_names = []
for matching_script in matching_scripts:
matching_ql_script = find_config_by_env(crons, matching_script['Script'], env_key='command')
# print(f'匹配到到青龙脚本{matching_ql_script}')
# if matching_ql_script is not None:
# print(matching_ql_script)
logging.info(f'匹配到到脚本{matching_script}')
scipy_names, ids = [], []
for item in matching_ql_script:
ids.append(item['id'])
scipy_names.append(item['name'])
run_crons(token, ids)
return scipy_names
def run_success():
logging.info('启动成功')
def make_app(flag):
if api_id == '' or api_hash == '' or bot_token == '' \
or ql_url == '' or client_id == '' or client_secret == '':
logging.info(f'api_id api_hash bot_token ql_url client_id client_secret 是必须的')
return None
if flag:
proxy = {
"scheme": proxy_scheme, # "socks4", "socks5" and "http" are supported
"hostname": proxy_hostname,
"port": proxy_port,
}
logging.info(f'走了代理')
_app = Client(
"JDHelper",
api_id=api_id,
api_hash=api_hash,
bot_token=bot_token,
proxy=proxy
)
else:
_app = Client(
"JDHelper",
api_id=api_id,
api_hash=api_hash,
bot_token=bot_token,
)
logging.info(f'没走代理')
return _app
def main():
# 创建一个APP
app = make_app(enable_proxy)
if app is None:
return
logging.info("启动中......")
@app.on_message()
async def echo(client: Client, message: Message):
if message.chat.id == group_chat_id:
# 监听到指定到群组到消息,然后开始回话
logging.info(f'接受到消息{message.text}')
# await message.reply(message.text)
msg = message.text
token = get_ql_toke()
flag = False
if token != -1:
results = get_qlva_config(token)
_send_msg = extract_key_value_with_title(msg)
if len(_send_msg) == 0:
logging.info("无关消息,无需处理")
return
for result in results:
if _send_msg['variables'][0]['key'] == result['key']:
result['value'] = _send_msg['variables'][0]['value']
flag = True
if not flag:
results.append(_send_msg['variables'][0])
update_str = ''
for result in results:
update_str += f'export {result["key"]}={result["value"]}\n'
update_qlva_config(token, update_str)
crons = search_corns_by_name(_name=_send_msg['title'], _token=token)
# scipy_names = match_script_id(_values, _send_msg, crons, token)
# crons 得到这样的数组[{id:1,name:xxx}], 将crons的id组成一个数组
ids = [cron['id'] for cron in crons]
logging.info(f'ids要执行{ids}{crons}')
run_crons(token,ids)
if need_notify:
await app.send_message(chat_id=user_id, text=f'监听到线报参数\n{",".join(_send_msg.title)}\n开始自动执行任务')
try:
app.run(run_success())
except ConnectionError as e:
logging.info(f'启动失败 {e}')
if __name__ == '__main__':
main()