forked from JHue58/ChatLearning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChatReply.py
461 lines (426 loc) · 15 KB
/
ChatReply.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
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
# Demo阶段
import copy
import json
import pickle
import random
import time
import traceback
import requests
import ChatAllfind
import ChatFilter
import simuse
nowtime = time.time()
def DelType(tempdict, answerlist):
freqdict = getconfig(10)
num = 0
deltype = ''
new_answerlist = copy.deepcopy(answerlist)
for answerdict in answerlist:
answertext = eval(answerdict['answertext'])
for i in answertext:
try:
i.pop('url')
except:
pass
try:
questiondict = tempdict[str(answertext)]
except:
try:
new_answerlist.remove(answerdict)
except:
pass
num += 1
deltype = deltype + i['type'] + ' '
try:
if questiondict['freq'] < freqdict[i['type']]:
new_answerlist.remove(answerdict)
num += 1
deltype = deltype + i['type'] + ' '
continue
except:
continue
if num != 0:
print('已过滤{}个不符合发送要求的{}'.format(num, ','.join(set(deltype.split()))))
return new_answerlist
def Judge_Fast_Delete(data, TempMessage, group, messagechain, sender):
try:
First_index = messagechain[0]
except:
return 1
IS_ME = 0
if First_index['type'] == 'Quote':
SourceId = First_index['id']
for i in messagechain:
if i['type'] == 'At':
if str(i['target']) == data['qq']:
IS_ME = 1
if i['type'] == 'Plain' and IS_ME == 1:
if i['text'].lower() == ' !delete' or i['text'].lower(
) == ' !delete':
if getconfig(13) == 0:
if not (sender in getconfig(14)):
return 1
Delete_Sign = Fast_Delete(TempMessage, group, SourceId)
break
else:
return 0
if Delete_Sign == 1:
global RecallList
simuse.Recall_Message(data, SourceId)
time.sleep(1)
RecallList.append(simuse.Send_Message(data, group, 1, '删除成功!', 1))
elif Delete_Sign == 0:
RecallList.append(
simuse.Send_Message(data, group, 1, '删除失败,该消息已不在缓存内', 1))
elif Delete_Sign == -1:
RecallList.append(
simuse.Send_Message(data, group, 1, '删除失败,词库中已无法找到该答案', 1))
return 1
else:
return 0
def Fast_Delete(TempMessage, group, SourceId):
try:
IDdict = TempMessage[group]
IDlist = IDdict[SourceId]
except:
return 0
filelist = ChatAllfind.getcllist()
IS_FIND = 0
for filename in filelist:
THIS_IS_FIND = 0
cldict = pickle.load(open('WordStock/' + filename, 'rb'))
try:
questiondict = cldict[IDlist[0]]
except:
continue
answerlist = questiondict['answer']
for answerdict in answerlist:
if answerdict['answertext'] == IDlist[1]:
answerlist.remove(answerdict)
IS_FIND = 1
THIS_IS_FIND = 1
if answerlist == []:
cldict.pop(IDlist[0])
break
if THIS_IS_FIND == 1:
pickle.dump(cldict, open('WordStock/' + filename, 'wb'))
if IS_FIND == 1:
IDdict.pop(SourceId)
return 1
else:
return -1
def talkvoice(data, group, messagechain):
global nowtime
try:
atmessage = messagechain[0]
except:
print('转换语音失败')
return None
if atmessage['type'] == 'At' and str(atmessage['target']) == data['qq']:
try:
textmessage = messagechain[1]
text = textmessage['text']
try:
text = text.strip(' ')
except:
pass
if text[:3] == '快说 ':
text = text[3:]
if len(text) > 50:
print('超过长度限制')
simuse.Send_Message(data, group, 1, '超过长度限制', 1)
return None
if canToVoice(text):
try:
if CanSendTask(nowtime, 10):
answer_message = Plain_Voice(data, text)
if answer_message == None:
print('转换语音失败')
return None
simuse.Send_Message_Chain(data, group, 1,
answer_message)
nowtime = time.time()
return 1
else:
print('转换冷却中')
simuse.Send_Message(data, group, 1,
'能不能让我休息下喝口水再说啊?', 1)
return 1
except:
print('转换语音失败')
return None
else:
print('存在违规字符,转换失败')
simuse.Send_Message(data, group, 1, '存在违规字符,转换失败', 1)
return None
except:
return None
def canToVoice(text):
usualchar = [',', ',', '.', '。', '!', '!', '?', '?', ' ']
chinesehave = 0
anotherchar = 0
for i in text:
if '\u4e00' <= i <= '\u9fa5':
chinesehave = 1
else:
if not (i in usualchar):
anotherchar = 1
if chinesehave == 1 and anotherchar == 0:
cansign = 1
else:
cansign = 0
return cansign
def CanSendTask(nowtime, cd):
if time.time() - nowtime >= cd:
return 1
else:
return 0
def Plain_Voice(data, text):
url = 'http://124.222.165.166:19630/ToVoice'
data_in = {'text': text, 'QQ': data['qq'], 'synthesizer': getconfig(7)}
try:
res = requests.request('post', url, json=data_in)
res = json.loads(res.text)
except:
return None
if res['code'] == 0:
return None
base64_data = res['base64']
messagechain = [{'type': 'Voice', 'base64': base64_data}]
return messagechain
def runchance(replychance):
ranom_num = random.uniform(0, 1)
replychance = replychance * 0.01
if ranom_num <= replychance:
chance = 1
else:
chance = 0
return chance
def getconfig(choice):
file = open('config.clc', 'r', encoding='utf-8-sig')
config = file.read()
file.close()
config = eval(config)
if choice == 1:
grouplist = config['replygrouplist']
grouptuple = tuple(grouplist)
return grouptuple
elif choice == 2:
sendmode = config['sendmode']
return sendmode
elif choice == 3:
reply = config['reply']
return reply
elif choice == 4:
replychance = config['replychance']
return replychance
elif choice == 5:
voicereply = config['voicereply']
return voicereply
elif choice == 6:
voicereplychance = config['voicereplychance']
return voicereplychance
elif choice == 7:
synthesizer_name = config['synthesizer']
return synthesizer_name
elif choice == 8:
Tagdict = config['tag']
return Tagdict
elif choice == 9:
replydict = config['singlereplychance']
return replydict
elif choice == 10:
imagefreq = config['typefreq']
return imagefreq
elif choice == 11:
voicereplydict = config['singlevoicereplychance']
return voicereplydict
elif choice == 12:
TempMessageNum = config['tempmessagenum']
return TempMessageNum
elif choice == 13:
FastDelete = config['fastdelete']
return FastDelete
elif choice == 14:
Adminlist = config['Administrator']
return Adminlist
def getanswer(group, question): # 从词库中获取答案
for i in question: # 去除作为问题中的变动因素“url”
try:
i.pop('url')
except:
continue
if getconfig(2) == 1:
Tagdict = getconfig(8)
if str(group) in Tagdict.keys():
group = Tagdict[str(group)]
else:
group = 'Merge'
question = str(question)
if type(group) == type([]):
answerlist = []
for i in group:
filename = str(i) + '.cl' # 读取已缓存的词库
try:
tempdict = pickle.load(open('WordStock/' + filename, 'rb'))
except:
return None
try: # 检索问题,若词库中无该问题,则函数返回-1,若有,则返回所有答案(答案列表)
#print(question)
questiondict = tempdict[question]
answerlist.extend(
copy.deepcopy(DelType(tempdict, questiondict['answer'])))
except:
continue
else:
filename = str(group) + '.cl' # 读取已缓存的词库
try:
tempdict = pickle.load(open('WordStock/' + filename, 'rb'))
except:
return None
try: # 检索问题,若词库中无该问题,则函数返回-1,若有,则返回所有答案(答案列表)
#print(question)
questiondict = tempdict[question]
answerlist = DelType(tempdict, questiondict['answer'])
except:
return -1
#print(answerlist)
if answerlist == []:
return -1
return answerlist
def replyanswer(data, group, answer): # 发送答案
global nowtime
replydict = getconfig(9)
if str(group) in replydict.keys():
if runchance(replydict[str(group)]) == 0:
print('已获取答案,但不发送')
return None
elif runchance(getconfig(4)) == 0:
print('已获取答案,但不发送')
return None
try:
answer = random.choice(answer) # 尝试从答案列表中随机抽取一个答案,若答案列表为空,则不回复
answer = answer['answertext']
except:
print('无答案,不给予回复')
#print('->',end='')
return None
try:
answer = eval(answer)
except:
pass
origin_answer = copy.deepcopy(answer)
for i in answer: # 去除答案中的imageId,不去除mirai api http会无法回复
#print(i)
try:
i.pop('imageId')
except:
continue
#print(answer)
#Voice Test
voicereplysign = 0
chance_True = 0
if ChatFilter.filtercheck(copy.deepcopy(answer)) == 0:
return None
print(answer, end='')
if getconfig(5) == 1:
voicereplydict = getconfig(11)
if str(group) in voicereplydict.keys():
if runchance(voicereplydict[str(group)]) == 1:
chance_True = 1
elif runchance(getconfig(6)) == 1:
chance_True = 1
if chance_True == 1:
for i in answer:
if i['type'] == 'Plain':
if canToVoice(i['text']):
if len(i['text']) > 55:
break
try:
if CanSendTask(nowtime, 10):
answer_new_message = Plain_Voice(data, i['text'])
if answer_new_message == None:
print('转换语音失败')
break
answer = answer_new_message
voicereplysign = 1
nowtime = time.time()
else:
break
except:
print('转换语音失败')
break
number = simuse.Send_Message_Chain(data, group, 1,
answer) # 让bot发送随机抽取中的答案
if number != None:
if voicereplysign == 0:
print('答案已发送', number)
return origin_answer, number
else:
print('答案已发送(语音)', number)
return origin_answer, number
#print('->',end='')
else:
print('答案发送失败')
def listening(data):
global RecallList
RecallList = []
AfterSecond = 0
TempMessage = {}
while 1:
if RecallList != []:
AfterSecond += 1
if RecallList != [] and AfterSecond >= 10:
for messageid in RecallList:
simuse.Recall_Message(data, messageid)
AfterSecond = 0
RecallList = []
if getconfig(3) == 0:
return None
message = simuse.Fetch_Message(data) # 监听消息链
if type(message) == type(0):
time.sleep(0.5)
continue
for i in message:
if i['type'] == 'GroupMessage': # 判断监听到的消息是否为群消息
group = i['group'] # 记录群号
try:
getconfig(1).index(group)
except:
continue
messagechain = i['messagechain']
messagechain.pop(0)
if talkvoice(data, group, messagechain) == 1:
continue
if Judge_Fast_Delete(data, TempMessage, group, messagechain,
i['sender']) == 1:
continue
question = messagechain
answer = getanswer(group, question) # 获取答案
if answer != -1 and answer != None:
reply_answer_info = replyanswer(data, group,
answer) # 让bot回复
if reply_answer_info != None:
reply_answer = reply_answer_info[0]
SourceId = reply_answer_info[1]
if reply_answer != None:
try:
IDdict = TempMessage[group]
# TempMessage:{group:{id:[question,answer],id:[question,answer]}}
except:
IDlist = [str(question), str(reply_answer)]
IDdict = {}
IDdict[SourceId] = IDlist
TempMessage[group] = IDdict
else:
IDlist = [str(question), str(reply_answer)]
IDdict[SourceId] = IDlist
if len(IDdict) > getconfig(12):
IDdict.pop(list(IDdict.keys())[0])
time.sleep(0.5)
def main():
data = simuse.Get_data()
data = simuse.Get_Session(data)
listening(data)
return None
#main()