itchat入门
import itchat
itchat.auto_login()
Getting uuid of QR code.
Downloading QR code.
Please scan the QR code to log in.
Please press confirm on your phone.
Loading the contact, this may take a little while.
Login successfully as 可愛いカエル
itchat.search_chatrooms(name='爸爸带你们上铂金')
[<Chatroom: {'KeyWord': '', 'Sex': 0, 'AttrStatus': 0, 'HeadImgUpdateFlag': 1, 'OwnerUin': 0, 'HeadImgUrl': '/cgi-bin/mmwebwx-bin/webwxgetheadimg?seq=0&username=@@32ca948d6e77884429f74fae414834d3b268b3093e4d699195b740ced4b96ffd&skey=@crypt_660631a3_d0505e6010be29679aad3949b6425c10', 'Province': '', 'ChatRoomId': 0, 'Statues': 0, 'PYInitial': 'BBDNMSBJ', 'StarFriend': 0, 'RemarkPYQuanPin': '', 'PYQuanPin': 'babadainimenshangbojin', 'ContactFlag': 3, 'City': '', 'EncryChatRoomId': '', 'UniFriend': 0, 'HideInputBarFlag': 0, 'RemarkPYInitial': '', 'ContactType': 0, 'Alias': '', 'RemarkName': '', 'Signature': '', 'MemberList': <ContactList: [<ChatroomMember: {'KeyWord': '', 'AttrStatus': 98749, 'DisplayName': '', 'NickName': '可愛いカエル', 'MemberList': <ContactList: []>, 'RemarkPYQuanPin': '', 'PYQuanPin': '', 'Uin': 0, 'UserName': '@bf1dd4a9dd3205bfb8887305421116989795f89bf1bc8b1c9d3ab9facd54ed52', 'RemarkPYInitial': '', 'PYInitial': '', 'MemberStatus': 0}>, <ChatroomMember: {'KeyWord': '', 'AttrStatus': 4194407, 'DisplayName': '张澍聪', 'NickName': '清木心', 'MemberList': <ContactList: []>, 'RemarkPYQuanPin': '', 'PYQuanPin': '', 'Uin': 0, 'UserName': '@eacca9dc84929e5792379fa0212b177dace9cb2de472c51bcc6aefb104b9e76a', 'RemarkPYInitial': '', 'PYInitial': '', 'MemberStatus': 0}>, <ChatroomMember: {'KeyWord': '', 'AttrStatus': 33661031, 'DisplayName': '你们的爸爸', 'NickName': 'Yalen_Fan', 'MemberList': <ContactList: []>, 'RemarkPYQuanPin': '', 'PYQuanPin': '', 'Uin': 0, 'UserName': '@9bd31ad056a8512e4da56d4b1f115fa3', 'RemarkPYInitial': '', 'PYInitial': '', 'MemberStatus': 0}>]>, 'Self': <ChatroomMember: {'KeyWord': '', 'AttrStatus': 98749, 'DisplayName': '', 'NickName': '可愛いカエル', 'MemberList': <ContactList: []>, 'RemarkPYQuanPin': '', 'PYQuanPin': '', 'Uin': 0, 'UserName': '@bf1dd4a9dd3205bfb8887305421116989795f89bf1bc8b1c9d3ab9facd54ed52', 'RemarkPYInitial': '', 'PYInitial': '', 'MemberStatus': 0}>, 'AppAccountFlag': 0, 'NickName': '爸爸带你们上铂金', 'ChatRoomOwner': '@9bd31ad056a8512e4da56d4b1f115fa3', 'VerifyFlag': 0, 'IsAdmin': None, 'IsOwner': 0, 'Uin': 0, 'DisplayName': '', 'UserName': '@@32ca948d6e77884429f74fae414834d3b268b3093e4d699195b740ced4b96ffd', 'MemberCount': 3, 'SnsFlag': 0}>]
itchat.search_chatrooms(name='爸爸带你们上铂金')[0]['UserName']
'@@32ca948d6e77884429f74fae414834d3b268b3093e4d699195b740ced4b96ffd'
itchat.send('我6号回去',itchat.search_chatrooms(name='爸爸带你们上铂金')[0]['UserName'])
<ItchatReturnValue: {'BaseResponse': {'ErrMsg': '请求成功', 'Ret': 0, 'RawMsg': '请求成功'}, 'LocalID': '15045289085987', 'MsgID': '2194463587752136458'}>
LOG OUT!
itchat.send('a',itchat.get_mps()[1]['UserName'])
<ItchatReturnValue: {'MsgID': '8968282241556872924', 'BaseResponse': {'RawMsg': '请求成功', 'Ret': 0, 'ErrMsg': '请求成功'}, 'LocalID': '15043467909534'}>
itchat.search_friends(name='duli hanqiu')[0]
<User: {'KeyWord': '', 'Sex': 1, 'AttrStatus': 103009, 'OwnerUin': 0, 'Province': '上海', 'ChatRoomId': 0, 'Statues': 0, 'DisplayName': '', 'Signature': '', 'RemarkPYQuanPin': '', 'PYQuanPin': 'dulihanqiu', 'ContactFlag': 1, 'City': '', 'EncryChatRoomId': '', 'HideInputBarFlag': 0, 'RemarkPYInitial': '', 'Alias': '', 'RemarkName': '', 'UniFriend': 0, 'PYInitial': 'DULIHANQIU', 'MemberList': <ContactList: []>, 'UserName': '@c3933e2b5674104ecb7793b06cc73076f89deff1d0c8cb508c56c09f17719841', 'AppAccountFlag': 0, 'NickName': 'duli hanqiu', 'VerifyFlag': 0, 'StarFriend': 0, 'MemberCount': 0, 'Uin': 0, 'HeadImgUrl': '/cgi-bin/mmwebwx-bin/webwxgeticon?seq=666010209&username=@c3933e2b5674104ecb7793b06cc73076f89deff1d0c8cb508c56c09f17719841&skey=@crypt_660631a3_d0505e6010be29679aad3949b6425c10', 'IsOwner': 0, 'SnsFlag': 1}>
@itchat.msg_register(itchat.content.TEXT,isMpChat=True )
def text_reply(msg):
return msg['FromUserName']
itchat.run()
Start auto replying.
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\itchat\components\register.py", line 62, in configured_reply
self.send(r, msg.get('FromUserName'))
File "C:\Python27\lib\site-packages\itchat\components\messages.py", line 508, in send
r = self.send_msg(msg, toUserName)
File "C:\Python27\lib\site-packages\itchat\components\messages.py", line 279, in send_msg
r = self.send_raw_msg(1, msg, toUserName)
File "C:\Python27\lib\site-packages\itchat\components\messages.py", line 274, in send_raw_msg
data=json.dumps(data, ensure_ascii=False).encode('utf8'))
File "C:\Python27\lib\site-packages\requests\sessions.py", line 555, in post
return self.request('POST', url, data=data, json=json, **kwargs)
File "C:\Python27\lib\site-packages\requests\sessions.py", line 508, in request
resp = self.send(prep, **send_kwargs)
File "C:\Python27\lib\site-packages\requests\sessions.py", line 618, in send
r = adapter.send(request, **kwargs)
File "C:\Python27\lib\site-packages\requests\adapters.py", line 440, in send
timeout=timeout
File "C:\Python27\lib\site-packages\urllib3\connectionpool.py", line 601, in urlopen
chunked=chunked)
File "C:\Python27\lib\site-packages\urllib3\connectionpool.py", line 380, in _make_request
httplib_response = conn.getresponse(buffering=True)
File "C:\Python27\lib\httplib.py", line 1121, in getresponse
response.begin()
File "C:\Python27\lib\httplib.py", line 438, in begin
version, status, reason = self._read_status()
File "C:\Python27\lib\httplib.py", line 394, in _read_status
line = self.fp.readline(_MAXLINE + 1)
File "C:\Python27\lib\socket.py", line 480, in readline
data = self._sock.recv(self._rbufsize)
File "C:\Python27\lib\ssl.py", line 766, in recv
return self.read(buflen)
File "C:\Python27\lib\ssl.py", line 653, in read
v = self._sslobj.read(len)
KeyboardInterrupt
import itchat
from itchat.content import TEXT,PICTURE
#itchat的附件下载方法存储在msg的Text键中。
@itchat.msg_register(PICTURE, isFriendChat=True, isGroupChat=True, isMpChat=True)
def simple_reply(msg):
msg['Text'](msg['FileName'])
itchat.send('@img@%s' % msg['FileName'], msg['FromUserName'])
# itchat.auto_login()
itchat.run()
Start auto replying.
LOG OUT!
import itchat
itchat.auto_login()
█
Getting uuid of QR code.
Downloading QR code.
Please scan the QR code to log in.
Please press confirm on your phone.
Loading the contact, this may take a little while.
Login successfully as 可愛いカエル
itchat.send('我是可爱的小青蛙','可愛いカエル')
<ItchatReturnValue: {'LocalID': '', 'BaseResponse': {'RawMsg': '', 'ErrMsg': '', 'Ret': -1}, 'MsgID': ''}>
itchat.send('@img@%s' % '0.jpeg','filehelper')
<ItchatReturnValue: {'LocalID': '15036546406745', 'BaseResponse': {'RawMsg': '请求成功', 'ErrMsg': '请求成功', 'Ret': 0}, 'MsgID': '7247015639414374080'}>
itchat.send_msg('wowowow', 'dulihanqiu')
<ItchatReturnValue: {'LocalID': '', 'BaseResponse': {'RawMsg': '', 'ErrMsg': '', 'Ret': -1}, 'MsgID': ''}>
# itchat.show_mobile_login()
itchat.search_friends()
<User: {'HeadImgUrl': '/cgi-bin/mmwebwx-bin/webwxgeticon?seq=408715135&username=@4ecfd69637e1be99f839d760eb6395af828cb547f6cb3bb3583dfb5878d260e3&skey=@crypt_e1aa6a49_f84e0567ada5d9ad1a5981cf745f7c63', 'KeyWord': '', 'City': '', 'Statues': 0, 'UniFriend': 0, 'SnsFlag': 1, 'Uin': 3489129122, 'ContactFlag': 0, 'Signature': '', 'DisplayName': '', 'NickName': 'dulihanqiu', 'AttrStatus': 0, 'Sex': 1, 'Alias': '', 'OwnerUin': 0, 'StarFriend': 0, 'PYInitial': '', 'RemarkName': '', 'PYQuanPin': '', 'MemberList': <ContactList: []>, 'UserName': '@4ecfd69637e1be99f839d760eb6395af828cb547f6cb3bb3583dfb5878d260e3', 'AppAccountFlag': 0, 'WebWxPluginSwitch': 0, 'EncryChatRoomId': '', 'HideInputBarFlag': 0, 'RemarkPYInitial': '', 'MemberCount': 0, 'Province': '', 'HeadImgFlag': 1, 'ChatRoomId': 0, 'VerifyFlag': 0, 'RemarkPYQuanPin': ''}>
from itchat.content import TEXT
@itchat.msg_register(TEXT, isFriendChat=True, isGroupChat=True, isMpChat=True)
def text_reply(msg):
itchat.send('%s: %s' % (msg['Type'], msg['Text']), msg['FromUserName'])
itchat.run()
Start auto replying.
import itchat
from itchat.content import TEXT
@itchat.msg_register(TEXT, isGroupChat = True)
def text_reply(msg):
print(msg['isAt'])
print(msg['ActualNickName'])
print(msg['Content'])
if msg['isAt'] == False:
return '%s 说 %s ' % ( msg['ActualNickName'], msg['Text'])
else:
return '%s @ me: %s' % (msg['ActualNickName'], msg['Text'])
itchat.auto_login()
itchat.run()
█
Getting uuid of QR code.
Downloading QR code.
Please scan the QR code to log in.
Please press confirm on your phone.
Loading the contact, this may take a little while.
Login successfully as dulihanqiu
Start auto replying.
False
可愛いカエル
哈哈哈
True
可愛いカエル
@dulihanqiu 你妹
False
呖呖呖
哦哦哦 我懂了
False
可愛いカエル
你懂啥
False
呖呖呖
哦哦哦 我懂了
False
呖呖呖
[擦汗][再见][再见][再见]
False
呖呖呖
懂个jb
False
可愛いカエル
好玩吧
False
呖呖呖
好玩好玩
False
可愛いカエル
这样下去我也会疯的
import itchat
from itchat.content import *
# itchat.auto_login()
@itchat.msg_register
def general_reply(msg):
return 'I received a %s' % msg['Type']
@itchat.msg_register(TEXT)
def text_reply(msg):
return 'You said to me one to one: %s' % msg['Text']
itchat.run()
Start auto replying.