2019-06-09
概述
微信群限制了用户最多只有500人。对于多群的培训和讲座,如何便捷实现转发呢?
或者你想对好友进行一个群发的祝福,但最好能有不同的称呼,如何自动群发祝福呢?
这就需要用到微信机器人。
itchat是python版的一个工具。可以用此工具便捷实现简单微信机器人。
其原理是通过调用web版的接口,达到控制群和发送消息的目的。
安装
$ sudo pip install itchat
>>> import itchat
>>>
操作
包括登录,查找群,找到用户id,发送消息。可以实现用户命令识别。
>>> 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('Hello, I am a robot', toUserName='woaichoufeng')
<ItchatReturnValue: {u'MsgID': u'', u'LocalID': u'', u'BaseResponse': {u'ErrMsg': u'', u'Ret': -1, 'RawMsg': u''}}>
>>> itchat.send('Hello, I am a robot', toUserName='filehelper')
<ItchatReturnValue: {u'MsgID': u'9144299777193180130', u'LocalID': u'15584927777671', u'BaseResponse': {u'ErrMsg': u'\u8bf7\u6c42\u6210\u529f', u'Ret': 0, 'RawMsg': u'\u8bf7\u6c42\u6210\u529f'}}>
>>> itchat.get_chatrooms(update=True)
<ContactList: [<Chatroom: {u'UserName': u'@@bc3265c96989e96da625d8ed6371f7bf42b0dc19906c53c44e345d3bab2ac919', u'City': u'', u'DisplayName': u'', u'UniFriend': 0, 'Self': <User: {'UserName': u'@1cf35f3f459cc1dc6ec91b625b42c657', 'City': '', 'DisplayName': '', 'UniFriend': 0, 'OwnerUin': 0, 'MemberList': <ContactList: []>, 'PYQuanPin': u'', 'RemarkPYInitial': u'', 'Uin': 2413301500, 'AppAccountFlag': 0, 'VerifyFlag': 0, 'Province': '', 'KeyWord': '', 'RemarkName': u'', 'PYInitial': u'', 'ChatRoomId': 0, 'HideInputBarFlag': 0, u'HeadImgFlag': 1, 'EncryChatRoomId': '', 'AttrStatus': 0, 'SnsFlag': 49, 'MemberCount': 0, u'WebWxPluginSwitch': 0, 'Alias': '', 'Signature': u'\u52a0\u597d\u53cb\u8bf7\u5b9e\u540d\u4ecb\u7ecd\u81ea\u5df1', 'ContactFlag': 0, 'NickName': u'\u68a6\u60f3\u5bb6', 'RemarkPYQuanPin': u'', 'HeadImgUrl': u'/cgi-bin/mmwebwx-bin/webwxgeticon?seq=1249928989&username=@1cf35f3f459cc1dc6ec91b625b42c657&skey=@crypt_9cda34e4_5a6cf08a2155b1a4e0dd98a92c3cdb31', 'Sex': 1, 'StarFriend': 0, 'Statues': 0}>, 'MemberList': <ContactList: []...
>>> chatroomName=u'怀化'
>>> chatrooms = itchat.search_chatrooms(name=chatroomName)
>>> chatrooms
[<Chatroom: {u'UserName': u'@@442ad96614a6b0fb234059a040acf2ff875f4f036c83802e6450e1165954fdd2', u'City': u'', u'DisplayName': u'', u'UniFriend': 0, 'Self': <ChatroomMember: {u'UserName': u'@1cf35f3f459cc1dc6ec91b625b42c657', u'RemarkPYQuanPin': u'', u'DisplayName': u'', u'KeyWord': u'abl', u'PYInitial': u'', u'Uin': 0, 'MemberList': <ContactList: []>, u'MemberStatus': 0, u'PYQuanPin': u'', u'RemarkPYInitial': u'', u'NickName': u'\u68a6\u60f3\u5bb6', u'AttrStatus': 33659943}>, 'MemberList': <ContactList: [<ChatroomMember: {u'UserName': u'@07103cc838ee46cd080d895f7cf7de0e8daff5da8fdc979a626b81c79c84570b', u'RemarkPYQuanPin': u'', u'DisplayName': u'', u'KeyWord': u'', u'PYInitial': u'', u'Uin': 0, 'MemberList': <ContactList: []>, u'MemberStatus': 0, u'PYQuanPin': u'', u'RemarkPYInitial': u'', u'NickName': u'\u738b\u4e1c\u65ed', u'AttrStatus': 2147487781}>, <ChatroomMember: {u'UserName': u'@3c71c30206fe47b882554ab61941fc985d7b04136764c1ae60355ecfa92d81b8', u'RemarkPYQuanPin': u'', u'DisplayName': u'', u'KeyWord': u'', u'PYInitial': u'', u'Uin': 0, 'MemberList': <ContactList: []>
>>> print u'\u68a6\u60f3\u5bb6'
梦想家
>>> chatroom = itchat.update_chatroom(chatrooms[0]['UserName'])
>>> chatroom
<Chatroom: {u'UserName': u'@@442ad96614a6b0fb234059a040acf2ff875f4f036c83802e6450e1165954fdd2', u'City': u'', u'DisplayName': u'', u'UniFriend': 0, 'Self': <ChatroomMember: {u'UserName': u'@1cf35f3f459cc1dc6ec91b625b42c657', u'RemarkPYQuanPin': u'', u'DisplayName': u'', u'KeyWord': u'abl', u'PYInitial': u'', u'Uin': 0, 'MemberList': <ContactList: []>, u'MemberStatus': 0, u'PYQuanPin': u'', u'RemarkPYInitial': u'', u'NickName': u'\u68a6\u60f3\u5bb6', u'AttrStatus': 33659943}>, 'MemberList': <ContactList: [<ChatroomMember: {u'UserName': u'@07103cc838ee46cd080d895f7cf7de0e8daff5da8fdc979a626b81c79c84570b', u'RemarkPYQuanPin': u'', u'DisplayName': u'', u'KeyWord': u'', u'PYInitial': u'', u'Uin': 0, 'MemberList': <ContactList: []>, u'MemberStatus': 0
>>> itchat.send(u"我是机器人",toUserName=chatroom[u'UserName'])
<ItchatReturnValue: {u'MsgID': u'4913987021355629039', u'LocalID': u'15584939982925', u'BaseResponse': {u'ErrMsg': u'\u8bf7\u6c42\u6210\u529f', u'Ret': 0, 'RawMsg': u'\u8bf7\u6c42\u6210\u529f'}}>
>>> chatrooms = itchat.search_chatrooms(name=u'家人')
>>> chatrooms
[<Chatroom: {u'UserName': u'@@44dcf57c604ad8e3482d4f5cdfb164eb6cf1b4e1295db075db66607b5f9d3ed9', u'City': u'', u'DisplayName': u'', u'UniFriend': 0, 'Self': <User: {'UserName': u'@1cf35f3f459cc1dc6ec91b625b42c657', 'City': '', 'DisplayName': '', 'UniFriend': 0, 'OwnerUin': 0, 'MemberList': <ContactList: []>, 'PYQuanPin': u'', 'RemarkPYInitial': u'', 'Uin': 2413301500, 'AppAccountFlag': 0, 'VerifyFlag': 0, 'Province': '', 'KeyWord': '', 'RemarkName': u'', 'PYInitial': u'', 'ChatRoomId': 0, 'HideInputBarFlag': 0, u'HeadImgFlag': 1, 'EncryChatRoomId': '', 'AttrStatus': 0, 'SnsFlag': 49, 'MemberCount': 0, u'WebWxPluginSwitch': 0, 'Alias': '', 'Signature': u'\u52a0\u597d\u53cb\u8bf7\u5b9e\u540d\u4ecb\u7ecd\u81ea\u5df1', 'ContactFlag': 0, 'NickName': u'\u68a6\u60f3\u5bb6', 'RemarkPYQuanPin': u'', 'HeadImgUrl': u'/cgi-bin/mmwebwx-bin/webwxgeticon?seq=1249928989&username=@1cf35f3f459cc1dc6ec91b625b42c657&skey=@crypt_9cda34e4_5a6cf08a2155b1a4e0dd98a92c3cdb31', 'Sex': 1, 'StarFriend': 0, 'Statues': 0}>, 'MemberList': <ContactList: []>, u'PYQuanPin': u'jiaren', u'RemarkPYInitial': u'', u'Sex': 0, u'AppAccountFlag': 0, u'VerifyFlag': 0, u'Province': u'', u'KeyWord': u'', u'RemarkName': u'', u'PYInitial': u'JR', u'IsOwner': 1, u'ChatRoomId': 0, u'HideInputBarFlag': 0, u'EncryChatRoomId': u'', u'AttrStatus': 0, u'SnsFlag': 0, u'MemberCount': 0, u'OwnerUin': 0, u'Alias': u'', 'IsAdmin': None, u'Signature': u'', u'ContactFlag': 2051, u'NickName': u'\u5bb6\u4eba', u'RemarkPYQuanPin': u'', u'HeadImgUrl': u'/cgi-bin/mmwebwx-bin/webwxgetheadimg?seq=664971536&username=@@44dcf57c604ad8e3482d4f5cdfb164eb6cf1b4e1295db075db66607b5f9d3ed9&skey=@crypt_9cda34e4_5a6cf08a2155b1a4e0dd98a92c3cdb31', u'Uin': 0, u'StarFriend': 0, u'Statues': 1}>]
>>> print u'\u5bb6\u4eba'
家人
>>> print u'\u68a6\u60f3\u5bb6'
梦想家
>>> chatrooms[0]['MemberList']
<ContactList: []>
>>> chatroom = itchat.update_chatroom(chatrooms[0]['UserName'])
>>> chatroom
<Chatroom: {u'UserName': u'@@44dcf57c604ad8e3482d4f5cdfb164eb6cf1b4e1295db075db66607b5f9d3ed9', u'City': u'', u'DisplayName': u'', u'UniFriend': 0, 'Self': <ChatroomMember: {u'UserName': u'@1cf35f3f459cc1dc6ec91b625b42c657', u'RemarkPYQuanPin': u'', u'DisplayName': u'', u'KeyWord': u'abl', u'PYInitial': u'', u'Uin': 0, 'MemberList': <ContactList: []>, u'MemberStatus': 0, u'PYQuanPin': u'', u'RemarkPYInitial': u'', u'NickName': u'\u68a6\u60f3\u5bb6', u'AttrStatus': 33659943}>, 'MemberList': <ContactList: [<ChatroomMember: {u'UserName': u'@cfa1391a19335953790dc5ee86569117', u'RemarkPYQuanPin': u'', u'DisplayName': u'', u'KeyWord': u'woa', u'PYInitial': u'', u'Uin': 0, 'MemberList': <ContactList: []>, u'MemberStatus': 0, u'PYQuanPin': u'', u'RemarkPYInitial': u'', u'NickName': u'yff', u'AttrStatus': 104895}>, <ChatroomMember: {u'UserName': u'@0fb9d35163148bc37b16f9e981fe3d6132aad40754d45362ebbecdc4cb5aa261', u'RemarkPYQuanPin': u'', u'DisplayName': u'\u6768\u5468\u6f7c', u'KeyWord': u'', u'PYInitial': u'', u'Uin': 0, 'MemberList': <ContactList: []>, u'MemberStatus': 0, u'PYQuanPin': u'', u'RemarkPYInitial': u'', u'NickName': u'\u5c0f\u751c', u'AttrStatus': 233573}>, <ChatroomMember: {u'UserName': u'@1cf35f3f459cc1dc6ec91b625b42c657', u'RemarkPYQuanPin': u'', u'DisplayName': u'', u'KeyWord': u'abl', u'PYInitial': u'', u'Uin': 0, 'MemberList': <ContactList: []>, u'MemberStatus': 0, u'PYQuanPin': u'', u'RemarkPYInitial': u'', u'NickName': u'\u68a6\u60f3\u5bb6', u'AttrStatus': 33659943}>, <ChatroomMember: {u'UserName': u'@a647e7804ab15e94ee7d398bbbcee602c09a32319e5616e78e43f992a0c701ba', u'RemarkPYQuanPin': u'', u'DisplayName': u'', u'KeyWord': u'', u'PYInitial': u'', u'Uin': 0, 'MemberList': <ContactList: []>, u'MemberStatus': 0, u'PYQuanPin': u'', u'RemarkPYInitial': u'', u'NickName': u'\u5988\u5988', u'AttrStatus': 4133}>]>, u'PYQuanPin': u'jiaren', u'RemarkPYInitial': u'', u'Sex': 0, u'AppAccountFlag': 0, u'VerifyFlag': 0, u'Province': u'', u'KeyWord': u'', u'RemarkName': u'', u'PYInitial': u'JR', u'IsOwner': 1, u'ChatRoomId': 0, u'HideInputBarFlag': 0, u'EncryChatRoomId': u'@a7df017cdb2d4c942eb3169f151a586b', u'AttrStatus': 0, u'SnsFlag': 0, u'MemberCount': 4, u'OwnerUin': 0, u'Alias': u'', 'IsAdmin': None, u'Signature': u'', u'ContactFlag': 2051, u'NickName': u'\u5bb6\u4eba', u'RemarkPYQuanPin': u'', u'HeadImgUrl': u'/cgi-bin/mmwebwx-bin/webwxgetheadimg?seq=664971536&username=@@44dcf57c604ad8e3482d4f5cdfb164eb6cf1b4e1295db075db66607b5f9d3ed9&skey=', u'Uin': 0, u'StarFriend': 0, u'Statues': 1}>
>>> itchat.send(u"我是机器人,小甜",toUserName=chatroom[u'UserName'])
<ItchatReturnValue: {u'MsgID': u'6374349129440414394', u'LocalID': u'15584945294232', u'BaseResponse': {u'ErrMsg': u'\u8bf7\u6c42\u6210\u529f', u'Ret': 0, 'RawMsg': u'\u8bf7\u6c42\u6210\u529f'}}>
>>> itchat.send(u"@\u5988\u5988,这是程序和你聊天",toUserName=chatroom[u'UserName'])
<ItchatReturnValue: {u'MsgID': u'1694421504485845806', u'LocalID': u'15584946052565', u'BaseResponse': {u'ErrMsg': u'\u8bf7\u6c42\u6210\u529f', u'Ret': 0, 'RawMsg': u'\u8bf7\u6c42\u6210\u529f'}}>
>>> itchat.send(u"@\u5s,,toUserName=chatroom[u'UserName'])
KeyboardInterrupt
>>> itchat.shat.send,这是程序和你聊天",toame=chatroom[u'UserName'])
KeyboardInterrupt
>>> itchat.send(u"@\u5c0f\u751c,这是机器人",toUserName=chatroom[u'UserName'])
<ItchatReturnValue: {u'MsgID': u'6570410823813335938', u'LocalID': u'15584948162742', u'BaseResponse': {u'ErrMsg': u'\u8bf7\u6c42\u6210\u529f', u'Ret': 0, 'RawMsg': u'\u8bf7\u6c42\u6210\u529f'}}>
>>> LOG OUT!
如非注明转载, 均为原创. 本站遵循知识共享CC协议,转载请注明来源