Skip to content

Instantly share code, notes, and snippets.

@littlecodersh
Created September 28, 2016 04:59
Show Gist options
  • Save littlecodersh/3fef7d2afb2d502e4735be083c9f79e1 to your computer and use it in GitHub Desktop.
Save littlecodersh/3fef7d2afb2d502e4735be083c9f79e1 to your computer and use it in GitHub Desktop.
Demo of checking friends' status of wechat.
#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()
@cunwow
Copy link

cunwow commented Mar 16, 2017

好像已经无法使用了...... 一直说"无法获取好友状态,预计已经达到接口调用限制"

@gladuo
Copy link

gladuo commented Mar 26, 2017

代码 45行
if r['BaseResponse']['ErrMsg'] == '':
=>
if r['BaseResponse']['ErrMsg'] == u'请求成功':

@lucasjinreal
Copy link

我设置了delay 3s,依旧只能检测5-6个好友就接口达到了限制了,咋回事

@xiaokai111
Copy link

和樓上一樣

@liuzm617
Copy link

被测试对象不是看不到拉群踢群信息么?事实是可以看到

@ximply
Copy link

ximply commented Aug 5, 2017

自动建群成功不了。。。

Copy link

ghost commented Aug 24, 2017

我现在使用的结果是一直无任何现象,怎么回事呢?

@Skyfacon
Copy link

求改进该代码

@matthewwu2002
Copy link

无法获取好友状态,预计已经达到接口调用限制。

@matthewwu2002
Copy link

30行改成:if r['BaseResponse']['ErrMsg'] == '请求成功':

@chenqping
Copy link

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