提交 7396ac5b 编写于 作者: L LittleCoder

Add different types of reply

上级 8ded04b5
*.pyc
*.swp
log/*
storage/*
plugin/config/tuling.json
......@@ -63,6 +63,21 @@ if 'msgdealers.vote' in pluginList:
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 plugin_load_succeed()
......@@ -74,7 +89,7 @@ if __name__ == '__main__':
r = r or '\n'.join(tuling.get_response(msg, 'ItChat'))
if not r: r = 'No plugin matched'
print r
send_msg(r)
except:
print 'Exit'
traceback.print_exc()
......
......@@ -9,15 +9,30 @@ try:
except:
TULING = False
def send_msg(client, toUserName, msg):
if len(msg) > 5:
if msg[:5] == '@fil@':
try:
with open(msg[5:]): pass
client.send_file(msg[5:], toUserName)
except:
pass
elif msg[:5] == '@msg@':
client.send_msg(toUserName, msg[:5])
else:
client.send_msg(toUserName, msg)
else:
client.send_msg(toUserName, msg)
def deal_with_msg(msg, s, client):
if msg['MsgType'] == 'Text':
content = msg['Content']
# test vote first
r = vote(client.storageClass, msg['FromUserName'], content)
if r != False: client.send_msg(msg['FromUserName'], r); return
if r: send_msg(client, msg['FromUserName'], r); return
# test user's autoreply
r = autoreply(content)
if r != False: client.send_msg(msg['FromUserName'], r); return
if r: send_msg(client, msg['FromUserName'], r); return
# no plugin matched
client.send_msg(msg['FromUserName'],
'\n'.join(tuling.get_response(msg['Content'], 'ItChat')) if TULING else 'I received: %s'%msg['Content'])
......
......@@ -3,10 +3,13 @@ import re, json, time, os
import traceback
from plugin.Sqlite3Client import Sqlite3Client
SQLITE_DIR = os.path.join('plugin', 'config')
FILE_DIR = os.path.join('storage', 'upload')
def compileRegex(tableName, regexList):
regex = ''
try:
with Sqlite3Client(os.path.join('plugin', 'config', 'autoreply.db')) as s3c:
with Sqlite3Client(os.path.join(SQLITE_DIR, '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]))
......@@ -14,10 +17,25 @@ def compileRegex(tableName, regexList):
raise Exception('Error occured when loading regex table %s: %s is not a correct regex'%(
tableName, regex))
def detectFiles(tableName):
if not os.path.exists(FILE_DIR): os.makedirs(FILE_DIR)
fileName = ''
try:
with Sqlite3Client(os.path.join(SQLITE_DIR, 'autoreply.db')) as s3c:
for qa in s3c.data_source('select * from %s'%tableName):
if qa[:5] == '@fil@':
fileName = qa[5:]
with open(os.join(FILE_DIR, fileName)): pass
except:
traceback.print_exc()
raise Exception('Error occured when loading %s in table %s, it should be in storage/upload'%(
fileName, tableName))
def getreply():
regexAnsList = []
tableNameList = ['default_reply']
for tableName in tableNameList:
detectFiles(tableName)
compileRegex(tableName, regexAnsList)
while 1:
msg = (yield)
......@@ -27,7 +45,10 @@ def getreply():
yield r
getreplyiter = getreply()
getreplyiter.next()
def autoreply(msg):
r = getreplyiter.send(msg)
if r and r[:5] == '@fil@': r = '@fil@%s'%(os.path.join(FILE_DIR, r[5:]))
getreplyiter.next()
return getreplyiter.send(msg)
return r
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册