index.md 9.5 KB
Newer Older
L
LittleCoder 已提交
1 2
# itchat

3
![py27][py27] ![py35][py35]
L
LittleCoder 已提交
4

L
LittleCoder 已提交
5
itchat是一个开源的微信个人号接口,使用python调用微信从未如此简单。
L
LittleCoder 已提交
6

L
LittleCoder 已提交
7
使用不到三十行的代码,你就可以完成一个能够处理所有信息的微信机器人。
L
LittleCoder 已提交
8

9
当然,该api的使用远不止一个机器人,更多的功能等着你来发现,比如[这些][tutorial2]
L
LittleCoder 已提交
10

11 12
该接口与公众号接口[itchatmp][itchatmp]共享类似的操作方式,学习一次掌握两个工具。

13
如今微信已经成为了个人社交的很大一部分,希望这个项目能够帮助你扩展你的个人的微信号、方便自己的生活。
L
LittleCoder 已提交
14

15
## 安装
L
LittleCoder 已提交
16 17 18 19 20 21 22

可以通过本命令安装itchat:

```python
pip install itchat
```

23
## 简单入门实例
L
LittleCoder 已提交
24

25 26 27 28 29 30 31 32 33 34 35
有了itchat,如果你想要给文件传输助手发一条信息,只需要这样:

```python
import itchat

itchat.auto_login()

itchat.send('Hello, filehelper', toUserName='filehelper')
```

如果你想要回复发给自己的文本消息,只需要这样:
L
LittleCoder 已提交
36 37 38 39

```python
import itchat

40
@itchat.msg_register(itchat.content.TEXT)
L
LittleCoder 已提交
41
def text_reply(msg):
L
LittleCoder 已提交
42
    return msg.text
L
LittleCoder 已提交
43 44 45 46 47

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

48
一些进阶应用可以在下面的开源机器人的源码和进阶应用中看到,或者你也可以阅览[文档][document]
L
LittleCoder 已提交
49

50
## 试一试
L
LittleCoder 已提交
51 52 53 54 55

这是一个基于这一项目的[开源小机器人][robot-source-code],百闻不如一见,有兴趣可以尝试一下。

![QRCode][robot-qr]

56
## 截屏
L
LittleCoder 已提交
57 58 59

![file-autoreply][robot-demo-file] ![login-page][robot-demo-login]

60
## 进阶应用
L
LittleCoder 已提交
61

L
LittleCoder 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
### 特殊的字典使用方式

通过打印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!')
```

L
LittleCoder 已提交
84 85
### 各类型消息的注册

L
LittleCoder 已提交
86 87 88 89
通过如下代码,微信已经可以就日常的各种信息进行获取与回复。

```python
import itchat, time
L
LittleCoder 已提交
90
from itchat.content import *
L
LittleCoder 已提交
91

L
LittleCoder 已提交
92
@itchat.msg_register([TEXT, MAP, CARD, NOTE, SHARING])
L
LittleCoder 已提交
93
def text_reply(msg):
L
LittleCoder 已提交
94
    msg.user.send('%s: %s' % (msg.type, msg.text))
L
LittleCoder 已提交
95

L
LittleCoder 已提交
96
@itchat.msg_register([PICTURE, RECORDING, ATTACHMENT, VIDEO])
L
LittleCoder 已提交
97
def download_files(msg):
L
LittleCoder 已提交
98 99 100 101 102
    msg.download(msg.fileName)
    typeSymbol = {
        PICTURE: 'img',
        VIDEO: 'vid', }.get(msg.type, 'fil')
    return '@%s@%s' % (typeSymbol, msg.fileName)
L
LittleCoder 已提交
103

L
LittleCoder 已提交
104
@itchat.msg_register(FRIENDS)
L
LittleCoder 已提交
105
def add_friend(msg):
L
LittleCoder 已提交
106 107
    msg.user.verify()
    msg.user.send('Nice to meet you!')
L
LittleCoder 已提交
108

L
LittleCoder 已提交
109
@itchat.msg_register(TEXT, isGroupChat=True)
L
LittleCoder 已提交
110
def text_reply(msg):
L
LittleCoder 已提交
111 112 113
    if msg.isAt:
        msg.user.send(u'@%s\u2005I received: %s' % (
            msg.actualNickName, msg.text))
L
LittleCoder 已提交
114

115
itchat.auto_login(True)
L
LittleCoder 已提交
116
itchat.run(True)
L
LittleCoder 已提交
117 118
```

