6
main.py
6
main.py
@@ -10,6 +10,7 @@ from robot import Robot
|
|||||||
def weather_report(robot: Robot) -> None:
|
def weather_report(robot: Robot) -> None:
|
||||||
"""模拟发送天气预报
|
"""模拟发送天气预报
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# 获取接收人
|
# 获取接收人
|
||||||
receivers = ["filehelper"]
|
receivers = ["filehelper"]
|
||||||
|
|
||||||
@@ -18,6 +19,7 @@ def weather_report(robot: Robot) -> None:
|
|||||||
|
|
||||||
for r in receivers:
|
for r in receivers:
|
||||||
robot.sendTextMsg(report, r)
|
robot.sendTextMsg(report, r)
|
||||||
|
# robot.sendTextMsg(report, r, "nofity@all") # 发送消息并@所有人
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
@@ -30,7 +32,9 @@ def main():
|
|||||||
signal.signal(signal.SIGINT, handler)
|
signal.signal(signal.SIGINT, handler)
|
||||||
|
|
||||||
robot = Robot(wcf)
|
robot = Robot(wcf)
|
||||||
robot.LOG.info("机器人已启动")
|
robot.LOG.info("正在启动机器人···")
|
||||||
|
# 机器人启动发送测试消息
|
||||||
|
robot.sendTextMsg("机器人启动成功!", "filehelper")
|
||||||
|
|
||||||
# 接收消息
|
# 接收消息
|
||||||
robot.enableRecvMsg()
|
robot.enableRecvMsg()
|
||||||
|
|||||||
13
robot.py
13
robot.py
@@ -70,6 +70,14 @@ class Robot(Job):
|
|||||||
def processMsg(self, msg: Wcf.WxMsg) -> None:
|
def processMsg(self, msg: Wcf.WxMsg) -> None:
|
||||||
"""当接收到消息的时候,会调用本方法。如果不实现本方法,则打印原始消息。
|
"""当接收到消息的时候,会调用本方法。如果不实现本方法,则打印原始消息。
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
""" 此处可进行自定义发送的内容,如通过 msg.content 关键字自动获取当前天气信息,并发送到对应的群组@发送者
|
||||||
|
群号:msg.roomid 微信ID:msg.sender 消息内容:msg.content
|
||||||
|
content = "xx天气信息为:"
|
||||||
|
receivers = msg.roomid
|
||||||
|
self.sendTextMsg(content, receivers, msg.sender)
|
||||||
|
"""
|
||||||
|
|
||||||
# 群聊消息
|
# 群聊消息
|
||||||
if msg.from_group():
|
if msg.from_group():
|
||||||
# 如果在群里被 @,回复发信人:“收到你的消息了!” 并 @他
|
# 如果在群里被 @,回复发信人:“收到你的消息了!” 并 @他
|
||||||
@@ -101,6 +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:
|
||||||
@@ -118,8 +127,10 @@ class Robot(Job):
|
|||||||
wxids = at_list.split(",")
|
wxids = at_list.split(",")
|
||||||
for wxid in wxids:
|
for wxid in wxids:
|
||||||
# 这里偷个懒,直接 @昵称。有必要的话可以通过 MicroMsg.db 里的 ChatRoom 表,解析群昵称
|
# 这里偷个懒,直接 @昵称。有必要的话可以通过 MicroMsg.db 里的 ChatRoom 表,解析群昵称
|
||||||
|
|
||||||
ats = f" @{self.allContacts.get(wxid, '')}"
|
ats = f" @{self.allContacts.get(wxid, '')}"
|
||||||
|
|
||||||
|
# {msg}{ats} 表示要发送的消息内容后面紧跟@,例如 北京天气情况为:xxx @张三,微信规定需这样写,否则@不生效
|
||||||
self.LOG.info(f"To {receiver}: {msg}{ats}")
|
self.LOG.info(f"To {receiver}: {msg}{ats}")
|
||||||
self.wcf.send_text(f"{msg}{ats}", receiver, at_list)
|
self.wcf.send_text(f"{msg}{ats}", receiver, at_list)
|
||||||
|
|
||||||
@@ -129,7 +140,7 @@ class Robot(Job):
|
|||||||
格式: {"wxid": "NickName"}
|
格式: {"wxid": "NickName"}
|
||||||
"""
|
"""
|
||||||
contacts = self.wcf.query_sql("MicroMsg.db", "SELECT UserName, NickName FROM Contact;")
|
contacts = self.wcf.query_sql("MicroMsg.db", "SELECT UserName, NickName FROM Contact;")
|
||||||
return {contact["UserName"]: contact["NickName"] for contact in contacts}
|
return {contact["UserName"]: contact["NickName"]for contact in contacts}
|
||||||
|
|
||||||
def keepRunningAndBlockProcess(self) -> None:
|
def keepRunningAndBlockProcess(self) -> None:
|
||||||
"""
|
"""
|
||||||
|
|||||||
Reference in New Issue
Block a user