使用itchat(用python开发的一段代码)来实现微信自动功能。
itchat项目代码地址:https://itchat.readthedocs.io/zh/latest/
安装完python后,比如安装目录为d:/python27
在scripts目录下查看是否有pip.exe, 如果没有安装pip
安装itchat
pip install itchat
向文件助手发一条文本消息:
在python命令窗口执行或在cmd下执行python xxx.py(将代码存入xxx.py)
import itchat
itchat.auto_login()
itchat.send(‘Hello, filehelper’, toUserName=’filehelper’)
用微信扫描二维码。
向所有好友发一段文本消息:
#coding=utf8
import itchat, time
SINCERE_WISH = u’hi, %s’
REAL_SINCERE_WISH = u’hi, %s’
def send_wishes():
friendList = itchat.get_friends(update=True)[1:]
for friend in friendList:
# 如果不是演示目的,把下面的方法改为itchat.send即可
itchat.send(SINCERE_WISH % (friend\['DisplayName'\]
or friend\['NickName'\]), friend\['UserName'\])
time.sleep(.5)
def send_special_wishes(chatroomName=’wishgroup’):
&nbsnbsp; itchat.get_chatrooms(update=True)
chatrooms = itchat.search_chatrooms(name=chatroomName)
if chatrooms is None:
itchat.send(u’没有找到群聊:’ + chatroomName)
else:
chatroom = itchat.update_chatroom(chatrooms[0][‘UserName’])
for friend in chatroom[‘MemberList’]:
friend = itchat.search_friends(userName=friend[‘UserName’])
# 如果不是演示目的,把下面的方法改为itchat.send即可
itchat.send(REAL\_SINCERE\_WISH % (friend\['DisplayName'\]
or friend\['NickName'\]), friend\['UserName'\])
time.sleep(.5)
itchat.auto_login(True)
send_wishes()
send_special_wishes()
自动回复
#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‘@%su2005I received: %s’ % (msg[‘ActualNickName’], msg[‘Content’]), msg[‘FromUserName’])
itchat.auto_login(enableCmdQR=True)
itchat.run()
wxBot里有一些具体的案例,可查看 https://github.com/leegtang/wxBot