diff --git a/README.md b/README.md index df1c5def..486b4423 100644 --- a/README.md +++ b/README.md @@ -2,18 +2,14 @@ [![Gitter][gitter-picture]][gitter] ![py27][py27] ![py35][py35] [English version][english-version] -itchat是一个开源的微信个人号接口,使用他你可以轻松的通过命令行使用个人微信号。 +itchat是一个开源的微信个人号接口,使用python调用微信从未如此简单。 使用不到三十行的代码,你就可以完成一个能够处理所有信息的微信机器人。 -当然,该api的使用远不止一个机器人。 +当然,该api的使用远不止一个机器人,更多的功能等着你来发现。 如今微信已经成为了个人社交的很大一部分,希望这个项目能够帮助你扩展你的个人的微信号、方便自己的生活。 -## Documents - -你可以在[这里][document]获取api的使用帮助。 - ## Installation 可以通过本命令安装itchat: @@ -24,27 +20,59 @@ pip install itchat ## Simple uses +有了itchat,如果你想要回复发给自己的文本消息,只需要这样: + +```python +import itchat + +@itcaht.msg_register(itchat.content.TEXT) +def text_reply(msg): + itchat.send(msg['Text'], msg['FromUserName']) + +itchat.auto_login() +itchat.run() +``` + +一些进阶应用可以在Advanced uses中看到,或者你也可以阅览[文档][document]。 + + + +## Have a try + +这是一个基于这一项目的[开源小机器人][robot-source-code],百闻不如一见,有兴趣可以尝试一下。 + +![QRCode][robot-qr] + +## Screenshots + +![file-autoreply][robot-demo-file] ![login-page][robot-demo-login] + +## Advanced uses + +### 各类型消息的注册 + 通过如下代码,微信已经可以就日常的各种信息进行获取与回复。 ```python #coding=utf8 import itchat, time +from itchat.content import * -@itchat.msg_register(['Text', 'Map', 'Card', 'Note', 'Sharing']) +@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']) +@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') +@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) +@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']) @@ -53,8 +81,6 @@ itchat.auto_login(True) itchat.run() ``` -## Advanced uses - ### 命令行二维码 通过以下命令可以在登陆的时候使用命令行显示二维码: @@ -86,7 +112,7 @@ itchat.auto_login(hotReload=True) ### 用户搜索 -使用`get_friends`方法可以搜索用户,有四种搜索方式: +使用`search_friends`方法可以搜索用户,有四种搜索方式: 1. 仅获取自己的用户信息 2. 获取特定`UserName`的用户信息 3. 获取备注、微信号、昵称中的任何一项等于`name`键值的用户 @@ -96,17 +122,19 @@ itchat.auto_login(hotReload=True) ```python # 获取自己的用户信息,返回自己的属性字典 -itchat.get_friends() +itchat.search_friends() # 获取特定UserName的用户信息 -itchat.get_friends(userName='@abcdefg1234567') +itchat.search_friends(userName='@abcdefg1234567') # 获取任何一项等于name键值的用户 -itchat.get_friends(name='littlecodersh') +itchat.search_friends(name='littlecodersh') # 获取分别对应相应键值的用户 -itchat.get_friends(wechatAccount='littlecodersh') +itchat.search_friends(wechatAccount='littlecodersh') # 三、四项功能可以一同使用 -itchat.get_friends(name='LittleCoder机器人', wechatAccount='littlecodersh') +itchat.search_friends(name='LittleCoder机器人', wechatAccount='littlecodersh') ``` +关于公众号、群聊的获取与搜索在文档中有更加详细的介绍。 + ### 附件的下载与发送 itchat的附件下载方法存储在msg的Text键中。 @@ -132,16 +160,6 @@ def download_files(msg): f.write(msg['Text']()) ``` -## Have a try - -这是一个基于这一项目的[开源小机器人][robot-source-code],百闻不如一见,有兴趣可以尝试一下。 - -![QRCode][robot-qr] - -## Screenshots - -![file-autoreply][robot-demo-file] ![login-page][robot-demo-login] - ## FAQ Q: 为什么中文的文件没有办法上传? @@ -158,9 +176,9 @@ A: 有两种方式:发送、接受自己UserName的消息;发送接收文件 ## Author -[LittleCoder][littlecodersh]: 整体构架及完成Python2版本。 +[LittleCoder][littlecodersh]: 整体构架及完成Python2 Python3版本。 -[Chyroc][Chyroc]: 完成Python3版本。 +[Chyroc][Chyroc]: 完成第一版本的Python3构架。 ## See also diff --git a/README.rst b/README.rst index c0b8d405..0a023115 100644 --- a/README.rst +++ b/README.rst @@ -3,13 +3,36 @@ itchat |Python2| |Python3| -itchat is a open souce wechat api project for personal account. +itchat is an open source api for WeChat, a commonly-used Chinese social networking app. -It enables you to access your personal wechat account through command line. +Accessing your personal wechat account through itchat in python has never been easier. -Here is the `document `__. +A wechat robot can handle all the basic messages with only less than 30 lines of codes. -So enjoy:) +Now Wechat is an important part of personal life, hopefully this repo can help you extend your personal wechat account's functionality and enbetter user's experience with wechat. + +**Installation** + +.. code:: bash + + pip install itchat + +**Simple uses** + +With itchat, you only need to write this to reply personal text messages. + +.. code:: python + + import itchat + + @itcaht.msg_register(itchat.content.TEXT) + def text_reply(msg): + itchat.send(msg['Text'], msg['FromUserName']) + + itchat.auto_login() + itchat.run() + +For more advanced uses you may continue on reading or browse the `document `__. **Try** @@ -17,36 +40,35 @@ You may have a try of the robot based on this project first: |QRCodeOfRobot| -Here is the `code `__. - -**Installation** +Here is the `code `__. -.. code:: bash +**Advanced uses** - pip install itchat +*Message register of various types* -**Simple uses** +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']) + @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']) + @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') + @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) + @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']) @@ -54,8 +76,6 @@ Here is the `code `__. itchat.auto_login(True) itchat.run() -**Advanced uses** - *Command line QR Code* You can access the QR Code in command line through using this command: @@ -87,7 +107,7 @@ By using the following command, you may reload the program without re-scan QRCod *User search* -By using `get_friends`, you have four ways to search a user: +By using `search_friends`, you have four ways to search a user: 1. Get your own user information 2. Get user information through `UserName` @@ -99,15 +119,17 @@ Way 3, 4 can be used together, the following is the demo program: .. code:: python # get your own user information - itchat.get_friends() + itchat.search_friends() # get user information of specific username - itchat.get_friends(userName='@abcdefg1234567') + itchat.search_friends(userName='@abcdefg1234567') # get user information of function 3 - itchat.get_friends(name='littlecodersh') + itchat.search_friends(name='littlecodersh') # get user information of function 4 - itchat.get_friends(wechatAccount='littlecodersh') + itchat.search_friends(wechatAccount='littlecodersh') # combination of way 3, 4 - itchat.get_friends(name='LittleCoder机器人', wechatAccount='littlecodersh') + itchat.search_friends(name='LittleCoder机器人', wechatAccount='littlecodersh') + +There are detailed information about searching and getting of massive platforms and chatrooms in document. *Download and send attachments* @@ -144,6 +166,10 @@ Q: Why I still can't show QRCode with command line after I set enableCmdQr key t A: That's because you need to install optional site-package pillow, try this script: pip install pillow +Q: How to use this package to use my wechat as an monitor? + +A: There are two ways: communicate with your own account or with filehelper. + **Comments** If you have any problems or suggestions, you can talk to me in this `issue `__ diff --git a/README_EN.md b/README_EN.md index 0f26d153..091104f9 100644 --- a/README_EN.md +++ b/README_EN.md @@ -2,16 +2,14 @@ [![Gitter][gitter-picture]][gitter] ![py27][py27] ![py35][py35] [Chinese version][chinese-version] -itchat is an open source api for WeChat, a commonly-used Chinese social networking app, you can easily access your personal wechat account through itchat in cmd. +itchat is an open source api for WeChat, a commonly-used Chinese social networking app. + +Accessing your personal wechat account through itchat in python has never been easier. A wechat robot can handle all the basic messages with only less than 30 lines of codes. Now Wechat is an important part of personal life, hopefully this repo can help you extend your personal wechat account's functionality and enbetter user's experience with wechat. -## Documents - -You may get the document of this api from [here][document]. - ## Installation itchat can be installed with this little one-line command: @@ -22,27 +20,57 @@ pip install itchat ## Simple uses +With itchat, you only need to write this to reply personal text messages. + +```python +import itchat + +@itcaht.msg_register(itchat.content.TEXT) +def text_reply(msg): + itchat.send(msg['Text'], msg['FromUserName']) + +itchat.auto_login() +itchat.run() +``` + +For more advanced uses you may continue on reading or browse the [document][document]. + +## Have a try + +This QRCode is a wechat account based on the framework of [demo code][robot-source-code]. Seeing is believing, so have a try:) + +![QRCode][robot-qr] + +## Screenshots + +![file-autoreply][robot-demo-file] ![login-page][robot-demo-login] + +## Advanced uses + +### 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']) +@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']) +@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') +@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) +@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']) @@ -51,8 +79,6 @@ itchat.auto_login(True) itchat.run() ``` -## Advanced uses - ### Command line QR Code You can access the QR Code in command line through using this command: @@ -84,7 +110,7 @@ itchat.auto_login(hotReload=True) ### User search -By using `get_friends`, you have four ways to search a user: +By using `search_friends`, you have four ways to search a user: 1. Get your own user information 2. Get user information through `UserName` 3. Get user information whose remark name or wechat account or nickname matches name key of the function @@ -94,17 +120,19 @@ Way 3, 4 can be used together, the following is the demo program: ```python # get your own user information -itchat.get_friends() +itchat.search_friends() # get user information of specific username -itchat.get_friends(userName='@abcdefg1234567') +itchat.search_friends(userName='@abcdefg1234567') # get user information of function 3 -itchat.get_friends(name='littlecodersh') +itchat.search_friends(name='littlecodersh') # get user information of function 4 -itchat.get_friends(wechatAccount='littlecodersh') +itchat.search_friends(wechatAccount='littlecodersh') # combination of way 3, 4 -itchat.get_friends(name='LittleCoder机器人', wechatAccount='littlecodersh') +itchat.search_friends(name='LittleCoder机器人', wechatAccount='littlecodersh') ``` +There are detailed information about searching and getting of massive platforms and chatrooms in document. + ### Download and send attachments The attachment download function of itchat is in Text key of msg @@ -130,16 +158,6 @@ def download_files(msg): f.write(msg['Text']()) ``` -## Have a try - -This QRCode is a wechat account based on the framework of [demo code][robot-source-code]. Seeing is believing, so have a try:) - -![QRCode][robot-qr] - -## Screenshots - -![file-autoreply][robot-demo-file] ![login-page][robot-demo-login] - ## FAQ Q: Why I can't upload files whose name is not purely english? @@ -150,11 +168,15 @@ Q: Why I still can't show QRCode with command line after I set enableCmdQr key t A: That's because you need to install optional site-package pillow, try this script: pip install pillow +Q: How to use this package to use my wechat as an monitor? + +A: There are two ways: communicate with your own account or with filehelper. + ## Author -[LittleCoder][littlecodersh]: Structure and py2 version +[LittleCoder][littlecodersh]: Structure and py2 py3 version -[Chyroc][Chyroc]: py3 version +[Chyroc][Chyroc]: first py3 version ## See also diff --git a/docs/1.Start.md b/docs/1.Start.md index 0f9aa5f3..34fb43e2 100644 --- a/docs/1.Start.md +++ b/docs/1.Start.md @@ -1,54 +1,57 @@ -#入门 -##最简单的回复 +# 入门 + +## 最简单的回复 通过如下代码,可以完成回复所有文本信息(包括群聊)。 ```python import itchat - -itchat.auto_login() +from itchat.content import TEXT @itchat.msg_register def simple_reply(msg): - if msg.get('Type', '') == 'Text': - return 'I received: %s'%msg.get('Content', '') + if msg['Type'] == TEXT: + return 'I received: %s' % msg['Content'] +itchat.auto_login() itchat.run() ``` -##常用消息的配置 +## 常用消息的配置 -itchat支持所有的消息类型与群聊,下面演示对于这些消息类型简单的配置。 +itchat支持所有的消息类型与群聊,下面的示例中演示了对于这些消息类型简单的配置。 ```python #coding=utf8 -import itchat, time - -itchat.auto_login() +import itchat +from itchat.content import * -@itchat.msg_register(['Text', 'Map', 'Card', 'Note', 'Sharing']) +@itchat.msg_register([TEXT, MAP, CARD, NOTE, SHARING]) def text_reply(msg): - itchat.send('%s: %s'%(msg['Type'], msg['Text']), msg['FromUserName']) + itchat.send('%s: %s' % (msg['Type'], msg['Text']), msg['FromUserName']) # 以下四类的消息的Text键下存放了用于下载消息内容的方法,传入文件地址即可 -@itchat.msg_register(['Picture', 'Recording', 'Attachment', 'Video']) +@itchat.msg_register([PICTURE, RECORDING, ATTACHMENT, VIDEO]) def download_files(msg): - fileDir = '%s%s'%(msg['Type'], int(time.time())) - msg['Text'](fileDir) - itchat.send('%s received'%msg['Type'], msg['FromUserName']) - itchat.send('@%s@%s'%('img' if msg['Type'] == 'Picture' else 'fil', fileDir), msg['FromUserName']) + msg['Text'](msg['FileName']) + return '@%s@%s' % ({'Picture': 'img', 'Video': 'vid'}.get(msg['Type'], 'fil'), msg['FileName']) # 收到好友邀请自动添加好友 -@itchat.msg_register('Friends') +@itchat.msg_register(FRIENDS) def add_friend(msg): - itchat.add_friend(**msg['Text']) - itchat.get_contract() + itchat.add_friend(**msg['Text']) # 该操作会自动将新好友的消息录入,不需要重载通讯录 itchat.send_msg('Nice to meet you!', msg['RecommendInfo']['UserName']) # 在注册时增加isGroupChat=True将判定为群聊回复 -@itchat.msg_register('Text', isGroupChat = True) -def text_reply(msg): - itchat.send(u'@%s\u2005I received: %s'%(msg['ActualNickName'], msg['Content']), msg['FromUserName']) +@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.auto_login(True) itchat.run() ``` + +当然这里不需要深究为什么这些东西可以这么写,我在这里放出了示例程序只是为了给你一个该sdk相关代码大概样子的概念。 + +有了大概的模式的了解之后我们就可以进入下一部分的介绍。 diff --git a/docs/2.Login.md b/docs/2.Login.md index b017e534..acd9d820 100644 --- a/docs/2.Login.md +++ b/docs/2.Login.md @@ -1,18 +1,45 @@ # 登陆 -itchat提供了`auto_login`方法,调用即可完成登录。 +在上一章中你看到了基本的注册与登陆,而显然登陆使用的是itchat提供了`auto_login`方法,调用即可完成登录。 -itchat也提供了登陆状态暂存,关闭程序后一定时间内不需要扫码即可登录。 +一般而言,我们都会在完成消息的注册后登陆。 + +当然这里需要特别强调的是三点,分别是短时间关闭重连、命令行二维码与自定义登陆内容。 +* itchat提供了登陆状态暂存,关闭程序后一定时间内不需要扫码即可登录。 +* 为了方便在无图形界面使用itchat,程序内置了命令行二维码的显示。 +* 如果你需要就登录状态就一些修改(例如更改提示语、二维码出现后邮件发送等)。 ## 短时间关闭程序后重连 这样即使程序关闭,一定时间内重新开启也可以不用重新扫码。 +最简单的用法就是给`auto_login`方法传入值为真的hotReload。 + +该方法会生成一个静态文件`itchat.pkl`,用于存储登陆的状态。 + +```python +import itchat +from itchat.content import TEXT + +@itchat.msg_register(TEXT) +def simple_reply(msg): + print(msg['Text']) + +itchat.auto_login(hotReload=True) +itchat.run() +itchat.dump_login_status() +``` + +通过设置statusStorageDir可以将静态文件指定为其他的值。 + +这一内置选项其实就相当于使用了以下两个函数的这一段程序: + ```python import itchat +from itchat.content import TEXT if itchat.load_login_status(): - @itchat.msg_register('Text') + @itchat.msg_register(TEXT) def simple_reply(msg): print(msg['Text']) itchat.run() @@ -20,21 +47,32 @@ if itchat.load_login_status(): else: itchat.auto_login() itchat.dump_login_status() - print('Config stored') + print('Config stored, so exit.') ``` -或者直接使用内置的代码也可: +其中load_login_status与dump_login_status分别对应读取与导出设置。 + +通过设置传入的fileDir的值可以设定导入导出的文件。 + +## 命令行二维码显示 + +通过以下命令可以在登陆的时候使用命令行显示二维码: ```python -import itchat +itchat.auto_login(enableCmdQR=True) +``` -@itchat.msg_register('Text') -def simple_reply(msg): - print(msg['Text']) +部分系统可能字幅宽度有出入,可以通过将enableCmdQR赋值为特定的倍数进行调整: -itchat.auto_login(hotReload = True) -itchat.run() -itchat.dump_login_status() +```python +# 如部分的linux系统,块字符的宽度为一个字符(正常应为两字符),故赋值为2 +itchat.auto_login(enableCmdQR=2) +``` + +默认控制台背景色为暗色(黑色),若背景色为浅色(白色),可以将enableCmdQR赋值为负值: + +```python +itchat.auto_login(enableCmdQR=-1) ``` ## 自定义登录过程 @@ -113,7 +151,7 @@ itchat自带的`auto_login`通过如下代码可以实现: import itchat, time, sys def output_info(msg): - print('[INFO] %s'%msg) + print('[INFO] %s' % msg) def open_QR(): for get_count in range(10): @@ -151,7 +189,7 @@ itchat.start_receiving() # Start auto-replying @itchat.msg_register def simple_reply(msg): - if msg.get('Type', '') == 'Text': - return 'I received: %s'%msg.get('Content', '') + if msg['Type'] == 'Text': + return 'I received: %s' % msg['Content'] itchat.run() ``` diff --git a/docs/3.Handler.md b/docs/3.Handler.md index 746f1cec..5499eb53 100644 --- a/docs/3.Handler.md +++ b/docs/3.Handler.md @@ -11,16 +11,19 @@ itchat将根据接收到的消息类型寻找对应的已经注册的方法。 你可以通过两种方式注册消息方法 ```python +import itchat +from itchat.content import * + # 不带参数注册,所有消息类型都将调用该方法(包括群消息) @itchat.msg_register def simple_reply(msg): - if msg.get('Type', '') == 'Text': - return 'I received: %s'%msg.get('Text', '') + if msg['Type'] == 'Text': + return 'I received: %s' % msg['Text'] # 带参数注册,该类消息类型将调用该方法 -@itchat.msg_register(['Text', 'Map', 'Card', 'Note', 'Sharing']) +@itchat.msg_register([TEXT, MAP, CARD, NOTE, SHARING]) def text_reply(msg): - itchat.send('%s: %s'%(msg['Type'], msg['Text']), msg['FromUserName']) + itchat.send('%s: %s' % (msg['Type'], msg['Text']), msg['FromUserName']) ``` ## 消息类型 @@ -29,27 +32,28 @@ def text_reply(msg): 本api增加`Text`、`Type`(也就是参数)键值,方便操作。 +itchat.content中包含所有的消息类型参数,内容如下表所示: + 参数 |类型 |Text键值 :----------|:----------|:--------------- -Text |文本 |文本内容 -Map |地图 |位置文本 -Card |名片 |推荐人字典 -Note |通知 |通知文本 -Sharing |分享 |分享名称 -Picture |图片/表情 |下载方法 -Recording |语音 |下载方法 -Attachment |附件 |下载方法 -Video |小视频 |下载方法 -Friends |好友邀请 |添加好友所需参数 +TEXT |文本 |文本内容 +MAP |地图 |位置文本 +CARD |名片 |推荐人字典 +NOTE |通知 |通知文本 +SHARING |分享 |分享名称 +PICTURE |图片/表情 |下载方法 +RECORDING |语音 |下载方法 +ATTACHMENT |附件 |下载方法 +VIDEO |小视频 |下载方法 +FRIENDS |好友邀请 |添加好友所需参数 Useless |无用信息 |'UselessMsg' 比如你需要存储发送给你的附件: ```python -@itchat.msg_register('Attachment') +@itchat.msg_register(ATTACHMENT) def download_files(msg): - fileDir = '%s%s'%(msg['FileName'], int(time.time())) - msg['Text'](fileDir) + msg['Text'](msg['FileName']) ``` 值得注意的是,群消息增加了三个键值: @@ -61,8 +65,9 @@ def download_files(msg): ```python 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']) @@ -80,14 +85,15 @@ itchat.run() ```python import itchat +from itchat.content import * itchat.auto_login() -@itchat.msg_register('Text') +@itchat.msg_register(TEXT) def text_reply(msg): return 'This is the old register' -@itchat.msg_register('Text') +@itchat.msg_register(TEXT) def text_reply(msg): return 'This is a new one' @@ -97,16 +103,17 @@ itchat.run() 在私聊发送文本时将会回复`This is a new one`。 ```python import itchat +from itchat.content import * 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') +@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() ``` @@ -122,6 +129,7 @@ itchat.run() ```python # 使用另一线程,但注意不要让程序运行终止 import thread + thread.start_new_thread(itchat.run, ()) # 使用configured_reply方法 @@ -135,7 +143,10 @@ while 1: ```python #coding=utf8 -import itchat, thread +import thread + +import itchat +from itchat.content import * replyToGroupChat = True functionStatus = False @@ -143,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 @@ -152,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 diff --git a/docs/4.Message content.md b/docs/4.Message content.md index 15b72fe4..1c4a7bf3 100644 --- a/docs/4.Message content.md +++ b/docs/4.Message content.md @@ -1,6 +1,6 @@ -#消息内容 +# 消息内容 -##微信一般消息内容 +## 微信一般消息内容 ```python { @@ -33,13 +33,13 @@ } ``` -微信回复的所有消息都遵循这一格式,下面将就每种消息类型具体分析 +微信回复的所有消息都遵循这一格式,下面将就每种消息类型具体分析。 -##消息的具体内容 +## 消息的具体内容 -本段部分参考了[Urinx的Github](https://github.com/Urinx/WeixinBot)并修改,整理的非常优秀! +本段部分参考[Urinx的Github](https://github.com/Urinx/WeixinBot)并修改。 -###微信初始化消息 +### 微信初始化消息 ``` MsgType: 51 @@ -71,7 +71,7 @@ Content: ``` -###文本消息 +### 文本消息 ``` MsgType: 1 @@ -80,7 +80,7 @@ ToUserName: 接收方ID Content: 消息内容 ``` -###图片消息 +### 图片消息 ``` MsgType: 3 @@ -94,7 +94,9 @@ Content: ``` -###小视频消息 +itchat添加了Text键,键值为下载该图片的方法。 + +### 小视频消息 ``` MsgType: 62 @@ -108,17 +110,20 @@ Content: ``` -###地理位置消息 +itchat添加了Text键,键值为下载该视频的方法。 + +### 地理位置消息 ``` MsgType: 1 FromUserName: 发送方ID ToUserName: 接收方ID Content: http://weixin.qq.com/cgi-bin/redirectforward?args=xxx -# 属于文本消息,只不过内容是一个跳转到地图的链接 ``` -###名片消息 +itchat添加了Text键,键值为该地点的文本形式。 + +### 名片消息 ``` MsgType: 42 @@ -147,7 +152,9 @@ RecommendInfo: } ``` -###语音消息 +itchat添加了Text键,键值为该调用`add_friend`需要的属性。 + +### 语音消息 ``` MsgType: 34 @@ -160,7 +167,9 @@ Content: ``` -###动画表情 +itchat添加了Text键,键值为下载该语音文件的方法。 + +### 动画表情 ``` MsgType: 47 @@ -173,7 +182,11 @@ Content: ``` -###普通链接或应用分享消息 +itchat添加了Text键,键值为下载该图片表情的方法。 + +由于版权问题,部分微信商店提供的表情是无法下载的,注意。 + +### 普通链接或应用分享消息 ``` MsgType: 49 @@ -200,7 +213,7 @@ Content: ``` -###音乐链接消息 +### 音乐链接消息 ``` MsgType: 49 @@ -262,7 +275,7 @@ Content: ``` -###群消息 +### 群消息 ``` MsgType: 1 @@ -272,7 +285,12 @@ Content: @xxx:
xxx ``` -###红包消息 +itchat增加了三个群聊相关的键值: +* isAt: 判断是否@本号 +* ActualNickName: 实际NickName +* Content: 实际Content + +### 红包消息 ``` MsgType: 49 @@ -282,7 +300,7 @@ ToUserName: 接收方ID Content: 未知 ``` -###系统消息 +### 系统消息 ``` MsgType: 10000 diff --git a/docs/5.Reply.md b/docs/5.Reply.md index 3cf2faed..088dd8a6 100644 --- a/docs/5.Reply.md +++ b/docs/5.Reply.md @@ -5,7 +5,7 @@ itchat提供五种回复方法,建议直接使用`send`方法。 ## send方法 * 方法: ```python -send(msg = 'Text Message', toUserName = None) +send(msg='Text Message', toUserName=None) ``` * 所需值: * msg:消息内容 @@ -29,7 +29,7 @@ itchat.send('@vid@%s' % 'demo.mp4') ## send_msg方法 * 方法: ```python -send_msg(msg = 'Text Message', toUserName = None) +send_msg(msg='Text Message', toUserName=None) ``` * 所需值: * msg:消息内容 @@ -47,7 +47,7 @@ itchat.send_msg('Hello world') ## send_file方法 * 方法: ```python -send_file(fileDir, toUserName = None) +send_file(fileDir, toUserName=None) ``` * 所需值: * fileDir:文件路径(不存在该文件时将打印无此文件的提醒) @@ -67,7 +67,7 @@ itchat.send_file('xlsx.xlsx') ## send_img方法 * 方法: ```python -send_img(fileDir, toUserName = None) +send_img(fileDir, toUserName=None) ``` * 所需值: * fileDir:文件路径(不存在该文件时将打印无此文件的提醒) @@ -87,7 +87,7 @@ itchat.send_img('gz.gif') ## send_video方法 * 方法: ```python -send_video(fileDir, toUserName = None) +send_video(fileDir, toUserName=None) ``` * 所需值: * fileDir:文件路径(不存在该文件时将打印无此文件的提醒) diff --git a/docs/6.Member stuff.md b/docs/6.Member stuff.md new file mode 100644 index 00000000..a76dccb4 --- /dev/null +++ b/docs/6.Member stuff.md @@ -0,0 +1,102 @@ +在使用个人微信的过程当中主要有三种账号需要获取,分别为: +* 好友 +* 公众号 +* 群聊 + +itchat为这三种账号都提供了整体获取方法与搜索方法。 + +而群聊多出获取用户列表方法以及创建群聊、增加、删除用户的方法。 + +这里我们分这三种分别介绍如何使用。 + +## 好友 + +好友的获取方法为`get_friends`,将会返回完整的好友列表。 +* 其中每个好友为一个字典 +* 列表的第一项为本人的账号信息 +* 传入update键为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') +``` + +## 公众号 + +公众号的获取方法为`get_mps`,将会返回完整的公众号列表。 +* 其中每个公众号为一个字典 +* 传入update键为True将可以更新公众号列表并返回 + +公众号的搜索方法为`search_mps`,有两种搜索方法: +1. 获取特定`UserName`的公众号 +2. 获取名字中含有特定字符的公众号 + +如果两项都做了特定,将会仅返回特定`UserName`的公众号,下面是示例程序: + +```python +# 获取特定UserName的公众号,返回值为一个字典 +itchat.search_mps(userName='@abcdefg1234567') +# 获取名字中含有特定字符的公众号,返回值为一个字典的列表 +itcaht.search_mps(name='LittleCoder') +# 以下方法相当于仅特定了UserName +itchat.search_mps(userName='@abcdefg1234567', name='LittleCoder') +``` + +## 群聊 + +群聊的获取方法为`get_chatrooms`,将会返回完整的群聊列表。 +* 其中每个群聊为一个字典 +* 传入update键为True将可以更新群聊列表并返回 + +群聊的搜索方法为`search_chatrooms`,有两种搜索方法: +1. 获取特定`UserName`的群聊 +2. 获取名字中含有特定字符的群聊 + +如果两项都做了特定,将会仅返回特定`UserName`的群聊,下面是示例程序: + +```python +# 获取特定UserName的群聊,返回值为一个字典 +itchat.search_chatrooms(userName='@abcdefg1234567') +# 获取名字中含有特定字符的群聊,返回值为一个字典的列表 +itcaht.search_chatrooms(name='LittleCoder') +# 以下方法相当于仅特定了UserName +itchat.search_chatrooms(userName='@abcdefg1234567', name='LittleCoder') +``` + +群聊用户列表的获取方法为`update_chatroom`。 +* 群聊在首次获取中不会获取群聊的用户列表,所以需要调用该命令才能获取群聊的成员 +* 该方法需要传入群聊的`UserName`,返回特定群聊的用户列表 + +```python +memberList = itchat.update_chatroom('@abcdefg1234567') +``` + +创建群聊、增加、删除群聊用户的方法如下所示: +* 由于之前通过群聊检测是否被好友拉黑的程序,目前这三个方法都被严格限制了使用频率 +* 删除群聊需要本账号为群管理员,否则会失败 + +```python +memberList = itchat.get_friends()[1:] +# 创建群聊,topic键值为群聊名 +chatroomUserName = itchat.create_chatroom(memberList, 'test chatroom') +# 删除群聊内的用户 +itchat.delete_member_from_chatroom(chatroomUserName, memberList[0]) +# 增加用户进入群聊 +itchat.add_member_into_chatroom(chatroomUserName, memberList[0]) +``` diff --git a/docs/6.Deploy.md b/docs/7.Deploy.md similarity index 86% rename from docs/6.Deploy.md rename to docs/7.Deploy.md index 13ee7c90..7ac85197 100644 --- a/docs/6.Deploy.md +++ b/docs/7.Deploy.md @@ -1,10 +1,10 @@ -#部署 +# 部署 -##Windows系统 +## Windows系统 直接运行即可。 -##Linux系统 +## Linux系统 若需要后台命令行运行可以: ```bash diff --git a/docs/7.FAQ.md b/docs/7.FAQ.md deleted file mode 100644 index 79d0ca52..00000000 --- a/docs/7.FAQ.md +++ /dev/null @@ -1,5 +0,0 @@ -##中文文件上传 - -Q: 为什么中文的文件没有办法上传? - -A: 这是由于`requests`的编码问题导致的。若需要支持中文文件传输,将[fields.py](https://github.com/littlecodersh/ItChat/blob/robot/plugin/config/fields.py)文件放入requests包的packages/urllib3下即可 diff --git a/docs/8.FAQ.md b/docs/8.FAQ.md new file mode 100644 index 00000000..faabe4ad --- /dev/null +++ b/docs/8.FAQ.md @@ -0,0 +1,15 @@ +Q: 为什么中文的文件没有办法上传? + +A: 这是由于`requests`的编码问题导致的。若需要支持中文文件传输,将[fields.py][fields.py-2](py3版本见[这里][fields.py-3])文件放入requests包的packages/urllib3下即可 + +Q: 为什么我在设定了`itchat.auto_login()`的`enableCmdQR`为`True`后还是没有办法在命令行显示二维码? + +A: 这是由于没有安装可选的包`pillow`,可以使用右边的命令安装:`pip install pillow` + +Q: 如何通过这个包将自己的微信号变为控制器? + +A: 有两种方式:发送、接受自己UserName的消息;发送接收文件传输助手(filehelper)的消息 + +Q: 如何保持itchat长时间稳定登陆? + +A: 目前建议使用IOS客户端扫码登陆并保持网络稳定。 diff --git a/docs/Robot/Home.md b/docs/Robot/Home.md deleted file mode 100644 index 1e85f4d3..00000000 --- a/docs/Robot/Home.md +++ /dev/null @@ -1,45 +0,0 @@ -# ItChat [![Gitter](https://badges.gitter.im/littlecodersh/ItChat.svg)](https://gitter.im/littlecodersh/ItChat?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge) ![python](https://img.shields.io/badge/python-2.7-ff69b4.svg) - -ItChat是一个个人微信号的机器人,他实现了一个机器人需要实现的绝大部分功能。自动加好友、认人、发送图片、发送文件、简单的外部接口都可以轻松的完成。ItChat各模块与插件有着明确的模块化,易于扩展功能编写自己的插件。ItChat配置及其方便,甚至不需要图形界面就可以完成安装。 - -顺带的,如果把ItChat.py文件中的ROBOT改为False,该程序也可以当命令行的微信聊天软件使用。 - -本项目的源码被存储在[robot](https://github.com/littlecodersh/ItChat/tree/robot)分支上。 - -##Have a try - -我将我的微信号挂上了这个小机器人,百闻不如一见,有兴趣可以尝试一下。 - -![QRCode](http://7xrip4.com1.z0.glb.clouddn.com/ItChat%2FQRCode2.jpg?imageView/2/w/200/) - -##Screenshot - -![Demo](http://7xrip4.com1.z0.glb.clouddn.com/ItChat%2FDemo2.png?imageView/2/w/300/) - -你可以在[wiki](https://github.com/littlecodersh/ItChat/wiki/Screenshots)看到更多的功能截图 - -##Installation - -可以通过本命令安装依赖库: - -`pip install requests Image` - -将本项目clone到本地安装依赖库后即可直接运行: - -`python ItChat.py` - -本项目基于python 2.7.11开发,使用python 3可能发生异常。 - -##Plugins - -本项目默认开启投票插件与自定义回复插件 - -若需要开启其他插件,可以参照[wiki](https://github.com/littlecodersh/ItChat/wiki/Plugin),或者运行`python PluginTest.py`一键检测插件 - -若需要支持中文文件传输,需要将plugin/config/fields.py文件放入requests包的packages/urllib3下,否则上传的文件将无法下载 - -##Comments - -如果有什么问题或者建议都可以在这个[Issue](https://github.com/littlecodersh/ItChat/issues/1)和我讨论 - -或者也可以在gitter上交流:[![Gitter](https://badges.gitter.im/littlecodersh/ItChat.svg)](https://gitter.im/littlecodersh/ItChat?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge) diff --git a/docs/Robot/Plugin.md b/docs/Robot/Plugin.md deleted file mode 100644 index 830dfa68..00000000 --- a/docs/Robot/Plugin.md +++ /dev/null @@ -1,112 +0,0 @@ -#已有插件介绍 - -该微信机器人提供了完备的接口,可以很容易的加入新的插件。 - -先就两个简单的插件做一个介绍,若想要了解具体如何编写新的插件,可以快速阅读这一节。 - -##vote - -该插件实现了基于用户识别的投票功能,即使机器人下线也不会影响用户的投票数据。 - -###配置 - -* 配置投票选项:在plugin/config/vote.json中更改投票选项 -* 若要更改文字说明,可以在plugin/msgdealers/vote.py中修改 -* 投票记录在storage/account/用户名.db的表vote中 - -##autoreply - -该插件实行了基于正则表达式匹配的自动回复。 - -###配置 - -* 配置自动回复的内容与匹配规则:在plugin/config/autoreply.db中增加或删除 -* 以`@fil@`开头后面接具体路径(一般建议放在storage/upload/文件夹下)可以自动回复文件 -* 以`@img@`开头后面接具体路径(一般建议放在storage/upload/文件夹下)可以自动回复图片 -* 若以文件方式发送图片将不会以缩略图方式收到信息 - -#自定义插件配置 - -##编写插件 - -编写插件并不困难,具体的调用可以参照已有的两个插件。 - -需要注意,文件的相对路径以ItChat.py所在位置为起始。 - -###编写插件的流程 -1. 编写插件、编写插件测试与样例 -1. 更新itchat/robot.py、PluginTest.py、pluginlist.json中的插件与测试文件 -1. 使用`python PluginTest.py`测试插件是否正常工作 - -第一步将在下面具体介绍。 - -其中第二步的配置是指: - -在itchat/robot.py中以`(方法名,方法)`的元组加入插件序列(次序代表优先匹配) - -![setting_itchat/robot.py](http://7xrip4.com1.z0.glb.clouddn.com/ItChat%2FPlugin%2Frobot.py%E4%B8%AD%E8%AE%BE%E7%BD%AE%E6%8F%92%E4%BB%B6.jpg?imageView/2/w/400/) - -在PluginTest.py中加入编写好的测试方法: - -![add_test_into_PluginTest.py](http://7xrip4.com1.z0.glb.clouddn.com/ItChat%2FPlugin%2F%E5%B0%86%E6%B5%8B%E8%AF%95%E5%91%BD%E4%BB%A4%E5%8A%A0%E5%85%A5PluginTest.jpg?imageView/2/w/400/) - -若需要测试,将插件插入PluginTest.py的测试名单中: - -![add_plugin_into_PluginTest.py_pluginlist](http://7xrip4.com1.z0.glb.clouddn.com/ItChat%2FPlugin%2F%E5%B0%86%E6%8F%92%E4%BB%B6%E5%8A%A0%E5%85%A5PluginTest.jpg?imageView/2/w/400) - -在pluginlist.json中加入改插件名字: - -![add_plugin_name_into_pluginlist.json](http://7xrip4.com1.z0.glb.clouddn.com/ItChat%2FPlugin%2Fpluginlist%E9%85%8D%E7%BD%AE.png?imageView/2/w/400/) - -至此第二步配置就完成了。 - -##给出的接口 - -插件需要提供一个方法并通过这个方法返回值。 - -该方法将会获得三个输入,分别为文本内容、全局存储对象、发送人的UserName - -这样就可以基于用户发送过来的消息作出反馈 - -通过全局存储对象,可以获得所有用户的信息与来往消息,下面以获取用户信息为例: - -```python -# 全局存储对象存储为storageClass, 发送人UserName存储为userName -# 更多的方法可以在itchat/storage.py中获取 - -# 获取昵称、唯一标示符 -nickName = storageClass.find_nickname(userName) -pyid = storageClass.find_PYQuanPin(userName) - -# 获取用户个人数据 -status = storageClass.get_dict_of_other(storageClass.get_other(userName)) - -# 存储用户个人数据 -storageClass.update_user(pyid, Other = storageClass.get_str_of_other(status)) -``` - -##返回类型 -* 插件需要返回一个Unicode格式的文本或是False -* 若匹配该插件成功,那么回复用户要通过返回Unicode格式的文本完成 -* 回复内容同样可以以`@fil@`等标示符开头,但请一定记得配置相关测试以免运行出现意外 -* 若匹配插件失败,回复False即可 - -##插件放置位置 -* 插件本身需要被放置于plugin/msgdealers/文件夹中 -* 插件调用的文件内容原则上建议存储在plugin文件夹中 - -![place_for_configs](http://7xrip4.com1.z0.glb.clouddn.com/ItChat%2FPlugin%2F%E9%85%8D%E7%BD%AE%E5%AD%98%E6%94%BE%E5%A4%84.png?imageView/2/w/400) - -##测试相关配置 - -如果涉及到活动用户信息的设置暂时没有编写完成,建议可以基于已有用户信息模拟测试。 - -测试中的异常情况通过`sys_print('WARN', msg)`输出信息,系统将会识别出测试出现了问题。 - -测试中的普通输出可以通过`sys_print`方法输出信息。 - -运行`python PluginTest.py`将会进入交互的测试,你可以测试返回内容是否如你所愿。 - -#结语 - -只是一个简单的介绍,若有错误或是任何建议,我会很高兴收到你的邮件! diff --git a/docs/Robot/Screenshots.md b/docs/Robot/Screenshots.md deleted file mode 100644 index 87119e5b..00000000 --- a/docs/Robot/Screenshots.md +++ /dev/null @@ -1,38 +0,0 @@ -##微信投票 - -![wechat_vote](http://7xrip4.com1.z0.glb.clouddn.com/ItChat%2FScreenshots%2F%E5%BE%AE%E4%BF%A1%E4%B8%AD%E6%96%87%E6%8A%95%E7%A5%A8.png?imageView/2/w/300/) - -投票的选项可以通过更改`plugin/config/vote.json`修改,但记得修改过以后使用`python PluginTest.py`测试是否能顺利启动哦! - -![plugin/config/vote.json](http://7xrip4.com1.z0.glb.clouddn.com/ItChat%2FScreenshots%2F%E8%87%AA%E5%AE%9A%E4%B9%89%E6%8A%95%E7%A5%A8%E5%86%85%E5%AE%B9.png) - -##自定义回复 - -![plain_text_autoreply](http://7xrip4.com1.z0.glb.clouddn.com/ItChat%2FScreenshots%2F%E5%BE%AE%E4%BF%A1%E8%87%AA%E5%AE%9A%E4%B9%89%E5%9B%9E%E5%A4%8D.png?imageView/2/w/300/) - -文件也是自定义回复的一种哦! - -![file_autoreply](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/) - -回复的规则可以通过更改`plugin/config/autoreply.db`修改,但记得修改或以后使用`python PluginTest.py`看看新规则是否能正常回复哦! - -![plugin/config/autoreply.db](http://7xrip4.com1.z0.glb.clouddn.com/ItChat%2FScreenshots%2F%E6%95%B0%E6%8D%AE%E5%BA%93%E8%87%AA%E5%AE%9A%E4%B9%89%E5%9B%9E%E5%A4%8D.png) -##登录界面 - -![login_page](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/300/) - -和登陆网页端微信一样,扫码即可登录。 - -##命令行微信 - -![cmd_wechat_chat](http://7xrip4.com1.z0.glb.clouddn.com/ItChat%2FScreenshots%2F%E5%91%BD%E4%BB%A4%E8%A1%8C%E8%81%8A%E5%A4%A9%E7%AA%97%E5%8F%A3.jpg) - -命令行微信的输入操作经过特殊的处理,显示输出的同时不会影响正在进行的输入,而且三平台支持。 - -![change_to_cmd_wechat](http://7xrip4.com1.z0.glb.clouddn.com/ItChat%2FScreenshots%2F%E5%88%87%E6%8D%A2%E5%88%B0%E5%91%BD%E4%BB%A4%E8%A1%8C%E5%BE%AE%E4%BF%A1.jpg) - -通过修改`ItChat.py`中第七行的True为False,可以登入命令行微信。 - -##其他 - -其他功能建议自己下载或者加测试号好友自己尝试啦! diff --git a/docs/index.md b/docs/index.md index df1c5def..486b4423 100644 --- a/docs/index.md +++ b/docs/index.md @@ -2,18 +2,14 @@ [![Gitter][gitter-picture]][gitter] ![py27][py27] ![py35][py35] [English version][english-version] -itchat是一个开源的微信个人号接口,使用他你可以轻松的通过命令行使用个人微信号。 +itchat是一个开源的微信个人号接口,使用python调用微信从未如此简单。 使用不到三十行的代码,你就可以完成一个能够处理所有信息的微信机器人。 -当然,该api的使用远不止一个机器人。 +当然,该api的使用远不止一个机器人,更多的功能等着你来发现。 如今微信已经成为了个人社交的很大一部分,希望这个项目能够帮助你扩展你的个人的微信号、方便自己的生活。 -## Documents - -你可以在[这里][document]获取api的使用帮助。 - ## Installation 可以通过本命令安装itchat: @@ -24,27 +20,59 @@ pip install itchat ## Simple uses +有了itchat,如果你想要回复发给自己的文本消息,只需要这样: + +```python +import itchat + +@itcaht.msg_register(itchat.content.TEXT) +def text_reply(msg): + itchat.send(msg['Text'], msg['FromUserName']) + +itchat.auto_login() +itchat.run() +``` + +一些进阶应用可以在Advanced uses中看到,或者你也可以阅览[文档][document]。 + + + +## Have a try + +这是一个基于这一项目的[开源小机器人][robot-source-code],百闻不如一见,有兴趣可以尝试一下。 + +![QRCode][robot-qr] + +## Screenshots + +![file-autoreply][robot-demo-file] ![login-page][robot-demo-login] + +## Advanced uses + +### 各类型消息的注册 + 通过如下代码,微信已经可以就日常的各种信息进行获取与回复。 ```python #coding=utf8 import itchat, time +from itchat.content import * -@itchat.msg_register(['Text', 'Map', 'Card', 'Note', 'Sharing']) +@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']) +@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') +@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) +@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']) @@ -53,8 +81,6 @@ itchat.auto_login(True) itchat.run() ``` -## Advanced uses - ### 命令行二维码 通过以下命令可以在登陆的时候使用命令行显示二维码: @@ -86,7 +112,7 @@ itchat.auto_login(hotReload=True) ### 用户搜索 -使用`get_friends`方法可以搜索用户,有四种搜索方式: +使用`search_friends`方法可以搜索用户,有四种搜索方式: 1. 仅获取自己的用户信息 2. 获取特定`UserName`的用户信息 3. 获取备注、微信号、昵称中的任何一项等于`name`键值的用户 @@ -96,17 +122,19 @@ itchat.auto_login(hotReload=True) ```python # 获取自己的用户信息,返回自己的属性字典 -itchat.get_friends() +itchat.search_friends() # 获取特定UserName的用户信息 -itchat.get_friends(userName='@abcdefg1234567') +itchat.search_friends(userName='@abcdefg1234567') # 获取任何一项等于name键值的用户 -itchat.get_friends(name='littlecodersh') +itchat.search_friends(name='littlecodersh') # 获取分别对应相应键值的用户 -itchat.get_friends(wechatAccount='littlecodersh') +itchat.search_friends(wechatAccount='littlecodersh') # 三、四项功能可以一同使用 -itchat.get_friends(name='LittleCoder机器人', wechatAccount='littlecodersh') +itchat.search_friends(name='LittleCoder机器人', wechatAccount='littlecodersh') ``` +关于公众号、群聊的获取与搜索在文档中有更加详细的介绍。 + ### 附件的下载与发送 itchat的附件下载方法存储在msg的Text键中。 @@ -132,16 +160,6 @@ def download_files(msg): f.write(msg['Text']()) ``` -## Have a try - -这是一个基于这一项目的[开源小机器人][robot-source-code],百闻不如一见,有兴趣可以尝试一下。 - -![QRCode][robot-qr] - -## Screenshots - -![file-autoreply][robot-demo-file] ![login-page][robot-demo-login] - ## FAQ Q: 为什么中文的文件没有办法上传? @@ -158,9 +176,9 @@ A: 有两种方式:发送、接受自己UserName的消息;发送接收文件 ## Author -[LittleCoder][littlecodersh]: 整体构架及完成Python2版本。 +[LittleCoder][littlecodersh]: 整体构架及完成Python2 Python3版本。 -[Chyroc][Chyroc]: 完成Python3版本。 +[Chyroc][Chyroc]: 完成第一版本的Python3构架。 ## See also diff --git a/itchat/__init__.py b/itchat/__init__.py index 3f2bce22..f76663cf 100644 --- a/itchat/__init__.py +++ b/itchat/__init__.py @@ -1,7 +1,7 @@ import time from .client import client -__version__ = '1.1.3' +__version__ = '1.1.5' __client = client() def auto_login(hotReload=False, statusStorageDir='itchat.pkl', enableCmdQR=False): @@ -76,7 +76,7 @@ def configured_reply(): replyFn = __functionDict.get(msg['Type'], __functionDict['GeneralReply']) if replyFn: send(replyFn(msg), msg.get('FromUserName')) -def msg_register(_type = None, *args, **kwargs): +def msg_register(_type=None, *args, **kwargs): if hasattr(_type, '__call__'): __functionDict['GeneralReply'] = _type return configured_reply diff --git a/itchat/content.py b/itchat/content.py new file mode 100644 index 00000000..3fc68ab9 --- /dev/null +++ b/itchat/content.py @@ -0,0 +1,10 @@ +TEXT = 'Text' +MAP = 'Map' +CARD = 'Card' +NOTE = 'Note' +SHARING = 'Sharing' +PICTURE = 'Picture' +RECORDING = 'Recording' +ATTACHMENT = 'Attachment' +VIDEO = 'Video' +FRIENDS = 'Friends'