119 120 121 122 123
### 命令行二维码

通过以下命令可以在登陆的时候使用命令行显示二维码:

```python
124
itchat.auto_login(enableCmdQR=True)
125 126 127 128 129 130
```

部分系统可能字幅宽度有出入,可以通过将enableCmdQR赋值为特定的倍数进行调整:

```python
# 如部分的linux系统,块字符的宽度为一个字符(正常应为两字符),故赋值为2
131 132 133 134 135 136 137
itchat.auto_login(enableCmdQR=2)
```

默认控制台背景色为暗色(黑色),若背景色为浅色(白色),可以将enableCmdQR赋值为负值:

```python
itchat.auto_login(enableCmdQR=-1)
138 139 140 141 142 143 144
```

### 退出程序后暂存登陆状态

通过如下命令登陆,即使程序关闭,一定时间内重新开启也可以不用重新扫码。

```python
145
itchat.auto_login(hotReload=True)
146 147
```

148 149
### 用户搜索

L
LittleCoder 已提交
150
使用`search_friends`方法可以搜索用户,有四种搜索方式:
151 152 153 154 155 156 157 158 159
1. 仅获取自己的用户信息
2. 获取特定`UserName`的用户信息
3. 获取备注、微信号、昵称中的任何一项等于`name`键值的用户
4. 获取备注、微信号、昵称分别等于相应键值的用户

其中三、四项可以一同使用,下面是示例程序:

```python
# 获取自己的用户信息,返回自己的属性字典
L
LittleCoder 已提交
160
itchat.search_friends()
161
# 获取特定UserName的用户信息
L
LittleCoder 已提交
162
itchat.search_friends(userName='@abcdefg1234567')
163
# 获取任何一项等于name键值的用户
L
LittleCoder 已提交
164
itchat.search_friends(name='littlecodersh')
165
# 获取分别对应相应键值的用户
L
LittleCoder 已提交
166
itchat.search_friends(wechatAccount='littlecodersh')
167
# 三、四项功能可以一同使用
L
LittleCoder 已提交
168
itchat.search_friends(name='LittleCoder机器人', wechatAccount='littlecodersh')
169 170
```

L
LittleCoder 已提交
171 172
关于公众号、群聊的获取与搜索在文档中有更加详细的介绍。

173 174 175 176
### 附件的下载与发送

itchat的附件下载方法存储在msg的Text键中。

177 178
发送的文件的文件名(图片给出的默认文件名)都存储在msg的FileName键中。

179 180 181
下载方法接受一个可用的位置参数(包括文件名),并将文件相应的存储。

```python
L
LittleCoder 已提交
182
@itchat.msg_register([PICTURE, RECORDING, ATTACHMENT, VIDEO])
183
def download_files(msg):
L
LittleCoder 已提交
184 185 186 187 188
    msg.download(msg.fileName)
    itchat.send('@%s@%s' % (
        'img' if msg['Type'] == 'Picture' else 'fil', msg['FileName']),
        msg['FromUserName'])
    return '%s received' % msg['Type']
189 190
```

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

```python
L
LittleCoder 已提交
194
@itchat.msg_register([PICTURE, RECORDING, ATTACHMENT, VIDEO])
195
def download_files(msg):
L
LittleCoder 已提交
196 197
    with open(msg.fileName, 'wb') as f:
        f.write(msg.download())
198 199
```

200
### 用户多开
L
LittleCoder 已提交
201

202
使用如下命令可以完成多开的操作:
L
LittleCoder 已提交
203

