准备
下载软件安装微信
安装python环境
conda create --prefix=D:\PythonEnvs\wechatrobotstu python=3.10
conda activate D:\PythonEnvs\wechatrobotstu
使用
新建python项目
安装依赖包
pip install --upgrade wcferry -i https://pypi.doubanio.com/simple
解压dll到一个目录下面,
创建监听
import ctypes
if __name__ == "__main__":
# 加载 sdk.dll (需要绝对路径)
sdk = ctypes.cdll.LoadLibrary("G:/Python/wechatfrerrystu/wechatfrerrystu/dll/sdk.dll")
# 初始化
sdk.WxInitSDK(False, 10086)
print("初始化完毕...")
# 退出 SDK
print("退出SDK")
sdk.WxDestroySDK()
# 注意关闭 Python 进程
创建客户端
from wcferry import Wcf, WxMsg
wcf = Wcf()
判断是否登录
# 检测当前PC端微信登录状态
is_login = wcf.is_login()
获取登录的微信信息
# 获取当前PC端微信账号信息
print(wcf.get_user_info())
# {'wxid': 'wxid_xxxxx', 'name': '昵称', 'mobile': '手机号', 'home': ''}
获取全部的联系人
# 获取全部的联系人包括公众号和好友、群聊等
contacts = wcf.get_contacts()
# 遍历所有联系人,
# gh_开头公众号 wxid_是微信好友或者好友自定义的
# @chatroom结尾是群聊 @openim是企业微信好友
for contact in contacts:
print(contact)
查询群成员
# 查找群成员
group_members = wcf.get_chatroom_members('xxxxx@chatroom')
print(group_members)
# 直接通过SQL查询查找群成员,name为空,重试了几次可以了
crs = wcf.query_sql("MicroMsg.db", f"SELECT RoomData FROM ChatRoom WHERE ChatRoomName = 'xxxx@chatroom';")
print(crs)
# 根据微信id获取微信号昵称,查询出来name为空,重试了几次可以了
user_info = wcf.get_info_by_wxid('wxid_xxxxx')
print(user_info)
发送消息
# receiver的值是filehelper则是文件传输助手
# 发送群聊并@某人
wcf.send_text("@昵称1@博昵称2\n\n机器人发送消息","xxxxx@chatroom","wxid_xxxx,wxid_xxxxx")
# 发送消息
wcf.send_text('机器人发送消息',"xxxxx@chatroom")
# print("发送消息成功")
监听消息
def processMsg(msg: WxMsg, wcf: Wcf):
# print(msg)
print(msg.type)
print(msg.id)
print(msg.xml)
print(msg.sender)
print(msg.roomid)
print(msg.content)
print(msg.thumb)
print(msg.extra)
if msg.is_at(wcf.get_self_wxid()):
print("我被@啦")
# 后续可以接入大模型
if msg.roomid:
group_members = wcf.get_chatroom_members(msg.roomid)
nickName = group_members[msg.sender]
wcf.send_text(f"@{nickName}机器人收到啦",msg.roomid, msg.sender)
else :
wcf.send_text("机器人收到啦", msg.sender)
# 加入群聊 msg.type=10000
# if msg.from_group():
# print(msg.content)
def enableReceivingMsg(wcf: Wcf):
def innerWcFerryProcessMsg():
while wcf.is_receiving_msg():
try:
msg = wcf.get_msg()
processMsg(msg, wcf)
except Empty:
continue
except Exception as e:
print(f"ERROR: {e}")
wcf.enable_receiving_msg()
Thread(target=innerWcFerryProcessMsg, name="ListenMessageThread", daemon=True).start()
代码如下
import ctypes
from queue import Empty
from threading import Thread
from wcferry import Wcf, WxMsg
def processMsg(msg: WxMsg, wcf: Wcf):
# print(msg)
print(msg.type)
print(msg.id)
print(msg.xml)
print(msg.sender)
print(msg.roomid)
print(msg.content)
print(msg.thumb)
print(msg.extra)
if msg.is_at(wcf.get_self_wxid()):
print("我被@啦")
# 后续可以接入大模型
if msg.roomid:
group_members = wcf.get_chatroom_members(msg.roomid)
nickName = group_members[msg.sender]
wcf.send_text(f"@{nickName}机器人收到啦",msg.roomid, msg.sender)
else :
wcf.send_text("机器人收到啦", msg.sender)
# 加入群聊 msg.type=10000
# if msg.from_group():
# print(msg.content)
def enableReceivingMsg(wcf: Wcf):
def innerWcFerryProcessMsg():
while wcf.is_receiving_msg():
try:
msg = wcf.get_msg()
processMsg(msg, wcf)
except Empty:
continue
except Exception as e:
print(f"ERROR: {e}")
wcf.enable_receiving_msg()
Thread(target=innerWcFerryProcessMsg, name="ListenMessageThread", daemon=True).start()
def find_name_by_wxid(wxid, contact_list):
"""
根据wxid查询name
参数:
wxid (str): 要查询的wxid
contact_list (list): 包含朋友信息的列表
返回:
str: 如果找到,返回对应的name;如果未找到,返回None
"""
for friend in contact_list:
if friend['wxid'] == wxid:
return friend['name']
return ""
if __name__ == "__main__":
# 加载 sdk.dll (需要绝对路径)
sdk = ctypes.cdll.LoadLibrary("G:/Python/wechatfrerrystu/wechatfrerrystu/dll/sdk.dll")
# 初始化
sdk.WxInitSDK(False, 10086)
print("初始化完毕...")
wcf = Wcf()
# 检测当前PC端微信登录状态
is_login = wcf.is_login()
if is_login is True:
print("微信登录了")
# 获取当前PC端微信账号信息
# print(wcf.get_user_info())
# {'wxid': 'wxid_xxxxx', 'name': '昵称', 'mobile': '手机号', 'home': ''}
enableReceivingMsg(wcf)
wcf.keep_running()
# 获取全部的联系人包括公众号和好友、群聊等
# contacts = wcf.get_contacts()
# 遍历所有联系人,
# gh_开头公众号 wxid_是微信好友或者好友自定义的
# @chatroom结尾是群聊 @openim是企业微信好友
# for contact in contacts:
# print(contact)
# 查找群成员
# group_members = wcf.get_chatroom_members('xxxxx@chatroom')
# print(group_members)
# 直接通过SQL查询查找群成员,name为空,重试了几次可以了
# crs = wcf.query_sql("MicroMsg.db", f"SELECT RoomData FROM ChatRoom WHERE ChatRoomName = 'xxxx@chatroom';")
# print(crs)
# 根据微信id获取微信号昵称,查询出来name为空,重试了几次可以了
# user_info = wcf.get_info_by_wxid('wxid_xxxxx')
# print(user_info)
# 可以根据这个实现wxid查询用户信息,不过需要加好友
# print(find_name_by_wxid("wxid_xxxxx",contacts))
# sdk.WxDestroySDK()
# nickname = wcf.query_sql("MicroMsg.db", f"SELECT NickName FROM Contact WHERE UserName = '{wxid}';")
# receiver的值是filehelper则是文件传输助手
# wcf.send_text("@昵称1@博昵称2\n\n机器人发送消息","xxxxx@chatroom","wxid_xxxx,wxid_xxxxx")
# wcf.send_text('机器人发送消息',"xxxxx@chatroom")
# print("发送消息成功")
else:
# 退出 SDK
print("退出SDK")
sdk.WxDestroySDK()
# 注意关闭 Python 进程
参考
https://github.com/lich0821/WeChatFerry
https://blog.csdn.net/qq_47452807/article/details/138536720
https://gitcode.com/gh_mirrors/we/WeChatFerry/overview
https://space.bilibili.com/479305033/video
https://blog.huangque888.com/archives/258.html
https://blog.csdn.net/gitblog_00872/article/details/141015773
公众号文章
获取群聊人员
https://pi3yyy.love/archives/d604709c.html