10

一个有趣的开源项目:itchat使用介绍

 2 years ago
source link: https://allenwind.github.io/blog/5694/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
Mr.Feng Blog

NLP、深度学习、机器学习、Python、Go

一个有趣的开源项目:itchat使用介绍

推荐一下Python的微信客户端,可以做很有趣的微信机器人。

  • itchat.login(enableCmdQR=False)
  • itchat.auto_login(enableCmdQR=-1) 背景为白色
  • itchat.auto_login(hotReload=True, loginCallback, exitCallback)退出程序后暂存登陆状态

获取本号的微信消息

回复发给自己的文本消息

@itchat.msg_register(itchat.content.TEXT)
def do_text(msg):
print(msg["Text"])

itchat.auto_login()
itchat.run()

发送给文件传输助手和本号都会接收。

可以接收了类型包括:

‘ATTACHMENT’, ‘CARD’, ‘FRIENDS’, ‘INCOME_MSG’, ‘MAP’, ‘NOTE’, ‘PICTURE’, ‘RECORDING’, ‘SHARING’, ‘SYSTEM’, ‘TEXT’, ‘VIDEO’, ‘VOICE’

  • 图片对应 itchat.content.PICTURE
  • 语音对应 itchat.content.RECORDING
  • 名片对应 itchat.content.CARD

itchat函数:

  • itchat.send(msg, toUserName, mediaId)
  • send_file
  • send_image
  • send_msg
  • send_raw_msg
  • send_video

返回结果示例:

<ItchatReturnValue: {'BaseResponse': {'ErrMsg': '返回值不带BaseResponse', 'Ret': -1000, 'RawMsg': 'no BaseResponse in raw response'}}>

toUserName是微信号,如果传送给文件传输助手:

itchat.send('测试消息', 'filehelper')

返回信息:

<ItchatReturnValue: {'BaseResponse': {'RawMsg': '请求成功', 'ErrMsg': '请求成功', 'Ret': 0}, 'LocalID': '14958673103729', 'MsgID': '6087828379088483558'}>

注册各类消息

# 自动回复

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的FileName键
# 附件下载方法存储在msg的Text键中
msg['Text'](msg['FileName'])
return '@%s@%s' % ({'Picture': 'img', 'Video': 'vid'}.get(msg['Type'], 'fil'), msg['FileName'])

# 仅想要读取二进制串进行进一步处理可以不传入参数

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


# 自动添加好友

@itchat.msg_register(FRIENDS)
def add_friend(msg):
itchat.add_friend(**msg['Text']) ## 该操作会自动将新好友的消息录入,不需要重载通讯录
itchat.send_msg('Hello new friend', msg['RecommendInfo']['UserName'])

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

itchat.auto_login(True)
itchat.run()

Search Friends

四种搜索方式

  1. 仅获取自己的用户信息
  2. 获取特定UserName的用户信息
  3. 获取备注、微信号、昵称中的任何一项等于name键值的用户
  4. 获取备注、微信号、昵称分别等于相应键值的用户
# 获取自己的用户信息,返回自己的属性字典
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')

搜索到的结果以列表返回,列表中每个item可以看作是用户对象,用户对象包括如下内容:

  • send_file
  • send_image
  • send_msg
  • send_raw_msg
  • send_video
instance = itchat.new_instance()
instance.auto_login(hotReload=True, statusStorageDir='instace.pkl')

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

instance.run()

以上的这些功能,可以做一个很有意思的微信自动化机器人。

转载请包括本文地址:https://allenwind.github.io/blog/5694
更多文章请参考:https://allenwind.github.io/blog/archives/


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK