Skip to content

Commit

Permalink
Update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
littlecodersh committed Mar 30, 2017
1 parent 61b19d7 commit 0161489
Show file tree
Hide file tree
Showing 9 changed files with 180 additions and 328 deletions.
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import itchat

@itchat.msg_register(itchat.content.TEXT)
def text_reply(msg):
return msg['Text']
return msg.text

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

```python
@itchat.msg_register(['Picture', 'Recording', 'Attachment', 'Video'])
@itchat.msg_register([PICTURE, RECORDING, ATTACHMENT, VIDEO])
def download_files(msg):
msg['Text'](msg['FileName'])
itchat.send('@%s@%s'%('img' if msg['Type'] == 'Picture' else 'fil', msg['FileName']), msg['FromUserName'])
return '%s received'%msg['Type']
msg.download(msg.fileName)
itchat.send('@%s@%s' % (
'img' if msg['Type'] == 'Picture' else 'fil', msg['FileName']),
msg['FromUserName'])
return '%s received' % msg['Type']
```

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

```python
@itchat.msg_register(['Picture', 'Recording', 'Attachment', 'Video'])
@itchat.msg_register([PICTURE, RECORDING, ATTACHMENT, VIDEO])
def download_files(msg):
with open(msg['FileName'], 'wb') as f:
f.write(msg['Text']())
with open(msg.fileName, 'wb') as f:
f.write(msg.download())
```

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

@newInstance.msg_register(TEXT)
def reply(msg):
return msg['Text']
return msg.text

newInstance.run()
```
Expand Down
61 changes: 44 additions & 17 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ And you only need to write this to reply personal text messages.
@itchat.msg_register(itchat.content.TEXT)
def text_reply(msg):
itchat.send(msg['Text'], msg['FromUserName'])
return msg.text
itchat.auto_login()
itchat.run()
Expand All @@ -56,37 +56,62 @@ Here is the `code <https://gist.github.com/littlecodersh/ec8ddab12364323c97d4e36

**Advanced uses**

*Special usage of message dictionary*

You may find out that all the users and messages of itchat are dictionaries by printing them out onto the screen.

But actually they are useful classes itchat created.

They have useful keys and useful interfaces, like:

.. code:: python
@itchat.msg_register(TEXT)
def _(msg):
# equals to print(msg['FromUserName'])
print(msg.fromUserName)
And like:

.. code:: python
author = itchat.search_friends(nickName='LittleCoder')[0]
author.send('greeting, littlecoder!')
*Message register of various types*

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

.. code:: python
#coding=utf8
import itchat, time
from itchat.content import *
@itchat.msg_register([TEXT, MAP, CARD, NOTE, SHARING])
def text_reply(msg):
itchat.send('%s: %s' % (msg['Type'], msg['Text']), msg['FromUserName'])
msg.user.send('%s: %s' % (msg.type, msg.text))
@itchat.msg_register([PICTURE, RECORDING, ATTACHMENT, VIDEO])
def download_files(msg):
msg['Text'](msg['FileName'])
return '@%s@%s' % ({'Picture': 'img', 'Video': 'vid'}.get(msg['Type'], 'fil'), msg['FileName'])
msg.download(msg.fileName)
typeSymbol = {
PICTURE: 'img',
VIDEO: 'vid', }.get(msg.type, 'fil')
return '@%s@%s' % (typeSymbol, msg.fileName)
@itchat.msg_register(FRIENDS)
def add_friend(msg):
itchat.add_friend(**msg['Text']) # 该操作会自动将新好友的消息录入,不需要重载通讯录
itchat.send_msg('Nice to meet you!', msg['RecommendInfo']['UserName'])
msg.user.verify()
msg.user.send('Nice to meet you!')
@itchat.msg_register(TEXT, isGroupChat=True)
def text_reply(msg):
if msg['isAt']:
itchat.send(u'@%s\u2005I received: %s' % (msg['ActualNickName'], msg['Content']), msg['FromUserName'])
if msg.isAt:
msg.user.send(u'@%s\u2005I received: %s' % (
msg.actualNickName, msg.text))
itchat.auto_login(True)
itchat.run()
itchat.run(True)
*Command line QR Code*

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

