提交 01614898 编写于 作者: L LittleCoder

Update docs

上级 61b19d74
......@@ -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()
......@@ -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())
```
### 用户多开
......@@ -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()
```
......
......@@ -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()
......@@ -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*
......@@ -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*
......
......@@ -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()
......@@ -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
......@@ -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
......
......@@ -2,7 +2,7 @@
Q: itchat稳定性如何?
A: 测试用机器人能稳定在线多个月。如果你在测试过程中发现无法稳定登陆,请检查**登陆手机**及主机是否稳定连接网络。
A: 测试用机器人能稳定在线多个月。如果你在测试过程中发现无法稳定登陆,请检查**登陆手机**及主机是否稳定连接网络。如果你需要最稳定的消息环境,建议使用[itchatmp][itchatmp]项目,两者使用方法类似。
## 中文文件名文件上传
......@@ -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
......@@ -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()
......@@ -59,36 +59,61 @@ itchat.run()
## 进阶应用
### 特殊的字典使用方式
通过打印itchat的用户以及注册消息的参数,可以发现这些值都是字典。
但实际上itchat精心构造了相应的消息、用户、群聊、公众号类。
其所有的键值都可以通过这一方式访问:
```python
@itchat.msg_register(TEXT)
def _(msg):
# equals to print(msg['FromUserName'])
print(msg.fromUserName)
```
属性名为键值首字母小写后的内容。
```python
author = itchat.search_friends(nickName='LittleCoder')[0]
author.send('greeting, littlecoder!')
```
### 各类型消息的注册
通过如下代码,微信已经可以就日常的各种信息进行获取与回复。
```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)
```
### 命令行二维码
......@@ -154,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())
```
### 用户多开
......@@ -182,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()
```
......@@ -234,12 +261,16 @@ A: 有些账号是天生无法给自己的账号发送信息的,建议使用`f
## 类似项目
[youfou/wxpy][youfou-wxpy]: 优秀的api包装和配套插件,微信机器人/优雅的微信个人号API
[liuwons/wxBot][liuwons-wxBot]: 类似的基于Python的微信机器人
[zixia/wechaty][zixia-wechaty]: 基于Javascript(ES6)的微信个人账号机器人NodeJS框架/库
[sjdy521/Mojo-Weixin][Mojo-Weixin]: 使用Perl语言编写的微信客户端框架,可通过插件提供基于HTTP协议的api接口供其他语言调用
[HanSon/vbot][HanSon-vbot]: 基于PHP7的微信个人号机器人,通过实现匿名函数可以方便地实现各种自定义的功能
## 问题和建议
如果有什么问题或者建议都可以在这个[Issue][issue#1]和我讨论
......@@ -265,7 +296,9 @@ A: 有些账号是天生无法给自己的账号发送信息的,建议使用`f
[littlecodersh]: https://github.com/littlecodersh
[tempdban]: https://github.com/tempdban
[Chyroc]: https://github.com/Chyroc
[youfou-wxpy]: https://github.com/youfou/wxpy
[liuwons-wxBot]: https://github.com/liuwons/wxBot
[zixia-wechaty]: https://github.com/zixia/wechaty
[Mojo-Weixin]: https://github.com/sjdy521/Mojo-Weixin
[HanSon-vbot]: https://github.com/hanson/vbot
[issue#1]: https://github.com/littlecodersh/ItChat/issues/1
......@@ -22,7 +22,7 @@ def simple_reply(msg):
# 带对象参数注册,对应消息对象将调用该方法
@itchat.msg_register(TEXT, isFriendChat=True, isGroupChat=True, isMpChat=True)
def text_reply(msg):
itchat.send('%s: %s' % (msg['Type'], msg['Text']), msg['FromUserName'])
msg.user.send('%s: %s' % (msg.type, msg.text))
```
## 消息类型
......@@ -67,11 +67,11 @@ def download_files(msg):
import itchat
from itchat.content import TEXT
@itchat.msg_register(TEXT, isGroupChat = True)
@itchat.msg_register(TEXT, isGroupChat=True)
def text_reply(msg):
print(msg['isAt'])
print(msg['ActualNickName'])
print(msg['Content'])
print(msg.isAt)
print(msg.actualNickName)
print(msg.text)
itchat.auto_login()
itchat.run()
......@@ -109,11 +109,11 @@ itchat.auto_login()
@itchat.msg_register
def general_reply(msg):
return 'I received a %s' % msg['Type']
return 'I received a %s' % msg.type
@itchat.msg_register(TEXT)
def text_reply(msg):
return 'You said to me one to one: %s' % msg['Text']
return 'You said to me one to one: %s' % msg.text
itchat.run()
```
......@@ -154,7 +154,7 @@ functionStatus = False
def change_function():
if replyToGroupChat != functionStatus:
if replyToGroupChat:
@itchat.msg_register(TEXT, isGroupChat = True)
@itchat.msg_register(TEXT, isGroupChat=True)
def group_text_reply(msg):
if u'关闭' in msg['Text']:
replyToGroupChat = False
......@@ -163,7 +163,7 @@ def change_function():
return u'已经在运行'
return u'输入"关闭"或者"开启"测试功能'
else:
@itchat.msg_register(TEXT, isGroupChat = True)
@itchat.msg_register(TEXT, isGroupChat=True)
def group_text_reply(msg):
if u'开启' in msg['Text']:
replyToGroupChat = True
......
# itchat
[![Gitter][gitter-picture]][gitter] ![py27][py27] ![py35][py35] [English version][english-version]
itchat是一个开源的微信个人号接口,使用python调用微信从未如此简单。
使用不到三十行的代码,你就可以完成一个能够处理所有信息的微信机器人。
当然,该api的使用远不止一个机器人,更多的功能等着你来发现,比如[这些][tutorial2]
如今微信已经成为了个人社交的很大一部分,希望这个项目能够帮助你扩展你的个人的微信号、方便自己的生活。
## 安装
可以通过本命令安装itchat:
```python
pip install itchat
```
## 简单入门实例
有了itchat,如果你想要给文件传输助手发一条信息,只需要这样:
```python
import itchat
itchat.auto_login()
itchat.send('Hello, filehelper', toUserName='filehelper')
```
如果你想要回复发给自己的文本消息,只需要这样:
```python
import itchat
@itchat.msg_register(itchat.content.TEXT)
def text_reply(msg):
return msg['Text']
itchat.auto_login()
itchat.run()
```
一些进阶应用可以在下面的开源机器人的源码和进阶应用中看到,或者你也可以阅览[文档][document]
## 试一试
这是一个基于这一项目的[开源小机器人][robot-source-code],百闻不如一见,有兴趣可以尝试一下。
![QRCode][robot-qr]
## 截屏
![file-autoreply][robot-demo-file] ![login-page][robot-demo-login]
## 进阶应用
### 各类型消息的注册
通过如下代码,微信已经可以就日常的各种信息进行获取与回复。
```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'])
@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'])
@itchat.msg_register(FRIENDS)
def add_friend(msg):
itchat.add_friend(**msg['Text']) # 该操作会自动将新好友的消息录入,不需要重载通讯录
itchat.send_msg('Nice to meet you!', msg['RecommendInfo']['UserName'])
@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'])
itchat.auto_login(True)
itchat.run()
```
### 命令行二维码
通过以下命令可以在登陆的时候使用命令行显示二维码:
```python
itchat.auto_login(enableCmdQR=True)
```
部分系统可能字幅宽度有出入,可以通过将enableCmdQR赋值为特定的倍数进行调整:
```python
# 如部分的linux系统,块字符的宽度为一个字符(正常应为两字符),故赋值为2
itchat.auto_login(enableCmdQR=2)
```
默认控制台背景色为暗色(黑色),若背景色为浅色(白色),可以将enableCmdQR赋值为负值:
```python
itchat.auto_login(enableCmdQR=-1)
```
### 退出程序后暂存登陆状态
通过如下命令登陆,即使程序关闭,一定时间内重新开启也可以不用重新扫码。
```python
itchat.auto_login(hotReload=True)
```
### 用户搜索
使用`search_friends`方法可以搜索用户,有四种搜索方式:
1. 仅获取自己的用户信息
2. 获取特定`UserName`的用户信息
3. 获取备注、微信号、昵称中的任何一项等于`name`键值的用户
4. 获取备注、微信号、昵称分别等于相应键值的用户
其中三、四项可以一同使用,下面是示例程序:
```python
# 获取自己的用户信息,返回自己的属性字典
itchat.search_friends()
# 获取特定UserName的用户信息
itchat.search_friends(userName='@abcdefg1234567')
# 获取任何一项等于name键值的用户
itchat.search_friends(name='littlecodersh')
# 获取分别对应相应键值的用户
itchat.search_friends(wechatAccount='littlecodersh')
# 三、四项功能可以一同使用
itchat.search_friends(name='LittleCoder机器人', wechatAccount='littlecodersh')
```
关于公众号、群聊的获取与搜索在文档中有更加详细的介绍。
### 附件的下载与发送
itchat的附件下载方法存储在msg的Text键中。
发送的文件的文件名(图片给出的默认文件名)都存储在msg的FileName键中。
下载方法接受一个可用的位置参数(包括文件名),并将文件相应的存储。
```python
@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']
```
如果你不需要下载到本地,仅想要读取二进制串进行进一步处理可以不传入参数,方法将会返回图片的二进制串。
```python
@itchat.msg_register(['Picture', 'Recording', 'Attachment', 'Video'])
def download_files(msg):
with open(msg['FileName'], 'wb') as f:
f.write(msg['Text']())
```
### 用户多开
使用如下命令可以完成多开的操作:
```python
import itchat
newInstance = itchat.new_instance()
newInstance.auto_login(hotReload=True, statusStorageDir='newInstance.pkl')
@newInstance.msg_register(TEXT)
def reply(msg):
return msg['Text']
newInstance.run()
```
### 退出及登陆完成后调用特定方法
登陆完成后的方法需要赋值在`loginCallback`中。
而退出后的方法需要赋值在`exitCallback`中。
```python
import time
import itchat
def lc():
print('finish login')
def ec():
print('exit')
itchat.auto_login(loginCallback=lc, exitCallback=ec)
time.sleep(3)
itchat.logout()
```
若不设置loginCallback的值,则将会自动删除二维码图片并清空命令行显示。
## 问题和建议
如果有什么问题或者建议都可以在这个[Issue][issue#1]和我讨论
或者也可以在gitter上交流:[![Gitter][gitter-picture]][gitter]
当然也可以加入我们新建的QQ群讨论:549762872
[gitter-picture]: https://badges.gitter.im/littlecodersh/ItChat.svg
[gitter]: https://gitter.im/littlecodersh/ItChat?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge
[py27]: https://img.shields.io/badge/python-2.7-ff69b4.svg
[py35]: https://img.shields.io/badge/python-3.5-red.svg
[english-version]: https://github.com/littlecodersh/ItChat/blob/master/README_EN.md
[document]: https://itchat.readthedocs.org/zh/latest/
[tutorial2]: http://python.jobbole.com/86532/
[robot-source-code]: https://gist.github.com/littlecodersh/ec8ddab12364323c97d4e36459174f0d
[robot-qr]: http://7xrip4.com1.z0.glb.clouddn.com/ItChat%2FQRCode2.jpg?imageView/2/w/400/
[robot-demo-file]: http://7xrip4.com1.z0.glb.clouddn.com/ItChat%2FScreenshots%2F%E5%BE%AE%E4%BF%A1%E8%8E%B7%E5%8F%96%E6%96%87%E4%BB%B6%E5%9B%BE%E7%89%87.png?imageView/2/w/300/
[robot-demo-login]: http://7xrip4.com1.z0.glb.clouddn.com/ItChat%2FScreenshots%2F%E7%99%BB%E5%BD%95%E7%95%8C%E9%9D%A2%E6%88%AA%E5%9B%BE.jpg?imageView/2/w/450/
[fields.py-2]: https://gist.github.com/littlecodersh/9a0c5466f442d67d910f877744011705
[fields.py-3]: https://gist.github.com/littlecodersh/e93532d5e7ddf0ec56c336499165c4dc
[littlecodersh]: https://github.com/littlecodersh
[tempdban]: https://github.com/tempdban
[Chyroc]: https://github.com/Chyroc
[liuwons-wxBot]: https://github.com/liuwons/wxBot
[zixia-wechaty]: https://github.com/zixia/wechaty
[Mojo-Weixin]: https://github.com/sjdy521/Mojo-Weixin
[issue#1]: https://github.com/littlecodersh/ItChat/issues/1
......@@ -7,6 +7,7 @@
当然这里需要特别强调的是三点,分别是短时间关闭重连、命令行二维码与自定义登陆内容。
* itchat提供了登陆状态暂存,关闭程序后一定时间内不需要扫码即可登录。
* 由于目前微信网页版提供上一次登录的微信号不扫码直接手机确认登陆,所以如果开启登陆状态暂存将会自动使用这一功能。
* 为了方便在无图形界面使用itchat,程序内置了命令行二维码的显示。
* 如果你需要就登录状态就一些修改(例如更改提示语、二维码出现后邮件发送等)。
......@@ -24,11 +25,10 @@ from itchat.content import TEXT
@itchat.msg_register(TEXT)
def simple_reply(msg):
print(msg['Text'])
print(msg.text)
itchat.auto_login(hotReload=True)
itchat.run()
itchat.dump_login_status()
```
通过设置statusStorageDir可以将静态文件指定为其他的值。
......
......@@ -10,9 +10,9 @@ from itchat.content import TEXT
@itchat.msg_register(TEXT, isFriendChat=True, isGroupChat=True, isMpChat=True)
def simple_reply(msg):
return 'I received: %s' % msg['Content']
return 'I received: %s' % msg.text
itchat.auto_login()
itchat.auto_login(True)
itchat.run()
```
......@@ -21,34 +21,34 @@ itchat.run()
itchat支持所有的消息类型与群聊,下面的示例中演示了对于这些消息类型简单的配置。
```python
#coding=utf8
import itchat
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))
# 以下四类的消息的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!')
# 在注册时增加isGroupChat=True将判定为群聊回复
@itchat.msg_register(TEXT, isGroupChat = True)
def groupchat_reply(msg):
if msg['isAt']:
itchat.send(u'@%s\u2005I received: %s' % (msg['ActualNickName'], msg['Content']), msg['FromUserName'])
@itchat.msg_register(TEXT, isGroupChat=True)
def text_reply(msg):
if msg.isAt:
msg.user.send(u'@%s\u2005I received: %s' % (
msg.actualNickName, msg.text))
itchat.auto_login(True)
itchat.run()
itchat.run(True)
```
当然这里不需要深究为什么这些东西可以这么写,我在这里放出了示例程序只是为了给你一个该sdk相关代码大概样子的概念。
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册