from PyWeChatSpy import WeChatSpy import random import re def my_parser(data): if data.type == 5: # 判断是微信消息数据 for message in data.message_list.message: # 遍历微信消息 if message.type == 10000: # 判断是微信拍一拍系统提示 # 因为微信系统消息很多 因此需要用正则匹配消息内容进一步过滤拍一拍提示 # {'self': 0, 'msg_type': 10000, 'wxid1': '179xxxxxx72@chatroom', 'content': '"Mandy的小脑袋" 拍了拍你'} m = re.search('".*" 拍了拍你', message.content) if m: # 搜索到了匹配的字符串 判断为拍一拍 image_path = f"images/{random.randint(1, 7)}.jpg" # 随机选一张回复用的图片 spy.send_file(message.wxid1, image_path) # 发送图片 spy = WeChatSpy(parser=my_parser) # 实例化WeChatSpy类 if __name__ == '__main__': spy.run() # 运行代码
for details in data.contact_list.contact: print(details.wxid) print(details.nickname) print(details.wechatid) print(details.remark) print(details.profilephoto) print(details.profilephoto_hd) print(details.sex) print(details.whats_up) print(details.country) print(details.province) print(details.city) print(details.source) elif data.type == HEART_BEAT: # 心跳 pass elif data.type == SET_REMARK: print("-" * 10, "备注设置完成", "-" * 10) print(data) elif data.type == CONTACT_STATUS: print("-" * 10, "联系人状态", "-" * 10) print(data) if __name__ == '__main__': spy = WeChatSpy(parser=my_proto_parser, key="18d421169d93611a5584affac335e690", logger=logger) spy.run(r"C:\Program Files (x86)\Tencent\WeChat\WeChat.exe") input()
print(data) # 查询联系人列表 # spy.query_contact_list() elif data["type"] == 203: # 微信登出 print("微信退出登录") elif data["type"] == 5: # 消息 for item in data["data"]: print(item) if item["msg_type"] == 37: # 好友请求消息 obj = etree.XML(item["content"]) encryptusername, ticket = obj.xpath( "/msg/@encryptusername")[0], obj.xpath("/msg/@ticket")[0] # spy.accept_new_contact(encryptusername, ticket) elif data["type"] == 2: # 联系人详情 print(data) elif data["type"] == 3: # 联系人列表 for contact in data["data"]: print(contact) elif data["type"] == 9527: spy.logger.warning(data) if __name__ == '__main__': spy = WeChatSpy(parser=parser, key="18d421169d93611a5584affac335e690") spy.run()
staff, "未知错误,发生于" + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) except: pass if __name__ == '__main__': file = open('key.txt', 'r') KEY = file.read() file.close() try: file = open('status.json', 'r') variables = json.load(file) file.close() except FileNotFoundError: file = open('status.json', 'w') variables = {'run': {}, 'Bot': {}, 'DouTu': {}, 'ano_uuid': {}} v = json.dumps(variables) file.write(v) file.close() scheduler = BackgroundScheduler() scheduler.add_job(save_status, 'interval', seconds=120) spy = WeChatSpy(response_queue=my_response_queue, key=KEY, logger=logger) pid = spy.run(r"C:\Program Files (x86)\Tencent\WeChat\WeChat.exe") scheduler.add_job(spy.get_contacts, 'interval', seconds=300) scheduler.start() while True: data = my_response_queue.get() handle_response(data)
from PyWeChatSpy import WeChatSpy def parser(data): # 获取文本信息 if data.get("type") == 1: # spy.query_personal_info() # 获取登录账号的个人信息 print(data) # 打印收到的信息 if data.get('chatroom_ID'): # 微信群消息 chatroom_ID = data.get('chatroom_ID') # 获取微信群id spy.send_text(chatroom_ID, '微信群消息自动回复测试!') # 在此,你可以根据需要来设置消息的内容 elif not data.get('chatroom_ID') and data.get('self') == 0: wx_ID = data.get('wx_ID') # 好友消息 spy.send_text(wx_ID, '好友消息自动回复测试!') # 在此,你可以根据需要来设置消息的内容 # 获取个人信息 elif data.get("type") == 2: print(data) if __name__ == '__main__': spy = WeChatSpy(parser=parser) spy.run()
logger.addHandler(fh) logger.addHandler(sh) WECHAT_PROFILE = rf"D:\{os.environ['USERNAME']}\Documents\WeChat Files" PATCH_PATH = rf"C:\Users\{os.environ['USERNAME']}\AppData\Roaming\Tencent\WeChat\patch" if not os.path.exists(WECHAT_PROFILE): logger.error("请先设置计算机用户名,并完善WECHAT_PROFILE和PATCH_PATH") exit() if os.path.isdir(PATCH_PATH): shutil.rmtree(PATCH_PATH) if not os.path.exists(PATCH_PATH): with open(PATCH_PATH, "a") as wf: wf.write("") my_response_queue = Queue() spy = WeChatSpy(response_queue=my_response_queue, key="ab28d8c4768ab3bc2ba86841313f6e32", logger=logger) tod = TruthOrDare(spy) def pop_response(): while True: data = my_response_queue.get() handle_response(data) def handle_response(data): if data.type == PROFESSIONAL_KEY: if not data.code: logger.warning(data.message) elif data.type == WECHAT_CONNECTED: # 微信接入
logger.error(data.message) elif data.type == GET_CONTACTS_LIST and not data.code: logger.error(data.message) elif data.type == CREATE_GROUP_CALLBACK: # 创建群聊回调 callback = spy_pb2.CreateGroupCallback() callback.ParseFromString(data.bytes) print(callback) elif data.type == GROUP_MEMBER_DETAILS: # 群成员详情 group_member_details = spy_pb2.GroupMemberDetails() group_member_details.ParseFromString(data.bytes) print(group_member_details) elif data.type == GROUP_MEMBER_EVENT: # 群成员进出事件 group_member_event = spy_pb2.GroupMemberEvent() group_member_event.ParseFromString(data.bytes) print(group_member_event) elif data.type == LOGIN_QRCODE: # 登录二维码 qrcode = spy_pb2.LoginQRCode() qrcode.ParseFromString(data.bytes) with open("qrcode.png", "wb") as wf: wf.write(qrcode.qrcodeBytes) else: print(data) if __name__ == '__main__': spy = WeChatSpy(response_queue=my_response_queue, key="18d421169d93611a5584affac335e690", logger=logger) pid = spy.run(r"C:\Program Files (x86)\Tencent\WeChat\WeChat.exe") handle_response()
from threading import Thread import os import re from PyWeChatSpy import WeChatSpy import qdarkstyle from PyWeChatSpy.proto.spy_pb2 import Response, AccountDetails, Contacts from PyWeChatSpy.command import WECHAT_CONNECTED, WECHAT_LOGIN, WECHAT_LOGOUT, ACCOUNT_DETAILS, CONTACTS_LIST my_response_queue = Queue() key = None if os.path.exists("key.txt"): with open("key.txt", "r") as rf: key = rf.read() print("key:", key) spy = WeChatSpy(response_queue=my_response_queue, key=key) contact_filter = ("qmessage", "qqmail", "tmessage", "medianote", "floatbottle", "fmessage") root_dir = os.path.dirname(__file__) profile_photo_dir = os.path.join(root_dir, "profilephotos") class MsgThread(QThread): signal = pyqtSignal(Response) def __init__(self): super().__init__() def run(self): while True: msg = my_response_queue.get() self.signal.emit(msg)
import re from queue import Queue my_response_queue = Queue() def parse(): while True: data = my_response_queue.get() if data.type == CHAT_MESSAGE: # 判断是微信消息数据 chat_message = spy_pb2.ChatMessage() chat_message.ParseFromString(data.bytes) for message in chat_message.message: # 遍历微信消息 _type = message.type # 消息类型 1.文本|3.图片...自行探索 _from = message.wxidFrom.str # 消息发送方 _to = message.wxidTo.str # 消息接收方 content = message.content.str # 消息内容 print(_type, _from, _to, content) if _type == 10000: # 判断是微信拍一拍系统提示 # 因为微信系统消息很多 因此需要用正则匹配消息内容进一步过滤拍一拍提示 m = re.search('".*" 拍了拍我', content) if m: # 搜索到了匹配的字符串 判断为拍一拍 image_path = f"images/{random.randint(1, 7)}.jpg" # 随机选一张回复用的图片 spy.send_file(_from, image_path) # 发送图片 if __name__ == '__main__': spy = WeChatSpy(response_queue=my_response_queue) spy.run(r"C:\Program Files (x86)\Tencent\WeChat\WeChat.exe") parse()