提交 8ded04b5 编写于 作者: L LittleCoder

Add PluginTest

上级 f7069467
#coding=utf8
import time
import time, sys
import itchat.storage, itchat.out, itchat.argparser, itchat.robot
from itchat.client import WeChatClient
from plugin.ChatLikeCMD import ChatLikeCMD
......@@ -14,6 +14,9 @@ def demo_robot(s, msgList, client): # ONLY FOR DEMO
if s.find_nickname(msg['FromUserName']): itchat.robot.deal_with_msg(msg, s, client)
time.sleep(.1)
if __name__ == '__main__':
from PluginTest import plugin_load_succeed
if not plugin_load_succeed(): print 'Try to fix the plugins and test them with PluginTest.py';sys.exit()
client_s = itchat.storage.Storage()
if ROBOT:
client = WeChatClient(client_s, robot = True)
......
#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, 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:
try:
pluginList.remove('tuling')
import plugin.tuling as tuling
try:
tuling.get_response('Hi', 'LittleCoder')
pluginList.append('tuling')
sys_print('SUCC', 'Tuling')
except:
sys_print('WARN', '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:
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 'msgdealers.autoreply' in pluginList:
try:
pluginList.remove('msgdealers.autoreply')
from plugin.msgdealers.autoreply import autoreply
pluginList.append('msgdealers.autoreply')
sys_print('SUCC', 'msgdealers.autoreply')
except Exception, e:
sys_print('WARN', str(e))
# Test msgdealers.vote
if 'msgdealers.vote' in pluginList:
try:
from plugin.msgdealers.vote import 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()
if __name__ == '__main__':
try:
print plugin_load_succeed()
while True:
msg = raw_input('>').decode(sys.stdin.encoding)
if 'msgdealers.autoreply' in pluginList:
r = autoreply(msg)
if 'tuling' in pluginList:
r = r or '\n'.join(tuling.get_response(msg, 'ItChat'))
if not r: r = 'No plugin matched'
print r
except:
print 'Exit'
traceback.print_exc()
else:
sys_print('INFO', 'Plugin loading finished')
......@@ -4,18 +4,21 @@ import traceback
from plugin.Sqlite3Client import Sqlite3Client
def compileRegex(tableName, regexList):
regex = ''
try:
with Sqlite3Client(os.path.join('plugin', 'config', 'autoreply.db')) as s3c:
for qa in s3c.data_source('select * from %s'%tableName):
regex = qa[0]
regexList.append((re.compile(qa[0]), qa[1]))
except:
print 'Error occured when loading regex table %s'%tableName
traceback.print_exc()
raise Exception('Error occured when loading regex table %s: %s is not a correct regex'%(
tableName, regex))
def getreply():
regexAnsList = []
tableNameList = ['default_reply']
for tableName in tableNameList: compileRegex(tableName, regexAnsList)
for tableName in tableNameList:
compileRegex(tableName, regexAnsList)
while 1:
msg = (yield)
r = False
......
......@@ -6,7 +6,7 @@ from ChatLikeCMD import ChatLikeCMD
try:
with open(os.path.join('plugin', 'config', 'tuling.json')) as f: key = json.loads(f.read())['key']
except:
raise Exception('You need to have a correct tuling.json file')
raise Exception('There is something wrong with the format of you plugin/config/tuling.json')
def get_response(msg, userid):
url = 'http://www.tuling123.com/openapi/api'
......
[
"QRCode",
"tuling",
"msgdealers.autoreply",
"msgdealers.vote"
]
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册