版权声明:(谢厂节的博客)博主文章绝大部分非原创,转载望留链接。 https://blog.csdn.net/xundh/article/details/81987263
文档: https://itchat.readthedocs.io/zh/latest/
安装
pip install itchat
发送示例
import itchat
itchat.auto_login()
itchat.send('Hello, filehelper', toUserName='filehelper')
获取所有群
rooms = itchat.get_chatrooms(update=True)
rooms = itchat.search_chatrooms(gname)
回复消息
import itchat
#@itchat.msg_register(itchat.content.TEXT)
#def text_reply(msg):
# return msg.text
@itchat.msg_register([TEXT,MAP,CARD,NOTE,SHARING])
def text_reply(msg):
itchat.send('%s: %s'%(msg['Type'],msg['Text']),msg['FromUserName'])
itchat.auto_login()
itchat.run()
新好友自动添加并回复
@itchat.msg_register(FRIENDS)
def add_friend(msg):
itchat.add_friend(**msg['Text'])# 该操作将自动将好友的消息录入,不需要重载通讯录
itchat.send_msg('Nice to meet you!',msg['RecommendInfo']['UserName'])
群聊
@itchat.msg_register(TEXT,isGroupChat=True)
def text_reply(msg):
if msg['isAt']:
itchat.send(u'@%s\u2005I received: %s '%(msg['ActualNickName'],msg['Content']),msg['FromUserName'])
获取好友列表
#coding=utf8
import itchat, time
itchat.auto_login(True)
SINCERE_WISH = u'好友:'
friendList = itchat.get_friends(update=True)[1:]
for friend in friendList:
print(SINCERE_WISH % (friend['DisplayName'] or friend['NickName']), friend['UserName'])
time.sleep(.5)
把print换成 itchat.send 即可群发。
输出群聊用户名称
#coding=utf8
import itchat, time
itchat.auto_login(True)
REAL_SINCERE_WISH = u'好友:'
chatroomName='群聊名称:'
itchat.get_chatrooms(update=True)
chatrooms = itchat.search_chatrooms(name=chatroomName)
print(chatrooms)
if chatrooms is None or len(chatrooms)==0:
print(u'没有找到群聊:' + chatroomName)
else:
chatroom = itchat.update_chatroom(chatrooms[0]['UserName'])
for friend in chatroom['MemberList']:
friend = itchat.search_friends(userName=friend['UserName'])
print(friend)
if friend is None:
continue
print((friend['DisplayName'] or friend['NickName']), friend['UserName'])
time.sleep(.5)