Skip to content

API for Wechat. 微信个人号接口(支持文件、图片上下载)、微信机器人及命令行微信。三十行即可自定义个人号机器人。

License

Notifications You must be signed in to change notification settings

kaitoulee/ItChat

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

79 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

itchat

Gitter python python English version

itchat是一个开源的微信个人号接口,使用他你可以轻松的通过命令行使用个人微信号。

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

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

Documents

你可以在这里获取api的使用帮助。

Installation

可以通过本命令安装itchat:

pip install itchat

Simple uses

通过如下代码,微信已经可以就日常的各种信息进行获取与回复。

#coding=utf8
import itchat, time

@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()

Advanced uses

命令行二维码

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

itchat.auto_login(enableCmdQR = True)

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

# 如部分的linux系统,块字符的宽度为一个字符(正常应为两字符),故赋值为2
itchat.auto_login(enableCmdQR = 2)

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

itchat.auto_login(enableCmdQR = -1)

退出程序后暂存登陆状态

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

itchat.auto_login(hotReload = True)

用户搜索

使用get_friends方法可以搜索用户,有四种搜索方式:

  1. 仅获取自己的用户信息
  2. 获取特定UserName的用户信息
  3. 获取备注、微信号、昵称中的任何一项等于name键值的用户
  4. 获取备注、微信号、昵称分别等于相应键值的用户

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

# 获取自己的用户信息,返回自己的属性字典
itchat.get_friends()
# 获取特定UserName的用户信息
itchat.get_friends(userName = '@abcdefg1234567')
# 获取任何一项等于name键值的用户
itchat.get_friends(name = 'littlecodersh')
# 获取分别对应相应键值的用户
itchat.get_friends(wechatAccount = 'littlecodersh')
# 三、四项功能可以一同使用
itchat.get_friends(name = 'LittleCoder机器人', wechatAccount = 'littlecodersh')

附件的下载与发送

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

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

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

@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']

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

@itchat.msg_register(['Picture', 'Recording', 'Attachment', 'Video'])
def download_files(msg):
    with open(msg['FileName'], 'wb') as f:
        f.write(msg['Text']())

Have a try

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

QRCode

Screenshots

file_autoreply login_page

FAQ

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

A: 这是由于requests的编码问题导致的。若需要支持中文文件传输,将fields.py文件放入requests包的packages/urllib3下即可

Q: 为什么我在设定了itchat.auto_login()enableCmdQRTrue后还是没有办法在命令行显示二维码?

A: 这是由于没有安装可选的包pillow,可以使用右边的命令安装:pip install pillow

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

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

Author

LittleCoder: 整体构架及完成Python2版本。

Chyroc: 完成Python3版本。

See also

liuwons/wxBot: 类似的基于Python的微信机器人

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

Comments

如果有什么问题或者建议都可以在这个Issue和我讨论

或者也可以在gitter上交流:Gitter

About

API for Wechat. 微信个人号接口(支持文件、图片上下载)、微信机器人及命令行微信。三十行即可自定义个人号机器人。

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 100.0%