Skip to content

Commit f0e3db0

Browse files
committed
Merge 1.3.0 into master and update to 1.3.1
2 parents 8c205a0 + 7781490 commit f0e3db0

13 files changed

+923
-508
lines changed

README.md

+37-9
Original file line numberDiff line numberDiff line change
@@ -59,36 +59,61 @@ itchat.run()
5959

6060
## 进阶应用
6161

62+
### 特殊的字典使用方式
63+
64+
通过打印itchat的用户以及注册消息的参数,可以发现这些值都是字典。
65+
66+
但实际上itchat精心构造了相应的消息、用户、群聊、公众号类。
67+
68+
其所有的键值都可以通过这一方式访问:
69+
70+
```python
71+
@itchat.msg_register(TEXT)
72+
def _(msg):
73+
# equals to print(msg['FromUserName'])
74+
print(msg.fromUserName)
75+
```
76+
77+
属性名为键值首字母小写后的内容。
78+
79+
```python
80+
author = itchat.search_friends(nickName='LittleCoder')[0]
81+
author.send('greeting, littlecoder!')
82+
```
83+
6284
### 各类型消息的注册
6385

6486
通过如下代码,微信已经可以就日常的各种信息进行获取与回复。
6587

6688
```python
67-
#coding=utf8
6889
import itchat, time
6990
from itchat.content import *
7091

7192
@itchat.msg_register([TEXT, MAP, CARD, NOTE, SHARING])
7293
def text_reply(msg):
73-
itchat.send('%s: %s' % (msg['Type'], msg['Text']), msg['FromUserName'])
94+
msg.user.send('%s: %s' % (msg.type, msg.text))
7495

7596
@itchat.msg_register([PICTURE, RECORDING, ATTACHMENT, VIDEO])
7697
def download_files(msg):
77-
msg['Text'](msg['FileName'])
78-
return '@%s@%s' % ({'Picture': 'img', 'Video': 'vid'}.get(msg['Type'], 'fil'), msg['FileName'])
98+
msg.download(msg.fileName)
99+
typeSymbol = {
100+
PICTURE: 'img',
101+
VIDEO: 'vid', }.get(msg.type, 'fil')
102+
return '@%s@%s' % (typeSymbol, msg.fileName)
79103

80104
@itchat.msg_register(FRIENDS)
81105
def add_friend(msg):
82-
itchat.add_friend(**msg['Text']) # 该操作会自动将新好友的消息录入,不需要重载通讯录
83-
itchat.send_msg('Nice to meet you!', msg['RecommendInfo']['UserName'])
106+
msg.user.verify()
107+
msg.user.send('Nice to meet you!')
84108

85109
@itchat.msg_register(TEXT, isGroupChat=True)
86110
def text_reply(msg):
87-
if msg['isAt']:
88-
itchat.send(u'@%s\u2005I received: %s' % (msg['ActualNickName'], msg['Content']), msg['FromUserName'])
111+
if msg.isAt:
112+
msg.user.send(u'@%s\u2005I received: %s' % (
113+
msg.actualNickName, msg.text))
89114

90115
itchat.auto_login(True)
91-
itchat.run()
116+
itchat.run(True)
92117
```
93118

94119
### 命令行二维码
@@ -234,6 +259,8 @@ A: 有些账号是天生无法给自己的账号发送信息的,建议使用`f
234259

235260
## 类似项目
236261

262+
[youfou/wxpy][youfou-wxpy]: 优秀的api包装和配套插件,微信机器人/优雅的微信个人号API
263+
237264
[liuwons/wxBot][liuwons-wxBot]: 类似的基于Python的微信机器人
238265

239266
[zixia/wechaty][zixia-wechaty]: 基于Javascript(ES6)的微信个人账号机器人NodeJS框架/库
@@ -267,6 +294,7 @@ A: 有些账号是天生无法给自己的账号发送信息的,建议使用`f
267294
[littlecodersh]: https://github.com/littlecodersh
268295
[tempdban]: https://github.com/tempdban
269296
[Chyroc]: https://github.com/Chyroc
297+
[youfou-wxpy]: https://github.com/youfou/wxpy
270298
[liuwons-wxBot]: https://github.com/liuwons/wxBot
271299
[zixia-wechaty]: https://github.com/zixia/wechaty
272300
[Mojo-Weixin]: https://github.com/sjdy521/Mojo-Weixin

itchat/components/contact.py

+13-11
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from .. import config, utils
88
from ..returnvalues import ReturnValue
9-
from ..storage import contact_change
9+
from ..storage import contact_change, templates
1010

1111
logger = logging.getLogger('itchat')
1212

@@ -99,8 +99,8 @@ def update_friend(self, userName):
9999
return r if len(r) != 1 else r[0]
100100

