0121017272
- latest_announcements 不再过滤 keyword_matched,返回全部最新5条 - 每条公告显示来源网站(广西政府采购网/大化县政府网采购公告) - 菜单「今日公告」改为顶级按钮「最新公告」,移除子菜单重复项
108 lines
3.1 KiB
Python
108 lines
3.1 KiB
Python
import logging
|
|
|
|
import httpx
|
|
|
|
from app.config import settings
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
MENU = {
|
|
"button": [
|
|
{
|
|
"name": "最新公告",
|
|
"type": "click",
|
|
"key": "latest_announcements",
|
|
},
|
|
{
|
|
"name": "查询",
|
|
"sub_button": [
|
|
{
|
|
"name": "监控配置",
|
|
"type": "click",
|
|
"key": "monitor_config",
|
|
},
|
|
{
|
|
"name": "系统状态",
|
|
"type": "click",
|
|
"key": "system_status",
|
|
},
|
|
{
|
|
"name": "今日工作日",
|
|
"type": "click",
|
|
"key": "workday_status",
|
|
},
|
|
],
|
|
},
|
|
{
|
|
"name": "系统管理",
|
|
"sub_button": [
|
|
{
|
|
"name": "立即爬取",
|
|
"type": "click",
|
|
"key": "trigger_crawl",
|
|
},
|
|
{
|
|
"name": "同步节假日",
|
|
"type": "click",
|
|
"key": "sync_holidays",
|
|
},
|
|
],
|
|
},
|
|
]
|
|
}
|
|
|
|
|
|
class MenuManager:
|
|
def __init__(self, client=None):
|
|
self.client = client
|
|
|
|
async def _get_token(self) -> str | None:
|
|
from app.wechat.client import WeChatClient
|
|
c = self.client or WeChatClient()
|
|
return await c._get_access_token()
|
|
|
|
async def create(self) -> bool:
|
|
token = await self._get_token()
|
|
if not token:
|
|
return False
|
|
|
|
url = "https://qyapi.weixin.qq.com/cgi-bin/menu/create"
|
|
params = {"access_token": token, "agentid": int(settings.wechat_agent_id)}
|
|
|
|
async with httpx.AsyncClient(timeout=15) as client:
|
|
response = await client.post(url, params=params, json=MENU)
|
|
data = response.json()
|
|
if data.get("errcode") == 0:
|
|
logger.info("企微菜单创建成功")
|
|
return True
|
|
logger.error(f"企微菜单创建失败: {data}")
|
|
return False
|
|
|
|
async def delete(self) -> bool:
|
|
token = await self._get_token()
|
|
if not token:
|
|
return False
|
|
|
|
url = "https://qyapi.weixin.qq.com/cgi-bin/menu/delete"
|
|
params = {"access_token": token, "agentid": int(settings.wechat_agent_id)}
|
|
|
|
async with httpx.AsyncClient(timeout=15) as client:
|
|
response = await client.get(url, params=params)
|
|
data = response.json()
|
|
return data.get("errcode") == 0
|
|
|
|
async def get(self) -> dict | None:
|
|
token = await self._get_token()
|
|
if not token:
|
|
return None
|
|
|
|
url = "https://qyapi.weixin.qq.com/cgi-bin/menu/get"
|
|
params = {"access_token": token, "agentid": int(settings.wechat_agent_id)}
|
|
|
|
async with httpx.AsyncClient(timeout=15) as client:
|
|
response = await client.get(url, params=params)
|
|
data = response.json()
|
|
if data.get("errcode") == 0:
|
|
return data
|
|
return None
|