forked from littlecodersh/ItChat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PluginTest.py
103 lines (91 loc) · 3.43 KB
/
PluginTest.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
#coding=utf8
import json
import sys, traceback, os
succeed = True
def plugin_load_succeed():
return succeed
def sys_print(level, msg):
if level == 'SUCC': msg += ' plugin loaded'
if level == 'WARN': global succeed; succeed = False
print '[%s] %s'%(level[:4], msg)
# load plugin list
try:
pluginListDir = 'pluginlist.json'
with open(pluginListDir) as f: pluginList = json.loads(f.read())
except:
pluginList = {}
sys_print('WARN', 'There is something wrong with the format of your pluginlist.json')
# Test tuling.py
if 'tuling' in pluginList['systemmodules']:
try:
pluginList['systemmodules'].remove('tuling')
import plugin.tuling as tuling
try:
tuling.get_response('Hi', 'LittleCoder')
pluginList['systemmodules'].append('tuling')
sys_print('SUCC', 'Tuling')
except:
sys_print('INFO', 'Your key for tuling robot can\'t be used now, change one in plugin/config/tuling.json')
sys_print('~~~~', 'You can get it from http://www.tuling123.com/')
except:
sys_print('WARN', 'There is something wrong with the format of your plugin/config/tuling.json')
# Test QRCode.py
if 'QRCode' in pluginList['systemmodules']:
try:
import plugin.QRCode as QRCode
sys_print('SUCC', 'Command line QRCode')
except:
sys_print('INFO', 'Command line QRCode loaded failed, if you want to use this, you need to run `pip install Image`')
# Test msgdealers.autoreply
if 'autoreply' in pluginList['msgdealers']:
try:
pluginList['msgdealers'].remove('autoreply')
from plugin.msgdealers.autoreply import autoreply
pluginList['msgdealers'].append('autoreply')
sys_print('SUCC', 'msgdealers.autoreply')
except Exception, e:
sys_print('WARN', e.message)
# Test msgdealers.vote
if 'vote' in pluginList['msgdealers']:
try:
pluginList['msgdealers'].remove('vote')
from plugin.msgdealers.vote import vote
pluginList['msgdealers'].append('vote')
sys_print('SUCC', 'msgdealers.vote')
sys_print('INFO', 'But whether it can be properly used need to be tested online')
except:
sys_print('WARN', 'Vote plugin loaded failed, this is strange, you need to contact me')
traceback.print_exc()
def send_msg(msg):
if len(msg) > 5:
if msg[:5] == '@fil@':
try:
with open(msg[5:]): pass
print msg[5:]
except:
pass
elif msg[:5] == '@msg@':
print msg[5:]
else:
print msg
else:
print msg[5:]
if __name__ == '__main__':
try:
print 'Loading %s'%('successfully' if plugin_load_succeed() else 'failed')
pluginOrder = [('autoreply', autoreply), ('tuling', tuling.get_response)]
while True:
msg = raw_input('>').decode(sys.stdin.encoding)
if not msg: continue
res = ''
for plugin in pluginOrder:
if plugin[0] in (pluginList['msgdealers'] + pluginList['systemmodules']):
r = plugin[1](content, None, None)
if r: res = r;break
if not res: r = 'No plugin matched'
send_msg(r)
except:
print 'Exit'
traceback.print_exc()
else:
sys_print('INFO', 'Plugin loading finished')