101101
def update_info_dict(oldInfoDict, newInfoDict):
102-
'''
103-
only normal values will be updated here
102+
''' only normal values will be updated here
103+
because newInfoDict is normal dict, so it's not necessary to consider templates
104104
'''
105105
for k, v in newInfoDict.items():
106106
if any((isinstance(v, t) for t in (tuple, list, dict))):
@@ -130,8 +130,8 @@ def update_local_chatrooms(core, l):
130130
if oldChatroom:
131131
update_info_dict(oldChatroom, chatroom)
132132
# - update other values
133-
memberList, oldMemberList = (c.get('MemberList', [])
134-
for c in (chatroom, oldChatroom))
133+
memberList = chatroom.get('MemberList', [])
134+
oldMemberList = oldChatroom.memberList
135135
if memberList:
136136
for member in memberList:
137137
oldMember = utils.search_dict_list(
@@ -141,17 +141,19 @@ def update_local_chatrooms(core, l):
141141
else:
142142
oldMemberList.append(member)
143143
else:
144-
oldChatroom = chatroom
145-
core.chatroomList.append(chatroom)
144+
oldChatroom = templates.wrap_user_dict(chatroom)
145+
core.chatroomList.append(oldChatroom)
146146
# delete useless members
147147
if len(chatroom['MemberList']) != len(oldChatroom['MemberList']) and \
148148
chatroom['MemberList']:
149149
existsUserNames = [member['UserName'] for member in chatroom['MemberList']]
150150
delList = []
151151
for i, member in enumerate(oldChatroom['MemberList']):
152-
if member['UserName'] not in existsUserNames: delList.append(i)
152+
if member['UserName'] not in existsUserNames:
153+
delList.append(i)
153154
delList.sort(reverse=True)
154-
for i in delList: del oldChatroom['MemberList'][i]
155+
for i in delList:
156+
del oldChatroom['MemberList'][i]
155157
# - update OwnerUin
156158
if oldChatroom.get('ChatRoomOwner') and oldChatroom.get('MemberList'):
157159
oldChatroom['OwnerUin'] = utils.search_dict_list(oldChatroom['MemberList'],
@@ -184,8 +186,8 @@ def update_local_friends(core, l):
184186
utils.emoji_formatter(friend, 'NickName')
185187
if 'DisplayName' in friend:
186188
utils.emoji_formatter(friend, 'DisplayName')
187-
if 'RemarkName' in member:
188-
utils.emoji_formatter(member, 'RemarkName')
189+
if 'RemarkName' in friend:
190+
utils.emoji_formatter(friend, 'RemarkName')
189191
oldInfoDict = utils.search_dict_list(
190192
fullList, 'UserName', friend['UserName'])
191193
if oldInfoDict is None:

itchat/components/hotreload.py

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from ..config import VERSION
77
from ..returnvalues import ReturnValue
8+
from ..storage import templates
89
from .contact import update_local_chatrooms, update_local_friends
910
from .messages import produce_msg
1011

@@ -50,6 +51,8 @@ def load_login_status(self, fileDir,
5051
'ErrMsg': 'cached status ignored because of version',
5152
'Ret': -1005, }})
5253
self.loginInfo = j['loginInfo']
54+
self.loginInfo['User'] = templates.User(self.loginInfo['User'])
55+
self.loginInfo['User'].core = self
5356
self.s.cookies = requests.utils.cookiejar_from_dict(j['cookies'])
5457
self.storageClass.loads(j['storage'])
5558
msgList, contactList = self.get_msg()

itchat/components/login.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
from .. import config, utils
1111
from ..returnvalues import ReturnValue
12+
from ..storage.templates import wrap_user_dict
1213
from .contact import update_local_chatrooms, update_local_friends
1314
from .messages import produce_msg
1415

@@ -182,7 +183,7 @@ def web_init(self):
182183
# deal with login info
183184
utils.emoji_formatter(dic['User'], 'NickName')
184185
self.loginInfo['InviteStartCount'] = int(dic['InviteStartCount'])
185-
self.loginInfo['User'] = utils.struct_friend_info(dic['User'])
186+
self.loginInfo['User'] = wrap_user_dict(utils.struct_friend_info(dic['User']))
186187
self.memberList.append(self.loginInfo['User'])
187188
self.loginInfo['SyncKey'] = dic['SyncKey']
188189
self.loginInfo['synckey'] = '|'.join(['%s_%s' % (item['Key'], item['Val'])
@@ -247,6 +248,7 @@ def maintain_loop():
247248
else:
248249
otherList.append(contact)
249250
chatroomMsg = update_local_chatrooms(self, chatroomList)
251+
chatroomMsg['User'] = self.loginInfo['User']
250252
self.msgList.put(chatroomMsg)
251253
update_local_friends(self, otherList)
252254
retryCount = 0

0 commit comments

Comments
 (0)