fix: install Hermes from Git
Browse files- app.py +29 -42
- wx config.txt +1 -0
app.py
CHANGED
|
@@ -6,60 +6,47 @@ from fastapi import FastAPI, Request
|
|
| 6 |
from fastapi.responses import PlainTextResponse
|
| 7 |
from Crypto.Cipher import AES
|
| 8 |
from Crypto.Util.Padding import unpad
|
| 9 |
-
import gradio as gr
|
| 10 |
|
| 11 |
-
app = FastAPI(
|
| 12 |
|
| 13 |
-
#
|
| 14 |
WECOM_TOKEN = os.getenv("WECOM_TOKEN", "").strip()
|
| 15 |
WECOM_AES_KEY = os.getenv("WECOM_AES_KEY", "").strip()
|
| 16 |
-
CORPID = os.getenv("CORPID", "").strip()
|
| 17 |
|
| 18 |
-
# ==========
|
|
|
|
|
|
|
| 19 |
@app.get("/gateway/wecom")
|
| 20 |
-
async def
|
| 21 |
try:
|
| 22 |
-
#
|
| 23 |
-
msg_signature =
|
| 24 |
-
timestamp =
|
| 25 |
-
nonce =
|
| 26 |
-
echostr_enc =
|
| 27 |
-
|
| 28 |
-
#
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
aes_key = base64.b64decode(WECOM_AES_KEY + "=")
|
| 39 |
cipher = AES.new(aes_key, AES.MODE_CBC, aes_key[:16])
|
| 40 |
encrypted = base64.b64decode(echostr_enc)
|
| 41 |
decrypted = unpad(cipher.decrypt(encrypted), AES.block_size)
|
| 42 |
msg = decrypted[16:].decode()
|
| 43 |
|
| 44 |
-
#
|
| 45 |
return PlainTextResponse(msg)
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
|
| 48 |
-
return PlainTextResponse(f"error: {str(e)}")
|
| 49 |
-
|
| 50 |
-
# 接收消息
|
| 51 |
@app.post("/gateway/wecom")
|
| 52 |
-
async def
|
| 53 |
-
return PlainTextResponse("
|
| 54 |
-
|
| 55 |
-
# ========== Gradio 界面 ==========
|
| 56 |
-
with gr.Blocks() as demo:
|
| 57 |
-
gr.Markdown("# ✅ 企业微信机器人已启动")
|
| 58 |
-
gr.Markdown(f"### 回调地址:https://franksoo-agent.hf.space/gateway/wecom")
|
| 59 |
-
|
| 60 |
-
gr.mount_gradio_app(app, demo, path="/")
|
| 61 |
-
|
| 62 |
-
# ========== 启动 ==========
|
| 63 |
-
if __name__ == "__main__":
|
| 64 |
-
import uvicorn
|
| 65 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 6 |
from fastapi.responses import PlainTextResponse
|
| 7 |
from Crypto.Cipher import AES
|
| 8 |
from Crypto.Util.Padding import unpad
|
|
|
|
| 9 |
|
| 10 |
+
app = FastAPI()
|
| 11 |
|
| 12 |
+
# 从环境变量读取(必须和企业微信一致)
|
| 13 |
WECOM_TOKEN = os.getenv("WECOM_TOKEN", "").strip()
|
| 14 |
WECOM_AES_KEY = os.getenv("WECOM_AES_KEY", "").strip()
|
|
|
|
| 15 |
|
| 16 |
+
# ==========================================
|
| 17 |
+
# 企业微信官方 URL 验证(绝对正确)
|
| 18 |
+
# ==========================================
|
| 19 |
@app.get("/gateway/wecom")
|
| 20 |
+
async def verify(request: Request):
|
| 21 |
try:
|
| 22 |
+
# 获取参数
|
| 23 |
+
msg_signature = request.query_params.get("msg_signature", "")
|
| 24 |
+
timestamp = request.query_params.get("timestamp", "")
|
| 25 |
+
nonce = request.query_params.get("nonce", "")
|
| 26 |
+
echostr_enc = request.query_params.get("echostr", "")
|
| 27 |
+
|
| 28 |
+
# 验签
|
| 29 |
+
arr = sorted([WECOM_TOKEN, timestamp, nonce])
|
| 30 |
+
check_str = ''.join(arr)
|
| 31 |
+
sig = hashlib.sha1(check_str.encode()).hexdigest()
|
| 32 |
+
|
| 33 |
+
if sig != msg_signature:
|
| 34 |
+
return PlainTextResponse("")
|
| 35 |
+
|
| 36 |
+
# AES 解密(官方标准)
|
| 37 |
+
aes_key = base64.b64decode(WECOM_AES_KEY)
|
|
|
|
| 38 |
cipher = AES.new(aes_key, AES.MODE_CBC, aes_key[:16])
|
| 39 |
encrypted = base64.b64decode(echostr_enc)
|
| 40 |
decrypted = unpad(cipher.decrypt(encrypted), AES.block_size)
|
| 41 |
msg = decrypted[16:].decode()
|
| 42 |
|
| 43 |
+
# 只返回明文!!! 不加任何东西!!!
|
| 44 |
return PlainTextResponse(msg)
|
| 45 |
+
|
| 46 |
+
except Exception:
|
| 47 |
+
return PlainTextResponse("")
|
| 48 |
|
| 49 |
+
# 必须返回空 success
|
|
|
|
|
|
|
|
|
|
| 50 |
@app.post("/gateway/wecom")
|
| 51 |
+
async def post():
|
| 52 |
+
return PlainTextResponse("")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
wx config.txt
CHANGED
|
@@ -5,5 +5,6 @@ WECOM_TOKEN=jrbaP8up4HgKz1VhVVTu2WBUlVmLG
|
|
| 5 |
WECOM_AES_KEY=jviaEHXvE2pRU6oKPzj8LXpWAhQ3YYmUTl8dbf2XHaU
|
| 6 |
SPACE_HOST=franksoo/agent
|
| 7 |
|
|
|
|
| 8 |
|
| 9 |
https://franksoo-agent.hf.space/gateway/wecom
|
|
|
|
| 5 |
WECOM_AES_KEY=jviaEHXvE2pRU6oKPzj8LXpWAhQ3YYmUTl8dbf2XHaU
|
| 6 |
SPACE_HOST=franksoo/agent
|
| 7 |
|
| 8 |
+
CORPID=wwff99ddc89331de81
|
| 9 |
|
| 10 |
https://franksoo-agent.hf.space/gateway/wecom
|