-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunc_echo.py
102 lines (95 loc) · 3.88 KB
/
func_echo.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
import re
import json
from utils import sendMessage, listFunctions, load, save
from interceptor import interceptor
from config import echoFuns
from func import func
class func_echo(func):
def __init__(self):
self.conf_path='./echo'
self.filters = {}
conf = load(self.conf_path)
if conf:
line = json.loads(conf[0])
if line:
self.filters[list(line.keys())[0]] = list(line.values())[0]
'''
massgae handler:
func:
keywords filer:
{keywords: dealing function}
'''
@interceptor.MessageInDealing
def func_handler(self, chat_id, message_type, message_text, message_id, user_id):
self.message_id = message_id
if message_type == 'messageText':
if re.search('^/echo.*', message_text):
for key, func in echoFuns.items():
if re.search('/echo.*'+key+'.*', message_text):
if key == 'list':
eval(func[0])(chat_id=chat_id, message_id=message_id, funs=eval('self.'+func[1]), text='关键词:\n')
elif key == 'update':
eval('self.'+func[0])(chat_id=chat_id, text=eval(func[1]))
return 0
listFunctions(chat_id, echoFuns, '', message_id)
for key, text in self.filters.items():
if re.search('^[^/]?.*'+key+'.*', message_text):
sendMessage(chat_id, text, self.message_id)
return 0
'''
Argu:
option: add # add keyword or filter if index not present
delete, # delete keyword
update # update replay text drop # delete filter
'''
def updateFilter(self, chat_id, option, index, key='', text=''):
#index, key, text
index = int(index)
if option == '添加':
# 添加新的关键字
if index < len(self.filters) and index >= 0:
index_key = list(self.filters.keys())[index]
self.filters[index_key + '|' + key] = self.filters.pop(index_key)
# 添加新的规则
else:
self.filters[key] = text
#key
if option == '删除':
if key in self.filters.keys():
del self.filters[key]
#key, text
if option == '更新':
self.filters[key] = text
#index, key, text
if option == '去关键词':
index_key = list(self.filters.keys())[index]
after_key = re.sub(text, '', index_key)
after_key = re.sub('\|\|', '', after_key)
self.filters[after_key] = self.filters.pop(index_key)
listFunctions(chat_id, self.filters, '更新后:\n', self.message_id)
save(self.conf_path, [json.dumps(self.filters, ensure_ascii=False)])
#text -> 指令,option,index,key,text
def arguExplain(self, chat_id, text):
text = re.sub('|', '|', text)
# 去除空格
text = re.sub(' ', '', text)
args = re.split(',|,', text)
try:
opt = args[2]
# 假定 args[2] 是 int,不是捕捉报错再按字符串对待
try:
index = int(args[3])
if index >= len(self.filters.keys()):
key = args[4]
else:
key = list(self.filters.keys())[index]
except ValueError:
key = args[3]
if key in self.filters.keys():
index = list(self.filters.keys()).index(key)
else:
index = len(self.filters)
text = args[-1]
self.updateFilter(chat_id, opt, index=index, key=key, text=text)
except IndexError:
sendMessage(chat_id, '请求格式:添加|删除|更新|去关键词,index,key,text', self.message_id)