feat:完成微信公众号的自动化工具
This commit is contained in:
80
app/services/ai_rewriter.py
Normal file
80
app/services/ai_rewriter.py
Normal file
@@ -0,0 +1,80 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from textwrap import shorten
|
||||
|
||||
from openai import OpenAI
|
||||
|
||||
from app.config import settings
|
||||
from app.schemas import RewriteRequest, RewriteResponse
|
||||
|
||||
|
||||
SYSTEM_PROMPT = """
|
||||
你是中文内容编辑与合规顾问。请把输入内容进行“原创改写”,要求:
|
||||
1) 保留核心事实,但避免逐句复述;
|
||||
2) 结构清晰:导语、3-5个小节、结尾行动建议;
|
||||
3) 风格适合微信公众号,表达自然,避免AI腔;
|
||||
4) 如果原文存在未经核实结论,请使用“可能/有待验证”等措辞;
|
||||
5) 输出必须是 JSON,字段:title, summary, body_markdown。
|
||||
""".strip()
|
||||
|
||||
|
||||
class AIRewriter:
|
||||
def __init__(self) -> None:
|
||||
self._client = None
|
||||
if settings.openai_api_key:
|
||||
self._client = OpenAI(
|
||||
api_key=settings.openai_api_key,
|
||||
base_url=settings.openai_base_url,
|
||||
)
|
||||
|
||||
def rewrite(self, req: RewriteRequest) -> RewriteResponse:
|
||||
if not self._client:
|
||||
return self._fallback_rewrite(req)
|
||||
|
||||
user_prompt = f"""
|
||||
原始内容:
|
||||
{req.source_text}
|
||||
|
||||
改写约束:
|
||||
- 标题参考:{req.title_hint or '自动生成'}
|
||||
- 目标语气:{req.tone}
|
||||
- 目标读者:{req.audience}
|
||||
- 必须保留观点:{req.keep_points or '无'}
|
||||
- 避免词汇:{req.avoid_words or '无'}
|
||||
""".strip()
|
||||
|
||||
completion = self._client.responses.create(
|
||||
model=settings.openai_model,
|
||||
input=[
|
||||
{"role": "system", "content": SYSTEM_PROMPT},
|
||||
{"role": "user", "content": user_prompt},
|
||||
],
|
||||
text={"format": {"type": "json_object"}},
|
||||
)
|
||||
|
||||
text = completion.output_text
|
||||
import json
|
||||
|
||||
data = json.loads(text)
|
||||
return RewriteResponse(**data)
|
||||
|
||||
def _fallback_rewrite(self, req: RewriteRequest) -> RewriteResponse:
|
||||
clean_text = re.sub(r"\n{2,}", "\n", req.source_text.strip())
|
||||
lines = [line.strip() for line in clean_text.split("\n") if line.strip()]
|
||||
head = lines[0] if lines else clean_text[:50]
|
||||
title = req.title_hint.strip() or f"{shorten(head, width=26, placeholder='')}:可执行解读"
|
||||
summary = shorten(clean_text, width=90, placeholder="...")
|
||||
body = (
|
||||
f"## 导语\n"
|
||||
f"这篇内容值得关注的核心在于:{summary}\n\n"
|
||||
f"## 重点拆解\n"
|
||||
f"1. 背景与问题:从原文可以看到关键矛盾已出现。\n"
|
||||
f"2. 方法与动作:建议按“目标-路径-验证”三步推进。\n"
|
||||
f"3. 风险与边界:避免绝对化表述,必要时补充数据来源。\n\n"
|
||||
f"## 公众号改写正文\n"
|
||||
f"{clean_text}\n\n"
|
||||
f"## 结尾\n"
|
||||
f"以上为原创重组版本,可继续补充案例与数据后发布。"
|
||||
)
|
||||
return RewriteResponse(title=title, summary=summary, body_markdown=body)
|
||||
54
app/services/im.py
Normal file
54
app/services/im.py
Normal file
@@ -0,0 +1,54 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
import hmac
|
||||
import base64
|
||||
import time
|
||||
from urllib.parse import quote_plus
|
||||
|
||||
import httpx
|
||||
|
||||
from app.config import settings
|
||||
from app.schemas import IMPublishRequest, PublishResponse
|
||||
|
||||
|
||||
class IMPublisher:
|
||||
async def publish(self, req: IMPublishRequest) -> PublishResponse:
|
||||
if not settings.im_webhook_url:
|
||||
return PublishResponse(ok=False, detail="缺少 IM_WEBHOOK_URL 配置")
|
||||
|
||||
webhook = self._with_signature(settings.im_webhook_url, settings.im_secret)
|
||||
payload = {
|
||||
"msg_type": "post",
|
||||
"content": {
|
||||
"post": {
|
||||
"zh_cn": {
|
||||
"title": req.title,
|
||||
"content": [[{"tag": "text", "text": req.body_markdown[:3800]}]],
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
async with httpx.AsyncClient(timeout=20) as client:
|
||||
r = await client.post(webhook, json=payload)
|
||||
try:
|
||||
data = r.json()
|
||||
except Exception:
|
||||
data = {"status_code": r.status_code, "text": r.text}
|
||||
|
||||
if r.status_code >= 400:
|
||||
return PublishResponse(ok=False, detail=f"IM 推送失败: {data}", data=data)
|
||||
|
||||
return PublishResponse(ok=True, detail="IM 推送成功", data=data)
|
||||
|
||||
def _with_signature(self, webhook: str, secret: str | None) -> str:
|
||||
# 兼容飞书 webhook 签名参数:timestamp/sign
|
||||
if not secret:
|
||||
return webhook
|
||||
|
||||
ts = str(int(time.time()))
|
||||
string_to_sign = f"{ts}\n{secret}".encode("utf-8")
|
||||
sign = base64.b64encode(hmac.new(string_to_sign, b"", hashlib.sha256).digest()).decode("utf-8")
|
||||
connector = "&" if "?" in webhook else "?"
|
||||
return f"{webhook}{connector}timestamp={ts}&sign={quote_plus(sign)}"
|
||||
72
app/services/wechat.py
Normal file
72
app/services/wechat.py
Normal file
@@ -0,0 +1,72 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import time
|
||||
|
||||
import httpx
|
||||
import markdown2
|
||||
|
||||
from app.config import settings
|
||||
from app.schemas import PublishResponse, WechatPublishRequest
|
||||
|
||||
|
||||
class WechatPublisher:
|
||||
def __init__(self) -> None:
|
||||
self._access_token = None
|
||||
self._expires_at = 0
|
||||
|
||||
async def publish_draft(self, req: WechatPublishRequest) -> PublishResponse:
|
||||
if not settings.wechat_appid or not settings.wechat_secret:
|
||||
return PublishResponse(ok=False, detail="缺少 WECHAT_APPID / WECHAT_SECRET 配置")
|
||||
|
||||
token = await self._get_access_token()
|
||||
if not token:
|
||||
return PublishResponse(ok=False, detail="获取微信 access_token 失败")
|
||||
|
||||
html = markdown2.markdown(req.body_markdown)
|
||||
payload = {
|
||||
"articles": [
|
||||
{
|
||||
"title": req.title,
|
||||
"author": req.author or settings.wechat_author,
|
||||
"digest": req.summary,
|
||||
"content": html,
|
||||
"content_source_url": "",
|
||||
"need_open_comment": 0,
|
||||
"only_fans_can_comment": 0,
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
async with httpx.AsyncClient(timeout=25) as client:
|
||||
url = f"https://api.weixin.qq.com/cgi-bin/draft/add?access_token={token}"
|
||||
r = await client.post(url, json=payload)
|
||||
data = r.json()
|
||||
|
||||
if data.get("errcode", 0) != 0:
|
||||
return PublishResponse(ok=False, detail=f"微信发布失败: {data}", data=data)
|
||||
|
||||
return PublishResponse(ok=True, detail="已发布到公众号草稿箱", data=data)
|
||||
|
||||
async def _get_access_token(self) -> str | None:
|
||||
now = int(time.time())
|
||||
if self._access_token and now < self._expires_at - 60:
|
||||
return self._access_token
|
||||
|
||||
async with httpx.AsyncClient(timeout=20) as client:
|
||||
r = await client.get(
|
||||
"https://api.weixin.qq.com/cgi-bin/token",
|
||||
params={
|
||||
"grant_type": "client_credential",
|
||||
"appid": settings.wechat_appid,
|
||||
"secret": settings.wechat_secret,
|
||||
},
|
||||
)
|
||||
data = r.json()
|
||||
|
||||
token = data.get("access_token")
|
||||
if not token:
|
||||
return None
|
||||
|
||||
self._access_token = token
|
||||
self._expires_at = now + int(data.get("expires_in", 7200))
|
||||
return token
|
||||
Reference in New Issue
Block a user