#itchat使用步骤
##1. 登陆
import itchat
itchat.auto_login() 会生成一个登陆二维码,如果想持久连接需要使用添加参数 hotReload=true
##2. 寻找联系人
users=itchat.search_friends(name='你对他的备注姓名')
print(users) #返回的是一个list,list中是一个字典(有关联系人的信息)
username=users[0]('UserName')#获得联系人的'UserName'属性对应的value值
-同理如果想获取
##3. 常用API(接口,函数,方法) 传送门
微信成员管理 (chatroom:群聊室 mps:公众号)
def logout(self):
def get_friends(self, update=False):
def get_chatrooms(self, update=False, contactOnly=False):
def get_mps(self, update=False):
def add_friend(self, userName, status=2, verifyContent='', autoUpdate=True):
def search_friends(self, name=None, userName=None, remarkName=None, nickName=None,wechatAccount=None):
def search_chatrooms(self, name=None, userName=None):
def search_mps(self, name=None, userName=None):
数据发送和接收
def msg_register(self, msgType,
isFriendChat=False, isGroupChat=False, isMpChat=False): #装饰器decorator
''' a decorator constructor
return a specific decorator based on information given
'''
raise NotImplementedError()
def send_msg(self, msg='Test Message', toUserName=None):
''' send plain text message
for options
- msg: should be unicode if there's non-ascii words in msg
- toUserName: 'UserName' key of friend dict
it is defined in components/messages.py
'''
raise NotImplementedError()
def send_file(self, fileDir, toUserName=None, mediaId=None):
def send_image(self, fileDir, toUserName=None, mediaId=None):
def send_video(self, fileDir=None, toUserName=None, mediaId=None):
def send(self, msg, toUserName=None, mediaId=None): #常用
函数中msg:
是如下格式:
{
"FromUserName": "",
"ToUserName": "",
"Content": "",
"StatusNotifyUserName": "",
"ImgWidth": 0,
"PlayLength": 0,
"RecommendInfo": {},
"StatusNotifyCode": 0,
"NewMsgId": "",
"Status": 0,
"VoiceLength": 0,
"ForwardFlag": 0,
"AppMsgType": 0,
"Ticket": "",
"AppInfo": {},
"Url": "",
"ImgStatus": 0,
"MsgType": 0,
"ImgHeight": 0,
"MediaId": "",
"MsgId": "",
"FileName": "",
"HasProductId": 0,
"FileSize": "",
"CreateTime": 0,
"SubMsgType": 0
}
##4. 示例代码
##5. 相关学习链接