Created
September 28, 2016 04:59
-
-
Save littlecodersh/3fef7d2afb2d502e4735be083c9f79e1 to your computer and use it in GitHub Desktop.
Demo of checking friends' status of wechat.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#coding=utf8 | |
import itchat | |
CHATROOM_NAME = 'friend' | |
CHATROOM = None | |
HELP_MSG = u'''\ | |
好友状态监测 | |
* 发送名片将会返回好友状态 | |
* 请确有名为%s的未使用的群聊 | |
* 并将该群聊保存到通讯录 | |
* 调用频率存在一定限制\ | |
''' % CHATROOM_NAME | |
CHATROOM_MSG = u'''\ | |
无法自动创建群聊,请手动创建 | |
确保群聊名称为%s | |
请不要使用已经使用过的群聊 | |
创建后请将群聊保存到通讯录\ | |
''' % CHATROOM_NAME | |
def get_chatroom(): | |
global CHATROOM | |
if CHATROOM is None: | |
itchat.get_chatrooms(update=True) | |
chatrooms = itchat.search_chatrooms(CHATROOM_NAME) | |
if chatrooms: | |
return chatrooms[0] | |
else: | |
r = itchat.create_chatroom(itchat.get_friends()[1:4], topic=CHATROOM_NAME) | |
if r['BaseResponse']['ErrMsg'] == '': | |
CHATROOM = {'UserName': r['ChatRoomName']} | |
return CHATROOM | |
else: | |
return CHATROOM | |
def get_friend_status(friend): | |
ownAccount = itchat.get_friends(update=True)[0] | |
if friend['UserName'] == ownAccount['UserName']: | |
return u'检测到本人账号。' | |
elif itchat.search_friends(userName=friend['UserName']) is None: | |
return u'该用户不在你的好友列表中。' | |
else: | |
chatroom = CHATROOM or get_chatroom() | |
if chatroom is None: return CHATROOM_MSG | |
r = itchat.add_member_into_chatroom(chatroom['UserName'], [friend]) | |
if r['BaseResponse']['ErrMsg'] == '': | |
status = r['MemberList'][0]['MemberStatus'] | |
itchat.delete_member_from_chatroom(chatroom['UserName'], [friend]) | |
return { 3: u'该好友已经将你加入黑名单。', | |
4: u'该好友已经将你删除。', }.get(status, | |
u'该好友仍旧与你是好友关系。') | |
else: | |
return u'无法获取好友状态,预计已经达到接口调用限制。' | |
@itchat.msg_register(itchat.content.CARD) | |
def get_friend(msg): | |
if msg['ToUserName'] != 'filehelper': return | |
friendStatus = get_friend_status(msg['RecommendInfo']) | |
itchat.send(friendStatus, 'filehelper') | |
itchat.auto_login(True) | |
itchat.send(HELP_MSG, 'filehelper') | |
itchat.run() |
代码 45行
if r['BaseResponse']['ErrMsg'] == '':
=>
if r['BaseResponse']['ErrMsg'] == u'请求成功':
我设置了delay 3s,依旧只能检测5-6个好友就接口达到了限制了,咋回事
和樓上一樣
被测试对象不是看不到拉群踢群信息么?事实是可以看到
自动建群成功不了。。。
我现在使用的结果是一直无任何现象,怎么回事呢?
求改进该代码
无法获取好友状态,预计已经达到接口调用限制。
30行改成:if r['BaseResponse']['ErrMsg'] == '请求成功':
2018.9.6
create_chatroom及add_member_into_chatroom接口已经无法正常使用
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
好像已经无法使用了...... 一直说"无法获取好友状态,预计已经达到接口调用限制"