From f2c70acd9cbd6acab3063496b221bfa914099f0d Mon Sep 17 00:00:00 2001 From: mzivv <984084954@qq.com> Date: Mon, 7 Nov 2022 16:28:28 +0800 Subject: [PATCH 1/5] add some notes Signed-off-by: mzivv <984084954@qq.com> --- main.py | 17 ++++++++++++----- robot.py | 25 ++++++++++++++++++++++--- 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/main.py b/main.py index 7228552..afff237 100644 --- a/main.py +++ b/main.py @@ -10,19 +10,21 @@ from robot import Robot def weather_report(robot: Robot) -> None: """模拟发送天气预报 """ + # 获取接收人 receivers = ["filehelper"] # 获取天气,需要自己实现,可以参考 https://gitee.com/lch0821/WeatherScrapy 获取天气。 report = "这就是获取到的天气情况了" - + for r in receivers: robot.sendTextMsg(report, r) + #robot.sendTextMsg(report, r, "nofity@all") # 发送消息并@所有人 def main(): wcf = Wcf() - + def handler(sig, frame): wcf.cleanup() # 退出前清理环境 exit(0) @@ -30,16 +32,21 @@ def main(): signal.signal(signal.SIGINT, handler) robot = Robot(wcf) - robot.LOG.info("机器人已启动") - + robot.LOG.info("正在启动机器人···") + # 机器人启动发送测试消息 + robot.sendTextMsg("机器人启动成功!", "filehelper") + # 接收消息 robot.enableRecvMsg() + # 每天7点发送天气预报 - robot.onEveryTime("07:00", weather_report, robot=robot) + robot.onEveryTime("22:30", weather_report, robot=robot) # 让机器人一直跑 robot.keepRunningAndBlockProcess() + + if __name__ == "__main__": diff --git a/robot.py b/robot.py index d3e28e7..096f7aa 100644 --- a/robot.py +++ b/robot.py @@ -12,6 +12,7 @@ from func_chengyu import cy from job_mgmt import Job + class Robot(Job): """个性化自己的机器人 """ @@ -21,8 +22,9 @@ class Robot(Job): self.config = Config() self.LOG = logging.getLogger("Robot") self.wxid = self.wcf.get_self_wxid() - self.allContacts = self.getAllContacts() - + #self.allContacts = self.getAllContacts() + + def toAt(self, msg: Wcf.WxMsg) -> bool: """ 处理被 @ 消息,现在只固定回复: "你@我干嘛?" @@ -70,6 +72,15 @@ class Robot(Job): def processMsg(self, msg: Wcf.WxMsg) -> None: """当接收到消息的时候,会调用本方法。如果不实现本方法,则打印原始消息。 """ + + """ 此处可进行自定义发送的内容,如通过关键字自动获取当前天气信息,并发送到对应的群组@发送者 + 群号:msg.roomid 微信ID:msg.sender 消息内容:msg.content + content = "天气查询" + receivers = msg.roomid + self.sendTextMsg(content, receivers, msg.sender) + """ + + # 群聊消息 if msg.from_group(): # 如果在群里被 @,回复发信人:“收到你的消息了!” 并 @他 @@ -101,6 +112,7 @@ class Robot(Job): def onMsg(self, msg: Wcf.WxMsg) -> int: self.LOG.info(msg) # 打印信息 + try: self.processMsg(msg) except Exception as e: @@ -117,7 +129,14 @@ class Robot(Job): if at_list: wxids = at_list.split(",") for wxid in wxids: - # 这里偷个懒,直接 @昵称。有必要的话可以通过 MicroMsg.db 里的 ChatRoom 表,解析群昵称 + # 这里偷个懒,直接 @昵称 + + """ 若出现直接 @wxid,则要通过 MicroMsg.db 里的 ChatRoom 表解析群昵称,取消此处注释,并注释141行 + userInfo = self.getAllContacts() + everyUser = {'notify@all': '所有人'} + userInfo.update(everyUser) # 追加dict @所有人所需字段 + ats = f" @{userInfo[wxid]}" + """ ats = f" @{self.allContacts.get(wxid, '')}" self.LOG.info(f"To {receiver}: {msg}{ats}") From 57580b20e1bb40e4b854b0e7fc642c44ff242b18 Mon Sep 17 00:00:00 2001 From: mzivv <984084954@qq.com> Date: Mon, 7 Nov 2022 17:13:54 +0800 Subject: [PATCH 2/5] add some notes Signed-off-by: mzivv <984084954@qq.com> --- main.py | 13 +++++-------- robot.py | 19 +++++++++---------- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/main.py b/main.py index afff237..b74e990 100644 --- a/main.py +++ b/main.py @@ -16,15 +16,15 @@ def weather_report(robot: Robot) -> None: # 获取天气,需要自己实现,可以参考 https://gitee.com/lch0821/WeatherScrapy 获取天气。 report = "这就是获取到的天气情况了" - + for r in receivers: robot.sendTextMsg(report, r) - #robot.sendTextMsg(report, r, "nofity@all") # 发送消息并@所有人 + # robot.sendTextMsg(report, r, "nofity@all") # 发送消息并@所有人 def main(): wcf = Wcf() - + def handler(sig, frame): wcf.cleanup() # 退出前清理环境 exit(0) @@ -35,18 +35,15 @@ def main(): robot.LOG.info("正在启动机器人···") # 机器人启动发送测试消息 robot.sendTextMsg("机器人启动成功!", "filehelper") - + # 接收消息 robot.enableRecvMsg() - # 每天7点发送天气预报 - robot.onEveryTime("22:30", weather_report, robot=robot) + robot.onEveryTime("00:30", weather_report, robot=robot) # 让机器人一直跑 robot.keepRunningAndBlockProcess() - - if __name__ == "__main__": diff --git a/robot.py b/robot.py index 096f7aa..518d306 100644 --- a/robot.py +++ b/robot.py @@ -12,7 +12,6 @@ from func_chengyu import cy from job_mgmt import Job - class Robot(Job): """个性化自己的机器人 """ @@ -22,9 +21,8 @@ class Robot(Job): self.config = Config() self.LOG = logging.getLogger("Robot") self.wxid = self.wcf.get_self_wxid() - #self.allContacts = self.getAllContacts() - - + #self.allContacts = self.getAllContacts() + def toAt(self, msg: Wcf.WxMsg) -> bool: """ 处理被 @ 消息,现在只固定回复: "你@我干嘛?" @@ -72,7 +70,7 @@ class Robot(Job): def processMsg(self, msg: Wcf.WxMsg) -> None: """当接收到消息的时候,会调用本方法。如果不实现本方法,则打印原始消息。 """ - + """ 此处可进行自定义发送的内容,如通过关键字自动获取当前天气信息,并发送到对应的群组@发送者 群号:msg.roomid 微信ID:msg.sender 消息内容:msg.content content = "天气查询" @@ -80,7 +78,6 @@ class Robot(Job): self.sendTextMsg(content, receivers, msg.sender) """ - # 群聊消息 if msg.from_group(): # 如果在群里被 @,回复发信人:“收到你的消息了!” 并 @他 @@ -112,7 +109,7 @@ class Robot(Job): def onMsg(self, msg: Wcf.WxMsg) -> int: self.LOG.info(msg) # 打印信息 - + try: self.processMsg(msg) except Exception as e: @@ -130,7 +127,7 @@ class Robot(Job): wxids = at_list.split(",") for wxid in wxids: # 这里偷个懒,直接 @昵称 - + """ 若出现直接 @wxid,则要通过 MicroMsg.db 里的 ChatRoom 表解析群昵称,取消此处注释,并注释141行 userInfo = self.getAllContacts() everyUser = {'notify@all': '所有人'} @@ -147,8 +144,10 @@ class Robot(Job): 获取联系人(包括好友、公众号、服务号、群成员……) 格式: {"wxid": "NickName"} """ - contacts = self.wcf.query_sql("MicroMsg.db", "SELECT UserName, NickName FROM Contact;") - return {contact["UserName"]: contact["NickName"] for contact in contacts} + contacts = self.wcf.query_sql( + "MicroMsg.db", "SELECT UserName, NickName FROM Contact;") + return {contact["UserName"]: contact["NickName"] + for contact in contacts} def keepRunningAndBlockProcess(self) -> None: """ From 9499621a609744801ed1ca184a490730eae03c4c Mon Sep 17 00:00:00 2001 From: mzivv <984084954@qq.com> Date: Mon, 7 Nov 2022 17:30:10 +0800 Subject: [PATCH 3/5] add some notes Signed-off-by: mzivv <984084954@qq.com> --- robot.py | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/robot.py b/robot.py index 518d306..eff455a 100644 --- a/robot.py +++ b/robot.py @@ -21,7 +21,7 @@ class Robot(Job): self.config = Config() self.LOG = logging.getLogger("Robot") self.wxid = self.wcf.get_self_wxid() - #self.allContacts = self.getAllContacts() + self.allContacts = self.getAllContacts() def toAt(self, msg: Wcf.WxMsg) -> bool: """ @@ -71,9 +71,9 @@ class Robot(Job): """当接收到消息的时候,会调用本方法。如果不实现本方法,则打印原始消息。 """ - """ 此处可进行自定义发送的内容,如通过关键字自动获取当前天气信息,并发送到对应的群组@发送者 + """ 此处可进行自定义发送的内容,如通过 msg.content 关键字自动获取当前天气信息,并发送到对应的群组@发送者 群号:msg.roomid 微信ID:msg.sender 消息内容:msg.content - content = "天气查询" + content = "xx天气信息为:" receivers = msg.roomid self.sendTextMsg(content, receivers, msg.sender) """ @@ -126,14 +126,8 @@ class Robot(Job): if at_list: wxids = at_list.split(",") for wxid in wxids: - # 这里偷个懒,直接 @昵称 + # 这里偷个懒,直接 @昵称。有必要的话可以通过 MicroMsg.db 里的 ChatRoom 表,解析群昵称 - """ 若出现直接 @wxid,则要通过 MicroMsg.db 里的 ChatRoom 表解析群昵称,取消此处注释,并注释141行 - userInfo = self.getAllContacts() - everyUser = {'notify@all': '所有人'} - userInfo.update(everyUser) # 追加dict @所有人所需字段 - ats = f" @{userInfo[wxid]}" - """ ats = f" @{self.allContacts.get(wxid, '')}" self.LOG.info(f"To {receiver}: {msg}{ats}") From 3a70da8d7688805ebc57cad543dddf1a63f16de7 Mon Sep 17 00:00:00 2001 From: mzivv <984084954@qq.com> Date: Mon, 7 Nov 2022 18:47:31 +0800 Subject: [PATCH 4/5] add some notes Signed-off-by: mzivv <984084954@qq.com> --- main.py | 2 +- robot.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index b74e990..c8b23b6 100644 --- a/main.py +++ b/main.py @@ -40,7 +40,7 @@ def main(): robot.enableRecvMsg() # 每天7点发送天气预报 - robot.onEveryTime("00:30", weather_report, robot=robot) + robot.onEveryTime("07:00", weather_report, robot=robot) # 让机器人一直跑 robot.keepRunningAndBlockProcess() diff --git a/robot.py b/robot.py index eff455a..30738bf 100644 --- a/robot.py +++ b/robot.py @@ -131,6 +131,7 @@ class Robot(Job): ats = f" @{self.allContacts.get(wxid, '')}" self.LOG.info(f"To {receiver}: {msg}{ats}") + # {msg}{ats} 表示要发送的消息内容后面紧跟@,例如 北京天气情况为:xxx @张三,微信规定需这样写,否则@不生效 self.wcf.send_text(f"{msg}{ats}", receiver, at_list) def getAllContacts(self): From 22ddef0f5a1f8c1839db993c8436b38e6d154af1 Mon Sep 17 00:00:00 2001 From: mzivv <984084954@qq.com> Date: Mon, 7 Nov 2022 19:00:06 +0800 Subject: [PATCH 5/5] add some notes Signed-off-by: mzivv <984084954@qq.com> --- robot.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/robot.py b/robot.py index 30738bf..0edf802 100644 --- a/robot.py +++ b/robot.py @@ -130,8 +130,8 @@ class Robot(Job): ats = f" @{self.allContacts.get(wxid, '')}" - self.LOG.info(f"To {receiver}: {msg}{ats}") # {msg}{ats} 表示要发送的消息内容后面紧跟@,例如 北京天气情况为:xxx @张三,微信规定需这样写,否则@不生效 + self.LOG.info(f"To {receiver}: {msg}{ats}") self.wcf.send_text(f"{msg}{ats}", receiver, at_list) def getAllContacts(self): @@ -139,10 +139,8 @@ class Robot(Job): 获取联系人(包括好友、公众号、服务号、群成员……) 格式: {"wxid": "NickName"} """ - contacts = self.wcf.query_sql( - "MicroMsg.db", "SELECT UserName, NickName FROM Contact;") - return {contact["UserName"]: contact["NickName"] - for contact in contacts} + contacts = self.wcf.query_sql("MicroMsg.db", "SELECT UserName, NickName FROM Contact;") + return {contact["UserName"]: contact["NickName"]for contact in contacts} def keepRunningAndBlockProcess(self) -> None: """