Skip to content

Commit 0161489

Browse files
committed
Update docs
1 parent 61b19d7 commit 0161489

File tree

9 files changed

+180
-328
lines changed

9 files changed

+180
-328
lines changed

README.md

+11-9
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import itchat
3939

4040
@itchat.msg_register(itchat.content.TEXT)
4141
def text_reply(msg):
42-
return msg['Text']
42+
return msg.text
4343

4444
itchat.auto_login()
4545
itchat.run()
@@ -179,20 +179,22 @@ itchat的附件下载方法存储在msg的Text键中。
179179
下载方法接受一个可用的位置参数(包括文件名),并将文件相应的存储。
180180

181181
```python
182-
@itchat.msg_register(['Picture', 'Recording', 'Attachment', 'Video'])
182+
@itchat.msg_register([PICTURE, RECORDING, ATTACHMENT, VIDEO])
183183
def download_files(msg):
184-
msg['Text'](msg['FileName'])
185-
itchat.send('@%s@%s'%('img' if msg['Type'] == 'Picture' else 'fil', msg['FileName']), msg['FromUserName'])
186-
return '%s received'%msg['Type']
184+
msg.download(msg.fileName)
185+
itchat.send('@%s@%s' % (
186+
'img' if msg['Type'] == 'Picture' else 'fil', msg['FileName']),
187+
msg['FromUserName'])
188+
return '%s received' % msg['Type']
187189
```
188190

189191
如果你不需要下载到本地,仅想要读取二进制串进行进一步处理可以不传入参数,方法将会返回图片的二进制串。
190192

191193
```python
192-
@itchat.msg_register(['Picture', 'Recording', 'Attachment', 'Video'])
194+
@itchat.msg_register([PICTURE, RECORDING, ATTACHMENT, VIDEO])
193195
def download_files(msg):
194-
with open(msg['FileName'], 'wb') as f:
195-
f.write(msg['Text']())
196+
with open(msg.fileName, 'wb') as f:
197+
f.write(msg.download())
196198
```
197199

198200
### 用户多开
@@ -207,7 +209,7 @@ newInstance.auto_login(hotReload=True, statusStorageDir='newInstance.pkl')
207209

208210
@newInstance.msg_register(TEXT)
209211
def reply(msg):
210-
return msg['Text']
212+
return msg.text
211213

212214
newInstance.run()
213215
```

README.rst

+44-17
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ And you only need to write this to reply personal text messages.
3939
4040
@itchat.msg_register(itchat.content.TEXT)
4141
def text_reply(msg):
42-
itchat.send(msg['Text'], msg['FromUserName'])
42+
return msg.text
4343
4444
itchat.auto_login()
4545
itchat.run()
@@ -56,37 +56,62 @@ Here is the `code <https://gist.github.com/littlecodersh/ec8ddab12364323c97d4e36
5656

5757
**Advanced uses**
5858

59+
*Special usage of message dictionary*
60+
61+
You may find out that all the users and messages of itchat are dictionaries by printing them out onto the screen.
62+
63+
But actually they are useful classes itchat created.
64+
65+
They have useful keys and useful interfaces, like:
66+
67+
.. code:: python
68+
69+
@itchat.msg_register(TEXT)
70+
def _(msg):
71+
# equals to print(msg['FromUserName'])
72+
print(msg.fromUserName)
73+
74+
And like:
75+
76+
.. code:: python
77+
78+
author = itchat.search_friends(nickName='LittleCoder')[0]
79+
author.send('greeting, littlecoder!')
80+
5981
*Message register of various types*
6082

6183
The following is a demo of how itchat is configured to fetch and reply daily information.
6284

6385
.. code:: python
6486
65-
#coding=utf8
6687
import itchat, time
6788
from itchat.content import *
6889
6990
@itchat.msg_register([TEXT, MAP, CARD, NOTE, SHARING])
7091
def text_reply(msg):
71-
itchat.send('%s: %s' % (msg['Type'], msg['Text']), msg['FromUserName'])
92+
msg.user.send('%s: %s' % (msg.type, msg.text))
7293
7394
@itchat.msg_register([PICTURE, RECORDING, ATTACHMENT, VIDEO])
7495
def download_files(msg):
75-
msg['Text'](msg['FileName'])
76-
return '@%s@%s' % ({'Picture': 'img', 'Video': 'vid'}.get(msg['Type'], 'fil'), msg['FileName'])
96+
msg.download(msg.fileName)
97+
typeSymbol = {
98+
PICTURE: 'img',
99+
VIDEO: 'vid', }.get(msg.type, 'fil')
100+
return '@%s@%s' % (typeSymbol, msg.fileName)
77101
78102
@itchat.msg_register(FRIENDS)
79103
def add_friend(msg):
80-
itchat.add_friend(**msg['Text']) # 该操作会自动将新好友的消息录入,不需要重载通讯录
81-
itchat.send_msg('Nice to meet you!', msg['RecommendInfo']['UserName'])
104+
msg.user.verify()
105+
msg.user.send('Nice to meet you!')
82106
83107
@itchat.msg_register(TEXT, isGroupChat=True)
84108
def text_reply(msg):
85-
if msg['isAt']:
86-
itchat.send(u'@%s\u2005I received: %s' % (msg['ActualNickName'], msg['Content']), msg['FromUserName'])
109+
if msg.isAt:
110+
msg.user.send(u'@%s\u2005I received: %s' % (
111+
msg.actualNickName, msg.text))
87112
88113
itchat.auto_login(True)
89-
itchat.run()
114+
itchat.run(True)
90115
91116
*Command line QR Code*
92117

