|
|
|
|
@ -15,35 +15,36 @@ from servermanager.api.commonapi import ChatGPT, CommandHandler # 聊天与命
|
|
|
|
|
from .MemcacheStorage import MemcacheStorage # 自定义基于缓存的会话存储
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 初始化微信机器人实例
|
|
|
|
|
#wjl# 初始化微信机器人实例
|
|
|
|
|
robot = WeRoBot(
|
|
|
|
|
token=os.environ.get('DJANGO_WEROBOT_TOKEN') or 'lylinux', # 微信公众号 Token
|
|
|
|
|
enable_session=True # 启用会话功能,用于维护用户状态
|
|
|
|
|
token=os.environ.get('DJANGO_WEROBOT_TOKEN') or 'lylinux', #wjl# 微信公众号 Token
|
|
|
|
|
enable_session=True #wjl# 启用会话功能,用于维护用户状态
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# 创建自定义的缓存会话存储实例(基于 Redis/Memcached)
|
|
|
|
|
#wjl# 创建自定义的缓存会话存储实例(基于 Redis/Memcached)
|
|
|
|
|
memstorage = MemcacheStorage()
|
|
|
|
|
|
|
|
|
|
# 检查缓存存储是否可用
|
|
|
|
|
#wjl# 检查缓存存储是否可用
|
|
|
|
|
if memstorage.is_available:
|
|
|
|
|
# 如果可用,使用 MemcacheStorage 作为会话后端
|
|
|
|
|
#wjl# 如果可用,使用 MemcacheStorage 作为会话后端
|
|
|
|
|
robot.config['SESSION_STORAGE'] = memstorage
|
|
|
|
|
else:
|
|
|
|
|
# 如果不可用,回退到文件存储
|
|
|
|
|
# 删除旧的会话文件(避免冲突或损坏)
|
|
|
|
|
#wjl# 如果不可用,回退到文件存储
|
|
|
|
|
#wjl # 删除旧的会话文件(避免冲突或损坏)
|
|
|
|
|
session_file = os.path.join(settings.BASE_DIR, 'werobot_session')
|
|
|
|
|
if os.path.exists(session_file):
|
|
|
|
|
os.remove(session_file)
|
|
|
|
|
# 使用本地文件存储会话数据
|
|
|
|
|
#wjl # 使用本地文件存储会话数据
|
|
|
|
|
robot.config['SESSION_STORAGE'] = FileStorage(filename='werobot_session')
|
|
|
|
|
|
|
|
|
|
# 实例化博客 API 和命令处理器
|
|
|
|
|
blogapi = BlogApi() # 提供博客文章查询服务
|
|
|
|
|
cmd_handler = CommandHandler() # 提供服务器命令执行功能
|
|
|
|
|
logger = logging.getLogger(__name__) # 获取当前模块的日志记录器
|
|
|
|
|
#wjl# 实例化博客 API 和命令处理器
|
|
|
|
|
blogapi = BlogApi() #wjl # 提供博客文章查询服务
|
|
|
|
|
cmd_handler = CommandHandler()#wjl # 提供服务器命令执行功能
|
|
|
|
|
logger = logging.getLogger(__name__) #wjl# 获取当前模块的日志记录器
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def convert_to_article_reply(articles, message):
|
|
|
|
|
# wjl
|
|
|
|
|
"""
|
|
|
|
|
将博客文章列表转换为微信图文消息回复格式。
|
|
|
|
|
|
|
|
|
|
@ -60,18 +61,18 @@ def convert_to_article_reply(articles, message):
|
|
|
|
|
3. 构造 Article 对象并添加到回复中
|
|
|
|
|
"""
|
|
|
|
|
reply = ArticlesReply(message=message)
|
|
|
|
|
from blog.templatetags.blog_tags import truncatechars_content # 导入截断内容的模板标签
|
|
|
|
|
from blog.templatetags.blog_tags import truncatechars_content #wjl# 导入截断内容的模板标签
|
|
|
|
|
|
|
|
|
|
for post in articles:
|
|
|
|
|
# 使用正则从文章内容中提取第一张图片 URL(png/jpg)
|
|
|
|
|
#wjl# 使用正则从文章内容中提取第一张图片 URL(png/jpg)
|
|
|
|
|
imgs = re.findall(r'(?:http\:|https\:)?\/\/.*\.(?:png|jpg)', post.body)
|
|
|
|
|
imgurl = imgs[0] if imgs else '' # 如果有图取第一张,否则为空
|
|
|
|
|
imgurl = imgs[0] if imgs else '' #wjl# 如果有图取第一张,否则为空
|
|
|
|
|
|
|
|
|
|
article = Article(
|
|
|
|
|
title=post.title,
|
|
|
|
|
description=truncatechars_content(post.body), # 截断内容作为描述
|
|
|
|
|
description=truncatechars_content(post.body), #wjl# 截断内容作为描述
|
|
|
|
|
img=imgurl,
|
|
|
|
|
url=post.get_full_url() # 文章完整 URL
|
|
|
|
|
url=post.get_full_url() #wjl# 文章完整 URL
|
|
|
|
|
)
|
|
|
|
|
reply.add_article(article)
|
|
|
|
|
return reply
|
|
|
|
|
@ -79,6 +80,7 @@ def convert_to_article_reply(articles, message):
|
|
|
|
|
|
|
|
|
|
@robot.filter(re.compile(r"^\?.*"))
|
|
|
|
|
def search(message, session):
|
|
|
|
|
# wjl
|
|
|
|
|
"""
|
|
|
|
|
处理以 '?' 开头的消息,用于搜索博客文章。
|
|
|
|
|
|
|
|
|
|
@ -104,6 +106,7 @@ def search(message, session):
|
|
|
|
|
|
|
|
|
|
@robot.filter(re.compile(r'^category\s*$', re.I))
|
|
|
|
|
def category(message, session):
|
|
|
|
|
# wjl
|
|
|
|
|
"""
|
|
|
|
|
处理 "category" 消息,返回所有文章分类目录。
|
|
|
|
|
|
|
|
|
|
@ -115,12 +118,13 @@ def category(message, session):
|
|
|
|
|
文本消息:列出所有分类名称
|
|
|
|
|
"""
|
|
|
|
|
categorys = blogapi.get_category_lists()
|
|
|
|
|
content = ','.join(map(lambda x: x.name, categorys)) # 拼接分类名
|
|
|
|
|
content = ','.join(map(lambda x: x.name, categorys)) #wjl# 拼接分类名
|
|
|
|
|
return '所有文章分类目录:' + content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@robot.filter(re.compile(r'^recent\s*$', re.I))
|
|
|
|
|
def recents(message, session):
|
|
|
|
|
# wjl
|
|
|
|
|
"""
|
|
|
|
|
处理 "recent" 消息,返回最新发布的文章。
|
|
|
|
|
|
|
|
|
|
@ -141,6 +145,7 @@ def recents(message, session):
|
|
|
|
|
|
|
|
|
|
@robot.filter(re.compile('^help$', re.I))
|
|
|
|
|
def help(message, session):
|
|
|
|
|
# wjl
|
|
|
|
|
"""
|
|
|
|
|
处理 "help" 消息,返回帮助文档。
|
|
|
|
|
|
|
|
|
|
@ -173,6 +178,7 @@ def help(message, session):
|
|
|
|
|
|
|
|
|
|
@robot.filter(re.compile(r'^weather\:.*$', re.I))
|
|
|
|
|
def weather(message, session):
|
|
|
|
|
# wjl
|
|
|
|
|
"""
|
|
|
|
|
处理 "weather:" 开头的消息(天气查询功能)。
|
|
|
|
|
当前为占位符,功能正在建设中。
|
|
|
|
|
@ -189,6 +195,7 @@ def weather(message, session):
|
|
|
|
|
|
|
|
|
|
@robot.filter(re.compile(r'^idcard\:.*$', re.I))
|
|
|
|
|
def idcard(message, session):
|
|
|
|
|
# wjl
|
|
|
|
|
"""
|
|
|
|
|
处理 "idcard:" 开头的消息(身份证信息查询功能)。
|
|
|
|
|
当前为占位符,功能正在建设中。
|
|
|
|
|
@ -205,6 +212,7 @@ def idcard(message, session):
|
|
|
|
|
|
|
|
|
|
@robot.handler
|
|
|
|
|
def echo(message, session):
|
|
|
|
|
# wjl
|
|
|
|
|
"""
|
|
|
|
|
默认消息处理器,当没有其他 filter 匹配时调用。
|
|
|
|
|
创建 MessageHandler 实例处理消息。
|
|
|
|
|
@ -221,6 +229,7 @@ def echo(message, session):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MessageHandler:
|
|
|
|
|
# wjl
|
|
|
|
|
"""
|
|
|
|
|
消息处理器类,负责处理用户消息,尤其是管理员命令和认证流程。
|
|
|
|
|
使用会话(session)维护用户状态(是否管理员、是否已认证等)。
|
|
|
|
|
@ -229,28 +238,29 @@ class MessageHandler:
|
|
|
|
|
def __init__(self, message, session):
|
|
|
|
|
self.message = message
|
|
|
|
|
self.session = session
|
|
|
|
|
self.userid = message.source # 用户唯一标识(OpenID)
|
|
|
|
|
self.userid = message.source #wjl# 用户唯一标识(OpenID)
|
|
|
|
|
|
|
|
|
|
# 尝试从会话中加载用户信息
|
|
|
|
|
try:
|
|
|
|
|
info = session[self.userid]
|
|
|
|
|
self.userinfo = jsonpickle.decode(info) # 反序列化为 WxUserInfo 对象
|
|
|
|
|
self.userinfo = jsonpickle.decode(info) #wjl# 反序列化为 WxUserInfo 对象
|
|
|
|
|
except Exception as e:
|
|
|
|
|
# 如果出错(首次访问或会话丢失),创建默认用户信息
|
|
|
|
|
#wjl# 如果出错(首次访问或会话丢失),创建默认用户信息
|
|
|
|
|
userinfo = WxUserInfo()
|
|
|
|
|
self.userinfo = userinfo
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def is_admin(self):
|
|
|
|
|
"""判断当前用户是否为管理员"""
|
|
|
|
|
#wjl"""判断当前用户是否为管理员"""
|
|
|
|
|
return self.userinfo.isAdmin
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def is_password_set(self):
|
|
|
|
|
"""判断当前管理员是否已完成密码验证"""
|
|
|
|
|
#wjl"""判断当前管理员是否已完成密码验证"""
|
|
|
|
|
return self.userinfo.isPasswordSet
|
|
|
|
|
|
|
|
|
|
def save_session(self):
|
|
|
|
|
# wjl
|
|
|
|
|
"""
|
|
|
|
|
将当前用户信息保存回会话。
|
|
|
|
|
使用 jsonpickle 序列化对象,并存入 session。
|
|
|
|
|
@ -259,6 +269,7 @@ class MessageHandler:
|
|
|
|
|
self.session[self.userid] = info
|
|
|
|
|
|
|
|
|
|
def handler(self):
|
|
|
|
|
# wjl
|
|
|
|
|
"""
|
|
|
|
|
核心消息处理逻辑,根据用户状态和输入内容返回相应响应。
|
|
|
|
|
|
|
|
|
|
@ -274,65 +285,66 @@ class MessageHandler:
|
|
|
|
|
"""
|
|
|
|
|
info = self.message.content
|
|
|
|
|
|
|
|
|
|
# 退出管理员模式
|
|
|
|
|
#wjl# 退出管理员模式
|
|
|
|
|
if self.userinfo.isAdmin and info.upper() == 'EXIT':
|
|
|
|
|
self.userinfo = WxUserInfo()
|
|
|
|
|
self.save_session()
|
|
|
|
|
return "退出成功"
|
|
|
|
|
|
|
|
|
|
# 请求进入管理员模式
|
|
|
|
|
#wjl# 请求进入管理员模式
|
|
|
|
|
if info.upper() == 'ADMIN':
|
|
|
|
|
self.userinfo.isAdmin = True
|
|
|
|
|
self.save_session()
|
|
|
|
|
return "输入管理员密码"
|
|
|
|
|
|
|
|
|
|
# 管理员密码验证阶段
|
|
|
|
|
#wjl# 管理员密码验证阶段
|
|
|
|
|
if self.userinfo.isAdmin and not self.userinfo.isPasswordSet:
|
|
|
|
|
# 获取配置中的管理员密码(SHA256 加密后)
|
|
|
|
|
#wjl# 获取配置中的管理员密码(SHA256 加密后)
|
|
|
|
|
passwd = settings.WXADMIN
|
|
|
|
|
if settings.TESTING:
|
|
|
|
|
passwd = '123' # 测试环境下使用简单密码
|
|
|
|
|
# 验证用户输入的密码(双重 SHA256)
|
|
|
|
|
passwd = '123' #wjl# 测试环境下使用简单密码
|
|
|
|
|
#wjl# 验证用户输入的密码(双重 SHA256)
|
|
|
|
|
if passwd.upper() == get_sha256(get_sha256(info)).upper():
|
|
|
|
|
self.userinfo.isPasswordSet = True
|
|
|
|
|
self.save_session()
|
|
|
|
|
return "验证通过,请输入命令或者要执行的命令代码:输入helpme获得帮助"
|
|
|
|
|
return #wjl"验证通过,请输入命令或者要执行的命令代码:输入helpme获得帮助"
|
|
|
|
|
else:
|
|
|
|
|
# 密码错误次数限制
|
|
|
|
|
#wjl# 密码错误次数限制
|
|
|
|
|
if self.userinfo.Count >= 3:
|
|
|
|
|
self.userinfo = WxUserInfo()
|
|
|
|
|
self.save_session()
|
|
|
|
|
return "超过验证次数"
|
|
|
|
|
return #wjl"超过验证次数"
|
|
|
|
|
self.userinfo.Count += 1
|
|
|
|
|
self.save_session()
|
|
|
|
|
return "验证失败,请重新输入管理员密码:"
|
|
|
|
|
return #wjl"验证失败,请重新输入管理员密码:"
|
|
|
|
|
|
|
|
|
|
# 管理员已认证,可执行命令
|
|
|
|
|
#wjl # 管理员已认证,可执行命令
|
|
|
|
|
if self.userinfo.isAdmin and self.userinfo.isPasswordSet:
|
|
|
|
|
if self.userinfo.Command != '' and info.upper() == 'Y':
|
|
|
|
|
# 用户确认执行命令
|
|
|
|
|
#wjl# 用户确认执行命令
|
|
|
|
|
return cmd_handler.run(self.userinfo.Command)
|
|
|
|
|
else:
|
|
|
|
|
if info.upper() == 'HELPME':
|
|
|
|
|
# 显示命令帮助
|
|
|
|
|
#wjl# 显示命令帮助
|
|
|
|
|
return cmd_handler.get_help()
|
|
|
|
|
# 记录待执行的命令,等待用户确认
|
|
|
|
|
#wjl# 记录待执行的命令,等待用户确认
|
|
|
|
|
self.userinfo.Command = info
|
|
|
|
|
self.save_session()
|
|
|
|
|
return "确认执行: " + info + " 命令?"
|
|
|
|
|
|
|
|
|
|
# 默认行为:调用 ChatGPT 进行普通聊天
|
|
|
|
|
#wjl # 默认行为:调用 ChatGPT 进行普通聊天
|
|
|
|
|
return ChatGPT.chat(info)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class WxUserInfo():
|
|
|
|
|
# wjl
|
|
|
|
|
"""
|
|
|
|
|
微信用户信息类,用于在会话中存储用户状态。
|
|
|
|
|
包括是否为管理员、是否已通过密码验证、尝试次数、待执行命令等。
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self.isAdmin = False # 是否请求成为管理员
|
|
|
|
|
self.isPasswordSet = False # 是否已通过密码验证
|
|
|
|
|
self.Count = 0 # 密码尝试次数
|
|
|
|
|
self.Command = '' # 待执行的命令
|
|
|
|
|
self.isAdmin = False #wjl # 是否请求成为管理员
|
|
|
|
|
self.isPasswordSet = False #wjl# 是否已通过密码验证
|
|
|
|
|
self.Count = 0 #wjl # 密码尝试次数
|
|
|
|
|
self.Command = '' #wjl# 待执行的命令
|