Use wcferry
This commit is contained in:
113
main.py
113
main.py
@@ -1,103 +1,10 @@
|
||||
#! /usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import re
|
||||
import signal
|
||||
from wcferry import Wcf
|
||||
|
||||
from func_chengyu import cy
|
||||
import robot.sdk.wcferry as WxSDK
|
||||
from robot.base_robot import BaseRobot
|
||||
from robot.configuration import Config
|
||||
|
||||
|
||||
class Robot(BaseRobot):
|
||||
"""个性化自己的机器人
|
||||
"""
|
||||
|
||||
def __init__(self, sdk: WxSDK, config: Config) -> None:
|
||||
super().__init__(sdk)
|
||||
self.config = config
|
||||
|
||||
def toAt(self, msg) -> bool:
|
||||
"""
|
||||
处理被 @ 消息,现在只固定回复: "你@我干嘛?"
|
||||
:param msg: 微信消息结构
|
||||
:return: 处理状态,`True` 成功,`False` 失败
|
||||
"""
|
||||
status = True
|
||||
rsp = "你@我干嘛?"
|
||||
self.sendTextMsg(msg.roomId, rsp, msg.wxId)
|
||||
|
||||
return status
|
||||
|
||||
def toChengyu(self, msg) -> bool:
|
||||
"""
|
||||
处理成语查询/接龙消息
|
||||
:param msg: 微信消息结构
|
||||
:return: 处理状态,`True` 成功,`False` 失败
|
||||
"""
|
||||
status = False
|
||||
texts = re.findall(r"^([#|?|?])(.*)$", msg.content)
|
||||
# [('#', '天天向上')]
|
||||
if texts:
|
||||
flag = texts[0][0]
|
||||
text = texts[0][1]
|
||||
if flag == "#": # 接龙
|
||||
if cy.isChengyu(text):
|
||||
rsp = cy.getNext(text)
|
||||
if rsp:
|
||||
self.sendTextMsg(msg.roomId, rsp)
|
||||
status = True
|
||||
elif flag in ["?", "?"]: # 查词
|
||||
if cy.isChengyu(text):
|
||||
rsp = cy.getMeaning(text)
|
||||
if rsp:
|
||||
self.sendTextMsg(msg.roomId, rsp)
|
||||
status = True
|
||||
|
||||
return status
|
||||
|
||||
def toChitchat(self, msg):
|
||||
"""闲聊,目前未实现
|
||||
"""
|
||||
pass
|
||||
|
||||
def processMsg(self, msg) -> None:
|
||||
"""当接收到消息的时候,会调用本方法。如果不实现本方法,则打印原始消息。
|
||||
"""
|
||||
|
||||
self.printRawMsg(msg) # 打印信息
|
||||
|
||||
# 群聊消息
|
||||
if self.isGroupChat(msg):
|
||||
# 如果在群里被 @,回复发信人:“收到你的消息了!” 并 @他
|
||||
if msg.roomId not in self.config.GROUPS: # 不在配置的响应的群列表里,忽略
|
||||
return
|
||||
|
||||
if self.isAt(msg): # 被@
|
||||
self.toAt(msg)
|
||||
|
||||
else: # 其他消息
|
||||
self.toChengyu(msg)
|
||||
|
||||
# 非群聊信息
|
||||
elif msg.type == 37: # 好友请求
|
||||
self.autoAcceptFriendRequest(msg)
|
||||
|
||||
elif msg.type == 10000: # 系统信息
|
||||
nickName = re.findall(r"你已添加了(.*),现在可以开始聊天了。", msg.content)
|
||||
if nickName:
|
||||
# 添加了好友,更新好友列表
|
||||
self.allContacts[msg.wxId] = nickName
|
||||
|
||||
elif msg.type == 0x01: # 文本消息
|
||||
# 让配置加载更灵活,自己可以更新配置。也可以利用定时任务更新。
|
||||
if msg.self and msg.content == "^更新$":
|
||||
self.config.reload()
|
||||
self.LOG.info("已更新")
|
||||
return
|
||||
|
||||
# 闲聊
|
||||
self.toChitchat(msg)
|
||||
from robot import Robot
|
||||
|
||||
|
||||
def weather_report(robot: Robot):
|
||||
@@ -110,14 +17,20 @@ def weather_report(robot: Robot):
|
||||
report = "这就是获取到的天气情况了"
|
||||
|
||||
for r in receivers:
|
||||
robot.sendTextMsg(r, report)
|
||||
robot.sendTextMsg(report, r)
|
||||
|
||||
|
||||
def main():
|
||||
robot = Robot(WxSDK, Config())
|
||||
wcf = Wcf()
|
||||
|
||||
# 初始化机器人
|
||||
robot.initSDK()
|
||||
def handler(sig, frame):
|
||||
wcf.cleanup() # 退出前清理环境
|
||||
exit(0)
|
||||
|
||||
signal.signal(signal.SIGINT, handler)
|
||||
|
||||
robot = Robot(wcf)
|
||||
robot.LOG.info("机器人已启动")
|
||||
|
||||
# 接收消息
|
||||
robot.enableRecvMsg()
|
||||
|
||||
Reference in New Issue
Block a user