@@ -153,20 +178,22 @@ Download function accept one location value (include the file name) and store at
153178

154179
.. code:: python
155180
156-
@itchat.msg_register(['Picture', 'Recording', 'Attachment', 'Video'])
181+
@itchat.msg_register([PICTURE, RECORDING, ATTACHMENT, VIDEO])
157182
def download_files(msg):
158-
msg['Text'](msg['FileName'])
159-
itchat.send('@%s@%s'%('img' if msg['Type'] == 'Picture' else 'fil', msg['FileName']), msg['FromUserName'])
160-
return '%s received'%msg['Type']
183+
msg.download(msg.fileName)
184+
itchat.send('@%s@%s' % (
185+
'img' if msg['Type'] == 'Picture' else 'fil', msg['FileName']),
186+
msg['FromUserName'])
187+
return '%s received' % msg['Type']
161188
162189
If you don't want a local copy of the picture, you may pass nothing to the function to get a binary string.
163190

164191
.. code:: python
165192
166-
@itchat.msg_register(['Picture', 'Recording', 'Attachment', 'Video'])
193+
@itchat.msg_register([PICTURE, RECORDING, ATTACHMENT, VIDEO])
167194
def download_files(msg):
168-
with open(msg['FileName'], 'wb') as f:
169-
f.write(msg['Text']())
195+
with open(msg.fileName, 'wb') as f:
196+
f.write(msg.download())
170197
171198
*Multi instance*
172199

README_EN.md

+44-17
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import itchat
3939

4040
@itchat.msg_register(itchat.content.TEXT)
4141
def text_reply(msg):
42-
return msg['Text']
42+
return msg.text
4343

4444
itchat.auto_login()
4545
itchat.run()
@@ -59,36 +59,61 @@ This QRCode is a wechat account based on the framework of [demo code][robot-sour
5959

6060
## Advanced uses
6161

62+
### Special usage of message dictionary
63+
64+
You may find out that all the users and messages of itchat are dictionaries by printing them out onto the screen.
65+
66+
But actually they are useful classes itchat created.
67+
68+
They have useful keys and useful interfaces, like:
69+
70+
```python
71+
@itchat.msg_register(TEXT)
72+
def _(msg):
73+
# equals to print(msg['FromUserName'])
74+
print(msg.fromUserName)
75+
```
76+
77+
And like:
78+
79+
```python
80+
author = itchat.search_friends(nickName='LittleCoder')[0]
81+
author.send('greeting, littlecoder!')
82+
```
83+
6284
### Message register of various types
6385

6486
The following is a demo of how itchat is configured to fetch and reply daily information.
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
### Command line QR Code
@@ -154,20 +179,22 @@ Name of the file (default name of picture) is in FileName key of msg
154179
Download function accept one location value (include the file name) and store attachment accordingly.
155180

156181
```python
157-
@itchat.msg_register(['Picture', 'Recording', 'Attachment', 'Video'])
182+
@itchat.msg_register([PICTURE, RECORDING, ATTACHMENT, VIDEO])
158183
def download_files(msg):
159-
msg['Text'](msg['FileName'])
160-
itchat.send('@%s@%s'%('img' if msg['Type'] == 'Picture' else 'fil', msg['FileName']), msg['FromUserName'])
161-
return '%s received'%msg['Type']
184+
msg.download(msg.fileName)
185+
itchat.send('@%s@%s' % (
186+
'img' if msg['Type'] == 'Picture' else 'fil', msg['FileName']),
187+
msg['FromUserName'])
188+
return '%s received' % msg['Type']
162189
```
163190

164191
If you don't want a local copy of the picture, you may pass nothing to the function to get a binary string.
165192

166193
```python
167-
@itchat.msg_register(['Picture', 'Recording', 'Attachment', 'Video'])
194+
@itchat.msg_register([PICTURE, RECORDING, ATTACHMENT, VIDEO])
168195
def download_files(msg):
169-
with open(msg['FileName'], 'wb') as f:
170-
f.write(msg['Text']())
196+
with open(msg.fileName, 'wb') as f:
197+
f.write(msg.download())
171198
```
172199

173200
### Multi instance

docs/FAQ.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Q: itchat稳定性如何?
44

5-
A: 测试用机器人能稳定在线多个月。如果你在测试过程中发现无法稳定登陆,请检查**登陆手机**及主机是否稳定连接网络。
5+
A: 测试用机器人能稳定在线多个月。如果你在测试过程中发现无法稳定登陆,请检查**登陆手机**及主机是否稳定连接网络。如果你需要最稳定的消息环境,建议使用[itchatmp][itchatmp]项目,两者使用方法类似。
66

77
## 中文文件名文件上传
88

@@ -30,3 +30,4 @@ A: 有些账号是天生无法给自己的账号发送信息的,建议使用`f
3030

3131
[fields.py-2]: https://gist.github.com/littlecodersh/9a0c5466f442d67d910f877744011705
3232
[fields.py-3]: https://gist.github.com/littlecodersh/e93532d5e7ddf0ec56c336499165c4dc
33+
[itchatmp]: https://github.com/littlecodersh/itchatmp

0 commit comments

Comments
 (0)