diff --git a/main.py b/main.py index c8b23b6..5ad872b 100644 --- a/main.py +++ b/main.py @@ -33,6 +33,7 @@ def main(): robot = Robot(wcf) robot.LOG.info("正在启动机器人···") + # 机器人启动发送测试消息 robot.sendTextMsg("机器人启动成功!", "filehelper") diff --git a/robot.py b/robot.py index 0edf802..a5243ef 100644 --- a/robot.py +++ b/robot.py @@ -62,16 +62,14 @@ class Robot(Job): return status - def toChitchat(self, msg: Wcf.WxMsg): + def toChitchat(self, msg: Wcf.WxMsg) -> None: """闲聊,目前未实现 """ pass def processMsg(self, msg: Wcf.WxMsg) -> None: """当接收到消息的时候,会调用本方法。如果不实现本方法,则打印原始消息。 - """ - - """ 此处可进行自定义发送的内容,如通过 msg.content 关键字自动获取当前天气信息,并发送到对应的群组@发送者 + 此处可进行自定义发送的内容,如通过 msg.content 关键字自动获取当前天气信息,并发送到对应的群组@发送者 群号:msg.roomid 微信ID:msg.sender 消息内容:msg.content content = "xx天气信息为:" receivers = msg.roomid @@ -80,7 +78,7 @@ class Robot(Job): # 群聊消息 if msg.from_group(): - # 如果在群里被 @,回复发信人:“收到你的消息了!” 并 @他 + # 如果在群里被 @ if msg.roomid not in self.config.GROUPS: # 不在配置的响应的群列表里,忽略 return @@ -120,21 +118,25 @@ class Robot(Job): def enableRecvMsg(self) -> None: self.wcf.enable_recv_msg(self.onMsg) - def sendTextMsg(self, msg: str, receiver: str, at_list: str = ""): + def sendTextMsg(self, msg: str, receiver: str, at_list: str = "") -> None: + """ 发送消息 + :param msg: 消息字符串 + :param receiver: 接收人wxid或者群id + :param at_list: 要@的wxid, @所有人的wxid为:nofity@all + """ # msg 中需要有 @ 名单中一样数量的 @ ats = "" if at_list: wxids = at_list.split(",") for wxid in wxids: # 这里偷个懒,直接 @昵称。有必要的话可以通过 MicroMsg.db 里的 ChatRoom 表,解析群昵称 - ats = f" @{self.allContacts.get(wxid, '')}" # {msg}{ats} 表示要发送的消息内容后面紧跟@,例如 北京天气情况为:xxx @张三,微信规定需这样写,否则@不生效 self.LOG.info(f"To {receiver}: {msg}{ats}") self.wcf.send_text(f"{msg}{ats}", receiver, at_list) - def getAllContacts(self): + def getAllContacts(self) -> dict: """ 获取联系人(包括好友、公众号、服务号、群成员……) 格式: {"wxid": "NickName"} @@ -150,7 +152,7 @@ class Robot(Job): self.runPendingJobs() time.sleep(1) - def autoAcceptFriendRequest(self, msg: Wcf.WxMsg): + def autoAcceptFriendRequest(self, msg: Wcf.WxMsg) -> None: try: xml = ET.fromstring(msg.content) v3 = xml.attrib["encryptusername"] @@ -160,7 +162,7 @@ class Robot(Job): except Exception as e: self.LOG.error(f"同意好友出错:{e}") - def sayHiToNewFriend(self, msg: Wcf.WxMsg): + def sayHiToNewFriend(self, msg: Wcf.WxMsg) -> None: nickName = re.findall(r"你已添加了(.*),现在可以开始聊天了。", msg.content) if nickName: # 添加了好友,更新好友列表