Skip to content

Commit

Permalink
Simple group reply finished
Browse files Browse the repository at this point in the history
  • Loading branch information
littlecodersh committed Mar 27, 2016
1 parent 340210e commit 292821c
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 16 deletions.
52 changes: 43 additions & 9 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import time, thread
from client import client

__version__ = '0.1b'

from client import client
__client = client()
def auto_login(): return __client.auto_login()
# The following method are all included in auto_login >>>
Expand Down Expand Up @@ -32,11 +34,43 @@ def send(msg = 'Test Message', toUserName = None):
return __client.send_msg(toUserName, msg)

# decorations
def msg_dealer(fn, *args, **kwargs):
def wrapped(*args, **kwargs):
try:
msg = __client.storageClass.msgList.pop()
except:
msg = None
return send(fn(msg, *args, **kwargs), None if msg is None else msg.get('FromUserName', None))
return wrapped
__functionDict = {}
def configured_reply():
try:
msg = __client.storageClass.msgList.pop()
if msg.get('GroupUserName') is None:
__functionDict[msg['Type']](msg)
else:
__functionDict['GroupChat'][msg['Type']](msg)
except:
pass

def msg_dealer(_type = None, *args, **kwargs):
if hasattr(_type, '__call__'):
fn = _type
def _msg_dealer(*args, **kwargs):
try:
msg = __client.storageClass.msgList.pop()
send(fn(msg, *args, **kwargs), None if msg is None else msg.get('FromUserName', None))
except:
pass
print('Reply function has been set, call %s to reply one message'%fn.__name__)
return _msg_dealer
elif _type is None:
return configured_reply
else:
def _msg_dealer(fn, *args, **kwargs):
if kwargs.get('isGroupChat', False):
__functionDict['GroupChat'][_type] = fn
else:
__functionDict[_type] = fn
return fn
return _msg_dealer

# in-build run
def run():
print('Start auto replying')
while 1:
configured_reply()
time.sleep(.3)

25 changes: 25 additions & 0 deletions client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import json, xml.dom.minidom, mimetypes
import config, storage, out, tools

import traceback

BASE_URL = config.BASE_URL

class client:
Expand Down Expand Up @@ -199,6 +201,7 @@ def __produce_msg(self, l):
rl = []
srl = [51, 53] # 51 messy code, 53 webwxvoipnotifymsg
for m in l:
if '@@' in m['FromUserName']: m = self.__produce_group_chat(m)
if m['MsgType'] == 1: # words
if m['Url']:
regx = r'(.+?\(.+?\))'
Expand Down Expand Up @@ -314,6 +317,28 @@ def download_video(videoDir):
m = dict(m, **msg)
rl.append(m)
return rl
def __produce_group_chat(self, msg):
def get_msg_from_raw(content):
regex = re.compile('(@[0-9a-z]*?):<br/>(.*)$')
r = re.findall(regex, content)
if r:
return r[0][0], r[0][1]
else:
return '', content
ActualUserName, Content = get_msg_from_raw(msg['Content'])
isAt = self.storageClass.nickName in Content
if '\342\200\205'.decode('utf8') in Content: Content = Content.split('\342\200\205'.decode('utf8'))[1]
try:
self.storageClass.groupDict[msg['FromUserName']][ActualUserName]
except:
groupMemberList = self.get_batch_contract(msg['FromUserName'])
self.storageClass.groupDict[msg['FromUserName']] = {member['UserName']: member for member in groupMemberList}
ActualNickName = self.storageClass.groupDict[msg['FromUserName']][ActualUserName]['NickName']
additionalItems = {
'ActualUserName': ActualUserName,
'ActualNickName': ActualNickName,
'Content': Content, }
return dict(msg, **additionalItems)
def send_msg(self, toUserName = None, msg = 'Test Message'):
url = '%s/webwxsendmsg'%self.loginInfo['url']
payloads = {
Expand Down
8 changes: 1 addition & 7 deletions storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,8 @@ def __init__(self):
self.nickName = None
self.memberList = []
self.msgList = []
self.groupDict = {}
self.lastInputUserName = None
def find_msg_list(self, userName, count):
r = []
for msg in self.historyMsg:
if msg['UserName'] == userName:
r.append(msg)
if len(r) >= count: break
return r
def find_username(self, n):
r = []
for member in self.memberList:
Expand Down

0 comments on commit 292821c

Please sign in to comment.