This repository was archived by the owner on Jun 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanswer.py
More file actions
390 lines (320 loc) · 12.7 KB
/
Copy pathanswer.py
File metadata and controls
390 lines (320 loc) · 12.7 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
#默认为使用wxid作为userid,如果想要更改为使用单一userid请更改所有调用API_answer后的wxid改为userid
from ast import Tuple
import os
from pluginlib import PluginLoader
import pluginlib
from Chino_old.model_definition import AnswerBase, AnswerBaseList
#from another_action import _a_ , image, name_write,data_name_write,read_name_all,warn,w_all
from . import api_action as api
from .another_action_base import get_roomNick_in_chatroom
import schedule
# from sympy import *
from file_action import config_read
from .file_action import data_read
from .preload import preload
import sys
import xmltodict
from .standard_print import printerr,printinf,printmsg
from .standard_print import logger
import func_timeout
import ujson
import xml.etree.ElementTree as ET
config=config_read()
address=config.connect.host
port=config.connect.port
robotname=config.robotname
preload()
# load_plugins()
# row_qukey_admin=action_sql.qu_key.admin.read()
# row_qukey=action_sql.qu_key.read()
# row_access=action_sql.access.read()
loader = PluginLoader(paths=[os.path.join(os.path.dirname(__file__),"plugins")],group="msg_plugin")
plugins = loader.plugins
print("plugins"+str( plugins))
def run_plugin(row):
global an_
printinf(f"查询到qu_key({row['NAME']})")
if row["an_way"] == "0":
an_ = row["answer"]
elif row["an_way"] == '1':
#print(.411)
Def=row["answer"]
if Def.find("{plugin}") != -1:
Def=Def.replace("{plugin}",f"plugins.{row['filename']}")
try:
an_=eval(f"{Def}")(msg_l)
except func_timeout.exceptions.FunctionTimedOut:
printerr("qu_key({row['NAME']})执行超时")
an_="超时-{row['NAME']}"
def keyword_(keyw,row):
qu=msg_l["qu"]
if row["key_way"] == "0":
#print(.21)
if qu == keyw:
#print(.31)
return run_plugin(row)
elif row["key_way"] == "1":
#print(.22)
#qu=qu.replace(":@ ","",1)
if qu.find(keyw) == 0:
#print(.32)
return run_plugin(row)
elif row["key_way"] == "2":
#print(.23)
if qu.find(keyw) != -1:
#print(.33)
return run_plugin(row)
def sql_row(rows):
for row in rows:
#print(row)
if row["Enabled"] == "True":
keyword=ujson.loads(row["keyword"])
#printinf(keyword)
if isinstance(keyword,(list)) is True:
for key_x in keyword:
keyword_(key_x,row)
elif isinstance(keyword,(str,int)) is True:
keyword_(keyword,row)
def sql_row_access(rows):
global an_
for row in rows:
if row["Enabled"] == "True":
if row["access_way"] == "0":
Def=row["access"]
if Def.find("{plugin}") != -1:
Def=Def.replace("{plugin}",f"plugins.{row['filename']}")
try:
an_=eval(f"{Def}")(msg_l)
except func_timeout.exceptions.FunctionTimedOut:
printerr("access({row['NAME']})执行超时")
#print(an_)
number_of_times={} #每10s清空一次number_of_times
def clear_number_of_times():
number_of_times.clear()
schedule.every(10).minutes.do(number_of_times.clear)
def run_an_replace(row,an):
printinf(f"查询到an_replace{row['NAME']}")
if row["re_way"] == "0":
return an.replace(row["keyword"],row["replace"])
elif row["re_way"] == '1':
Def=row["replace"]
if Def.find("{plugin}") != -1:
Def=Def.replace("{plugin}",f"plugins.{row['filename']}")
try:
an=eval(f"{Def}")(msg_l,an)
except func_timeout.exceptions.FunctionTimedOut:
printerr("qu_key({row['NAME']})执行超时")
an="超时-an_replace({row['NAME']})"
return an
def sql_row_an_replace(rows,an):
for row in rows:
if row["Enabled"] == "True":
if row["key_way"] == "0":
if an == row["keyword"]:
an=run_an_replace(row,an)
elif row["key_way"] == "1":
if an.find(row["keyword"]) != -1:
an=run_an_replace(row,an)
return an
def Replace_msg(context:str): #TODO
...
async def send_msg_an(plugin_result: AnswerBase): #允许传入列表,实现每次调用加一次number_of_times[wxid_group],且如果这个数字超过指定数量后不回复
wxid=msg_l["wxid"]
#wxid_group=l["wxid_group"]
#print(an)
#print(an)
#if 'wxid_group' in locals():
# if wxid_group in number_of_times:
# number_of_times[wxid_group] += 1
# else:
# number_of_times[wxid_group] = 1
# if number_of_times[wxid_group] > 5:
# return
#else:
if wxid in number_of_times:
number_of_times[wxid] += 1
else:
number_of_times[wxid] = 1
if number_of_times[wxid] > 10:
return
answer_send=plugin_result.answer
if isinstance(answer_send,str):
if answer_send=="":
logger.info("answer_send为空字符串")
return
if plugin_result.Replace: #TODO
pass
match plugin_result.send_way:
case "Text":
if isinstance(answer_send,str):
api.send_msg.text(wxid,answer_send)
elif isinstance(answer_send,dict):
api.send_msg.text(wxid,**answer_send)
case "Image":
if isinstance(answer_send,str):
api.send_msg.image(wxid,answer_send)
elif isinstance(answer_send,dict):
api.send_msg.image(wxid,**answer_send)
case "File":
if isinstance(answer_send,str):
api.send_msg.file(wxid,answer_send)
elif isinstance(answer_send,dict):
api.send_msg.file(wxid,**answer_send)
case "Gif":
if isinstance(answer_send,str):
api.send_msg.gif(wxid,answer_send)
if isinstance(answer_send,dict):
api.send_msg.gif(wxid,**answer_send)
case "Url":
if isinstance(answer_send,str):
raise RuntimeError("Url发送方式仅支持dict")
if isinstance(answer_send,dict):
api.send_msg.url(wxid,**answer_send)
case "xml":
if isinstance(answer_send,str):
api.send_msg.xml(wxid,answer_send)
if isinstance(answer_send,dict):
api.send_msg.xml(wxid,**answer_send)
case "quote":
if isinstance(answer_send,str):
raise RuntimeError("quote发送方式仅支持dict")
if isinstance(answer_send,dict):
api.send_msg.quote(wxid,**answer_send)
case "Fav":
if isinstance(answer_send,str):
try:
answer_send_int=int(answer_send)
except ValueError as e:
raise RuntimeError(f"Fav发送中需传入可转为int的favLocalID:{e}")
else:
api.send_msg.fav(wxid,answer_send_int)
if isinstance(answer_send,dict):
api.send_msg.fav(wxid,**answer_send)
# if isinstance(an_,(str,int)) is True:
# send_msg.text(wxid,sql_row_an_replace(action_sql.an_replace.read(),an_))
# elif isinstance(an_,(list)) is True:
# for x in an_:
# send_msg.text(wxid,sql_row_an_replace(action_sql.an_replace.read(),x))
# else:
# send_msg.text(wxid,str(an_))
return
#@func_timeout.func_set_timeout(30)
async def answer(wxid,wxid_group,qu): #主调用
data=data_read()
#global an_
#an_=None
qu_xml=qu_xml_data=qu_reply_content=qu_reply_wxid=None
if qu.find("<?xml") == 0:
qu_xml=qu
try:
qu_xml_data=xmltodict.parse(qu)
qu=qu_xml_data["msg"]['appmsg']['title']
if "msg" in qu_xml_data:
if 'appmsg' in qu_xml_data["msg"]:
if 'refermsg' in qu_xml_data["msg"]['appmsg']:
qu_reply_content=qu_xml_data["msg"]['appmsg']['refermsg']['content']
qu_reply_wxid=qu_xml_data["msg"]['appmsg']['refermsg']['chatusr']#如果是None是回复的消息与发送这条回复消息的人是一个人(注意由于提前已经定义为None,所以需要先行判断是否有qu_reply_content)
#if 'img' in qu_xml_data["msg"]:
except: # noqa: E722
pass
#@识别
if qu.find("@") == 0 and qu.find(" ") != -1:
if wxid_group != "":
own_inf=data["own_inf"]
own_inf_roomNick=get_roomNick_in_chatroom(wxid,own_inf["userName"])
print(f"@{own_inf_roomNick}")
if qu.find(f"@{own_inf_roomNick}"+" ") != -1:
qu=qu.replace(f"@{own_inf_roomNick}"+" ",":@")
#print(qu)
if wxid_group is not None:
if qu_xml_data is not None:
printmsg.rece(f"{wxid}({wxid_group}) >> {qu_xml_data}")
else:
printmsg.rece(f"{wxid}({wxid_group}) >> {qu}")
else:
if qu_xml_data is not None:
printmsg.rece(f"{wxid} >> {qu_xml_data}")
else:
printmsg.rece(f"{wxid} >> {qu}")
if wxid_group == "":
isChatroom = False
else:
isChatroom = True
global msg_l
msg_l={"robotname":robotname,"qu":qu,"wxid":wxid,"wxid_group":wxid_group,"qu_xml":qu_xml,"qu_xml_data":qu_xml_data,"qu_reply_content":qu_reply_content,"qu_reply_wxid":qu_reply_wxid,'isChatroom': isChatroom}
print(msg_l)
# #admin部分
# data=data_read()
# wxid_admin=data["wxid_admin"]
# if wxid in wxid_admin or wxid_group in wxid_admin:
# sql_row(row_qukey_admin)
# if an_ is not None:
# await send_msg_an()
# return
block=data["wxid_block"]
white=data["wxid_white"]
if wxid in block or wxid_group in block : #黑名单
return
if len(white) != 0:
if wxid not in white or wxid_group not in white: #白名单
return
#plugin
#print(plugins.items())
for plugins_type, plugins_sec in plugins.items():
match plugins_type:
case "plugin_common":
for plugin_name, plugin_class in plugins_sec.items():
plugin_result=plugin_class.main(msg_l)
if plugin_result is not None:
if isinstance(plugin_result,AnswerBase):
await send_msg_an(plugin_result)
elif isinstance(plugin_result,AnswerBaseList):
for answer_result_base in plugin_result.answers:
await send_msg_an(answer_result_base)
case "plugin_admin":
if msg_l["wxid"] in data["wxid_admin"]:
for plugin_name,plugin_class in plugins_sec:
plugin_result=plugin_class.main(msg_l)
if plugin_result is not None:
if isinstance(plugin_result,AnswerBase):
await send_msg_an(plugin_result)
elif isinstance(plugin_result,AnswerBaseList):
for answer_result_base in plugin_result.answers:
await send_msg_an(answer_result_base)
# #other部分
# sql_row(row_qukey)
# if an_ is not None :
# await send_msg_an()
# return
#access接入部分
# sql_row_access(row_access)
# #print(an_)
# if an_ is not None :
# await send_msg_an()
# return
# return
# #answerAPI部分
# answer=API_answer(qu,wxid)
# if 'status' in answer:
# st=answer['status']
# if 'answer' in answer :
# an_=answer['answer']
# if 'options' in answer:
# options=answer['options']
# an_=an_+ ' : ' + str(options)
# if an_ != None :
# send_msg.text_an()
# return
if __name__ == "__main__":
qu=""
#print(sys.argv)
for x in sys.argv[3:]:
qu=f"{qu} {x}"
qu=qu.strip()
#print(sys.argv[1],sys.argv[2],qu)
#print(qu)
try:
print(answer(sys.argv[1],sys.argv[2],qu))
except func_timeout.exceptions.FunctionTimedOut:
printerr("Timeout-主函数执行超时(30s)")
#send_msg.text("timeout-主函数执行超时")