.. code:: python
@itchat.msg_register(['Picture', 'Recording', 'Attachment', 'Video'])
@itchat.msg_register([PICTURE, RECORDING, ATTACHMENT, VIDEO])
def download_files(msg):
msg['Text'](msg['FileName'])
itchat.send('@%s@%s'%('img' if msg['Type'] == 'Picture' else 'fil', msg['FileName']), msg['FromUserName'])
return '%s received'%msg['Type']
msg.download(msg.fileName)
itchat.send('@%s@%s' % (
'img' if msg['Type'] == 'Picture' else 'fil', msg['FileName']),
msg['FromUserName'])
return '%s received' % msg['Type']
If you don't want a local copy of the picture, you may pass nothing to the function to get a binary string.

.. code:: python
@itchat.msg_register(['Picture', 'Recording', 'Attachment', 'Video'])
@itchat.msg_register([PICTURE, RECORDING, ATTACHMENT, VIDEO])
def download_files(msg):
with open(msg['FileName'], 'wb') as f:
f.write(msg['Text']())
with open(msg.fileName, 'wb') as f:
f.write(msg.download())
*Multi instance*

Expand Down
61 changes: 44 additions & 17 deletions README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import itchat

@itchat.msg_register(itchat.content.TEXT)
def text_reply(msg):
return msg['Text']
return msg.text

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

## Advanced uses

### Special usage of message dictionary

You may find out that all the users and messages of itchat are dictionaries by printing them out onto the screen.

But actually they are useful classes itchat created.

They have useful keys and useful interfaces, like:

```python
@itchat.msg_register(TEXT)
def _(msg):
# equals to print(msg['FromUserName'])
print(msg.fromUserName)
```

And like:

```python
author = itchat.search_friends(nickName='LittleCoder')[0]
author.send('greeting, littlecoder!')
```

### Message register of various types

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

```python
#coding=utf8
import itchat, time
from itchat.content import *

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

@itchat.msg_register([PICTURE, RECORDING, ATTACHMENT, VIDEO])
def download_files(msg):
msg['Text'](msg['FileName'])
return '@%s@%s' % ({'Picture': 'img', 'Video': 'vid'}.get(msg['Type'], 'fil'), msg['FileName'])
msg.download(msg.fileName)
typeSymbol = {
PICTURE: 'img',
VIDEO: 'vid', }.get(msg.type, 'fil')
return '@%s@%s' % (typeSymbol, msg.fileName)

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

@itchat.msg_register(TEXT, isGroupChat=True)
def text_reply(msg):
if msg['isAt']:
itchat.send(u'@%s\u2005I received: %s' % (msg['ActualNickName'], msg['Content']), msg['FromUserName'])
if msg.isAt:
msg.user.send(u'@%s\u2005I received: %s' % (
msg.actualNickName, msg.text))

itchat.auto_login(True)
itchat.run()
itchat.run(True)
```

### Command line QR Code
Expand Down Expand Up @@ -154,20 +179,22 @@ Name of the file (default name of picture) is in FileName key of msg
Download function accept one location value (include the file name) and store attachment accordingly.

```python
@itchat.msg_register(['Picture', 'Recording', 'Attachment', 'Video'])
@itchat.msg_register([PICTURE, RECORDING, ATTACHMENT, VIDEO])
def download_files(msg):
msg['Text'](msg['FileName'])
itchat.send('@%s@%s'%('img' if msg['Type'] == 'Picture' else 'fil', msg['FileName']), msg['FromUserName'])
return '%s received'%msg['Type']
msg.download(msg.fileName)
itchat.send('@%s@%s' % (
'img' if msg['Type'] == 'Picture' else 'fil', msg['FileName']),
msg['FromUserName'])
return '%s received' % msg['Type']
```

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

```python
@itchat.msg_register(['Picture', 'Recording', 'Attachment', 'Video'])
@itchat.msg_register([PICTURE, RECORDING, ATTACHMENT, VIDEO])
def download_files(msg):
with open(msg['FileName'], 'wb') as f:
f.write(msg['Text']())
with open(msg.fileName, 'wb') as f:
f.write(msg.download())
```

### Multi instance
Expand Down
3 changes: 2 additions & 1 deletion docs/FAQ.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Q: itchat稳定性如何?

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

## 中文文件名文件上传

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

[fields.py-2]: https://gist.github.com/littlecodersh/9a0c5466f442d67d910f877744011705
[fields.py-3]: https://gist.github.com/littlecodersh/e93532d5e7ddf0ec56c336499165c4dc
[itchatmp]: https://github.com/littlecodersh/itchatmp
Loading

0 comments on commit 0161489

Please sign in to comment.