204 205
```python
import itchat
206

207 208
newInstance = itchat.new_instance()
newInstance.auto_login(hotReload=True, statusStorageDir='newInstance.pkl')
209

210 211
@newInstance.msg_register(TEXT)
def reply(msg):
L
LittleCoder 已提交
212
    return msg.text
213

214 215
newInstance.run()
```
216

217
### 退出及登陆完成后调用特定方法
218

219
登陆完成后的方法需要赋值在`loginCallback`中。
220

221
而退出后的方法需要赋值在`exitCallback`中。
L
LittleCoder 已提交
222

223 224
```python
import time
L
LittleCoder 已提交
225

226
import itchat
L
LittleCoder 已提交
227

228 229 230 231
def lc():
    print('finish login')
def ec():
    print('exit')
L
LittleCoder 已提交
232

233 234 235 236
itchat.auto_login(loginCallback=lc, exitCallback=ec)
time.sleep(3)
itchat.logout()
```
L
LittleCoder 已提交
237

238
若不设置loginCallback的值,则将会自动删除二维码图片并清空命令行显示。
L
LittleCoder 已提交
239

240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
## 常见问题与解答

Q: 为什么中文的文件没有办法上传?

A: 这是由于`requests`的编码问题导致的。若需要支持中文文件传输,将[fields.py][fields.py-2](py3版本见[这里][fields.py-3])文件放入requests包的packages/urllib3下即可

Q: 如何通过这个包将自己的微信号变为控制器?

A: 有两种方式:发送、接受自己UserName的消息;发送接收文件传输助手(filehelper)的消息

Q: 为什么我发送信息的时候部分信息没有成功发出来?

A: 有些账号是天生无法给自己的账号发送信息的,建议使用`filehelper`代替。

## 作者

[LittleCoder][littlecodersh]: 构架及维护Python2 Python3版本。

[tempdban][tempdban]: 协议、构架及日常维护。

[Chyroc][Chyroc]: 完成第一版本的Python3构架。

## 类似项目

L
LittleCoder 已提交
264 265
[youfou/wxpy][youfou-wxpy]: 优秀的api包装和配套插件,微信机器人/优雅的微信个人号API

266 267 268 269 270 271
[liuwons/wxBot][liuwons-wxBot]: 类似的基于Python的微信机器人

[zixia/wechaty][zixia-wechaty]: 基于Javascript(ES6)的微信个人账号机器人NodeJS框架/库

[sjdy521/Mojo-Weixin][Mojo-Weixin]: 使用Perl语言编写的微信客户端框架,可通过插件提供基于HTTP协议的api接口供其他语言调用

L
LittleCoder 已提交
272 273
[HanSon/vbot][HanSon-vbot]: 基于PHP7的微信个人号机器人,通过实现匿名函数可以方便地实现各种自定义的功能

274
## 问题和建议
L
LittleCoder 已提交
275

276 277 278 279
如果有什么问题或者建议都可以在这个[Issue][issue#1]和我讨论

或者也可以在gitter上交流:[![Gitter][gitter-picture]][gitter]

280 281
当然也可以加入我们新建的QQ群讨论:549762872

282 283 284 285 286
[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
287
[itchatmp]: https://github.com/littlecodersh/itchatmp
288
[document]: https://itchat.readthedocs.org/zh/latest/
289
[tutorial2]: http://python.jobbole.com/86532/
290 291 292 293 294 295 296
[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
297
[tempdban]: https://github.com/tempdban
298
[Chyroc]: https://github.com/Chyroc
L
LittleCoder 已提交
299
[youfou-wxpy]: https://github.com/youfou/wxpy
300 301
[liuwons-wxBot]: https://github.com/liuwons/wxBot
[zixia-wechaty]: https://github.com/zixia/wechaty
302
[Mojo-Weixin]: https://github.com/sjdy521/Mojo-Weixin
L
LittleCoder 已提交
303
[HanSon-vbot]: https://github.com/hanson/vbot
304
[issue#1]: https://github.com/littlecodersh/ItChat/issues/1