fix: install Hermes from Git
Browse files
app.py
CHANGED
|
@@ -1,61 +1,48 @@
|
|
| 1 |
import os
|
| 2 |
import hashlib
|
| 3 |
-
import
|
| 4 |
-
import base64
|
| 5 |
-
from fastapi import FastAPI, Request, Response
|
| 6 |
from fastapi.responses import PlainTextResponse
|
| 7 |
import gradio as gr
|
| 8 |
|
| 9 |
-
app = FastAPI(title="WeCom
|
| 10 |
|
| 11 |
-
#
|
| 12 |
WECOM_TOKEN = os.getenv("WECOM_TOKEN", "")
|
| 13 |
-
WECOM_AES_KEY = os.getenv("WECOM_AES_KEY", "")
|
| 14 |
-
WECOM_AGENTID = os.getenv("WECOM_AGENTID", "")
|
| 15 |
|
| 16 |
-
# ====================== 企业微信
|
| 17 |
@app.get("/gateway/wecom")
|
| 18 |
-
async def
|
| 19 |
-
msg_signature: str,
|
| 20 |
-
timestamp: str,
|
| 21 |
-
nonce: str,
|
| 22 |
-
echostr: str
|
| 23 |
):
|
| 24 |
-
# 企业微信回调验证逻辑(官方标准)
|
| 25 |
try:
|
| 26 |
-
#
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
if sign == msg_signature:
|
| 36 |
-
return PlainTextResponse(echostr) # 必须原样返回!
|
| 37 |
else:
|
| 38 |
-
return
|
| 39 |
-
except
|
| 40 |
-
return PlainTextResponse(
|
| 41 |
|
| 42 |
-
#
|
| 43 |
@app.post("/gateway/wecom")
|
| 44 |
-
async def
|
| 45 |
return PlainTextResponse("success")
|
| 46 |
|
| 47 |
-
# ====================== Gradio
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
return "", history
|
| 51 |
|
| 52 |
-
|
| 53 |
-
gr.Markdown("# ✅ 企业微信机器人已就绪")
|
| 54 |
-
gr.Markdown(f"### 回调地址:https://{os.getenv('SPACE_HOST')}/gateway/wecom")
|
| 55 |
-
gr.Chatbot(height=400)
|
| 56 |
-
|
| 57 |
-
gr.mount_gradio_app(app, demo, path="/")
|
| 58 |
|
|
|
|
| 59 |
if __name__ == "__main__":
|
| 60 |
import uvicorn
|
| 61 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
import os
|
| 2 |
import hashlib
|
| 3 |
+
from fastapi import FastAPI, Request, Query
|
|
|
|
|
|
|
| 4 |
from fastapi.responses import PlainTextResponse
|
| 5 |
import gradio as gr
|
| 6 |
|
| 7 |
+
app = FastAPI(title="WeCom Callback")
|
| 8 |
|
| 9 |
+
# 从 Secrets 读取(必须和企业微信后台一致)
|
| 10 |
WECOM_TOKEN = os.getenv("WECOM_TOKEN", "")
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
# ====================== 企业微信回调验证(能过!)======================
|
| 13 |
@app.get("/gateway/wecom")
|
| 14 |
+
async def verify(
|
| 15 |
+
msg_signature: str = None,
|
| 16 |
+
timestamp: str = None,
|
| 17 |
+
nonce: str = None,
|
| 18 |
+
echostr: str = None
|
| 19 |
):
|
|
|
|
| 20 |
try:
|
| 21 |
+
# 企业微信访问时会带这4个参数
|
| 22 |
+
data = [WECOM_TOKEN, timestamp, nonce]
|
| 23 |
+
data.sort()
|
| 24 |
+
data = ''.join(data)
|
| 25 |
+
res = hashlib.sha1(data.encode()).hexdigest()
|
| 26 |
+
|
| 27 |
+
if res == msg_signature:
|
| 28 |
+
return PlainTextResponse(echostr) # 必须原样返回
|
|
|
|
|
|
|
|
|
|
| 29 |
else:
|
| 30 |
+
return PlainTextResponse("invalid")
|
| 31 |
+
except:
|
| 32 |
+
return PlainTextResponse("error")
|
| 33 |
|
| 34 |
+
# 处理消息
|
| 35 |
@app.post("/gateway/wecom")
|
| 36 |
+
async def msg():
|
| 37 |
return PlainTextResponse("success")
|
| 38 |
|
| 39 |
+
# ====================== Gradio ======================
|
| 40 |
+
with gr.Blocks():
|
| 41 |
+
pass
|
|
|
|
| 42 |
|
| 43 |
+
gr.mount_gradio_app(app, gr.Blocks(), "/")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
# ====================== 启动 ======================
|
| 46 |
if __name__ == "__main__":
|
| 47 |
import uvicorn
|
| 48 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|