13
main.py
13
main.py
@@ -16,15 +16,15 @@ def weather_report(robot: Robot) -> None:
|
|||||||
|
|
||||||
# 获取天气,需要自己实现,可以参考 https://gitee.com/lch0821/WeatherScrapy 获取天气。
|
# 获取天气,需要自己实现,可以参考 https://gitee.com/lch0821/WeatherScrapy 获取天气。
|
||||||
report = "这就是获取到的天气情况了"
|
report = "这就是获取到的天气情况了"
|
||||||
|
|
||||||
for r in receivers:
|
for r in receivers:
|
||||||
robot.sendTextMsg(report, r)
|
robot.sendTextMsg(report, r)
|
||||||
#robot.sendTextMsg(report, r, "nofity@all") # 发送消息并@所有人
|
# robot.sendTextMsg(report, r, "nofity@all") # 发送消息并@所有人
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
wcf = Wcf()
|
wcf = Wcf()
|
||||||
|
|
||||||
def handler(sig, frame):
|
def handler(sig, frame):
|
||||||
wcf.cleanup() # 退出前清理环境
|
wcf.cleanup() # 退出前清理环境
|
||||||
exit(0)
|
exit(0)
|
||||||
@@ -35,18 +35,15 @@ def main():
|
|||||||
robot.LOG.info("正在启动机器人···")
|
robot.LOG.info("正在启动机器人···")
|
||||||
# 机器人启动发送测试消息
|
# 机器人启动发送测试消息
|
||||||
robot.sendTextMsg("机器人启动成功!", "filehelper")
|
robot.sendTextMsg("机器人启动成功!", "filehelper")
|
||||||
|
|
||||||
# 接收消息
|
# 接收消息
|
||||||
robot.enableRecvMsg()
|
robot.enableRecvMsg()
|
||||||
|
|
||||||
|
|
||||||
# 每天7点发送天气预报
|
# 每天7点发送天气预报
|
||||||
robot.onEveryTime("22:30", weather_report, robot=robot)
|
robot.onEveryTime("00:30", weather_report, robot=robot)
|
||||||
|
|
||||||
# 让机器人一直跑
|
# 让机器人一直跑
|
||||||
robot.keepRunningAndBlockProcess()
|
robot.keepRunningAndBlockProcess()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
19
robot.py
19
robot.py
@@ -12,7 +12,6 @@ from func_chengyu import cy
|
|||||||
from job_mgmt import Job
|
from job_mgmt import Job
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Robot(Job):
|
class Robot(Job):
|
||||||
"""个性化自己的机器人
|
"""个性化自己的机器人
|
||||||
"""
|
"""
|
||||||
@@ -22,9 +21,8 @@ class Robot(Job):
|
|||||||
self.config = Config()
|
self.config = Config()
|
||||||
self.LOG = logging.getLogger("Robot")
|
self.LOG = logging.getLogger("Robot")
|
||||||
self.wxid = self.wcf.get_self_wxid()
|
self.wxid = self.wcf.get_self_wxid()
|
||||||
#self.allContacts = self.getAllContacts()
|
#self.allContacts = self.getAllContacts()
|
||||||
|
|
||||||
|
|
||||||
def toAt(self, msg: Wcf.WxMsg) -> bool:
|
def toAt(self, msg: Wcf.WxMsg) -> bool:
|
||||||
"""
|
"""
|
||||||
处理被 @ 消息,现在只固定回复: "你@我干嘛?"
|
处理被 @ 消息,现在只固定回复: "你@我干嘛?"
|
||||||
@@ -72,7 +70,7 @@ class Robot(Job):
|
|||||||
def processMsg(self, msg: Wcf.WxMsg) -> None:
|
def processMsg(self, msg: Wcf.WxMsg) -> None:
|
||||||
"""当接收到消息的时候,会调用本方法。如果不实现本方法,则打印原始消息。
|
"""当接收到消息的时候,会调用本方法。如果不实现本方法,则打印原始消息。
|
||||||
"""
|
"""
|
||||||
|
|
||||||
""" 此处可进行自定义发送的内容,如通过关键字自动获取当前天气信息,并发送到对应的群组@发送者
|
""" 此处可进行自定义发送的内容,如通过关键字自动获取当前天气信息,并发送到对应的群组@发送者
|
||||||
群号:msg.roomid 微信ID:msg.sender 消息内容:msg.content
|
群号:msg.roomid 微信ID:msg.sender 消息内容:msg.content
|
||||||
content = "天气查询"
|
content = "天气查询"
|
||||||
@@ -80,7 +78,6 @@ class Robot(Job):
|
|||||||
self.sendTextMsg(content, receivers, msg.sender)
|
self.sendTextMsg(content, receivers, msg.sender)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
# 群聊消息
|
# 群聊消息
|
||||||
if msg.from_group():
|
if msg.from_group():
|
||||||
# 如果在群里被 @,回复发信人:“收到你的消息了!” 并 @他
|
# 如果在群里被 @,回复发信人:“收到你的消息了!” 并 @他
|
||||||
@@ -112,7 +109,7 @@ class Robot(Job):
|
|||||||
|
|
||||||
def onMsg(self, msg: Wcf.WxMsg) -> int:
|
def onMsg(self, msg: Wcf.WxMsg) -> int:
|
||||||
self.LOG.info(msg) # 打印信息
|
self.LOG.info(msg) # 打印信息
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.processMsg(msg)
|
self.processMsg(msg)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@@ -130,7 +127,7 @@ class Robot(Job):
|
|||||||
wxids = at_list.split(",")
|
wxids = at_list.split(",")
|
||||||
for wxid in wxids:
|
for wxid in wxids:
|
||||||
# 这里偷个懒,直接 @昵称
|
# 这里偷个懒,直接 @昵称
|
||||||
|
|
||||||
""" 若出现直接 @wxid,则要通过 MicroMsg.db 里的 ChatRoom 表解析群昵称,取消此处注释,并注释141行
|
""" 若出现直接 @wxid,则要通过 MicroMsg.db 里的 ChatRoom 表解析群昵称,取消此处注释,并注释141行
|
||||||
userInfo = self.getAllContacts()
|
userInfo = self.getAllContacts()
|
||||||
everyUser = {'notify@all': '所有人'}
|
everyUser = {'notify@all': '所有人'}
|
||||||
@@ -147,8 +144,10 @@ class Robot(Job):
|
|||||||
获取联系人(包括好友、公众号、服务号、群成员……)
|
获取联系人(包括好友、公众号、服务号、群成员……)
|
||||||
格式: {"wxid": "NickName"}
|
格式: {"wxid": "NickName"}
|
||||||
"""
|
"""
|
||||||
contacts = self.wcf.query_sql("MicroMsg.db", "SELECT UserName, NickName FROM Contact;")
|
contacts = self.wcf.query_sql(
|
||||||
return {contact["UserName"]: contact["NickName"] for contact in contacts}
|
"MicroMsg.db", "SELECT UserName, NickName FROM Contact;")
|
||||||
|
return {contact["UserName"]: contact["NickName"]
|
||||||
|
for contact in contacts}
|
||||||
|
|
||||||
def keepRunningAndBlockProcess(self) -> None:
|
def keepRunningAndBlockProcess(self) -> None:
|
||||||
"""
|
"""
|
||||||
|
|||||||
Reference in New Issue
Block a user