This commit is contained in:
jxxghp
2023-09-05 11:20:06 +08:00
parent 4bf9045784
commit 2826b9411d
11 changed files with 55 additions and 27 deletions

View File

@ -1,5 +1,5 @@
import json
from typing import Optional, Union, List, Tuple, Any
from typing import Optional, Union, List, Tuple, Any, Dict
from app.core.context import MediaInfo, Context
from app.core.config import settings
@ -120,7 +120,7 @@ class TelegramModule(_ModuleBase):
"""
return self.telegram.send_torrents_msg(title=message.title, torrents=torrents, userid=message.userid)
def register_commands(self, commands: dict):
def register_commands(self, commands: Dict[str, dict]):
"""
注册命令,实现这个函数接收系统可用的命令菜单
:param commands: 命令字典

View File

@ -2,7 +2,7 @@ import re
import threading
from pathlib import Path
from threading import Event
from typing import Optional, List
from typing import Optional, List, Dict
import telebot
from telebot import apihelper
@ -198,7 +198,7 @@ class Telegram(metaclass=Singleton):
return True if ret else False
def register_commands(self, commands: dict):
def register_commands(self, commands: Dict[str, dict]):
"""
注册菜单命令
"""

View File

@ -1,5 +1,5 @@
import xml.dom.minidom
from typing import Optional, Union, List, Tuple, Any
from typing import Optional, Union, List, Tuple, Any, Dict
from app.core.config import settings
from app.core.context import Context, MediaInfo
@ -152,7 +152,7 @@ class WechatModule(_ModuleBase):
"""
return self.wechat.send_torrents_msg(title=message.title, torrents=torrents, userid=message.userid)
def register_commands(self, commands: dict):
def register_commands(self, commands: Dict[str, dict]):
"""
注册命令,实现这个函数接收系统可用的命令菜单
:param commands: 命令字典

View File

@ -2,7 +2,7 @@ import json
import re
import threading
from datetime import datetime
from typing import Optional, List
from typing import Optional, List, Dict
from app.core.config import settings
from app.core.context import MediaInfo, Context
@ -273,7 +273,7 @@ class WeChat(metaclass=Singleton):
logger.error(f"发送请求失败,错误信息:{err}")
return False
def create_menus(self, commands: dict):
def create_menus(self, commands: Dict[str, dict]):
"""
自动注册微信菜单
:param commands: 命令字典
@ -282,10 +282,11 @@ class WeChat(metaclass=Singleton):
"/cookiecloud": {
"func": CookieCloudChain(self._db).remote_sync,
"description": "同步站点",
"category": "站点",
"data": {}
}
}
注册报文格式:
注册报文格式子菜单最多只有5条
{
"button":[
{
@ -313,24 +314,34 @@ class WeChat(metaclass=Singleton):
"""
# 请求URL
req_url = self._create_menu_url % (self.__get_access_token(), self._appid)
# 按钮
buttons = []
# 对commands按category分组
category_dict = {}
for key, value in commands.items():
category: Dict[str, dict] = value.get("category")
if category:
if not category_dict.get(category):
category_dict[category] = {}
category_dict[category][key] = value
# 一级菜单
buttons = []
for category, menu in category_dict.items():
# 二级菜单
sub_buttons = []
for key, value in menu.items():
sub_buttons.append({
"type": "click",
"name": value.get("description"),
"key": key
})
buttons.append({
"type": "click",
"name": value.get("description"),
"key": key
"name": category,
"sub_button": sub_buttons
})
if buttons:
# 请求参数
menus = {
"button": [
{
"name": "操作",
"sub_button": buttons
}
]
}
# 发送请求
self.__post_request(req_url, menus)
self.__post_request(req_url, {
"button": buttons
})