diff --git a/main.py b/main.py index 6a8c2ee..049beb3 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,7 @@ #! /usr/bin/env python3 # -*- coding: utf-8 -*- +import re import time import robot.sdk.wcferry as WxSDK @@ -17,12 +18,23 @@ class Robot(BaseRobot): """ self.printRawMsg(msg) # 打印信息 + # 如果在群里被 @,回复发信人:“收到你的消息了!” 并 @他 if self.isGroupChat(msg): # 是群消息 if self.isAt(msg): # 被@ if msg.roomId in self.config.GROUPS: # 在配置的响应的群列表里 self.sendTextMsg(msg.roomId, "收到你的消息了!", msg.wxId) + # 非群聊信息 + elif msg.type == 37: # 好友请求 + self.autoAcceptFriendRequest(msg) + + elif msg.type == 10000: # 系统信息 + nickName = re.findall(r"你已添加了(.*),现在可以开始聊天了。", msg.content) + if nickName: + # 添加了好友,更新好友列表 + self.allContacts[msg.wxId] = nickName + def main(): robot = Robot(WxSDK, Config()) diff --git a/robot/base_robot.py b/robot/base_robot.py index 950b2ab..d71016b 100644 --- a/robot/base_robot.py +++ b/robot/base_robot.py @@ -2,6 +2,7 @@ import re import logging +import xml.etree.ElementTree as ET class BaseRobot(object): @@ -72,5 +73,15 @@ class BaseRobot(object): contacts = self.sdk.WxExecDbQuery("MicroMsg.db", "SELECT UserName, NickName FROM Contact;") return {contact["UserName"]: contact["NickName"] for contact in contacts} + def autoAcceptFriendRequest(self, msg): + try: + xml = ET.fromstring(msg.content) + v3 = xml.attrib["encryptusername"] + v4 = xml.attrib["ticket"] + self.sdk.WxAcceptNewFriend(v3, v4) + + except Exception as e: + self.LOG.error(f"同意好友出错:{e}") + def processMsg(self, msg) -> None: raise NotImplementedError("Method [processMsg] should be implemented.")